Skip to content

Instantly share code, notes, and snippets.

@iomz
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iomz/86e340f112afba0a816d to your computer and use it in GitHub Desktop.
Save iomz/86e340f112afba0a816d to your computer and use it in GitHub Desktop.
accelXY to double servo angles (for Node-RED demo)
/* 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