0

How would I calculate the orientation of a sensor within a JavaScript function? I saw the example in 3d Accelerometer calculate the orientation but I'm unsure how to apply the values of msg.payload.xAxis/yAxis/zAxis as such within the calculation. I'd need to apply it to the example payload below in the form of a new field if possible.

{
"device":"368640",
"deviceType":"Sigfox-Tracker",
"version":3,
"timestamp":"1535121228",
"rssi":"-113.00",
"battery":3.28,
"temp":21.5,
"soil":2107,
"xAxis":61,
"yAxis":-26,
"zAxis":994
}
hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Are you asking how to access the properties inside the object you pasted? Also can you show more code, it would help a lot with the context and in getting you an answer that helps you best. – Andrew Lohr Aug 24 '18 at 14:55
  • Thank you Andrew. I'm trying to take the properties of the msg.payload: xAxis, yAxis and zAxis and calculate the orientation in a similar manner to the link posted above, however it would need to be done within JavaScript as that's what works with Node-RED. – Matthew Butler Aug 24 '18 at 16:05

2 Answers2

0

What you pasted is a javascript object and one of the ways you can access the properties of it is by using dot notation like data.xAxis below.

I created 2 functions to do the calculation from the answer in your link. I then used the built-in Math object for the formula provided.

Here's the example:

let data = {
  "device": "368640",
  "deviceType": "Sigfox-Tracker",
  "version": 3,
  "timestamp": "1535121228",
  "rssi": "-113.00",
  "battery": 3.28,
  "temp": 21.5,
  "soil": 2107,
  "xAxis": 61,
  "yAxis": -26,
  "zAxis": 994
}

function calcRoll(y, z){
  return Math.atan2(y, z) * 180/Math.PI;
}

function calcPitch(x, y, z){
  return Math.atan2(-x, Math.sqrt(y*y + z*z)) * 180/Math.PI;
}

let dataX = data.xAxis;
let dataY = data.yAxis;
let dataZ = data.zAxis;

let roll = calcRoll(dataY, dataZ);
let pitch = calcPitch(dataX, dataY, dataZ);

console.log(`roll is: ${roll} and pitch is ${pitch}`);
Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
  • Hi Andrew, I've posted an answer above. Is that the correct syntax to take the current msg.payload, and produce a new one with those numbers added to the field? I've shown an example code with a newly generated payload - if it's the correct numbers. – Matthew Butler Aug 24 '18 at 19:21
  • @MatthewButler you should edit your question with that additional info instead of creating an answer, but yes that looks correct – Andrew Lohr Aug 24 '18 at 20:08
0

Thank you Andrew, is this the correct syntax to take in msg.payload as each payload reports different values?

let data = msg.payload

function calcRoll(y, z){
  return Math.atan2(y, z) * 180/Math.PI;
}

function calcPitch(x, y, z){
  return Math.atan2(-x, Math.sqrt(y*y + z*z)) * 180/Math.PI;
}

let dataX = data.xAxis;
let dataY = data.yAxis;
let dataZ = data.zAxis;

let roll = calcRoll(dataY, dataZ);
let pitch = calcPitch(dataX, dataY, dataZ);

msg.payload.roll = roll;
msg.payload.pitch = pitch;
return msg;

Here is the debug message I view in Node-RED when the new modified payload arrives:

{"device":"368640","deviceType":"Sigfox-Tracker","version":3,"timestamp":"1535136195","rssi":"-109.00","battery":3.28,"temp":19.5,"soil":2105,"xAxis":71,"yAxis":-32,"zAxis":1002,"roll":-1.8291836314227448,"pitch":-4.05104777734416}