资讯详情

Android中陀螺仪传感器正确旋转值的计算

我正在使用Android 3.陀螺仪传感器在设备中的方向值旋转矩形。

我必须快速旋转我的设备才能获得值1.0以上的值。

这是代码

final float currentRotVector[] = { 1, 0, 0, 0 };

if (timestamp != 0)

{

final float dT = (event.timestamp - timestamp) * NS2S;

// Axis of the rotation sample, not normalized yet.

// Calculate the angular speed of the sample

float omegaMagnitude = (float) Math.sqrt(X * X Y * Y Z * Z);

// Normalize the rotation vector if it's big enough to get the axis

if (omegaMagnitude > EPSILON)

{

X /= omegaMagnitude;

Y /= omegaMagnitude;

Z /= omegaMagnitude;

}

// Integrate around this axis with the angular speed by the timestep

// in order to get a delta rotation from this sample over the timestep

// We will convert this axis-angle representation of the delta rotation

// into a quaternion before turning it into the rotation matrix.

float thetaOverTwo = dT * omegaMagnitude / 2.0f;

float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);

float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);

deltaRotationVector[0] = cosThetaOverTwo;

deltaRotationVector[1] = sinThetaOverTwo * X;

deltaRotationVector[2] = sinThetaOverTwo * Y;

deltaRotationVector[3] = sinThetaOverTwo * Z;

/* quaternion multiplication

Reference: http://www.cprogramming.com/tutorial/3d/quaternions.html

*/

currentRotVector[0] = deltaRotationVector[0] * currentRotVector[0] -

deltaRotationVector[1] * currentRotVector[1] -

deltaRotationVector[2] * currentRotVector[2] -

deltaRotationVector[3] * currentRotVector[3];

currentRotVector[1] = deltaRotationVector[0] * currentRotVector[1]

deltaRotationVector[1] * currentRotVector[0]

deltaRotationVector[2] * currentRotVector[3] -

deltaRotationVector[3] * currentRotVector[2];

currentRotVector[2] = deltaRotationVector[0] * currentRotVector[2] -

deltaRotationVector[1] * currentRotVector[3]

deltaRotationVector[2] * currentRotVector[0]

deltaRotationVector[3] * currentRotVector[1];

currentRotVector[3] = deltaRotationVector[0] * currentRotVector[3]

deltaRotationVector[1] * currentRotVector[2] -

deltaRotationVector[2] * currentRotVector[1]

deltaRotationVector[3] * currentRotVector[0];

final float rad2deg = (float) (180.0f / Math.PI);

RotAngle = currentRotVector[0] * rad2deg;

axisX = currentRotVector[1];

axisY = currentRotVector[2];

axisZ = currentRotVector[3];

Log.i("Sensor Orientation GyroScope", "axisX: " axisX //

" axisY: " axisY //

" axisZ: " axisZ //

" RotAngle: " RotAngle);

}timestamp = event.timestamp;

我得到了一些输出

axisX:0.69363713 axisY:0.18359372 axisZ:0.0228636 RotAngle:36.7191

而且由于轴的值,当设备放在桌子上时,输出矩形似乎会被调整。

上面的代码有问题吗?

标签: 3d陀螺仪传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台