获取手机的倾斜角度

-(void)startCollectSensorData2:(GetGyroSensorIDBlock)block{

 

    if(block){

        _getSensorIDBlock = block;

    }

    

    _sensorID = [MathUtil zeros:3 columns:3];

    

    //判断陀螺仪是否可用

    if(![self.motionManager isGyroAvailable]){

        NSLog(@"陀螺仪不可用");

        return;

    }

 

    //设置陀螺仪更新频率,以秒为单位

//    self.motionManager.gyroUpdateInterval = 1000;

    self.motionManager.deviceMotionUpdateInterval  = 0.1;

    

    _gyro_data_1 = [[NSMutableArray alloc] init];

    _gyro_data_2 = [[NSMutableArray alloc] init];

    _gyro_data_3 = [[NSMutableArray alloc] init];

 

    __weak GyroManager *weakSelf = (GyroManager *)self;

    [self.motionManager startDeviceMotionUpdatesToQueue:[[NSOperationQueue alloc] init] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {

        if (error) {

            return;

        }

    

        double roll = motion.attitude.roll;

        double pitch = motion.attitude.pitch;

        double yaw = motion.attitude.yaw;

 

        //http://www.codexiu.cn/ios/blog/23506/

 

        //顶部朝正北,从左向右旋转一整圈。  web 0 到 360   native 0 到 π -π 到 0

        double alpha = yaw<0 ? 360 + yaw*(180/M_PI) : yaw*(180/M_PI);

 

        //屏幕朝上水平放置,手机头部向上抬,直至屏幕朝下到水平方向。 web  0 到 180     native 0 到 π/2  再到0

        //屏幕朝上水平放置,手机尾部向上抬,直至屏幕朝下到水平方向。 web  -0 到 -180     native -0 到 -π/2  再到-0

        //double beta = 0;

        

        //屏幕朝上水平放置,手机左侧向上抬,直至屏幕朝下到水平方向。 web 0 到 π/2 -π/2 到 -0       native  0 到 π

        //屏幕朝上水平放置,手机右侧向上抬,直至屏幕朝下到水平方向。 web -0 到 -π/2 π/2 到 0       native -0 到 -π

 

//        NSLog(@"加速度 == x:%f, y:%f, z:%f", roll, pitch, alpha);

 

        NSDictionary *analySize = [self test:motion];

 

        NSLog(@"加速度 == x:%f, y:%f, z:%f", ((NSNumber *)analySize[@"gamma"]).doubleValue, ((NSNumber *)analySize[@"beta"]).doubleValue, ((NSNumber *)analySize[@"alpha"]).doubleValue );

    }];

 

}

 

-(NSDictionary *)test:(CMDeviceMotion *)motion{

     // Acceleration is user + gravity

     CMAcceleration userAccel = motion.userAcceleration;

     CMAcceleration gravity = motion.gravity;

     CMAcceleration totalAccel;

     totalAccel.x = userAccel.x + gravity.x;

     totalAccel.y = userAccel.y + gravity.y;

     totalAccel.z = userAccel.z + gravity.z;

 

 

     CMAttitude* attitude = motion.attitude;

 

     // Compose the raw motion data to an intermediate ZXY-based 3x3 rotation

     // matrix (R) where [z=attitude.yaw, x=attitude.pitch, y=attitude.roll]

     // in the form:

     //

     //   /  R[0]   R[1]   R[2]  \

     //   |  R[3]   R[4]   R[5]  |

     //   \  R[6]   R[7]   R[8]  /

 

     double cX = cos(attitude.pitch);

     double cY = cos(attitude.roll);

     double cZ = cos(attitude.yaw);

     double sX = sin(attitude.pitch);

     double sY = sin(attitude.roll);

     double sZ = sin(attitude.yaw);

 

     double R[] = {

         cZ * cY - sZ * sX * sY,

         - cX * sZ,

         cY * sZ * sX + cZ * sY,

         cY * sZ + cZ * sX * sY,

         cZ * cX,

         sZ * sY - cZ * cY * sX,

         - cX * sY,

         sX,

         cX * cY

     };

 

     // Compute correct, normalized values for DeviceOrientation from

     // rotation matrix (R) according to the angle conventions defined in the

     // W3C DeviceOrientation specification.

 

     double zRot;

     double xRot;

     double yRot;

 

     if (R[8] > 0) {

         zRot = atan2(-R[1], R[4]);

         xRot = asin(R[7]);

         yRot = atan2(-R[6], R[8]);

     } else if (R[8] < 0) {

         zRot = atan2(R[1], -R[4]);

         xRot = -asin(R[7]);

         xRot += (xRot >= 0) ? -M_PI : M_PI;

         yRot = atan2(R[6], -R[8]);

     } else {

         if (R[6] > 0) {

             zRot = atan2(-R[1], R[4]);

             xRot = asin(R[7]);

             yRot = -M_PI_2;

         } else if (R[6] < 0) {

             zRot = atan2(R[1], -R[4]);

             xRot = -asin(R[7]);

             xRot += (xRot >= 0) ? -M_PI : M_PI;

             yRot = -M_PI_2;

         } else {

             zRot = atan2(R[3], R[0]);

             xRot = (R[7] > 0) ? M_PI_2 : -M_PI_2;

             yRot = 0;

         }

     }

 

     // Rotation around the Z axis (pointing up. normalized to [0, 360] deg).

     double alpha = rad2deg(zRot > 0 ? zRot : (M_PI * 2 + zRot));

     // Rotation around the X axis (top to bottom).

     double beta  = rad2deg(xRot);

     // Rotation around the Y axis (side to side).

     double gamma = rad2deg(yRot);

    

     return @{@"alpha":@(alpha),

              @"beta":@(beta),

              @"gamma":@(gamma),

              };

}

Android提供了一些传感器API来获取手机的运动方向和角度。其中,最常用的是重力感应器和磁场感应器。 重力感应器可以用来检测手机倾斜和旋转,可以通过以下代码获取手机的加速度和重力加速度: ``` SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); Sensor gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); sensorManager.registerListener(new SensorEventListener() { float[] gravity = new float[3]; float[] linear_acceleration = new float[3]; @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_GRAVITY) gravity = event.values.clone(); if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { final float alpha = 0.8f; gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; linear_acceleration[0] = event.values[0] - gravity[0]; linear_acceleration[1] = event.values[1] - gravity[1]; linear_acceleration[2] = event.values[2] - gravity[2]; // 计算手机倾斜角度 float xAngle = (float) Math.atan2(linear_acceleration[1], linear_acceleration[2]) * 180 / (float) Math.PI; float yAngle = (float) Math.atan2(linear_acceleration[0], linear_acceleration[2]) * 180 / (float) Math.PI; float zAngle = (float) Math.atan2(linear_acceleration[0], linear_acceleration[1]) * 180 / (float) Math.PI; } } }, accelerometer, gravitySensor, SensorManager.SENSOR_DELAY_GAME); ``` 磁场感应器可以用来检测手机的方向,可以通过以下代码获取手机的方向角度: ``` SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(new SensorEventListener() { float[] accelerometerValues = new float[3]; float[] magneticValues = new float[3]; @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { accelerometerValues = event.values.clone(); } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { magneticValues = event.values.clone(); } float[] R = new float[9]; float[] values = new float[3]; SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues); SensorManager.getOrientation(R, values); float azimuth = (float) Math.toDegrees(values[0]); float pitch = (float) Math.toDegrees(values[1]); float roll = (float) Math.toDegrees(values[2]); } }, magneticSensor, accelerometer, SensorManager.SENSOR_DELAY_GAME); ``` 通过以上代码,你可以获取手机倾斜角度、方向角度等信息,从而获得手机的运动方向和角度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值