Online Circuit Simulator
Self-balancing robot simulation: inverted pendulum, sensors and AVR control
We typically respond within one business day.
A two-wheeled robot balances on its wheels. A gyro and an accelerometer tell an ATmega328P how the body tilts, an encoder on the motor tells it how far the wheels have turned, and the program drives both motors through an H-bridge 200 times a second. The robot, the motors, the sensors and the chip are simulated together.
Recorded run of the C program: the robot is held at 2.9 degrees for 0.5 s, then let go, and pushed for 50 ms at 3 s. Blue: tilt; orange: travel of the wheels.
What is being simulated?
The body is an inverted pendulum on a pair of wheels: its center of mass is 7 cm above the axle. The two equations of motion come from the energy of the wheels and the body, with the motor torque acting on the wheels and back on the body. Each motor has resistance, inductance and back voltage, and drives its wheel through a 34 to 1 gear.
Left alone with free wheels the robot falls with a growth rate of 16.1 per second: a tilt grows e times every 62 ms. With the motors shorted their back voltage slows that to 8.67 per second.
| Part | Value |
|---|---|
| Body mass and height of its center | 0.6 kg, 70 mm |
| Wheels | 68 mm across, 60 g for both |
| Motors (each) | 4 ohm, 2 mH, 34:1 |
| Battery | 7.4 V |
| Gyro | 5 mV per degree per second |
| Accelerometer | 0.4 V per g, 60 mm above the axle |
| Encoder | 816 counts per wheel turn |
What does the default program do?
While it is held still, the program learns the gyro zero. Then, every 5 ms, it integrates the gyro and pulls the result slowly toward the angle from the accelerometer, which reads gravity but also every push of the wheels. It adds the wheel angle and speed from the encoder and sets the motor voltage from all four: 18.74 V per radian of tilt, 1.84 V per radian per second of tilt rate, 0.56 V per radian of wheel angle and 0.54 V per radian per second of wheel speed.
The gains come from a discrete-time LQR design on the linear model. The C program uses floating point; the assembly program does the same in whole numbers. In the recorded runs the C program holds the robot within 0.15 degrees of upright and the assembly program within 0.21 degrees, and both recover from the push after leaning 7.4 degrees.
Default program in AVR assembly
Default program in C
Lessons and challenges
- Falling. Turn the motors off and let go. Measure how fast the tilt grows and compare it with the growth rate above.
- Tilt only. Keep the two tilt terms and set the wheel terms to zero. The robot stands for a moment, then runs away: in the run it went 1.5 m and fell.
- Full state. Put the wheel terms back. Check: within 0.35 degrees of upright from 3 s on, within 30 mm of where it started.
- Real sensors. A faster design, with twice the tilt gain, balances well with perfect measurements but rocks with these sensors: about 1.8 degrees in the run, against 0.15 with the default.
- Push. A 2 N push for 50 ms at 3 s. Measure how far it leans and how far it rolls before it settles.
- Challenge: whole numbers. Port a change of gains from the C program to the assembly program and keep the same behaviour.
Where do the parts go?
You can move the sensor board up and down the body and move the battery, which moves the center of mass. A higher center of mass falls more slowly and is easier to balance; a sensor far above the axle feels more of the wheel acceleration.
How were the numbers checked?
The model was checked against the small-angle fall of the linear model, its unstable pole with the motors shorted and conservation of energy in a large fall without friction. The closed loop was run with sensor noise, a gyro offset, ADC steps and the PWM of the motors.
Get your project started
We typically respond within one business day.