knaxuslearn · build · ship

Projects / Line-following robot / Wrap up

Wrap up

Keep going

Pick one when you want more:

  • An integral term. Add the I of PID, with a limit so it cannot wind up. See whether it helps on a track that curves the same way for a long time.
  • Slow down for corners. Drop the speed when the error is large and raise it on straights. How much faster can the hard track go?
  • Calibrate the sensors. Give each one its own floor and ceiling, the way a real robot learns them at the start of a run, instead of assuming every sensor is identical.
  • Crossings and gaps. Add a track with a crossing and a gap in the line. What should the robot do when every sensor sees line, or none?
  • More sensors. Try seven or eight, and different spacing. Where does an extra sensor help and where does it not?
  • A real chip. Port IntPD to C for a board you own, with a sensor array and two small motors. Expect to tune it again. The numbers in this guide belong to the simulator.

What this version leaves out, and what a real line follower does

We kept the ideas and dropped the mess.

  • Real sensors differ from one another and change with the light in the room. Each one needs its own calibration, and ours all behave the same.
  • Our motors follow a speed command. A real motor driven by plain PWM turns at a speed that depends on the battery voltage and the load. Many robots add wheel encoders and a speed loop underneath the line follower.
  • Our wheels never slip. On a real floor, grip limits how fast you can take a corner, whatever the controller wants.
  • We read all five sensors at one instant. A real chip often reads them one after another, and each read takes time.
  • The noise, the delay and the dead zone in this guide are numbers chosen for practice. Measure your own.
  • Real tracks have crossings, gaps and thin or faded lines. Ours is one clean, closed loop.

Check yourself

Answer each question in your head first, then open it to check.

  1. In drive, why does a robot with wheel speeds of -0.25 and 0.25 turn without moving?
    The forward speed is the average of the two wheels, which is 0, and the turn rate is their difference divided by the wheel base, which is not 0. So the robot rotates on the spot.
  2. Why does the weighted average locate the line better than just asking which sensor sees it?
    The sensors are 12 mm apart, and the line is 2 cm wide, so neighbouring sensors each see part of it. Weighting their positions by how much they see gives an answer between the sensors. A reading of 0.5 and 1.0 on adjacent sensors puts the line at 1.33, a third of the way from the stronger sensor to the weaker one. "Which sensor" can only give five answers.
  3. In Step 3.2, why does a small D term reduce the wiggle, and why did a kd of 0.16 slow the lap?
    The D term responds to how fast the error is changing, so it pushes against a swing that is already closing in on the line, before the robot overshoots. A kd of 0.16 brakes too much. It holds back the turn at the corners, so the robot follows them slowly and the lap takes 12.38 seconds.
  4. Why did bang-bang survive the tidy simulator at full speed but lose the line with a 20 ms delay?
    With a 20 ms delay it acts on where the line was 20 ms ago, which at 0.8 m/s is 1.6 cm of travel. Bang-bang answers every error with a full-strength turn, so each late correction can push the robot further the other way. PD asks for less when the error is small, so late corrections do less harm.
  5. Teach it back: explain to a teammate why the derivative needs the time between updates, and what happened when we gave it the wrong one.
    The derivative is a rate: how much the error changed per second. To get it, divide the change between two updates by the time between them. If the code assumes 5 ms and the loop really runs every 10 ms, the rate comes out twice too big, so kd acts twice as strong. In Step 4.2 that made a lap of 9.34 seconds instead of 8.03.

Sources