I am working on an ground surveillance robot using an Arduino mega for programming, am using components like the HMC5883L compass, Adafruit GPS for assigning of coordinates (latitude and longitude) which are the way points, I have written up the code for both the compass and the GPS and am able to get information from them, but now what I want for my robot to move to those specified coordinates (latitude and longitude waypoints) which I don't know how to do please do pardon me for asking such question cause I am new to coding GPS and compass and I would appreciate it if anyone could help me out or explain a bit in details what I need to do please find my code here http://textuploader.com/drqwv

  • 1
    Do you remember your high school trigonometry lessons?
    – Majenko
    Apr 21, 2017 at 19:03
  • @Majenko yes I do remember Apr 21, 2017 at 19:07
  • 1
    Then you know where you are, and you know where you want to be. Between you is a right angle triangle. Calculate the angle of the hypotenuse turn to that angle, and drive to your destination.
    – Majenko
    Apr 21, 2017 at 19:08
  • yes I am aware of how to do that cause I have already done it in my code (Turning Calculation) what I need to know is how to write the code that to move the robot in-respect to the coordinates Apr 21, 2017 at 19:33
  • You have code to turn left and right already. How far to turn I guess is the question. You have the angle you want, and you have a compass. There's multiple ways of working out how long to turn for. Experimentation may give you a specific time-per-degree, or you could turn some amount and see how far it actually turned then use that to calculate how long to turn for the rest of the turn. Then just drive straight until you're there.
    – Majenko
    Apr 21, 2017 at 19:58

1 Answer 1


Due to my robot I have been thinking about this problem quite a few times but I never got to coding. So this is a purely my thinking no experience.

The problem is not only calculating where you are and where to go. The robot needs to move so it needs to know whether it needs to turn (left or right; slow or fast) to drive into the correct direction.
So basically you want a method that takes the compass direction the 2 gps locations and returns a speed and turn value.
The speed and turn value will be different for a waypoint and the final destination. I thought to do so by switching to the next waypoint before coming in the "slow down area". This should make that only one algorithm can handle both situations.
I think the algorithm is likely to look like this.
Given the 2 gps locations calculate the expected compass reading.
Given the 2 gps locations calculate the distance
Based on the distance and the difference between the expected and actual compass reading decide on the speed and turn.

As to the last step there is no "one size fits all" for instance a skid driven robot may opt to turn on the spot until it hits the expected compass reading.
If you have a slow turning robot and you are close to the waypoint you may opt to drive backwards first (like parking a car)

I hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.