Go Down

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

#### jboy_529

##### Apr 04, 2011, 12:22 pm
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

#### fdufnews

#1
##### Apr 04, 2011, 12:46 pm
Code: [Select]
`heading = heading % 360;orheading %=360;`

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

#### AWOL

#2
##### Apr 04, 2011, 01:33 pm
Quote

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

Add a dash of
Code: [Select]
`heading = (heading < 0) ? 360 + heading : heading;`
maybe?
"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.

#### MarkT

#3
##### Apr 04, 2011, 06:00 pm
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 ]

#### robtillaart

#4
##### Apr 04, 2011, 09:51 pm
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

Please enter a valid email to subscribe