Hi,
I have built a self-balancing robot, which does not perform at all well.
While it will balance, it will oscillate quickly around vertical which is not bad in itself I guess, but this is even if the kP is very low. In addition, it cannot recover from quite small tilt angle.
I cannot see any significant conceptual difference between my code/build and various other examples on the internet.
Schematic: Attached (fritzing file and picture of fritzing schematic)
Code: Attached.
Video: 20170524 213104 001 - YouTube
What have I tried:
I originally took the accelerometer and gyro readings from the MPU and calculated the angles on the arduino.
I could not get it to balance at all this way, I think it was just too slow to respond, although the time taken to calculate the angle after receiving MPU data was ~800us which didn't seem too slow to me.
Anyway, I changed to using Jeff Rowberg's code to utilise the MPU's onboard DMP features and this made a huge difference - it can now balance a bit, but with the limitations described at the start.
I have spent a long time playing with the PID parameters. Most advice I could find on tuning the PID controller was roughly one of the following 2 approaches:
- increase P until it oscillates, then increase D to smooth, then increase I to correct for drift
- increase P until it oscillates (only a little), then increase I to correct for drift, then increase D to make it correct after a shock
I've tried both, and I cannot seem to get any better than what you can see in the video. One problem is that when increasing P, it goes from not balancing at all to oscillating quickly - there doesn't appear to be much in the middle.
So I'm not really sure where to go from there.
I originally used my own PID controller, although I also tried Brett Beauregard's PID library (which is still in the current code) in case I'd done something wierd, but there was no difference.
In the current code/video I and D are zero.
I have tried having the batteries right at the top, and a bit lower down (as they are now).
I have changed from 4xAA (~5V) to 6xAA (~7.5V) batteries - no change in balancing ability (it just meant that the wheels would turn at slightly lower PWM).
I was considering different motors but in this instructable (http://www.instructables.com/id/Self-Balancing-Robot/) the exact same ones have been used and it does not have my issue
I originally had the MPU6050 on the top of the robot but have now moved it down to be close to the axis of rotation as was suggested. This possibly gave a little improvement.
I've also tried playing with the
- low pass filter setting
- DMP sample/FIFO output rate
- PID calculation interval
What I have not tried:
Encoders, but I'm not concerned about it holding position yet so I don't this this is relevant yet.
Kalman filter - but I've seem plenty of examples without it.
Changing PWM frequency (as done for Balanduino).
Can anyone give me any suggestions for what is causing this (if there's something I've done wrong or badly), or just ideas of things I could try and change? I am running out of ideas.
Any suggestions greatly appreciated.
self_balancing.ino (7.13 KB)