I'm using the accelerometer to move something around in X/Y on the screen.
This is easy if the phone starts flat on the table.
I've come up with something in an attempt to be able to start at any given position, and work from there. But it doesn't seem to work naturally.
How do I calibrate things so I can get the difference in orientation from the starting point?
cursorX -= accelerometerCalibrationY - getAccelerometerY();
cursorY += accelerometerCalibrationX - getAccelerometerX();
This works fine for flat on the table, and SOME starting positions. The variables are just getAccelerometerXY at the start.
You need to do some math. Use getRotationMatrix to determine current phone orientation Matrix (M). Using M, you can calc vector g (Gravity) in device local coordinate system. Then get accel (Accel) from sensor. Calc Ans = Accel-Gravity vector. Ans' x/y components is what you need. Filter it to get better results.
Here is another easy way. Get Accel vector. Filter it with high-pass filter to remove DC component and low-pass to remove unnecessary noise (you will spent some time looking for appropriate filter params). Filtered x/y components is what you need.
Accelerometer delivers you an vector - so just compute some reasonable average on start of your activity and use it as staring point. Converting difference to coordinate steering values is straightforward.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With