zoukankan      html  css  js  c++  java
  • iOS平台使用陀螺仪传感器

    在移动端开发过程中,有时候会用到陀螺仪传感器获取当前手机的姿态,下面给出iOS端如何获取陀螺仪姿态数据的代码:

     1 //根据陀螺仪的四元数转换为矩阵
     2 + (GLKMatrix4)calculateMatrixFromQuaternion:(CMQuaternion*)quaternion orientation:(AVCaptureVideoOrientation) orientation{
     3     float xx = quaternion->x * quaternion->x;
     4     float yy = quaternion->y * quaternion->y;
     5     float zz = quaternion->z * quaternion->z;
     6     float xy = quaternion->x * quaternion->y;
     7     float wz = quaternion->w * quaternion->z;
     8     float wy = quaternion->w * quaternion->y;
     9     float xz = quaternion->x * quaternion->z;
    10     float yz = quaternion->y * quaternion->z;
    11     float wx = quaternion->w * quaternion->x;
    12     
    13     float r00 = 1 - 2 * (yy + zz);
    14     float r01 = 2 * (xy - wz);
    15     float r02 = 2 * (wy + xz);
    16     float r03 = 0;
    17     
    18     float r10 = 2 * (xy + wz);
    19     float r11 = 1 - 2 * (xx + zz);
    20     float r12 = 2 * (yz - wx);
    21     float r13 = 0;
    22     
    23     float r20 = 2 * (xz - wy);//xy - wy
    24     float r21 = 2 * (yz + wx);
    25     float r22 = 1 - 2 * (xx + yy);
    26     float r23 = 0;
    27     
    28     float r30 = 0;
    29     float r31 = 0;
    30     float r32 = 0;
    31     float r33 = 1;
    32     
    33     return GLKMatrix4Make(r00,r01,r02,r03,
    34                       r10,r11,r12,r13,
    35                       r20,r21,r22,r23,
    36                       r30,r31,r32,r33);
    37 }
    38 
    39 
    40 //当前屏幕的方向
    41 @property (nonatomic, readwrite, assign) AVCaptureVideoOrientation orientation;
    42 //开启陀螺仪
    43 - (void)updateAngleFromAttitudeSensor{
    44     if(self.cmmotionManager){
    45         
    46     }else{
    47         self.cmmotionManager = [[CMMotionManager alloc] init];
    48         self.cmmotionManager.deviceMotionUpdateInterval = 1.0/25.0;
    49         self.cmmotionManager.gyroUpdateInterval = 1.0/25.0;
    50         self.cmmotionManager.showsDeviceMovementDisplay = YES;
    51         
    52         NSOperationQueue *mQueue = [[NSOperationQueue alloc]init];
    53         __weak typeof(self) wSelf = self;
    54         [wSelf.cmmotionManager startDeviceMotionUpdatesToQueue:mQueue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error){
    55             CMAttitude *attitude = motion.attitude;
    56             if (attitude == nil) {
    57                 return;
    58             }
    59             
    60             //UID
    61             CMQuaternion quaternion = attitude.quaternion;
    62             wSelf.sensorMatrix = GLKMatrix4Identity;
    63             wSelf.sensorMatrix = [GyroscopeUtil calculateMatrixFromQuaternion:&quaternion orientation:orientation];
    64             
    65             wSelf.sensorMatrix = GLKMatrix4RotateX(wSelf.sensorMatrix,M_PI_2);
    66             wSelf.sensorInverseMatrix = GLKMatrix4Identity;
    67             
    68             bool isInvertible = true;
    69             wSelf.sensorInverseMatrix = GLKMatrix4Invert(wSelf.sensorMatrix, &isInvertible);
    70         }];
    71     }
    72 }
  • 相关阅读:
    17. 文件查找
    18. 后台进程
    16. Linux 文件目录权限
    15. SSH 远程
    14. 用户管理
    Emacs Python 自动补全--Elpy
    C++ 程序在运行时不显示dos界面
    OpenCV设置摄像头分辨率及全屏显示
    #error : Xiron Platform Abstraction Layer
    Win10 下Cmake编译配置 Opencv3.1 + Cuda7.5 + VS2013
  • 原文地址:https://www.cnblogs.com/calence/p/6207634.html
Copyright © 2011-2022 走看看