Intro: 2 Wheel Self Balancing Robot From Broken Toy Car
As usual, first of all, I would say, English is not my native language; so please excuse any errors on my part!!
Today, I'd like to share with you how to make a 2 wheel self-balancing robot with Arduino from my boy's broken toy car. Of course, two motors and two wheels are still good. laugh. Balancing itself on two wheels is similar to balancing a stick on our finger. I think everyone have ever tried it before!? To balance, we have to move our fingers quickly or slowly following inclination's direction and inclination's speed of stick. Let's start and discover how Arduino can adjust itself.
Step 1: Preperation
We need to take our times to study the following information:
- P.I.D Closed Loop Control:
- MPU-6050 with I2C Protocol:
- Inverted pendulum:
Step 2: MPU-6050
- The MPU-6050™ from InvenSense, are the world’s first MotionTracking devices designed for the low power, low cost, and high-performance. It is integrated a 3-axis gyroscope and a 3-axis accelerometer
- Tri-Axis angular rate sensor (gyro) with a sensitivity up to 131 LSBs/dps and a full-scale range of ±250, ±500, ±1000, and ±2000dps.
- Tri-Axis accelerometer with a programmable full scale range of ±2g, ±4g, ±8g and ±16g.
Article below is good reference for yaw, pitch, raw motion.
How many types of motion that an aircraft can do. What is the name of the axis about which the motion occurs?
An aircraft can do three types of motion. It can pitch, roll and yaw.
Pitching motion is the motion in which the aircraft’s nose goes up or down. Pitching motion occurs about the lateral axis. (See Fig 1 [a] - [b] - [c]).
Rolling motion is the motion in which one of the aircraft’s wings drops while the other rises. For instance, if the aircraft is rolling to the left, the left wing is dropping while the right wing is rising. Rolling motion occurs about the longitudinal axis. (See Fig 2 [a] - [b] - [c]).
Yawing motion is the motion in which the aircraft's nose moves to the right or to the left. Yawing motion occurs about the normal axis [vertical axis]. (See Fig 3 [a] - [b] - [c]).
Stability and control are much more complex for an airplane, which can move freely in three dimensions, than for cars or boats, which only move in two. A change in any one of the three types of motion affects the other two. But understanding the movements of the aircraft above will help us to easily visualize how the self-balancing robot works!
You can see all motion simulations on video above or link:
- Take note that gyroscope measures the angular velocity. It is the number of degrees that is rotated in a second, so called degrees/second.
- In addition, you need to calibrate the offsets for MPU-6050, check this topic:
Step 3: Sensor Fusion
"Sensor fusion" is the art of combining multiple physical sensors, performs some complex calculations to minimize the errors in each sensor, even though each sensor might be unreliable on its own. Accelerometers and gyros have different inherent limitations, when used on their own.
- The accelerometer has no accumulated error but has the gravity component problem.
- The gyro has accumulated error but is not sensitive to gravity.
-->Sensor fusion: combining 2 sensors together to compensate each other’s weakness.
Check picture above for MPU-6050 connection diagram.
Step 4: P.I.D Closed Loop Control
Here are my P.I.D tests for DC servo motor with SPEED and POSITION control through potentiometer. Let take a look through diagram above & videos below, it may be useful for you before coming to next part.
- SPEED CONTROL
- POSITION CONTROL
Step 5: B.O.M
I reused 2 DC motors and 2 wheels from broken toy car for 1st version. The 2nd version, I bought new motors and wheels.
- Wheel diameter 65mm.
- DC Gear-Box Reducer Electric Motor GA25 620RPM.
Step 6: SCHEMATIC
This is circuit diagram for self balancing robot, include 3 PID tuning potentiometers. In my real design, I add one more potentiometer for options, like adjust motor speed or adjust set point...., & it looks symmetrical.
Step 7: Modify Broken Car (1st Version)
Let see pictures showing how to do a robot from broken toy car.
Step 8: Better Self Balancing Robot (2nd Version)
Because my son loved a version of self balancing robot from his toy. So I had to buy another 2 dc motors and wheels as describing on B.O.M.
Everything is the same as previous version except for motors, wheels and robot's frames.
Step 9: Robot With Led Message (3rd Version)
I added one more layer with LoLShield (Lots of Lights Shield) that I made it by myself previous time. With LoLShield, it consists of 126 single leds arranged into 9x14 matrix & controlled by Charliplexing method to save Arduino pins. It's designed as a "Shield" for Arduino & operating without limit current resistor for LED.
It's so cool now!!!
Step 10: PID CONTROL DIAGRAM
Self-balancing robot is similar to an inverted pendulum and balancing an inverted pendulum is a challenge, because it is not stable. To deal with this problem, we applied a PID controller that uses angle feedback, generate PWM to control motors and keep the robot balancing.
The P.I.D Set Point (SP) is the equilibrium point (vertical) that can be found during calibration. Or it can be slightly adjusted at fine-tuning period. If robot's body is perfect, balanced and symmetrical, then Set Point will be 180,0 degree, in my case it is 180.70 degree. Why 180,0 or 180.70 degree, let read the program below. Feedback signal or Process Variable (PV) is a combination of Gyroscope and Accelerometer collected from the MPU-6050. The P.I.D Output is PWM signal that continuously control two DC motors so that the PV achieve the SP.
An simplistic self balancing algorithm with PID controller would be applied as following:
- If robot is tilting forward, PID controller will adjust motors to move wheels in the direction forward until got balance point (Set Point).
- Else, if robot is tilting backward, PID controller will adjust motors to move wheels in the backward direction until got balance point (Set Point)..
- Else, robot's at balance point, do nothing.
As I said before, the gyroscope can measure the tilt speed of robot so feedback signal from the MPU-6050 will give PID controller a proper adjustments based on inclination angle & inclination speed.
With addition of potentiometers to adjust P, I, D coefficients, it saved us an immeasurable amount of time while tuning the PID controller. The coefficients P, I and D are found as follows:
- Set all P, I, D potentiometers to 0.
- Increase P potentiometer until the robot starts to oscillate rapidly around the equilibrium point but the robot does not fall.
- Increase D potentiometer until robot is no longer oscillating. At this time, robot works relatively stable but will be oscillated when pushing at one side.
- Gradually increase I potentiometer until the system operates smoothly even if robot is pushed at one side. If "I coefficient" becomes big it will cause robot to respond slowly.
Step 11: Robot Library
Step 12: Main Program
Step 13: Program Explanation
KP, KI, KD coefficients can be found easily through 3 potentiometers following previous step. Another way is tuning PID through Serial but it will not work effectively due to the Arduino randomly pending / freezing. Let check the topic:
With main program, if you got trouble like hanging/ pending/ freezing, try running it without the Serial prints.
With 2nd & 3rd version robots, balance point is 180.70 degree & the P.I.D coefficients are:
- KP = 10.50.
- KI = 67.44.
- KD = 0.88.
With toy version robots, balance point is 178.70 degree & the P.I.D coefficients are:
- KP = 17.10.
- KI = 301.07.
- KD = 1.20
Step 14: Testing Toy Robot
Robot from toy is easy to find balance points because of its small spines. Due to broken toy, it is slightly crooked. As video, it worked still good.
Step 15: Testing 2nd Version Robot
- First test:
- Carry things test:
- Carry things and un-leveled road test:
- Slope test:
The two-wheeled self-balancing robot worked well even on rugged roads, sloping surfaces, on rugs, on mattresses and carrying lightweight things on it. It can also regain balance when it is rotated left - right or pushed forward - backward.
Step 16: Testing Robot With Led Message (LoLShield)
It showed some animations as: test scrolling, double sine wave, plasma effect and so on.....
I'd satisfied with this results!
Step 17: Summary
Fine-tuning PID parameters through potentiometers will give us a better understanding of the closed loop control that is inherently complicated and objects motion simulation through the MPU-6050 gives us a real sense of motion in space. With above sharing, I think it will definitely be many mistakes. I am looking forward to your contributed suggestions to do better.
The self-balancing robot project is an exciting experience because it's the first time my son loved the toy that I created and started asking questions about the Arduino. It also helped me to relieve stress and seemed to find a balance for myself.
You can see all videos at:
HAPPY NEW YEAR 2018!!!