12

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.

PeterJ
  • 3,705
  • 28
  • 51
  • 71
Richard Taylor
  • 2,702
  • 2
  • 22
  • 44
  • For a start you probably want a high-pass filter to remove the DC component of acceleration caused by the 1G towards earth, see http://stackoverflow.com/questions/6937433/ios-high-pass-filter-equation-for-accelerometer. It might be something like 0.2G on the X & Y axis when it starts on a tilt, I assume you ignore Z? Try that for a start but then you'll probably want to look at the absolute values as well to determine the position in free space and transform the movements, but try the HPF for a start. – PeterJ Dec 20 '12 at 12:38
  • @PeterJ, do you mean that the phone starts in some orientation, then moves without rotation in the plane of the table top? And you want it to assign some axes to the table top (perhaps aligned somehow to the XYZ axes of the phone) and track movement in that coordinate system? – Beta Dec 26 '12 at 02:51
  • @Beta, I'm thinking of the situation of a person picking up the phone and say has it tilted 30 degrees towards them but you still want to track movement across X/Y as though the phone was flat, so something that compensates for both the additional tilt and static gravity. So normally if holding fairly still (say after a 2 second filter) it would be an X/Y of zero, but then maybe an X of 0.5G when the user pushes the phone straight up along it's axis. – PeterJ Dec 26 '12 at 02:58
  • I've tried answering questions like this before, about vectors and rotations and different coordinate systems. The math is actually pretty simple, but if people use imprecise language it's just hopeless. *Are you talking about motion without rotation? Are you interested only in the components of motion in the plane of the phone's screen?* – Beta Dec 26 '12 at 03:47
  • @Beta, Yes just talking about motion without rotation. – PeterJ Dec 26 '12 at 04:10

3 Answers3

3

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.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
1

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.

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

You Need Gyroscope also ,accelerometer values are with respect to Phone coordinate system

nayab
  • 2,332
  • 1
  • 20
  • 34
  • The question is about determining a relative movement over a 2D plane, not absolute position in 3D. – PeterJ Dec 20 '12 at 12:42
  • Do you mean converting accelerometer values to screen resolution ? – nayab Dec 20 '12 at 12:50
  • No, I mean if he's happy with the results on a flat surface the problems will be the 1G DC offset caused by the earth. A gyro is only needed if you need a full inertial solution, like being able to detect the phone was pushed forward, rotated 90 degrees and then pushed forward again. – PeterJ Dec 20 '12 at 13:21
  • Linear accelearator will cancel gravity easily or write code to find differentce between accelerometer and gravity sensor. – nayab Dec 20 '12 at 13:25
  • please check the app Acmeter in playstore – nayab Dec 21 '12 at 18:06