Go Down

Topic: Compass Equations HELP (Read 709 times) previous topic - next topic


Hey everyone

I am currently building a robot, and i decided to put a compass in it. I want to get the robot to take a reading then subtract or add 90 degrees and turn to that heading.

My problem is what if the heading it takes is 350 degrees. It will then add 90 and get 440. which doesn't work.

I have not made a code for this yet as i want to work this out first.


Code: [Select]
heading = heading % 360;
heading %=360;

look here:http://arduino.cc/en/Reference/Modulo


heading = heading % 360;

Still not quite there:
Code: [Select]
int heading = 45;  // heading NE
int turn = -90; // turn 90 degrees to port
heading = (heading + turn) % 360;


Add a dash of
Code: [Select]
heading = (heading < 0) ? 360 + heading : heading;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


Yes, unfortunately because the % operator is not a modular-arithmetic operator for negative values (a hangover from the early days of C compilers) I would recommend coding things this way:
Code: [Select]

  heading += change_in_heading ;
  if (heading < 0)
    heading += 360 ;
  else if (heading >= 360)
    heading -= 360 ;

Partly because on a microcontroller this will run much faster than the % operator too!
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


still room for improvement - try change = 1000? then one subtraction is not enough ;)
Code: [Select]

heading += change_in_heading ;
  while (heading < 0) heading += 360 ;
  while (heading >= 360) heading -= 360 ;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up