iOS事件-加速計陀螺儀

selena4040 7年前發布 | 7K 次閱讀 iOS開發 移動開發

傳感器

傳感器是一種感應\檢測裝置, 目前已經廣泛應用于智能手機上,用于感應\檢測設備周邊的信息,不同類型的傳感器, 檢測的信息也不一樣

iOS中傳感器

核心運動框架-CoreMotion.framework

我們可以使用iOS提供給我們的CoreMotion 框架來訪問加速度器和陀螺儀的相關數據!

它不僅僅提供給你獲得實時的加速度值和旋轉速度值,更重要的是,蘋果在其中集成了很多算法,可以直接給你輸出把重力加速度分量剝離的加速度,省去你的高通濾波操作,以及提供給你一個專門的設備的三維attitude信息!

加速計

速度計的原理很簡單,現在手機里面基本配備的都是3維線傳感器,也就是說,用來測量x,y,z三個軸上的加速力。加速力就是當物體在加速過程中作用在物體上的力,就好比地球引力,也就是重力。

陀螺儀

陀螺儀可以檢測設備的持握方式,原理是檢測在X、Y、Z軸上的角速度,當物體運動方向偏離預定方向時,陀螺儀可以檢測出來

CoreMotion處理的數據有三種

  • 加速度值CMAccelerometerData
  • 陀螺儀值 CMGyroData
  • 設備motion值 CMDeviceMotion

CMDeviceMotion屬性

  • attitude: 手機當前空間位置姿勢
  • gravity : 重力信息,重力加速度矢量在當前設備的參考坐標系中的表達
  • userAcceleration: 加速度信息
  • rotationRate: 即使旋轉速率,陀螺儀輸出
1.對加速度器和陀螺儀相關的訪問,都被封裝在CoreMotion.framework框架下的CMMotionManager類中。
我們通過使用類的方法,來得到我們想要的加速度數據和陀螺儀數據。

  1. isAccelerometerAvailable方法用來查看加速度器是否可用。
  2. isAccelerometerAvailable方法用來查看加速度器的狀態:是否Active(啟動)。 4.同理isGyroAvailable方法和isGyroActive方法用來檢測陀螺儀。 5.獲取加速度器和陀螺儀的數據。主要有兩種方式: push方式 這種方式,是實時獲取到Accelerometer的數據,并且用相應的隊列來顯示。即主動。 pull方式 就是獲取數據,如果要顯示,就要向Accelerometer來索要數據。即:被動的方式。</code></pre>

    簡單調用

    加速計push方式

    // 初始化
     self.manger = [[CMMotionManager alloc] init];
     //  設備是否支持
     if (![self.manger isAccelerometerAvailable])
     {
         NSLog(@"加速計不可用");
     }
    
     // 采樣頻率
     self.manger.accelerometerUpdateInterval = 1;
    
     [self.manger startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
         if (error) return;
    
         // 獲取加速計的信息
         CMAcceleration acceleration = accelerometerData.acceleration;
         NSLog(@"x:%f y:%f z:%f", acceleration.x, acceleration.y, acceleration.z);
     }];
    // 結束
     [self.manger stopAccelerometerUpdates];

    加速計pull方式

    self.manger = [[CMMotionManager alloc] init];
    
     if (![self.manger isAccelerometerAvailable])
     {
         NSLog(@"加速計不可用");
     }
    
     [self.manger startAccelerometerUpdates];
    
     // 需要時獲取
     CMAccelerometerData *accelerometerdata = self.manger.accelerometerData;
     NSLog(@"x:%f y:%f z:%f", accelerometerdata.acceleration.x, accelerometerdata.acceleration.y, accelerometerdata.acceleration.z);
    -(void)stopAction
    {
     if ([self.manger isAccelerometerActive] == YES)
     {
         [self.manger stopAccelerometerUpdates];
     }
    }

    陀螺儀push方式

    // 初始化陀螺儀
     self.manger = [[CMMotionManager alloc] init];
    
     if (![self.manger isGyroAvailable])
     {
         NSLog(@"陀螺儀不可用");
     }
    
     if (![self.manger isGyroActive])
     {
         [self.manger setGyroUpdateInterval:0.5];
    
         [self.manger startGyroUpdatesToQueue:[[NSOperationQueue alloc] init] 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);
    
         }];
     }
    [self.manger stopGyroUpdates];

     

    來自:http://www.jianshu.com/p/844e61e1c219

     

 本文由用戶 selena4040 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!