我正在尝试计算 Android 设备的绝对航向,并相应地旋转 GameObject。因此,无论设备方向如何,游戏对象都将充当 3D 指南针,在 3D 空间中指向地球的北方。
为了实现这一点,我使用Madgwick AHRS 算法作为由 x-io Technologies 维护的单个文件 C# 库。
这是我附加到CompassArrow
游戏对象的脚本。
using System;
using UnityEngine;
using AHRS;
namespace MyCompass
{
public class CompassArrowController : MonoBehaviour
{
static MadgwickAHRS AHRSInst = new MadgwickAHRS(1f / 256f, 0.1f);
void Start()
{
Input.compass.enabled = true;
Input.gyro.enabled = true;
Input.location.Start();
}
void Update()
{
UpdateAHRS();
//Get algorithm result quaternion
var q = AHRSInst.Quaternion;
//Create unity quaternion
Quaternion arrowRotation = new Quaternion(q[0], q[1], q[2], q[3]);
transform.rotation = Quaternion.Inverse(arrowRotation);
}
static void UpdateAHRS()
{
AHRSInst.Update(
//Gyro
Input.gyro.rotationRateUnbiased.x,
Input.gyro.rotationRateUnbiased.y,
Input.gyro.rotationRateUnbiased.z,
//Acceleration
Input.acceleration.x,
Input.acceleration.y,
Input.acceleration.z,
//Magnetometer
Input.compass.rawVector.x,
Input.compass.rawVector.y,
Input.compass.rawVector.z
);
}
}
}
这是我能得出的最接近所需输出的结果。但是轴仍然被交换了,我必须将设备旋转大约 4 次才能使指南针箭头完成一次完整的旋转。
我的猜测是,输入算法的传感器数据单位错误。我向您保证,我使用的设备具有运行该算法所需的所有 3 个传感器。
以下是Update()
库中方法的参数说明。
/*
Signature: public void Update(float gx, float gy, float gz, float ax, float ay, float az, float mx, float my, float mz)
Summary: Algorithm AHRS update method. Requires only gyroscope and accelerometer data.
gx: Gyroscope x axis measurement in radians/s.
gy: Gyroscope y axis measurement in radians/s.
gz: Gyroscope z axis measurement in radians/s.
ax: Accelerometer x axis measurement in any calibrated units.
ay: Accelerometer y axis measurement in any calibrated units.
az: Accelerometer z axis measurement in any calibrated units.
mx: Magnetometer x axis measurement in any calibrated units.
my: Magnetometer y axis measurement in any calibrated units.
mz: Magnetometer z axis measurement in any calibrated units.
*/
该库还有一种Update()
不需要磁力计读数的方法。但由于我正在开发指南针,我认为这种方法不会有用。
谁能指出我做错了什么?我可以根据要求提供更多详细信息。