I have a parallax compass module, it gives out a degree from 0-360 which I stored in a double.
I was wondering if there is a script, example or anything that will allow me to make my robot
turn a given amount of degrees. I was able to do this until the values rolled over 360 or under 0.
thank you for your help
How about having it turn a certain number of degrees based on the difference in degrees from (current reading) vs (last reading).
Then the absolute number from the compass won't matter.
You can arrange your math so that for example if the change is from 340 to 10 you get 30 right, and from 10 to 350 you get 20 left.
I tried doing that and had trouble. Can you give a example code?
Try this with some examples, I think its correct:
if ( (current_reading - previous_reading) >0){
direction = right_turn;
degrees_to_turn = abs(current_reading - previous_reading);
if (degrees_to_turn>180) { degrees_to_turn = 360 - degrees_to_turn;} // handle 360 rollover
}
else{
direction = left_turn;
degrees to turn = abs(current_reading - previous_reading);
if (degrees_to_turn>180) { degrees_to_turn = 360 - degrees_to_turn;} //handle 0/360 rollover
}
[code]
[/code]