CoreMotion (加速儀與陀螺儀)


CoreMotion (加速儀與陀螺儀)

主要是使用 CMMotionManager

iOS中的主要傳感器:

@ | 加速度傳感器 | 電子羅盤 | 陀螺儀 | 接近傳感器
------------ | ------------- | ------------ | ------------
功能 | 通過測量三個軸的加速度大小來判斷人體運動 | 通過測量設備周圍地磁場的強度和方向來判斷朝向 | 通過測量三個軸的旋轉速率來判斷朝向 | 無須物理接觸就判斷附近物體的存在
主要局限性 | 受重力干擾大,瞬時誤差大 | 誤差大, 容易受其他磁場和金屬物體影響。主要用於校正其他設備 | 誤差會累積,長時間讀數的准確性差 |不通用,大多數只針對幾種材質
應用 | 活動測量 | 導航 | 導航 | 智能省電


加速儀 (類型:CMAcceleration)

簡介:

加速儀可以檢測三維空間中的加速度 ,坐標對應如下:

加速儀坐標

例如:當垂直手持手機且頂部向上,Y坐標上回收到 -1G的加速度。


陀螺儀 (類型:CMRotationRate)

簡介:

陀螺儀用於檢測設備繞XYZ軸轉動的速度,坐標對應如下:

陀螺儀對應坐標


deviceMotion

包含下面四種數據:

  1. attitude(類型:CMAttitude)
    • 返回設備的方位信息,包含roll 、pitch、yaw三個歐拉角的值
    • roll: 設備繞 Z 軸轉過的角度
    • pitch: 設備繞 X 軸轉過的角度
    • yaw: 設備繞 Y 軸轉過的角度
  • rotationRate(類型:CMRotationRate)

    • 經過濾波操作之后的陀螺儀數據,即 靜止時,三個方向的轉動速度接近於0;
  • gravity(類型:CMAcceleration)

    • 返回重力對設備在三個方向上的加速度
    • 即重力加速度矢量在當前設備的參考坐標系中的表達,開發中不再需要通過濾波來提取這個信息
  • userAcceleration(類型:CMAcceleration)

    • 返回用戶對設備在三個方向上的加速度
    • 不再需要濾波,但根據程序需求而加的濾波算法可以保留

示例:

數據獲取方式有兩種:主動獲取(pull),基於代碼塊獲取(push)

主動獲取

-(void)viewDidLoad
{
[super viewDidLoad];
// 創建CMMotionManager對象
self.motionManager = [[CMMotionManager alloc] init];  // ①
// 如果CMMotionManager的支持獲取加速度數據
if (self.motionManager.accelerometerAvailable)
{
	[self.motionManager startAccelerometerUpdates];
}
else
{
	NSLog(@"該設備不支持獲取加速度數據!");
}
// 如果CMMotionManager的支持獲取陀螺儀數據
if (self.motionManager.gyroAvailable)
{
	[self.motionManager startGyroUpdates];
}
else
{
	NSLog(@"該設備不支持獲取陀螺儀數據!");
}
// 如果CMMotionManager的支持獲取磁場數據
if (self.motionManager.magnetometerAvailable)
{
	[self.motionManager startMagnetometerUpdates];
}
else
{
	NSLog(@"該設備不支持獲取磁場數據!");
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 啟動定時器來周期性地輪詢加速度、陀螺儀、磁場數據
updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
	target:self selector:@selector(updateDisplay)
	userInfo:nil repeats:YES];  // ②
}
-(void)updateDisplay
{
// 如果CMMotionManager的加速度數據可用
if (self.motionManager.accelerometerAvailable)
{
	// 主動請求獲取加速度數據
	CMAccelerometerData* accelerometerData = self.motionManager.accelerometerData;
	self.accelerometerLabel.text  = [NSString stringWithFormat:
		@"加速度為\n-----------\nX軸: %+.2f\nY軸: %+.2f\nZ軸: %+.2f",
		accelerometerData.acceleration.x,
		accelerometerData.acceleration.y,
		accelerometerData.acceleration.z];
}
// 如果CMMotionManager的陀螺儀數據可用
if (self.motionManager.gyroAvailable)
{
	// 主動請求獲取陀螺儀數據
	CMGyroData* gyroData = self.motionManager.gyroData;
	self.gyroLabel.text = [NSString stringWithFormat:
		@"繞各軸的轉速為\n--------\nX軸: %+.2f\nY軸: %+.2f\nZ軸: %+.2f",
		gyroData.rotationRate.x,
		gyroData.rotationRate.y,
		gyroData.rotationRate.z];
}
// 如果CMMotionManager的磁場數據可用
if (self.motionManager.magnetometerAvailable)
{
	// 主動請求獲取磁場數據
	CMMagnetometerData* magnetometerData = self.motionManager.magnetometerData;
	self.magnetometerLabel.text =  [NSString stringWithFormat:
		@"磁場數據為\n--------\nX軸: %+.2f\nY軸: %+.2f\nZ軸: %+.2f",
		magnetometerData.magneticField .x,
		magnetometerData.magneticField .y,
		magnetometerData.magneticField .z];
}
}

結果如下:

示例圖1

基於代碼塊獲取

    self.motionManager = [[CMMotionManager alloc]init];
    if (_motionManager.isDeviceMotionAvailable) {
        
        //更新數據頻率
        _motionManager.deviceMotionUpdateInterval = 1/60 ;

        TestViewController * __weak weakSelf = self;

        NSOperationQueue *queue = [[ NSOperationQueue alloc]init];

        [_motionManager startDeviceMotionUpdatesToQueue:queue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
            
            double userX = motion.userAcceleration.x;
            double userY = motion.userAcceleration.y;
            //...
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    //更新UI
            }];


        }];


    }

簡單應用

圖片無論在設備如何傾斜的情況下都保持水平

RotationViewController * __weak weakSelf = self;if (manager.accelerometerAvailable) {
manager.accelerometerUpdateInterval = 0.01f;
[manager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue]
                          withHandler:^(CMAccelerometerData *data, NSError *error) {
    double rotation = atan2(data.acceleration.x, data.acceleration.y) - M_PI;
    weakSelf.imageView.transform = CGAffineTransformMakeRotation(rotation);
}];
}	

結果如下:

應用1

敲擊手掌的時候實現導航返回

ClunkViewController * __weak weakSelf = self;if (manager.deviceMotionAvailable) {
manager.deviceMotionUpdateInterval = 0.01f;
[manager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                                   withHandler:^(CMDeviceMotion *data, NSError *error) {
    if (data.userAcceleration.x < -2.5f) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }
}];
}

結果如下:

應用2

旋轉改變頁面

double showPromptTrigger = 1.0f;
double showAnswerTrigger = 0.8f;

+(double)magnitudeFromAttitude:(CMAttitude *)attitude {
return sqrt(pow(attitude.roll, 2.0f) + pow(attitude.yaw, 2.0f) + pow(attitude.pitch, 2.0f));
}


FacingViewController * __weak weakSelf = self;if (manager.deviceMotionAvailable) {
[manager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                                   withHandler:^(CMDeviceMotion *data, NSError *error) {
    // translate the attitude
    [data.attitude multiplyByInverseOfAttitude:initialAttitude];
    // calculate magnitude of the change from our initial attitude
    double magnitude = [FacingViewController magnitudeFromAttitude:data.attitude];
    // show the prompt
    if (!showingPrompt && (magnitude > showPromptTrigger)) {
        showingPrompt = YES;
        PromptViewController *promptViewController = [weakSelf.storyboard instantiateViewControllerWithIdentifier:@"PromptViewController"];
        promptViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [weakSelf presentViewController:promptViewController animated:YES completion:nil];
    }
    // hide the prompt
    if (showingPrompt && (magnitude < showAnswerTrigger)) {
        showingPrompt = NO;
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }
}];
}

結果如下:

應用3

計算設備的位移

理論上設備一開始靜止,就可以通過加速度和時間的值來計算設備的位移,(時間越長 誤差越大),然是嘗試了一下 做不到,誤差太大,看來加速計做不了這么精細的活。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM