Part 2: balance it with a PID
Flash the balancer, tell it how the IMU is mounted, and tune the gains from a web page on your phone until it stands on its own.
On this page
What the loop does. The ESP32 reads the IMU at 400 Hz. A complementary filter blends the gyro (fast, but it drifts) with the accelerometer (steady, but noisy) into one number: how far the robot leans, its pitch. The PID turns that into a motor command. The P term (kp) drives the wheels under the lean, harder the further it leans. The D term (kd) acts on how fast the lean is changing and damps the swing. The I term (ki) adds up any lean that stays, and slowly cancels it. Two more terms, on the wheel speed (kv) and the distance rolled (kx), stop it from creeping away across the floor. There is no wheel sensor, so the firmware estimates both from what it commanded the motor.
You tune all of this from a web page the robot serves over WiFi. The sketch is teeter_web. teeter_balance is the same controller with the serial monitor only; it has not run on the robot yet.
The PID part passes when the robot stands for 60 s on a flat floor, recovers when released from 8 degrees,
and survives light pokes; and its measured wobble, motor speed and dead zone have been fed back into sim.py.
1Flash the balancer
To have the robot join your WiFi, copy firmware/teeter_web/wifi_secrets.h.example to wifi_secrets.h in the same folder and put in your network's name and password (2.4 GHz only). Without that file the robot makes its own network. Then, with the motor plugged in and the battery on (plug in USB with the battery on only if your board passed the diode check):
arduino-cli compile -b esp32:esp32:esp32 firmware/teeter_web
arduino-cli upload -b esp32:esp32:esp32 -p /dev/cu.usbserial-XXXX firmware/teeter_web
arduino-cli monitor -p /dev/cu.usbserial-XXXX -c baudrate=115200
teeter_web.ino on GitHub Raw file
Hold it upright and still while it starts: it calibrates the gyro. Then open the address it prints, or http://teeter.local. Without wifi_secrets.h, join the network teeter (password balance123) and open http://192.168.4.1.
If it does not work: the start-up message must not say MPU-6050 not found; see IMU wiring. No page: type ip in the serial monitor for the address.
2Tell it how the IMU is mounted
Under "IMU mounting", press the button and follow the page: hold the robot upright and still, then tilt it about 30 degrees forward. It works out which way the chip faces and saves it.
If it does not work: press Cancel and do it again, holding each pose still until the page moves on. If pitch still goes the wrong way, stop: the balance loop will drive the wheels the wrong way.
3Check the motor direction
Hold the robot with its wheels off the ground. Press "Motor test: forward 1 s".
If it does not work: if nothing turns, go back to the motor test in part 1.
4First balance

On a flat floor, press "Enable balance". Hold the robot upright and still within 5 degrees of upright for 500 ms and the PID takes over. Past 22 degrees it lets go and waits. STOP on the page, or the space bar, cuts the motor at once. Keep a hand ready: the snap body has no crash arms.
If it does not work: if it creeps away, falls slowly or shakes, go on to the next step: it sets the trim and the gains.
5Tune it
Change one slider at a time, and only a little. Creeps one way: change trim. Falls over slowly: raise kp. Shakes hard: lower kd. Rolls away: raise kv. When it stands, press "Save to flash". For comparison, the gains tuned by hand on the first Teeter were kp 1.6,
kd 0.19, ki 0.21, kv 2.01, kx 0.137, dead zone 0.005.
If it does not work: press "Simulator defaults" and start again from there. The simulation page shows what each limit looks like.
6Log a run and compare
Let it stand on its own for 20 s, then press "Download last 20 s (CSV)". Compare the pitch with the simulated run above. The simulator's robots, left alone, wobble by about 0.76 deg rms about their own balance point. The simulator uses the first body's numbers, so treat this as a rough guide.
If it does not work: keep tuning. Save the log anyway: part 3 uses it to fit the simulator to your robot.
7Drive it
Under "Drive", hold "Forward" or "Back" (or an arrow key) while it balances. Start at the lowest speed.
If it does not work: if it falls when it starts to move, lower the speed slider. One motor drives both wheels, so it cannot steer: it goes where it is pointed.