HiTechnic Compass Sensor value out of limits
Page 1 of 1

Author:  KingofKool [ Thu Nov 05, 2015 8:28 pm ]
Post subject:  Re: HiTechnic Compass Sensor value out of limits

Hi therobotmaster.
Your problem is just a simple maths error.
In simple speak, the compass sensor reads values in a circle. This circle ending at 359 degrees.
But because its a circle, each value has another value that is the same point.
e.g. 20 degrees is the same as 380 degrees, because its one entire loop of the circle.
The problem with this is that 380 is outside the range of values the compass sensor can process.
Luckily there is a simple solution.
Because the circle loops like I described before, if your value is above 359 degrees (the max compass value), all you have to do is subtract 359 degrees to bring the value down one whole loop and back into the range for the compass sensor, which is, as I explained before, still the same point.

so your code would look something like this:

int CheckCompassReading(int reading)
   //if the value is less than 0 add 1 while turn
   if (reading < 0) return reading + 359;
   //if the compass value is above 59 subtract 1 whole turn
   if (reading > 359) return reading - 359;
   return reading;

Hope that helps.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group