0

Question is about Node-RED for raspberry pi 3. I have 3 input that give acceleration of X,Y,Z axis. I want to make one output from these 3 inputs. For this , I use √X^2+Y^2+Z^2 formula. According to my function my output is still 3 piece and giving NaN output when i debug. What should i do in Acc to Freq function

Here is my collecting X,Y,Z info from my sql.

var str = msg.payload;
str = str[0]['IX']; // Choose last data from IX column
a = str * 10;   // Scaling the value
msg.payload = a
return msg;

var str = msg.payload;
str = str[0]['IY']; // Choose last data from IY column
b = str * 10;   // Scaling the value
msg.payload = b
return msg;

var str = msg.payload;
str = str[0]['IZ']; // Choose last data from IZ column
c = str * 10;   // Scaling the value
msg.payload = c
return msg;

And the function that i m try to calculate one output ( Acc to Freq )

var str = msg.payload;
var a;
var b;
var c;

str = Math.pow(a^2+b^2+c^2);
d = str * 10;
msg.payload = d;
return msg;

enter image description here

hardillb
  • 54,545
  • 11
  • 67
  • 105

1 Answers1

0

The point to remember is that a function node runs every time a message arrives, if you send it 3 separate messages then it will run 3 times. Also each function node is totally independent of all others, you can't declare a variable in one and use it in another (well there is something called the Context, but that's not particularly useful here)

You've not actually shown your flow so we are going to have to guess a little here, but you imply that all the starting values are coming from a single SQL query that returns multiple columns. If this is the case then you have 2 options.

  1. Just do all the calculations in one place e.g. one function node with the following:

    var str = msg.payload;
    var strA = str[0]['IX']; // Choose last data from IX column
    var a = strA * 10;   // Scaling the value
    var strB = str[0]['IY']; // Choose last data from IY column
    var b = strB * 10;   // Scaling the value
    var strC = str[0]['IZ']; // Choose last data from IZ column
    var c = strC * 10;   // Scaling the value
    var strC = Math.pow(a^2+b^2+c^2);
    var d = strC * 10;
    msg.payload = d;
    return msg;
    
  2. You can run the output of your current 3 function nodes into a Join node set to collect 3 values. This will generate a new msg object with a payload containing an array of the 3 values. You can then modify your final function node as follows:

    var a = msg.payload[0];
    var b = msg.payload[1];
    var c = msg.payload[2];
    var d = Math.pow(a^2+b^2+c^2) * 10 ;
    msg.payload = d;
    return msg;
    
hardillb
  • 54,545
  • 11
  • 67
  • 105