Thomas Green

Embedded control · Applied machine learning

Straight-line control on a differential-drive robot

A small differential-drive robot curves when both motors get the same command. We compared four ways of correcting this. The last was a network trained on the robot to copy the best of the other three. Fusing two heading sources fixed the drift. The learned controller did no better, because the heading estimate it learned to copy was drifting.

Context
MSc Robotics, Bristol
Role
Control stack and experimental design, in a team of three
Period
Autumn 2025
Report
Full report (PDF)
Code
tgreen-fe/3pi-imitation-control
Lateral deviation against distance for sixteen runs. Five open-loop runs reach about 0.6 metres by 2 metres. Five PID runs reach 0.12 to 0.32 metres by 1.9 metres. Three fused-heading runs stay within about 0.05 metres to 3.2 metres. Three learned-controller runs spread from minus 0.12 to plus 0.26 metres.
All unweighted runs, by controller. The open-loop and PID runs were shorter, so their traces stop earlier.
0.60 mdrift at the end of an open-loop run on a fault-free robot
43trainable parameters in the network, trained on the robot
0.25 mlargest gap between odometry and the tape measure

The problem

With the same PWM on two nominally identical motors, the robot curves. Ours curved left, about 0.6 m off the line over a two-metre run, with no fault on the robot. Manufacturing tolerance, tyre wear and an uneven floor are enough to cause it.

A gain tuned on the bench does not hold with a payload over one wheel. We worked up from the simplest controller to one that learns its own correction, and measured the finishing distance off the line each time.

Four controllers

  • Open-loop PWM. The same duty cycle on both motors. This is the baseline.
  • PID per wheel. Closed loop on encoder wheel speed, with gains tuned by Ziegler-Nichols. It matches the two motors to each other but does not correct heading. It halved the drift.
  • Fused-heading correction. Steers on heading from odometry corrected by the gyro. The gyro is noisy but picks up real rotation. Odometry is smooth but misses wheel slip. Neither is usable on its own.
  • A network trained on the robot. Five inputs, one hidden layer of six units, 43 parameters. It runs in a 30 ms control step on the ATmega32U4 alongside the rest of the firmware. It is first trained to copy the fused-heading controller's steering commands, then tuned online against a shaped score. The weights are written to EEPROM, so a policy survives a power cycle.

How it was measured

Odometry was under test, so the main measurement came from outside the robot. Graph paper ran the length of the track. A 3D-printed jig set the same starting pose for each run. We read the finishing offset off the paper with a tape and also logged odometry.

A Pololu 3Pi+ 32U4 robot on a lab bench in front of a laptop showing the Arduino IDE, with the sketch open and a board-not-available error in the console.
A Pololu 3Pi+ 32U4, with two encoders, a five-element line sensor array, an LSM6 inertial unit and very little spare compute.
The robot in a 3D-printed alignment jig on graph paper. The jig brackets the wheels so the robot starts square to the grid.
The alignment jig. Without it, the starting heading varied by more than the effect we were measuring.
A sealed food container labelled 150 g strapped to a printed platform over one side of the robot.
150 g over one wheel. We repeated this on each side to test turning bias in both directions.

Results

PID roughly halved the open-loop drift. Fusing the two heading sources removed almost all of it. Unweighted, the robot finished within a centimetre or two of the line on every run.

Unweighted, the learned controller finished about 0.03 m off against 0.01 m for fused heading, and its runs were much less repeatable. With 150 g over one wheel it finished around 0.05 m off against 0.11 m.

Measured deviation at the end of each run, by controller and load. Unweighted: open-loop plus 0.60 metres, PID plus 0.32, fused heading minus 0.01, learned controller minus 0.03. With 150 grams on the left: fused heading plus 0.15, learned plus 0.04. With 150 grams on the right: fused heading minus 0.07, learned minus 0.05.
Tape measurements against the track, three runs per condition. The learned controller finished further off the line unweighted and closer under load. We had not expected this.

Source of the error

At the end of a run, the robot's odometry and the tape measurement disagree by up to a quarter of a metre. That gap is the robot's own position error. The controller was steering on odometry.

Paired bars of odometry estimate and tape measurement for the fused-heading and learned controllers under three load conditions. The pairs disagree in every condition. The largest gaps are for the learned controller under load, with odometry at plus 0.29 metres against a measured plus 0.04, and minus 0.21 against a measured minus 0.05.
The robot's own estimate against the tape measurement at the end of each run.

The fusion weights odometry heavily. The gyro only adds a small correction term, so the fused heading inherits odometry's slow drift. The network was trained to copy a PI controller running on that same estimate. It learned that odometry predicts the expert's output. It also learned to ignore the disagreement between the two sensors, which was the one input that could have shown the estimate going wrong.

The fault is in the training signal. Two changes are worth trying. Weight the gyro more heavily in the fusion before generating training data. Train over a distance long enough for the drift to show up in the score. Our runs were about three metres, which is roughly where the divergence starts.

Summary

The learned controller was the part of the brief I was most interested in. The fix was combining two heading sources that fail in different ways, which we found by measuring from outside the robot.

The result went against our hypothesis. The odometry comparison above shows the cause.

Credit

This was a three-person study for the Robotics Systems unit, with Callum Edwards and Leon Weyrich. I wrote the embedded control stack: the drive loop, the odometry-gyro fusion, the PI expert, calibration and logging. I designed and built the test setup and ran the experiments. Leon did most of the neural policy and its online tuning phase. We shared the analysis and report.

The submitted report is titled around reinforcement learning. The implementation is imitation learning from a PI expert, followed by a tuning phase with no exploration, so it does not estimate a policy gradient. This page uses the more accurate description, and the repository explains the difference against the code.

← All projects