Pages: [1]   Go Down
Author Topic: Compass Equations HELP  (Read 472 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Thanks
JBoy_529
Logged

France
Offline Offline
Faraday Member
**
Karma: 39
Posts: 3626
There is an Arduino for that
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
heading = heading % 360;
or
heading %=360;

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
heading = heading % 360;

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

Add a dash of
Code:
heading = (heading < 0) ? 360 + heading : heading;
maybe?
Logged

"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.

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12525
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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!
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

still room for improvement - try change = 1000? then one subtraction is not enough smiley-wink
Code:
heading += change_in_heading ;
  while (heading < 0) heading += 360 ;
  while (heading >= 360) heading -= 360 ;
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: