So, I have coded up what I need, and been playing about and with 6 instances running at 0.2500 intervals I was hitting around 20 to 24 percent of my CPU on a raspberry pi 2 b overclocked at 1000mhz.

the problem I think I am getting is lagg is it possible the msg's are bottlenecking?

I tried using the multithread function but unable to gain access to context items.

my coding skills are probably not the cleanest so, may be able to get better results by cleaning them up.

I am thinking of creating a table for COS and SIN and instead of doing the calculation just look up the required figure, would this be a faster option?

Body Rotation Matrix

```
var l1x= global.get("X");
var l1y= global.get("Y");
var l1z= global.get("Z");
var Yrot= global.get("Yr")*Math.PI/180;
var Xrot= global.get("Xr")*Math.PI/180;
var Zrot= global.get("Zr")*Math.PI/180;
//var cos= Math.cos(angle);
// X Axis rotation
var Y1 = l1y*Math.cos(Xrot)-l1z*Math.sin(Xrot);
var Z1 = l1z*Math.cos(Xrot)+l1y*Math.sin(Xrot);
// Y Axis rotation
var X1 = l1x*Math.cos(Yrot)-l1z*Math.sin(Yrot);
var Z2 = l1z*Math.cos(Yrot)+l1x*Math.sin(Yrot);
// Z Axis rotation
var X2 = l1x*Math.cos(Zrot)-l1y*Math.sin(Zrot);
var Y2 = l1y*Math.cos(Zrot)+l1x*Math.sin(Zrot);
flow.set("z1",(Z1 + Z2 - (l1z + l1z)));
flow.set("x1",(X1 + X2 - (l1x + l1x)));
flow.set("y1",(Y1 + Y2 - (l1y + l1y)));
//var msg1 = { payload:X };
//var msg2 = { payload:Y };
//var msg3 = { payload:Z };
//return [msg1,msg2,msg3];
```

Inverse Kinematics

```
//Global Sets
var L1 = global.get("L1");
var L2 = global.get("L2");
var L3 = global.get("L3");
var Z0 = global.get("Z0");
var X0 = global.get("X0");
var Y0 = global.get("Y0");
var A = global.get("A");
//Inflow Sets
var L4s = flow.get("L4");
var L5s = flow.get("L5");
var cts = flow.get("ct");
var cts1 = flow.get("ct1");
var cts2 = flow.get("ct2");
var cts3 = flow.get("ct3");
var cts4 = flow.get("ct4");
var cts5 = flow.get("ct5");
var cts6 = flow.get("ct6");
var PHIs = flow.get("PHI");
//Gamma Rotation
var st = Math.atan(Z0/X0);
var GR = st*180/Math.PI;
//L4 calculation
flow.set("L4",Math.sqrt((Math.pow(Y0,2)+Math.pow(X0,2))-(2*Y0*X0*Math.cos(A*Math.PI/180))));
//B Calculation
var st1 = (Math.pow(L4s,2)+Math.pow(X0,2)-Math.pow(Y0,2))/(2*L4s*X0);
var mt = Math.acos(st1);
flow.set("ct",mt*180/Math.PI);
//C Calculation
var st2 = (Math.pow(L4s,2)+Math.pow(Y0,2)-Math.pow(X0,2))/(2*L4s*Y0);
var mt1 = Math.acos(st2);
flow.set("ct1",mt1*180/Math.PI);
//J1 combonation result.
var T1 = Math.round(cts2 + cts4 + cts1 - 90);
//L5 Calculation
flow.set("L5",Math.sqrt((Math.pow(L3,2)+Math.pow(L4s,2))-(2*L3*L4s*Math.cos(PHIs*Math.PI/180))));
//D Calculation
var st3 = (Math.pow(L5s,2)+Math.pow(L4s,2)-Math.pow(L3,2))/(2*L5s*L4s);
var mt2 = Math.acos(st3);
flow.set("ct2",mt2*180/Math.PI);
//E Calculation
var st4 = (Math.pow(L5s,2)+Math.pow(L3,2)-Math.pow(L4s,2))/(2*L5s*L3);
var mt3 = Math.acos(st4);
flow.set("ct3",mt3*180/Math.PI);
//PHI Calculation
flow.set("PHI",90-cts);
//J2 combonation result.
var T2 = Math.round(180 - cts6);
//F Calculation
var st5 = (Math.pow(L1,2)+Math.pow(L5s,2)-Math.pow(L2,2))/(2*L1*L5s);
var mt4 = Math.acos(st5);
flow.set("ct4",mt4*180/Math.PI);
//G Calculation
var st6 = (Math.pow(L2,2)+Math.pow(L5s,2)-Math.pow(L1,2))/(2*L2*L5s);
var mt5 = Math.acos(st6);
flow.set("ct5",mt5*180/Math.PI);
//H Calculation
var st7 = (Math.pow(L2,2)+Math.pow(L1,2)-Math.pow(L5s,2))/(2*L2*L1);
var mt6 = Math.acos(st7);
flow.set("ct6",mt6*180/Math.PI);
//J3 combonation result
var T3 = Math.round(-180 + cts5 + cts3);
var msg1 = { payload:GR };
var msg2 = { payload:T1 };
var msg3 = { payload:T2 };
var msg4 = { payload:T3 };
return [msg1,msg2,msg3,msg4];
```

Also, I was using Math.round on some of my calc's but this takes the number to the nearest whole number how can I do it so that it reduces it to 2 decimal places?