Project contents
← Self-balancing robot
Updates
Self-balancing robot
Every update on this project, newest first.
Updates are bullet-point summaries generated with AI from my commits and build notes, then checked by me. Project articles and learning notes are written by me.
-
- Added velocity feedback at the kit’s reference gain of 7000. In a restrained capture, seven hand-applied disturbances each rose to a peak and decayed to zero, in both directions.
- A simple estimate of the velocity term,
2n * Kp/100, matched four hardware samples within ~10%, so it can now size gain changes. - Found that the forward command
move_xreaches the motors only through the integral term, so with the integral gain at 0 it has no effect.
-
- Found why both wheel encoders read near zero:
TIM_ICInitcleared theCC1Sbits thatTIM_EncoderInterfaceConfighad just set. After the fix, hand-spinning each wheel gave counts up to ±190 per window. - Normalised the right encoder’s sign at the hardware boundary. Rolling the car forward now sums to +8,752 counts and backward to −8,036, against −158 and −66 before.
- Found why both wheel encoders read near zero:
-
- Proved the stale-sensor cutoff on hardware with an injected IMU fault: zero PWM was commanded 25 ms after the last fresh sample and the wheels stopped. A clean image then confirmed the shutdown came from the injected fault.
- Raised the balance gain to the kit’s reference of 10200. The car then showed a 0.35 Hz limit cycle, 30× slower than the pendulum mode, which balance-only control cannot stop.
- Fixed a recovery defect found in review: an angle fault did not reset the fresh-sample count.
-
- Wrote nine host tests for the stale-sensor motor cutoff before writing the cutoff itself. The suite stood at 27 tests, 20 passing and 7 failing, with the failing ones acting as the specification.
- Found that
VelocityPI::compute()updates its internal state on every call, so even a control output that is thrown away still advances the velocity loop.
-
- Caught a safety defect in the plan for the stale-sensor motor cutoff before it reached the firmware. It printed its
STALEwarning before zeroing the motors, and blocking serial output would have kept them driving for 2.7-3.2 ms. The motor write now comes first. - Measured the worst-case sensor
poll()at 4 ms against the 25 ms stale timeout, about 6x margin, so the timeout now rests on a measurement. - Worked out that the observed shutdown latency will be up to ~35 ms rather than 25, since the check also waits on the poll and the telemetry output.
- Caught a safety defect in the plan for the stale-sensor motor cutoff before it reached the firmware. It printed its
-
- Verified TIM2 counts real milliseconds: 6,212 ms on the MCU against 6,208 ms on a PC stopwatch over 63 telemetry lines (ratio 1.001). Cross-checked a second way against the sensor cadence.
- The battery ADC read between 4.96 V and 13.29 V under motor load on an 8.4 V pack, so the measurement is wrong rather than the pack. Leading suspect is ADC sampling that isn’t synchronised with the 25 kHz motor PWM. Not yet confirmed.
- The low-battery cutoff stays disabled until the reading is understood. Averaging more samples first needs a wider accumulator than
uint16_t.
-
- Removed the
delay_ms(5)after each loop. It added 5 ms on top of about 3.34 ms of work, so the loop ran near 120 Hz against a 200 Hz sensor. Motor jitter dropped straight away. - The controller, encoders and telemetry now advance only on a complete fresh DMP packet, because the loop polls 625 to 770 times a second without the delay.
- Added an
IMonotonicClockinterface for the stale-IMU motor timeout. Measured cost: 616 bytes of flash and 8 bytes of BSS, under 1% of the 256 KB flash.
- Removed the
-
- Confirmed the MPU6050 interrupt is on PA12, not PB12. EXTI line 12 is shared across ports and the port is chosen in
GPIO_EXTILineConfig. PB12 is the PS2 controller’s chip select. Cross-checked by extracting the netlist from the board schematic PDF. - Withdrew a finding that the 1300 motor deadzone needed rescaling. The vendored reference uses the same 2880 timer period, so the deadzone and the PID gains carry over directly.
- Capped the FIFO drain loop at 3 reads (about 2.5 ms) instead of 32 (about 27 ms on the bit-banged bus). Past the cap it zeroes PWM, resets the FIFO and counts the event.
- Found that a failed IMU read leaves the last PWM command latched on the motors, with nothing expiring it. A stale-data motor cutoff has to land before the gains go up.
- Added hysteresis to the tilt fault: it trips at 40° and clears at 10°, so the loop only restarts near upright.
- Confirmed the MPU6050 interrupt is on PA12, not PB12. EXTI line 12 is shared across ports and the port is chosen in
-
- Worked out the real control loop period from the telemetry log: about 12 ms against a nominal 5 ms, from 23-sample blocks of repeated angles overflowing a 32-packet FIFO.
- Blocking serial output cost about 3.47 ms per 40-character line at 115200 baud, roughly 70% of a 5 ms tick. Printing one tick in twenty brought that down to about 3%.
- Switched the UART wait from
TCtoTXEbecause it is the correct flag. Measured no speedup, since the baud rate is the limit. - Confirmed the feedback sign was already correct. The suspected inversion came from describing wheel rotation without recording which side of the car it was viewed from.
-
- The register diagnostic now prints each
i2cReadreturn code next to the value. On the bit-banged bus, a zero-initialised buffer made00indistinguishable from a failed read. - Tracked a dead left motor to uninitialised TIM8 config structs. Channels 1 to 3 read the unset complementary-output fields and channel 4 does not, so only the right motor (channels 3 and 4) ran. Value-initialising the structs fixed it.
- Swapping the motor connectors could not separate hardware from firmware. Flashing Yahboom’s vendor image onto the same board did: both wheels drove.
- The register diagnostic now prints each