Search Unity

Accelerometer vibration, How to smooth?

Discussion in 'iOS and tvOS' started by Juan, Jul 24, 2009.

  1. Juan

    Juan

    Joined:
    May 21, 2009
    Posts:
    142
    That's my question:

    I have the accelerometer controling the rotation of a table, it's working fine but it's vibrating a lot, how can i smooth that vibration, like passing some filter to the accelerometer information.

    Thanks :)

    Edit1:

    I found the lowpass filter inside the documentation, but i don't understand very well how to implement this in a js script, someone can point me in the right direction please? :)
     
  2. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    Instead of setting the rotation to the accelerometer rotation, try Vector3.Lerp to smoothly move to the desired rotation.
     
  3. dbokser

    dbokser

    Joined:
    Jul 14, 2009
    Posts:
    27
    I ran into the same problem, here's how I smoothed mine out

    Code (csharp):
    1.  
    2. // rotation multiplier for the accelerometer
    3. // (rotating the iPhone 90 degrees will give this rotation)
    4. var maxTilt : float = 65.0;
    5.  
    6. // set this lower to smooth it out more, but then
    7. // the rotation is slower
    8. var rotationDamp : float = 5.0;
    9.  
    10. // rotation of the table
    11. var currentRotation : float = transform.eulerAngles.z;
    12.  
    13. // rotation of the accelerometer
    14. var wantedRotation : float = iPhoneInput.acceleration.normalized.y * maxTilt * -1;
    15.  
    16. // rotation value is smoothed out with the LerpAngle
    17. z = Mathf.LerpAngle(currentRotation, wantedRotation, rotationDamp * Time.deltaTime);
    18.  
     
  4. Juan

    Juan

    Joined:
    May 21, 2009
    Posts:
    142
    Your way is really great dbokser, the rotation is so smooth, but when the ipod/iphone is stopped the sensor is still vibrating, is there some way to implement the low pass filter to avoid this? (or any other technique)

    Thanks for your answer :D