IOS Accelerometer and Gyroscope

Source: Internet
Author: User

First, the concept

Accelerometer: Measures the acceleration in three directions of the x, Y, Z axis by sensing the total amount of inertial forces in a particular direction.

Gyroscope: Measures the value of rotation around the x, Y, and z three directions. Feature: If you keep your phone flat while rotating it, the accelerometer value will not change, at which point the rotation value around the z axis is changed. Rotating the device clockwise generates a negative value and rotates counterclockwise to generate a positive value.

Second, coremotion framework

Accelerometers and gyroscopes are accessed through the Core Motion framework. This framework provides classes such as Cmmotionmanager that all the data provided by this class is used to describe how a user moves a device.

There are two ways that Core Motion obtains data:

Push: Acquisition of all data in real time, high frequency of acquisition;

Pull: To collect the data when it is needed;

Accelerometer:

Push:-(void) Startaccelerometerupdatestoqueue: (Nsoperationqueue *) queue Withhandler: (Cmaccelerometerhandler) handler;

Pull:-(void) startaccelerometerupdates;

To determine if the accelerometer is available:

@property (readonly, nonatomic, getter=isaccelerometeravailable) BOOL accelerometeravailable;

to set the time interval for the accelerometer sampling:

@property (Assign, nonatomic) Nstimeinterval accelerometerupdateinterval;

Gyroscope:

push:-(void) Startgyroupdatestoqueue: (Nsoperationqueue *) queue Withhandler: (Cmgyrohandler) handler;

Pull:-(void) startgyroupdates;

Determine if the gyroscope is available:

@property (readonly, nonatomic, getter=isgyroavailable) BOOL gyroavailable;

Set the time interval for gyroscope sampling:

@property (Assign, nonatomic) Nstimeinterval gyroupdateinterval;

Magnetic:

push:-(void) Startmagnetometerupdatestoqueue: (Nsoperationqueue *) queue Withhandler: (Cmmagnetometerha Ndler) handler;

Pull:-(void) startmagnetometerupdates;

determine if the magnetic force is available:

@property (readonly, nonatomic, getter=isgyroavailable) BOOL gyroavailable

to set the time interval for magnetic sampling:

@property (Assign, nonatomic) Nstimeinterval magnetometerupdateinterval

Example code:

detect Shake

mode one: In normal use, the registration value on 3 axes is as high as 1.3g, but getting a value larger than this one usually requires a deliberate force. The accelerometer is unlikely to register a value larger than 2.3g, so you do not need to set a value larger than this. To detect a shake, we can detect a subtle shake by checking the absolute value of the larger than 1.5, and detect a strong shake by checking for values larger than 2.0.

detects any motion on any axis greater than 2.0g:

    

We can implement more complex shake detection, requiring the user to shake a certain number of times to register as a shake:

Way two:

There is also a method for detecting shaking, which is incorporated into the responder chain. iOS provides 3 similar responder methods to detect actions:

When the action starts:-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event;

At the end of the action:-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event;

When the action is canceled:-(void) motioncancelled: (uieventsubtype) Motion withevent: (uievent *) event; (phone ringing or other interference action during shaking)

Third, sensing device movement

In addition to obtaining acceleration data, gyroscope data, and magnetic field data, the Cmmotionmanager can also be used to perceive device movement data. In the same way as before to obtain acceleration data, gyroscope data, magnetic field data, the program can be used in the following two ways to perceive the device's mobile data.

Use code-based methods to obtain device movement data;

Which periodic unsolicited request method is used to obtain the device mobile data;

When you get the device to move data, Cmmotionmanager returns a Cmdevicemotion object that contains the following properties:

Attitude: This property returns the orientation information for the device. The attribute's point is a Cmattitude object that contains the values of the 3 Euler angles of roll, pitch, yaw, and the spatial orientation of the device is obtained through these 3 values;

Rotationrate: This property returns the original gyroscope information, which is the cmrotationrate struct variable, which is basically equivalent to the gyroscope data described earlier;

Gravity: This property returns the gravitational acceleration applied by the Earth's gravity to the device on the X, Y, z axes;

Useracceleration: This property returns the gravitational acceleration applied by the user to the device on the X, Y, z axes;

Magneticfield: This property returns the magnetic field information after calibration. The property value is a cmcalibratedmagneticfield struct variable. Variables of the Cmcalibratedmagneticfield type include field and accuracy two fields, where field represents the intensity of the magnetic field on the X, Y, Z axes, and accuracy represents the accuracy of the magnetic field strength;

Iv. added pedometer for IOS 7

iOS 7 's new Cmstepcounter as a pedometer that gets the number of steps a user can take with the device, and the iOS device uses the phone's built-in hardware to store the number of steps the user walks. Through this class, the program can get the number of steps the user is currently walking, as well as the historical activity data of the user walking.

Note: IOS 8 is replaced with Cmpedometer.

iOS 8 's new Cmpedometer replaces iOS 7 's cmstepcounter as a pedometer that gets the number of steps a user can take to carry the device. The class contains the following methods:

This method, like Cmstepcounter, determines whether the pedometer service is available:

+ (BOOL) isstepcountingavailable;

Determine if the calculated distance is available:

+ (BOOL) isdistanceavailable;

Determine if calculation of floor measurements is available:

+ (BOOL) isfloorcountingavailable;

The method is measured in real time, the first parameter refers to the start time of real-time measurement, the second parameter is block ^ (Cmpedometerdata *pedometerdata, Nserror *error), where pedometerdata is the measurement data, Error is a measure of the wrong message:

-(void) Startpedometerupdatesfromdate: (nsdate*) Start Withhandler: (Cmpedometerhandler) handler;

To stop the step count service:

-(void) stoppedometerupdates;

The first parameter is the start time, the second argument is the end time, the third parameter is block^ (Cmpedometerdata *pedometerdata, Nserror *error), where pedometerdata is the measurement data, error To measure error messages.

-(void) Querypedometerdatafromdate: (nsdate*) Start toDate: (nsdate*) End Withhandler: (Cmpedometerhandler) handler;

IOS Accelerometer and Gyroscope

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.