I am using the adafruit 9DOF sensor and I am using it as an ahrs system. i am able to read the heading which is a float having a range from -180 to +180. I need to convert the absolute position to a relative position system, in which the starting point can be anywhere in the -180 to +180 range. My output is as follows:
1225 corresponds to the initial starting point, 1700 - 20 degrees or greater clockwise, and 750- 20 degrees or greater counterclockwise. One problem already in mind is the the spot at which the sensor data transition from +180 side to the -180 side.
Any ideas on how to approach this??
I figured this was more of a math question than code, so I don't have any code at the moment.
Thanks
With integer values you can use the modulo (%) operator to map the values into +-180. If you really need floats, subtract 360 while value>180, or add 360 while value<-180.
You need to have some idea of how fast the position can change in between readings. Your Arduino is probably able to read the heading sensor so quickly that most readings would only be 1 degree different from the previous reading. Maybe your unspecified flying thing could turn as much as 5 degrees between readings in the absolute worst case. Then you know any difference larger than this has passed through the +/-180 position and you need to add or subtract 360.