There must be more than 100 examples of two wheel balancing robots on the internet (just check google), so the temptation to "copy and paste" can be quite high, but you can only learn so much by running someone else's code on someone else's hardware, so I did some research and started almost from scratch.
The Hardware
Balancer wheel fitted to servo on plywood chassis |
Wheels are 75 mm model aircraft wheels
Motors are Futaba FPS148 Servos modified to rotate continuously.
Controller is Raspberry Pi B+ running Raspian
Sensor is MPU6050 6 axis (3 gyro + 3 accelerometer)
Power 2x 18650 Lithium Ion Cells (7.2v)
The battery voltage is regulated using a cheap 3A switch mode buck regulator from eBay.
The software
All written in python (if you want the source code send me a message) I wont post it on-line to help others resist the temptation to "copy and paste".
Balancer Wheel with servo horn and mounting hardware |
Start.I'm a big fan of keeping things simple, but as I quickly found out whilst developing the balancer you can go too far and too simple just doesn't work!
Get tilt angle from sensor
feed data into control algorithm
set wheel speed based on output of control algorithm
Goto Start
Making the Chassis simple did work, making the software simple didn't.
After a little testing I soon discovered that accelerometer data from the MPU-6050 is just too noisy to use for control, especially when the control loop gain is turned up and the robot is oscillating quite rapidly. The solution to this problem is quite clever and as it turns out, quite simple too. You need to use all of the sensor data, and not just a single axis of accelerometer data as I had originally tried to do. The data from the accelerometer and the gyro needs to be combined or fused using a filter. I wont go into the details as others have covered this already, but basically the filter uses the gyro data to determine tilt angle and the accelerometer data to reduce (eliminate?) the gyro drift.
Having implemented this 'Complementary' filter the balancer was instantly more stable, even managing to stutter around the floor for a few seconds and able to withstand a push or two without falling.
**Pic of balancer balancing here**
Success! Well, partial success... I still need to fine tune the controller constants to improve the stability some more, and probably get some bigger wheels to allow the balancer to recover from bigger excursions from upright.
I also need to look into a secondary control loop that will prevent the balancer from creeping forward or backwards and also allow me to control it so that it can be driven around.