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 if anyone could just write an example code for me or push me to the write place I could get a sample code 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

  • $\begingroup$ what do loops like: while(!GPS.newNMEAreceived()) { character1=GPS.read(); } actually expecting to accomplish? It looks like it keeps throwing away a character from the GPS until some called function has received a complete NMEA sentence. Why would it be throwing away a character? Doesn't the current NMEA sentence need that char? $\endgroup$ Apr 24, 2017 at 16:30

1 Answer 1


The GPS gives the actual coordinates. The compass gives the orientation. The waypoints also have coordinates, and as I see in your code, you have defined movement primitives for your robot.

From the actual and waypoint coordinates you cen compute the heading vector required. You can compare that with the current heading vector from the compass. Your goal is to match the required heading and then match the coordinates of the waypoints.

If you have a current measured value and a desired, waypoint value, in most cases you use a closed loop controller to make the actual value equal your desired value. PID controllers are used frequently for this. Arduino implementation here.

Your problem still is that you have defined discrete motion primitives for moving the robot, so either you go left or forward. It would help you to implement a more flexible motion submodule, which can take a direction vector and move in that direction. This direction vector should come from the unit vector of the difference for between the current position and the waypoint (desired, reference) position. The speed of the motion could (or should) be correlated with the distance between the current position and the waypoint with a threshold at the end.

A function with the header:

void move_robot(Vector2 dir, float speed)

would make it possible to use a PID controller for the heading and another PID controller for the velocity. One takes care of the heading ant the other one of the speed. Both run in the same interrupt.

  • $\begingroup$ be sure to ramp your velocity from 0 to some max and back to 0 so 1) do not upset the robot due to the acceleration/deceleration. 2) use similar considerations when turning. 3) a GPS does not actually give absolute location (more like somewhere in a circle that has a 50foot radius) So, use a Cartesian coordinate system to calculate the difference in locations (as distance and heading) then use the compass and the stepper motors for the wheels to count off that distance. $\endgroup$ Apr 24, 2017 at 16:16
  • 1
    $\begingroup$ (cont) naturally, there can be obstacles in the path of the robot, so need some kind of sensor system to be aware of the environment (ranging and angle using a range sensor(it pings, then counts the time before the echo is heard) is one highly usable (and cheap) sensor, Although a camera with appropriate code to distinguish objects is much better but much more difficult to incorporate. $\endgroup$ Apr 24, 2017 at 16:16

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.