# Inverse Pendulum Balancing Robot

## Stick balancing

IPBAR side view

The small cart will move forward and backwards in order to balance the stick with the weight on the end.  I am using this to eventually being able to create a similar balancing robot with only two wheels placed side by side.
The Angle is sensed with this encoder attached to the long stick.
I used a pid controller with the angle as the input and the the motor controller PWM value as the output.  The microcontroller board is an Arduino (see arduino.cc).  I made the motor controller myself and used a wheel/motor assembly based around this.

Currently, it is able to balance itself with some assistance via a dial.

I still need to improve my control algorithm.

## Controller

The controller is basically a PID based on the angle of the stick.  I initially used the following:

$PWM_{out} = k_{1}e+k_2\int_{0}^{t}e\,dt+k_3\frac{d e}{d t}$

where e is the angle error (angle from vertical).

However, I realized that this didn’t work. Since the I term didn’t seem to do anything, I removed it and added a manually adjusted bias to the angle:

$PWM_{out} = k_{1}(e-b)+k_2\frac{d e}{d t}$

where b is the manually adjusted angle bias.  By constantly adjusting the angle bias, I was able to balance the stick as is seen below.

Next, I will add the wheel encoders to the control equation. Using this, I might be able to get it to balance unassisted.

## Source Code

A subversion repository with my code for some test programs, the microcontroller firmware, and a simulator is available here:  http://keegan.webhop.net/svn/tipsybot/.  The microcontroller firmware uses Arduino and the arduino IDE, while the programs that run on the computer use Processing which is Java with additional libraries and some preprocessing and a minimalistic IDE.

## Simulation

The simulator itself is available as a java applet here: http://keegan.webhop.net/applets/tipsybot_e_sim/

The mathematical model is based on an explanation here.

## Videos

First successful assisted balance:

## Pictures

 IPBAR side view Front Back
 Back side of the bx housing the dials and power supply. Serial data stream waveform with annotations A supplies power and houses some dials for tuning the pid. Encoder waveform with annotations. Here it is with the LCD mounted. I later changed the card stock to plastic. The general setup

### 2 Responses to IPBAR

1. Mark Paynter says:

Keegan,

That’s so cool. I’m glad you got your robot working. Can you post videos?

If it’s not balancing perfectly, I can think of a few possible reasons. You said that you were using an encoder to sense the angle. If the encoder is low resolution, that could be an issue. You could try using a low friction potentiometer and reading with the ADC. Another possible issue could be gearbox backlash. That said, yours works, and mine doesn’t. Hope to see it in person soon.

regards,

Mark

• Keegan says:

Although it moves in the right direction when you bump the stick, it doesn’t get close to actually balancing it successfully. I have the encoder set to 4096 steps per revolution which is more than what I’d get out of a potentiometer.

One problem might be that I have trouble setting the exact center point exactly so I’m going to add a dial to adjust that. I also might be able to add a little bit more voltage to the motor power supply because of the voltage drop associated with the motor controller.