user2997154
user2997154

Reputation: 455

How to smooth gyroscope input?

I'm making a game in Unity where the game's main camera is controlled by the phone orientation. The problem I have is that the gyroscope's data is very noisy and make the camera rotate in a very jittery way. I tried on different phone to make sure the hardware is not the problem(Galaxy s6 and Sony Xperia T2).

I've tried the following but none seems to work:

-Slerp between current rotation and new attitude(too much jitter no matter the what I multiply Time.deltaTime by)

    //called at each update
    transform.rotation = Quaternion.Slerp(transform.rotation, new Quaternion(-Input.gyro.attitude.x, -Input.gyro.attitude.y, 
                Input.gyro.attitude.z, Input.gyro.attitude.w), 60 * Time.deltaTime);

-Average the last gyro samples (either Euleur angles average or Quaternion average; both cases still offer too much jitter no matter how many samples I track)

//called at each update
    if (vectQueue.Count >= 5)
        {
            vectQueue.Enqueue(Input.gyro.attitude.eulerAngles);
            vectQueue.Dequeue();



            foreach (Vector3 vect in vectQueue) {
                avgr = avgr + vect;

            }
            avgr = new Vector3(avgr.x/5, avgr.y/5,avgr.z/5);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(avgr),Time.deltaTime*100);

        }
        else
        {
            vectQueue.Enqueue(Input.gyro.attitude.eulerAngles);
        }             

-Round the gyroscope data(best solution so far to prevent jitter but obviously this isn't smooth at all)

-Apply high/low pass filter (doesn't seem to do anything)

public float alpha = 0.5f;
//called at each update
v1 = Input.gyro.attitude.eulerAngles;
    if (v2== null)
        {
            v2 =v1;
            v3 = Input.gyro.attitude.eulerAngles;
        }
        else
        {
            v3 = (1 - alpha) * v3 + (1 - alpha)*(v1 - v2);
            v2 = v1;
            transform.Rotate(v3);
        }

-Copy paste code from people like this; but the jitter is way too strong in all cases.

So now I'm at a point where I guess I'll try learning kalman filters and implement it in unity but obviously I'd prefer a black box solution to save time.

Upvotes: 1

Views: 3769

Answers (3)

Ryper Netic
Ryper Netic

Reputation: 1

hello although i think you have found a solution for that i had an idea about how to smoothen it up using Quaternion.Lerp.

#the rot is used to face forward

Quaternion rot = new Quaternion(0,0,1,0)
Quaternion.lerp(transform.rotation* rot,gyro*rot,10)

Upvotes: 0

user2997154
user2997154

Reputation: 455

Found the problem; any of the ways I mentioned earlier are valid under normal circumstances(use a combination for optimal effects). The thing that was causing the insane jitter was the position of the camera. It had very large numbers as coordinates and I guess this was messing up the engine.

Upvotes: 2

Awaisullah Khan
Awaisullah Khan

Reputation: 81

Always use noise cancellation when using analogue input , you can do so by calculating the difference between gyro values in current frame and gyro values in previous frame , and if the difference is greater then desired amount (0.002 or 0.03 might be good) rotate your camera on gyro values. This will eventually solve your problem of jittering .Hope you will get it

Upvotes: 2

Related Questions