gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:38:15 24 4
gpt4 key购买 nike

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

我必须非常快速地旋转我的设备才能获得 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);
}

时间戳=事件.时间戳;

我得到了一些输出,比如axisX: 0.69363713 axisY: 0.18359372 axisZ: 0.0228636 RotAngle: 36.7191由于轴值的原因,当设备放在 table 上时,输出矩形看起来会发生变化。

上面的代码有没有问题?

最佳答案

这些值以弧度/秒为单位测量。这已经从 Android 2.3 开始标准化

要获得大约 1.0 的值,您必须以将近 60 度/秒的速度转弯

某些具有以前 Android 版本的设备以度/秒为单位返回(返回)值,但这只是少数。例如,配备 android 2.2 的 LG Optimus Black (P970) 是这些返回 deg/s 的设备之一,但这并不常见。

关于android - Android 中陀螺仪传感器正确旋转值的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8531232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com