BuildRobotzRobotsTeeter
Teeter.
BuildRobotz robot · Part 3 · RL

Part 3: train a policy with reinforcement learning

Train a small neural network in a MuJoCo simulation of Teeter, check it against the PID, put it on the ESP32 and run it beside the PID.

On this page
  1. 1. Set up Python
  2. 2. Train
  3. 3. Compare it with the PID
  4. 4. Export it to the ESP32
  5. 5. Run it in shadow
  6. 6. Hand over to the policy
  7. 7. Fit the simulator to your robot
Simulation onlyThe policy has been trained and tested in simulation only. It has not driven the real robot. The simulator's motor numbers are estimates, and the one comparison so far says they are off: the gains tuned by hand on the real robot balance it well, but balance only 38 of 60 simulated robots. Run the policy in shadow first, and expect to fit the simulator to your robot before you hand over.

What you train. A network with 18 inputs, two layers of 64 and one output takes the PID's place: 100 times a second it reads the same numbers the firmware already has (pitch, pitch rate, estimated wheel speed and position, the drive target, its last command) and outputs the motor command. Training runs thousands of simulated robots, each with different mass, motor strength, gearbox slack and sensor errors, and rewards the network for keeping them upright and still. In the run that made rl/policies/teeter.zip, standing still for 20 s, the policy kept 59 of 60 random simulated robots up against 56 of 60 for the PID with the simulator's gains.

You need a laptop with Python 3.11 and the robot from part 2, balancing on its PID.

1Set up Python

From the top of the repository:

python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/python rl/plant.py

plant.py checks the training simulator against sim.py, the simulator the PID gains came from.

CheckIt prints PARITY OK.

If it does not work: a missing module means the install did not finish; run the pip install line again and read its last error.

2Train

.venv/bin/python rl/train.py

It prints a line per update: the return, how long the robots stay up, how often they fall. On the reference machine it took 20 minutes for 8 M steps on a 10-core laptop. For a quick look first, add --steps 1000000. Watch it in TensorBoard with tensorboard --logdir rl/runs.

CheckIt ends with saved .../rl/policies/teeter.zip, and in the last lines "fell" is well below where it started.

If it does not work: if the return stops rising and "fell" stays high, train on from where it stopped with --init rl/policies/teeter.zip.

3Compare it with the PID

.venv/bin/python rl/evaluate.py

It runs your policy and two PIDs on the same 60 random simulated robots: standing still, then released at 5 degrees and shoved twice. The policy runs through the same float arithmetic the ESP32 uses.

CheckThe table shows how many robots each controller kept up. Your policy should keep at least as many up as the PID with the simulator's gains.

If it does not work: train longer, or compare your numbers with the published run.

4Export it to the ESP32

.venv/bin/python rl/export_policy.py

This writes firmware/teeter_web/policy.h: the network's weights as C arrays, about 21 KB of flash.

CheckIt prints the parameter count and max |numpy - torch| of about 1e-7: the C version and the trained network agree.

If it does not work: a shape error means the network size in train.py and export_policy.py no longer match.

5Run it in shadow

Flash teeter_web again, as in part 2, and let the robot balance on the PID. The plot now has a third, dashed line: what the policy would have commanded. The policy runs every step; it just does not drive the motor yet.

CheckThe dashed line moves with the PID's line, the same way at the same time.

If it does not work: if it sits at the rail or moves opposite, do not hand over. Fit the simulator to your robot (the last step), retrain, and try again.

6Hand over to the policy

Hold the robot loosely and choose "RL policy" under Controller. The 22 degree cut-off and STOP work the same as for the PID. Choose "PID" to go back; you can switch while it balances.

CheckIt keeps balancing on the policy, and goes back to the PID when you switch.

If it does not work: press STOP or choose "PID". Expect jitter: in simulation the policy's command changes about ten times as much per step as the hand-tuned PID's. Fit the simulator and retrain.

7Fit the simulator to your robot

While it stands on the PID, left alone, download a log from the page ("Download last 20 s (CSV)"). Then:

.venv/bin/python rl/fit.py stand1.csv
.venv/bin/python rl/train.py --init rl/policies/teeter.zip --steps 4000000

fit.py runs the same PID on thousands of random simulated robots and keeps the ones that behave most like your log. Their spread becomes the new training range, and the second line trains on inside it.

Checkfit.py writes out/fit.json and prints the narrowed ranges.

If it does not work: one standing log narrows only one or two numbers. Logs with different gains, a log with a push in it, the robot's weight and its measured no-load wheel speed narrow more. See fitting the simulator.