Projects / Line-following robot
Robotics · about 10 hours · 4 phases
Build a line-following robot
A line follower is a small robot that drives along a dark line on the floor. A row of light sensors under its nose tells it where the line is, and it steers to stay on top of it. That sounds easy until you try. Turn too little and it shoots off at a corner. Turn too much and it zigzags. Then every real part adds its own trouble: sensors are noisy, motors are slow to respond, and the chip only looks at the sensors so many times a second. In about 10 hours you will build a simulator, teach a robot to follow a line two different ways, and then make it cope with the things that go wrong on real hardware.
You do not need any hardware. You work in four phases of about 2.5 hours each, and every phase ends with something you can run and look at. By the end you will have:
- a simulator with a two-wheeled robot, five line sensors and tracks you can draw, plus a picture of every run,
- a bang-bang follower and a PD follower, and lap times that show what the second one buys you,
- a follower that keeps going with noisy sensors, late readings and a slow control loop,
- the same controller written in integer maths with a motor dead zone handled, and a final test that checks it on three tracks.
You only need basic Python: functions, classes, lists and running a script. Everything else is explained when it first shows up.
The four phases
Build the robot's world
You end with a simulator where a robot drives, five sensors read a track under it, and a picture shows a robot with no steering rolling off the line after about 2.6 seconds.
- 1.1 Plan the robot15 min
- 1.2 Move the robot30 min
- 1.3 Add motors that lag25 min
- 1.4 Draw a track30 min
- 1.5 Give the robot eyes25 min
- 1.6 Run a lap and draw it25 min
Follow the line, the simple way
You end with a controller that finishes both tracks at every speed we try, and three measurements for judging it: lap time, time off the line, and how much it wiggles.
- 2.1 Where is the line?20 min
- 2.2 Bang-bang steering40 min
- 2.3 Measure the wiggle20 min
- 2.4 Find where it stops being enough35 min
- 2.5 Two knobs and what they do25 min
- 2.6 See the zigzag10 min
Steer in proportion
You end with a PD controller that laps the hard track in 6.46 seconds at full speed with a wiggle of 1.0, against 7.69 seconds and a wiggle of 2.13 for bang-bang.
- 3.1 Turn in proportion to the error30 min
- 3.2 Add the brake30 min
- 3.3 Tune it with a sweep40 min
- 3.4 Push the speed30 min
- 3.5 Put the two side by side20 min
Make it ready for real hardware
You end with a controller written in integer maths that finishes three tracks, one of them never used for tuning, with noisy sensors, late readings, a slow loop and a motor dead zone, plus a test that fails when the robot does.
- 4.1 Noisy sensors30 min
- 4.2 Slow loops and late readings30 min
- 4.3 Tune again under real conditions30 min
- 4.4 Integers only30 min
- 4.5 The motor dead zone20 min
- 4.6 A final test10 min