Lessons and the learning game

Build a boat autopilot

A simulated tiller pilot: a tilt-compensated compass, a limited rudder, a PID heading loop that knows 359 from 1, and GPS waypoints. Never for navigation.

What the course teaches

Six lessons in three chapters, at /learn/path/build-boat-autopilot on /learn. It builds on Control systems and PID and Accelerometers, gyros and IMUs rather than teaching them again.

  • Which way is north. The LSM303AGR's field turned into a heading with atan2 and kept in 0 to 360, the declination that turns magnetic into true (11 degrees west at Annapolis), and tilt compensation from the accelerometer: at 20 degrees of heel the flat formula reads 329 for 11, and the compensated one reads 11.
  • Steer. A rudder servo held to 30 degrees and 40 degrees a second, then a PID heading loop with the error wrapped to -180..180 (course 1 from heading 359 is +2, not -358), derivative on the turn rate, a knock it recovers from, and a deadband that stops the rudder hunting in waves.
  • Navigate. A GPS fix, haversine distance and bearing, an arrival radius, and a route of waypoints sailed end to end.

The model, and the warning

The simulator has no water. The boathelm sketch models the boat: its turn rate eases toward 0.6 degrees a second per degree of rudder with a one second lag, and it moves at 4 m/s from the GPS fix. Dragging the compass's heading knocks the modeled bow; heeling it tests the tilt compensation. Real boats add current, leeway, wind and waves the model does not have.

This course is for learning. It is not for real navigation: a real autopilot is a tested product, and a person stays on watch.

The circuit

Part Wiring
LSM303AGR SDA and SCL, 5 V
Rudder servo signal on pin 9, its own 5 V
NEO-6M TX to pin 8, RX from pin 7
Rotary knob CLK on 2, DT on 3, SW on 4: 5 degrees a click, press for AUTO or STANDBY

The sketch takes auto, standby, course N, rudder N, compass, waves on, waves off, nav and status in the Serial Monitor.

Troubleshooting

  • The heading is off by about 11 degrees. That is the declination: check DECLINATION for your place.
  • The heading jumps when the boat heels. TILT_COMP is off.
  • The rudder works all the time in waves. Raise DEADBAND above the yaw the waves cause.
  • "no GPS fix yet". Wait a second or two after Run.
  • The boat circles a waypoint. The arrival radius is too small for its turning circle.

Paths and lessons explains how a course, its checkpoints and its chapter quizzes work.