Contents

CMMotionManager

The object for starting and managing motion services.

Declaration

class CMMotionManager

Mentioned in

Overview

Use a CMMotionManager object to start the services that report movement detected by the device’s onboard sensors. Use this object to receive four types of motion data:

  • Accelerometer data, indicating the instantaneous acceleration of the device in three dimensional space.

  • Gyroscope data, indicating the instantaneous rotation around the device’s three primary axes.

  • Magnetometer data, indicating the device’s orientation relative to Earth’s magnetic field.

  • Device-motion data, indicating key motion-related attributes such as the device’s user-initiated acceleration, its attitude, rotation rates, orientation relative to calibrated magnetic fields, and orientation relative to gravity. Core Motion’s sensor fusion algorithms provide this data.

The processed device-motion data gives the device’s attitude, rotation rate, calibrated magnetic fields, the direction of gravity, and the amount of acceleration the user contributes to the device.

You can receive live sensor data at a specified update interval, or you can let the sensors collect data and store it for retrieval later. With both of these approaches, call the appropriate stop method (stopAccelerometerUpdates(), stopGyroUpdates(), stopMagnetometerUpdates(), and stopDeviceMotionUpdates()) when you no longer need the data.

Receive regular motion updates

To receive motion data at specific intervals, the app calls a start method that takes an operation queue (instance of OperationQueue) and a block handler of a specific type for processing those updates. The motion data is passed into the block handler. The frequency of updates is determined by the value of an interval property.

Sample motion data periodically

To sample motion data periodically, start a motion service using a method that takes no parameters and periodically access the properties of the CMMotionManager. This approach is the recommended approach for apps such as games. Handling accelerometer data in a block introduces additional overhead, and most game apps are only interested in the latest sample of motion data when they render a frame.

Determine hardware availability and state

If a hardware feature (for example, a gyroscope) is not available on a device, calling a start method related to that feature has no effect. You can find out whether a hardware feature is available or active by checking the appropriate property; for example, for gyroscope data, you can check the value of the isGyroAvailable or isGyroActive properties.

Identify the coordinate axes of the device

To interpret accelerometer, gyroscope, or attitude information, you need to know the orientation of the device’s coordinate axes. The following illustration shows the positive x-axis, positive y-axis, and positive z-axis for motion-capable Apple devices.

[Image]

Topics

Determining the Availability of Services

Determining Which Services Are Active

Managing Device Motion Updates

Managing Accelerometer Updates

Managing Gyroscope Updates

Managing Magnetometer Updates

Accessing Attitude Reference Frames

Understanding Errors

See Also

Essentials