-(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),
};
}