# Measuring angle relative to the floor

Our team’s robot is a climber and during the climb the chassis’ angle, relative to ground is changing.
I’d like to use a sensor to measure it and eventually to use this info in order to achieve an autonomous climb.

I don’t know whether to use a gyro or an accelerometer to do that.
Which of them should I use?

Accelerometer measures acceleration along three linear axis. It won’t give you rotational data. A gyro will give you rotational velocity. It won’t directly give you rotational data, but an integrating the gyro will. That is, of course, assuming you have the gyro aligned in the proper orientation.

If you can filter out the signal noise caused by the movement of the vehicle, it will tell you which direction gravity is pointing. And that will give you the rotational angle of the robot.

I think the answer is either one, though in different cases. The “best” approach would be to use both and combine the information from both sensors using a filter. This is probably overkill for what you’re doing, though.

In general, over short periods of time (seconds to a couple of minutes) gyros are good sensors for measurement of angle. They’re differential sensors (they only measure change in angle), though, so they suffer from two disadvantages: 1. they’re susceptible to accumulated error and 2. you have to set the initial angle through some other means.

Accelerometers are somewhat complementary to gyros. As [Ether] says, you can get an absolute measure of angle relative to the local gravity vector, but this only gives a good reading when the sensor is not otherwise accelerating (practically, this means when the sensor is stationary) - under other conditions, the reading has noise introduced into the reading by the other accelerations. If the angle being measured isn’t changing too quickly, and the system isn’t under a constant acceleration, you can get a better measure by averaging the angle reading over time, and all the transient accelerations will more or less cancel out. This is called low-pass filtering.

Kalman filters and complementary filters work by essentially combining these two sources of data in ways that allow each to counteract the other’s short-comings, creating a better estimate of the current angle.

Since you’re probably only interested in tracking the angle for a few seconds and you can start with the knowledge that the robot is flat on the floor, (tl;dr) the gyro is probably the best sensor for your application.

P.S. Another approach would be to use distance sensor(s) (I would probably use infrared) pointed downwards from the bottom of your chassis to measure the distance from the ground, which you could then use to get an estimate of the robot angle.

Thank you for the detailed answer!
I will try to do it with a gyro, and I’ll try to look for some filtering guides online just to make my programming skills a bit better

“Filter” is a general term meaning a system which does some operation on a signal. They encompass a whole sub-field of (usually) electrical engineering called signal processing, so the best I can do is point you to an introductory book to get you started, if that’s what you’re interested in.

Regarding complementary and Kalman filters specifically, there’s a rather good white paper (pdf version) posted to CD a few years ago on complementary filters, that also includes most of what I posted above. I suggest you start here.

You can also get several good webpages that address both complementary filters and Kalman filters by Googling on complementary filters.

To quote from the white paper, though, “[Kalman filters are] mathematically complex, requiring some knowledge of linear algebra.” Understanding the modern derivation of them also requires a basic understanding of multivariate probability. Assuming you’re still in high school, these may be a little out of reach for you until you’ve taken some college courses. However, there’s a fantastic online course on Udacity called “Artificial Intelligence for Robotics” that includes a lesson on Kalman filters. If you’re a good math student and are feeling adventurous, try it out - it does a good job of breaking down the algorithm so it’s more accessible (much more so than trying to figure out the raw equations on the Wikipedia page).

If you keep doing robotics after high school, check out Probabilistic Robotics by Thrun (who also teaches the online course I linked), Burgard, and Fox after you’ve gotten some college math courses under your belt. It’s a great overview of techniques for state estimation in robotics from some of the leaders in the field.

Here’s a very simple low-pass IIR filter:

``````filtered_value = a*filtered_value + (1-a)*new_value
``````

“new_value” is the new reading from the accelerometer.

“filtered_value” is the filtered version of the accelerometer reading.

“a” is a tuning constant which tunes the strength of the filter. “a” has a range of 0 to 1.

When a=0, there’s no filtering – the filtered_value always equals the accelerometer reading.

As “a” approaches 1, the filtering gets more aggressive – the filtered_value has less noise (but more phase lag).