Best way to us atan or atan2
electric550
Posts: 122
Is there a math library that allows for use of integers for the atan2 math function. The only math library that I can find that supports atan2 uses floating point. The problem is that I am getting data then I want to use the atan2 method and send out the value in degrees from the serial port but the value is in floating point so when I send the data out the serial port using the decimal format the result is a large number because of the formating. Thanks!
Comments
There are a couple of other options - multiple the float by 100 and convert to int and send that, it gives you a 2 deciaml place number - you can do it to other no of places as well.
Look at FloatString.spin it allows to to convert a float to a string and control the format - no of places etc.
when the code runs an example of the two variables would be X = 315 Y = -375
On my calculator this returns -0.6986 in radians so multiply by 180/pi = -40.030 but here it is just outputing zero.
Is it posible to use these two libraries together or is this a problem?
math : "DynamicMathLib"
floatm : "FloatMath"
Z := math.ATan2(X,Y)
Z := Z*180/3.14159
Z := floatm.FRound(Z)
uarts.dec(0,Z)
FloatString : "FloatString"
math : "DynamicMathLib"
Z := X/Y
Z := math.ATan(Z)
Z := Z*180/3.14159
Z := FloatString.FloatToString(Z)
uarts.str(0,Z)
uarts.tx(0,10)
uarts.tx(0,13)
here is some data that si returned
Mag XYZ: 47 -525 -673
1.401298e-45
Mag XYZ: 48 -528 -671
1.401298e-45
Mag XYZ: 52 -527 -669
1.401298e-45
Mag XYZ: 54 -535 -672
1.401298e-45
Mag XYZ: 58 -531 -669
1.401298e-45
It always returns either the small number shown above or 0 depending on orientation. So I am not sure what is going on at this point. I will look for something silly.
e.g. Z:= math.FMul(math.FDiv(180,3.14159))
This also works (from some of my code - looks like its doing similar)