Project contents
Robotics
Self-balancing robot
A two-wheeled robot built to learn robotics
- stm32
- c++
- embedded
- robotics
This project started with a Yahboom self-balancing car kit. After playing around with it and working through some of the supplied tutorials, I decided to turn it into a larger project that I could learn more from.
I have never found strictly following tutorials to be the best way for me to learn. Instead, I decided to replace most of the original C application and board-support code with C++. I normally work with Python and TypeScript, so this gave me a reason to get more experience with modern C++ on an actual embedded system. Rebuilding the firmware has also helped me understand the complete path from an IMU packet to a motor command, while keeping the original project as a reference whenever something goes wrong.
My longer-term plan is to add a Radxa Q6A for vision, path planning and voice interaction. I had originally planned to use a Raspberry Pi 5, but the prices became difficult to justify, so I decided to give the Q6A a go instead. It will send movement setpoints over UART while the STM32 handles the real-time balance loop.
This project also has a Build it yourself section for anyone who would like to try something similar. The robot is not finished and does not balance unassisted yet, but the C++ firmware now reads fresh IMU packets and drives both motors in the correct direction during restrained tests. I will continue updating the walkthrough whenever I complete a major feature or milestone.
Where it is at right now
The car does not balance unassisted yet, but here is what is working, what is currently in development, and what I have planned.
Working now
- The complete restrained signal path: a fresh MPU6050 DMP packet reaches the balance controller, which commands both motors in the physically correct direction
- One control implementation running against both the STM32 hardware and a simulation physics model
- A GCC firmware build that flashes over ST-Link/SWD, with a second compiler building it in CI
- Fresh-sample gating, so a rejected IMU poll cannot advance the controller using cached data
- A TIM2 monotonic clock measured at 1.001 against an independent host clock
- Angle-fault hysteresis, observed tripping at about 40 degrees and clearing near 10 degrees
- Eighteen host tests in source. The last recorded complete run passed 18/18 on 22 August; the local host build currently needs reconfiguration before that result is reproducible again
In development
- A stale-data shutdown that writes zero PWM after 25 ms without a fresh IMU sample
- Nine host tests for its deadline, recovery behaviour and 32-bit timer wrap
- A restrained fault-injection test. The measured worst-case IMU poll is 4 ms, but the final zero-PWM evidence still has to be captured
Planned
- Tuning the upright gains and recording the first sustained free balance
- Validating encoder signs before enabling velocity hold
- Moving the control schedule onto the IMU data-ready interrupt and deciding how the FIFO should be drained without exceeding the loop budget
- Enabling turn control once upright and velocity behaviour are understood separately
- Adding the Radxa Q6A as a second compute domain for vision, navigation and voice interaction
Updates
Recent progress
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.
-
- Finished the firmware side of the serial command link: received bytes now go through a buffer and a parser in the main loop, and commands are clamped, rate-limited and dropped to zero if the link goes quiet for 250 ms.
- Bench-tested it with the wheels off the ground. Arm and disarm keys still worked, a corrupted frame was rejected, and pausing the sender reported link loss after 251 ms. It has not driven on the floor yet.
- Battery readings fell to 5.4-6.0 V with the motors off, unexplained so far; the pack needs a meter check.
-
- Wrote the tests for a serial link that will let a computer send drive commands to the robot: a frame parser with a checksum, a speed governor and a receive buffer. With only stub code behind them, 22 of the 94 host tests failed, as intended.
- Checked the checksum values independently in Python, confirming that the example command frame in the plan was correct.
- Pinned down edge cases in the tests, including the maximum frame length, Windows line endings, and how far speed may change after a long gap.
-
- Designed a watchdogged serial link for driving the car: checksummed command frames with a sequence number, a 20 Hz heartbeat and a 250 ms timeout. Losing the link zeroes the targets and keeps the car balancing rather than disarming it.
- Set the initial limits at ±100 mm/s forward and ±30 deg/s turning. Not yet implemented.