3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Seg...stick.

Step 9Two sensors, one angle.

Two sensors, one angle.
«
  • stick22.jpg
  • stick21.jpg
  • stick23.jpg
Another common self-balancing question: Does it use an accelerometer? Or a gyro?

Answer: Both.

The reason for using two sensors even though there is only one relevant physical variable (angle) is because each type of sensor has advantages and disadvantages by itself. By mixing the best parts of each together, a better overall estimate of the angle is achieved. I wrote this all up in this white paper, but here I will give a brief summary.

The Accelerometer.
It measures acceleration, right? Well, not really. It measure force per unit mass. So it will measure the force due to gravity as if it were an actual acceleration. The sensitivity for the Sparkfun Razor IMU is given as 300mV/g, meaning the output will change by 0.3V per 9.8m/s^2 of acceleration.

How is this converted into an angle? Well imagine using the accelerometer axis that is pointed in the direction of travel of the vehicle. As the vehicle pitches forward, the axis sees positive force due to gravity. As the vehicle pitches backwards, it sees negative force. It's tempting to say that the gain should be 300mV/90º, since pitching 90º corresponds to 1g. However, it's the slope of the output near 0º that matters, and thanks to trigonometry this slope happens to be 300mV/rad or 300mV/57.3º. See the image below for an illustration of this.

Since the Arduino's ADC gives a 10-bit value based on a 5V reference, you can calculate the gain on the raw ADC value by:

(57.3º/0.3V)*(5V/1024LSB) = 0.932º/LSB

LSB (least significant bit) is just a way of saying one bit. This is the value by which to multiply the raw analog value to get an angle in degrees. Note that you also need to subtract the zero angle offset from the analog signal. This is best found experimentally, by holding the platform at zero angle and reading off the value.

So why not just use the accelerometer to measure the angle and be done? The problem is that the accelerometer can't tell the difference between gravity and actual acceleration. So, if the platform is perfectly level but the vehicle accelerates forwards, it will register the same as tilting backwards. Taking a long-term average, though, the only "acceleration" that remains is gravity. Unfortunately, long-term averaging is not conducive to snappy feedback control. Enter...

The Gyroscope.
More accurately it should be called an angular rate sensor, since it has little to do with an actual flywheel-based gyroscope. It reports back a signal proportional to the rate of rotation. On a balancing platform, its sensitive axis would be parallel to the axis of rotation of the wheels. The Sparkfun IMU gyros have a sensitivity of 3.33mV/º/s on the 4x channels, meaning the output changes 3.33mV for every º/s of rotation.

Since the Arduino's ADC gives a 10-bit value based on a 5V reference, you can calculate the gain on the raw ADC value by:

[(1º/s)/0.00333V]*(5V/1024LSB) = 1.466(º/s)/LSB

This is the value by which to multiply the raw analog input to get an angular velocity in degrees-per-second. Like the accelerometer, the zero offset for the gyro must be subtracted first. It can be found by holding the platform stationary and reading off the analog value.

To get from degrees-per-second to degrees, the gyro signal can be integrated. For every step in time, the gyro signal multiplied by the duration of time between steps gives an incremental change in angle. The total angle is the running sum of these increments. This causes a problem, though: If the gyro signal is not exactly zero when the platform isn't rotating (and it never will be) the integration will drift. With no absolute reference, there is no way to correct for this drift with the gyro signal alone. However, for short durations, the gyro provides a very sensitive angle estimate that is immune to noise from horizontal acceleration of the vehicle

So one sensor is great for short-term, fast-response angle estimates. The other is great for long term, drift-free absolute angle averages. Are you starting to see where this is going?...
« Previous StepDownload PDFView All StepsNext Step »
1 comment
Jan 4, 2011. 9:36 PMluisma.suarez says:
ok, its offcial, you rock! congrats. excellent work i'll pour over your math when i have the time- but gyro AND A meter? really nice!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
19
Followers
1
Author:scolton