Let's say my system is receiving data from a tri-axial accelerometer and a tri-axial gyroscope. I want to calculate velocity from this data. I lack experience with sensors, so I researched and found that I should pre-process the data to remove the effect of gravity. I implemented a method as shown below: public void onSensorChanged(SensorEvent event) { final float alpha = 0.8;
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
} Now, I'm uncertain about the best way to calculate velocity.
I attempted using the conventional equation of motion and integrating acceleration to find velocity, but the results weren't accurate. Currently, I'm considering taking the average acceleration over, say, 100 data points and calculating velocity as v = at (assuming initial velocity is 0). This would give the velocity at the 100th data point. To find the velocity for the 101st data point, I would take the average acceleration over data points 2-101 and again calculate velocity as v = at.
Is this method correct? If not, how can I improve it?