Notes About the iOS Accelerometer, Gyro, Magnetometer and Attitude
All sensors have a frequency you can configure. Use the least possible for the task at hand, to conserve power. Including CPU power.
Sensors are very noisy. It took multiple person-weeks to decide if a phone is on a tripod, and we still had to accept false negatives.
Accelerometer
Measures how fast the device is accelerating.
The acceleration data is measured in all three axes. These axes are relative to the phone, so if you’re in an accelerating train, the axes will be interchanged if you turn around to face the back of the train rather than the front.
The accelerometer:
- Can’t tell you how fast you’re going — it measures acceleration, not velocity.
- Can’t tell you whether you’re going north, south, east or west — it doesn’t know the earth; it knows only relative to the phone. The accelerometer works the same way if you’re in a spacecraft traveling to Mars.
- The acceleration measured also includes acceleration due to gravity.
Gryoscope
Tells you how fast the phone is rotating, again in three axes relative to the phone.
It can’t tell you:
- Which way the phone is pointed
- Which way it’s accelerating
- Whether you’re rotating the phone towards or away from the north pole. It doesn’t know the earth; it knows only relative to the phone. It works the same way if you’re in a spacecraft traveling to Mars.
Magnetometer
Tells you the magnetic field strengths in three axes. It doesn’t directly tell you which way is north. That’s why it’s called a magnetometer and not a compass. And that’s why it’s typically not useful as is.
The magnetometer is affected by metallic objects. Like the metals in the iPhone itself. iOS detects and filters this out. It can sometimes also detect and filter out nearby metallic objects. If you don’t want this filtering, you can ask for the raw data as well.
Unlike the accelerometer and gyro, the magetometer also gives you an error — it measures how inaccurate it is.
Attitude
Attitude means which way the device is pointed relative to a point of reference, like the earth.
This is not a physical sensor. Instead, it combines data from multiple sensors to tell you something useful in the real world.
Attitude is measured relative to X, Y, Z axes. These map to the earth’s axes as determined by a reference frame. In all the reference frames, Z is the vertical axis — the value is zero if you’re pointing at the horizon, positive if you’re pointing up at the sky, and negative if you’re pointing down at the ground.
If you use the xMagneticNorthZVertical reference frame, then X points to the north pole. Of course, once you’ve determined two axes, you’ve determined the third as well — since Z is vertical, and X points to the north pole, Y points east/west.
By contrast, if you use the xArbitraryCorrectedZVertical reference frame, X can point any direction in the horizontal plane, but not up or down. Before we discuss more, we need to understand how the compass and the gryo differ when it comes to error:
If you take one measurement from the gyro, it’s accurate. But if you want to track attitude change over time, you have to take many readings like “+1, +2, -1, +3” and add them together. But each of them has an error, so it adds up. Soon it becomes useless.
The compass is less accurate than the gyro, but it doesn’t get worse over time.
In other words, the gryo starts out more accurate than the compass but after a while becomes less accurate. When you combine data from these two, you get a more accurate result than either sensor by itself. This is called sensor fusion.
That’s what xArbitraryCorrectedZVertical does.