Software and electronics: I had wanted to add electronic control to my mechanical designs for a while, and this was a good first project. The plan was to have an Arduino Uno read an MPU-6050 gyroscope and accelerometer. After filtering, that data tells the robot its orientation, which can drive PID control of the motors and, in theory, produce a balancing robot. Simple, right? If you want to skip straight to the code, it is on GitHub: Balancing Robot Code.
The first step was designing the chassis. Since most of the components were common, I downloaded their CAD files, which made designing and printing the frame straightforward. Unfortunately, my small battery pack couldn't power both the Arduino and the motors, so I made some modifications with duct tape. In hindsight, this probably made the job easier: a higher center of gravity makes an inverted pendulum easier to balance. (Think of balancing a pen on your finger versus a yardstick.)
Once everything was assembled, wired, and duct-taped, the real work began. My first challenge was reading data from the MPU-6050. There is a library for this chip, but I wanted to do it myself. The MPU-6050 communicates with the Arduino over I2C, so I had to learn that protocol. Once I could read the data, I used some trigonometry to calculate the chip's pitch and roll from the accelerometer, then combined those with the gyroscope's readings. The gyroscope is accurate in the short term, but its readings must be integrated, so they drift over time. The accelerometer is the opposite: stable in the long term but noisy in the short term. Combining the two with the right filter cancels out the weaknesses of each, giving accurate and stable angle readings.
With pitch and roll sorted, I fed them into the motor control and tuned the loop to balance the robot. In my Systems and Vibrations course, I learned how to calculate Butterworth (and other) values for optimal PID tuning. However, nearly everyone online tunes experimentally. Since calculating the exact center of mass of a duct-taped robot would have been a real pain, I joined them and took the experimental route.
After some fine-tuning and a close call involving my cat, the robot could stand on its own. It can even recover from a small shove, though the motors are far too weak to handle anything more.
Ironically, this project was meant to apply what I knew about PID tuning, but what I learned the most about was communicating with the MPU-6050 and processing its data. Overall, I am very happy with the project and how much I learned from it, and I look forward to taking on more challenging projects.
Special thanks to An, who pointed me in the right direction when I got lost.