View unanswered posts | View active topics It is currently Wed Aug 22, 2018 1:31 am






Reply to topic  [ 1 post ] 
HiTechnic Compass Sensor value out of limits 
Author Message
Rookie

Joined: Thu Nov 05, 2015 8:02 pm
Posts: 2
Post 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:
Code:

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.


Thu Nov 05, 2015 8:28 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.