Last active
August 29, 2015 14:16
-
-
Save iomz/86e340f112afba0a816d to your computer and use it in GitHub Desktop.
accelXY to double servo angles (for Node-RED demo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Parse a string received as MQTT message to a JSON object */ | |
var data = JSON.parse(msg.payload); | |
/* Extrace attributes stored in the JSON */ | |
var accelX = data.accelX; | |
var accelY = data.accelY; | |
/* Compute the Theta and Phi from Acceleration along x/y axis */ | |
var theta = - (Math.asin(accelY)); | |
var phi = Math.asin(accelX/Math.cos(theta)); | |
/* Compute the angles and translate them from radians to degrees */ | |
var angleX = 180 * (theta / Math.PI) + 90; | |
var angleY = 180 * (phi / Math.PI) + 90; | |
/* Return the computed angles in a message format for Node-RED */ | |
return { | |
payload: { | |
pwm0: angleX, | |
pwm1: angleY | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment