I am working on an android app built in Unity that would allow the users to track their movements by just using the data coming from the device sensors. I have read about similar issues already and I am aware that this approach doesn't really give any good results in practice, due to the inaccuracy of the sensors, but I still need to measure the performance of this method for research purposes.
My implementation makes use of the Unity function InvokeRepeating to fetch the acceleration values at a certain rate and to then calculate the velocity and displacement by using a two step integration:
private readonly float dt = 0.001f;
private void Start()
{
//...
Input.gyro.enabled = true;
Input.gyro.updateInterval = 0.001f;
InvokeRepeating("AccelerationSample", 0.0f, dt);
}
private void AccelerationSample()
{
Vector3 data = Input.gyro.userAcceleration * 9.81f; //Fetch the current acceleration
lastAccel = data * kFilteringFactor + lastAccel * (1.0f - kFilteringFactor); //Apply Kalman filter
totalAccel += data - lastAccel; //Accumulate the acceleration value
vel = initVel + (totalAccel * dt); //Calculate the current velocity
initVel = vel; //Update the initial velocity for the next frames
distance += vel * dt; //Calculate the distance from the velocity value
}
The raw acceleration values are multiplied by 9.81f to match the real world acceleration (since the values coming from the gyroscope are normalized) and are filtered through a Kalman filter to reduce noise. The acceleration values are accumulated in order to take into consideration both the positive and negative parts of acceleration that the gyroscope provides when the device moves. The velocity and displacement values are then calculated using simple physics formulae.
This approach gives decent results when considering acceleration and velocity only. Though the displacement value does not seem to be updating as fast as I expected, and sometimes the calculations go completely off. Also, when the acceleration goes back to zero after any movement, the evaluated velocity does not go down to zero as well, therefore I need to find some conditions under which I can safely reset its value.
I hope I was able to properly describe the issue and that some of you might help me to revise my implementation and/or give me any hints on how I should go about the problem.