Simple Analog Balancing Bot
News, Sept. 25, 2009: Dominique Nardi, a professor in France, has replicated this design in a larger robot. Click here to visit the web site.
July 20 through August 10, 2008
In this episode of Dale's Homemade Robots, Dale tries his hand at building a small bot that stands erect on it's own two wheels. That's all it will do. Balancing on two wheels is hard enough without other complicating factors like moving and turning. That will come later.
OK, where does one start in the quest to design and build a simple balancing robot? Google of course. There is a surprising amount of information out there on this subject.
Here are a few resources I perused.
N-Bot Balancing Robot
Balancing Scooter Homemade Segway like scooter
U-Bot Another Balancing Robot
Crunch Yet another
Kalman Filter Gyro and accelerometer sensor fusion
Vex based balancer This guy made an remote controlled balancing bot mostly from VEX parts.
How PID controllers work Basics of Proportional, Integral, Derivative controls.
Measurement of a Vehicle’s Dynamic Motion Combine Angular Rate Sensors with Accelerometers
Combining gyro and accelerometer with a complimentary filter. A white paper by Shane Colton.
After a few hours of Googling for information on balancing robots and related subjects I came up the basic block diagram below. I implemented it with op-amps an a motor driver chip. The schematic (pdf) is here. Most part values were determined by trial and error using a protoboard.
To maintain balance the bot needs to know the tilt angle and then drive the wheels in the direction of the tilt to keep the center of gravity directly over the wheels and the tilt at zero. It's a feedback loop that drives the wheels to maintain zero tilt. The key to the whole operation is accurate tilt measurement and that turns out to be difficult.
So, how do you measure tilt? It can be directly measured with an accelerometer and indirectly with a rate gyro. My first impulse was to use and accelerometer to directly measure tilt. Simply orientate it perpendicular to the force of gravity and it will measure zero G unless there is tilt, in which case it will output the sine of the tilt angle. Unfortunately that can't be done because accelerometers measure acceleration too (duh). They only output accurate tilt data when not accelerating. Since the bot accelerates when correcting for tilt errors the tilt data is contaminated with acceleration data.
The other way to measure tilt is with a rate gyro. The gyro outputs a voltage in proportion to it's rate of rotation. If you integrate that you get amount of rotation. Rotation is relative to tilt. The gyro signal is not sensitive to acceleraton. Great! But there's a catch. Gyro and integrator drift will cause the zero tilt calibration point to drift with time. In reality a balancing bot based only on the gyro will stand for a few seconds but eventually fall over as the zero drifts.
One partial solution to the gyro vs accelerometer quandry is to use both. Several balance bot builders have solved the problem by using the gyro plus integrator as the main tilt sensor and the accelerometer only to correct long term drift in the gyro and integrator. Accelerometer data is fed to the gyro integrator greatly attenuated so large short term errors are averaged out. Some go so far as to only use the accelerometer data to correct the gyro drift when acceleration is zero or very low. Neither one of these methods remove 100% of acceleration contamination all the time but work well enough to allow the bot to balance. I choose to use the attenuated accelerometer data to correct gyro drift because it was simple to implement in analog circuits and fairly effective.
Wheel Encoders? What wheel encoders?
This version of the balance bot doesn't have any wheel encoders. It has no clue how fast the wheels are actually spinning or how far the bot has moved. Without speed/distance information the bot cannot hold position on an incline. In fact, when given a push it continues to roll as if on a very low friction surface. I believe wheel encoders will be required for the next version that will be radio controlled if it's to move on other than level surfaces.
Is it really a PID loop? Where's the "I"?
Yes it is. We need Proportional, Integral and Derivative of tilt summed to produce a motor torque value. Refering to the schematic, the rate gyro directly outputs "rate of tilt" which is the derivative. R3 adjust the weighting of "D". Integrating the rate gyro produces a signal proportional to "tilt". And finally, the accelerometer provides "tilt" which is summed with the gyro signal before being integrated. This is the signal labeled "Gyro tilt compensation". One integrator produces the integral of both accelerometer tilt and the gyros rate of tilt. So, we have the integral of tilt also. R1 adjusts the weighting of I+P . To change the weighting of the I signal the fixed resistor R13 would need to be changed.
PWM generator block diagram
Ok, lets get down the the real hardware starting with the motor controller. The circuit I designed is an implemenation of locked anti-phase pulse width modulation, also know as a class D amplifier. Input is a voltage that varies from 0 to 5 volts centered on 2.5 . A comparator compares the control input voltage with a 24 KHZ triangle wave on the other input. The output is a square wave with a duty cycle proportional to the control voltage. The square wave drives the DC motor with a L293D motor driver chip. It's wired so the square wave rapidly switches the motor beetween forward and reverse. The motor responds to the average DC level of the square wave. With a 50% duty cycle the DC average is zero and the motor is stopped. As the duty cycle changes the motor runs either forward or reverse with a speed proportional to the duty cycle. A bonus feature of locked anti-phase PWM is automatic braking. When stopped the motor is connected to a low impedance and resists motion.
Locked anti-phase PWM depends on motor inductance to limit current flow when stopped (50% duty cycle) . The idle current is inversely proportional to the PWM frequency and inductance. If the inductance or PWM frequency is too low excessive current will flow. Placing a power inductor in series with the motor will reduce the current. I used 220uH inductors to reduce the idle current from 800MA to 200MA.
The gear motors I used were from some surplus outlet. They are rated 450 rpm at 12 volts. This bot runs them on 9.6 volts. Max RPM is around 350. The wheels are 2.25 inches in diameter.
I used a ADXL105 accelerometer made by Analog Devices. It has a full scale of +/- 5 G and a sensitivity of 250 mv per G. To minimize unwanted effects from acceleration I mounted it near the bots center of mass. Originally it was mounted on the top where it moved a lot relative to the wheels. Tilt data was serioulsly contaminated by acceleration. Moving it down to the CG cleaned up the data considerably.
The gyro is an ENC-05 made by Murata. I was given a handful one night at an AHRC RBNO meeting. I was not able to locate a datasheet but did find information on the similar Murata ENC-03. As far as I can tell they are basically the same except for the range of Vcc they will accept. The max rate is 300 degrees per second and the sensitivity is 0.67 mv/deg/sec.
I used an 8 cell nicad pack designed for toy R/C cars. It's rated at 9.6 volts and seems to have about 1000mah capacity. A 7805 regulator drops it down to 5 volts to run most of the analog chips. The L293D motor driver chip is connected directly to 9.6 volt source.
Op-Amps and Comparator
I used National LMC660 quad CMOS op-amp chips because they have rail to rail output voltage swing, fast slew rates and a high bandwidth. The comparator is the good old LM339. Unfortunately it doesn't have rail to rail common mode on the inputs. It's Vcc - 1.5 volts. Connecting the power pin to unregulated 9.6 volts solved that problem.
Adjustment trim pots
I used 10K single turn trimpots to fine tune the PID loop and zero the tilt. In hindsight multiturn trimpots should have been used for the zero adjusters because the correct range for balance is very small and tiny adjustments result in large tilt changes
Getting the polarities right
For the bot to balance, the gyro, accelerometer and motors must all be wired and/or orientated so they act to minimize tilt. To get these right I disconnected the accelerometer signal that corrects gyro drift and turned up only the proportional tilt pot. I observed which way the wheels turned as the bot was tilted. If they turned the wrong way I reverse the motor leads. Then I reconnected the accelerometer and noted the influence it had. If incorrect I simply turned the sensor 180 degrees.
Version 1.0 of the balance bot controller
The whole bot
Hit Counter = 19532
Disclaimer:The information, programs and schematics on this site are provided "as-is," without any express or implied warranty. In no event shall Dale A. Heatherington be held liable for any damages arising from the use of information or programs on this website.