• 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:
    		@"加速度为
    -----------
    X轴: %+.2f
    Y轴: %+.2f
    Z轴: %+.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:
    		@"绕各轴的转速为
    --------
    X轴: %+.2f
    Y轴: %+.2f
    Z轴: %+.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:
    		@"磁场数据为
    --------
    X轴: %+.2f
    Y轴: %+.2f
    Z轴: %+.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

    计算设备的位移

    理论上设备一开始静止,就可以通过加速度和时间的值来计算设备的位移,(时间越长 误差越大),然是尝试了一下 做不到,误差太大,看来加速计做不了这么精细的活。。

  • 相关阅读:
    高级查询
    简单查询
    CRUD
    T-SQL语句
    数据库规范
    导出含有特定字符串的注册表
    .net、jquery、ajax、wcf实现数据库用户名检测局部刷新
    数据结构实验之二叉树二:遍历二叉树
    传纸条
    数据结构实验之栈与队列五:下一较大值(一)
  • 原文地址:https://www.cnblogs.com/sunyanyan/p/5213854.html
Copyright © 2020-2023  润新知