iOS Learning note 34-accelerometer and gyroscope

Source: Internet
Author: User

I. INTRODUCTION of COREMOTION Framework

We know that there are some iOS apps that have special requirements, such as:
1. E-Compass Compass application: Let us know the direction.
2. Sport type Software: Let us know how many kilometres we run.
3. Shake and shake function in social software.
4. Play in the role class in the game according to the device's shaking and so on operation.

In fact, they're mostly using a core motion framework in iOS.CoreMotion.framework

    • Use the framework that iOS provides to us CoreMotion , primarily to access the relevant data for accelerometers and gyroscopes .
    • Not only does it provide you with real-time acceleration values and rotational speed values, but more importantly, Apple integrates a number of algorithms that can directly give you the acceleration to peel off the gravitational acceleration component, eliminating your high-pass filtering operations, and providing you with three-dimensional location information for a dedicated device.
Sensor Introduction:
    1. Acceleration Meter:
      The accelerometer principle is very simple, now the phone is basically equipped with 3-dimensional line sensor, that is, to measure the acceleration of x, Y, z three axes. Acceleration is the force that acts on an object as it accelerates, like the gravity of the Earth.
    2. Gyroscope:
      The main function of a gyroscope is to measure the rotational rate along a particular axis, based on the theory of conservation of angular momentum. In use, the gyroscope's rotor rotates at high speed, always pointing to a fixed direction, and the gyroscope can feel it when the moving object is moving in a direction that deviates from the predetermined direction.
Second, the use of coremotion

Coremotion is mainly responsible for three kinds of data:
    • Acceleration valueCMAccelerometerData
    • Gyro valueCMGyroData
    • Device Motion ValueCMDeviceMotion

In fact, the motion value of this device is calculated by the transformation of acceleration and rotational speed.

Cmdevicemotion Property Description:
    1. attitude: In layman's terms, the location and posture of your phone in the current space
    2. gravity: Gravity information, whose essence is the expression of the gravitational acceleration vector in the reference coordinate system of the current device
    3. userAcceleration: Acceleration Information
    4. rotationRate: Instantaneous rotation rate, is the output of the gyroscope
Steps to use Coremotion:
    1. Initializing the CMMotionManager Management object
    2. Call the object method of the management object to get the data, there are 2 ways
    3. Working with Data
    4. Stop getting the data when you don't need it
-(void)stopAccelerometerUpdates;//停止获取加速度计数据-(void)stopGyroUpdates;//停止获取陀螺仪数据-(void)stopDeviceMotionUpdates;//停止获取设备motion数据
There are 2 ways to get data in Coremotion:
    1. PushWay:
      Provides a thread manager NSOperationQueue and a callback Block that CoreMotion automatically callback this when each sampled data arrives and Block is processed. In this case, Block the actions in will be executed within your own main thread.
    2. PullWay:
      You have CMMotionManager to take the initiative to get the data, which is the last sampled data. If you don't, you CMMotionManager won't.
1. The accelerometer uses Pull method to obtain:
- (void) useaccelerometerpull{//Initialize global management ObjectsCmmotionmanager *manager = [[Cmmotionmanager alloc] init]; Self. Motionmanager= Manager;//The accelerometer is not available to determine if the accelerometer is turned on    if([manager Isaccelerometeravailable] && [manager isaccelerometeractive]) {//Tell manager, Update frequency is 100HzManager. Accelerometerupdateinterval=0.01;//Start the update and the background thread starts running. This is the Pull method. [Manager Startaccelerometerupdates]; }//acquisition and processing of accelerometer dataCmaccelerometerdata *newestaccel = Self. Motionmanager. Accelerometerdata;NSLog(@"X =%.04f", Newestaccel. Acceleration. x);NSLog(@"Y =%.04f", Newestaccel. Acceleration. Y);NSLog(@"Z =%.04f", Newestaccel. Acceleration. Z);}
2. The accelerometer uses push mode to obtain:
- (void) useaccelerometerpush{//Initialize global management ObjectsCmmotionmanager *manager = [[Cmmotionmanager alloc] init]; Self. Motionmanager= Manager;//The accelerometer is not available to determine if the accelerometer is turned on    if([manager Isaccelerometeravailable] && [manager isaccelerometeractive]) {//Tell manager, Update frequency is 100HzManager. Accelerometerupdateinterval=0.01; Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//push Way to get and process data[Manager Startaccelerometerupdatestoqueue:queue withhandler:^ (Cmaccelerometerdata *accelerometerdata,Nserror*error) {NSLog(@"X =%.04f", Accelerometerdata. Acceleration. x);NSLog(@"Y =%.04f", Accelerometerdata. Acceleration. Y);NSLog(@"Z =%.04f", Accelerometerdata. Acceleration. Z);    }]; }}
3. Gyro is obtained using push mode, pull mode is not listed, and the accelerometer uses similar:
- (void) usegyropush{//Initialize global management ObjectsCmmotionmanager *manager = [[Cmmotionmanager alloc] init]; Self. Motionmanager= Manager;//Can you judge whether the gyroscope is open or not?    if([manager Isgyroavailable] && [manager isgyroactive]) {Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//Tell manager, Update frequency is 100HzManager. Gyroupdateinterval=0.01;//push Way to get and process data[Manager Startgyroupdatestoqueue:queue withhandler:^ (Cmgyrodata *gyrodata,Nserror*error) {NSLog(@"Gyro Rotation x =%.04f", Gyrodata. Rotationrate. x);NSLog(@"Gyro Rotation y =%.04f", Gyrodata. Rotationrate. Y);NSLog(@"Gyro Rotation z =%.04f", Gyrodata. Rotationrate. Z);    }]; }}

The above code must be on the real machine to work properly, using the above knowledge we can do similar to the effect:

What suggestions can be made in the comments section below!

iOS Learning note 34-accelerometer and gyroscope

Related Article

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.