Article

Understanding Reference Frames and Device Attitude

Learn about the initial frame of reference used to report rotation and attitude values.

Overview

The device attitude (or device orientation) reported by the device-motion service is always specified relative to a fixed reference frame. You decide which reference frame to use based on the interfaces you use to start the service. You can transform attitude values to other reference frames as needed.

The CMAttitudeReferenceFrame type defines several fixed initial reference frames that you may use. Each reference frame assumes a device that is lying on a flat surface and is rotated in a particular direction. For example, the xMagneticNorthZVertical reference frame corresponds to a device whose x axis points toward magnetic north, as illustrated in Figure 1. Other reference frames assume a slightly different device orientation.

Figure 1

An initial reference frame where the device’s x axis points to magnetic north

The positive x axis corresponds to north.

The attitude property of the CMDeviceMotion object contains pitch, roll, and yaw values for the device that are relative to the reference frame. These values provide the device’s orientation in three-dimensional space. When all three values are 0, the device’s orientation matches the orientation of the reference frame. As the device rotates around each axis, the pitch, roll, and yaw values reflect the amount of rotation (in radians) around the given axis. Rotation values may be positive or negative and are in the range -∏ to ∏.

Figure 2 shows how pitch, roll, and yaw are reported relative to the initial reference frame. Pitch reports rotations around the device’s x axis, roll reports rotations around the y axis, and yaw reports rotations around the z axis.

Figure 2

Pitch, roll, and yaw axes

Pitch refers to rotation around the X axis. Roll is rotation around the Y axis. Yaw is rotation around the Z axis.

For some types of apps, you can simplify your calculations by using the device’s current attitude as the starting point for tracking changes. For example, a baseball app that measures the user’s bat swing might use the current attitude as the starting bat position. As the user swings the bat, the app would then track only the changes from this initial start position.

Listing 1 shows how a baseball app would establish a new initial attitude for tracking the user’s bat swing. The startPitch method saves the device’s current attitude immediately before the swing begins. As the user swings, the drawView method multiplies the device’s current attitude by the inverse of the reference attitude, which yields the amount of change between the two attitudes. It then uses this transformed data to update the bat’s position onscreen.

Listing 1

Getting the change in attitude prior to rendering

-(void) startPitch {
    // referenceAttitude is a property
    self.referenceAttitude = self.motionManager.deviceMotion.attitude;
}
- (void)drawView {
    CMAttitude *currentAttitude = self.motionManager.deviceMotion.attitude;
    [currentAttitude multiplyByInverseOfAttitude: self.referenceAttitude];
    // Render bat using currentAttitude
    [self updateModelsWithAttitude:currentAttitude];
    [renderer render];
}