Hello, I want to hang a low-pass filter on the gyroscope so that it does not drift. But at the output from the filter, I get NaN . I can not understand why this is happening.

Here I did like this: http://wiki.unity3d.com/index.php?title=Low_Pass_Filter

Here is my code:

  private Quaternion filteredQuarternion; float lowPassFactor = 0.5f; bool init = true; void Update() { Quaternion cameraRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w); filteredQuarternion = lowPassFilterQuaternion(filteredQuarternion, cameraRotation, lowPassFactor , init); init = false; this.transform.localRotation = filteredQuarternion; } Quaternion lowPassFilterQuaternion(Quaternion intermediateValue, Quaternion targetValue, float factor, bool init) { if (init){ intermediateValue = targetValue; } intermediateValue = Quaternion.Lerp(intermediateValue, targetValue, factor); return intermediateValue; } 

But on my output, that is, all the fields of the filteredQuarternion object are equal to NaN .

Help please, what am I doing no so? Maybe I misunderstood or missed something.

    1 answer 1

     Вот рабочий вариант: float lowPassFactor = 0.8f; bool init = true; GameObject _cameraContainer; Transform _cameraTransform; private void Awake() { _cameraTransform = Camera.main.GetComponent<Transform>(); _cameraContainer = new GameObject(); _cameraContainer.transform.position = _cameraTransform.position; _cameraTransform.SetParent(_cameraContainer.transform); _cameraContainer.transform.rotation *= Quaternion.Euler(90f, 90f, 0f); } void Update() { _cameraTransform.localRotation = lowPassFilterQuaternion(_cameraTransform.localRotation, GyroToUnity(Input.gyro.attitude), lowPassFactor , init); init = false; } Quaternion lowPassFilterQuaternion(Quaternion intermediateValue, Quaternion targetValue, float factor, bool init) { //intermediateValue needs to be initialized at the first usage. if(init) { intermediateValue = targetValue; } intermediateValue = Quaternion.Lerp(intermediateValue, targetValue, factor); return intermediateValue; } private static Quaternion GyroToUnity(Quaternion q) { return new Quaternion(qx, qy, -qz, -qw); }