I'm a coremotion beginner.
I need to detect iPhone movement on a flat surface like table - so far, I made it to detect its sideways movement by accessing the yaw of the gyro, but I can't think of a way to detect the up/down changes. I tried using the accelerometer, but it detects more of a device tilt than movement. Also, there is a counterforce when the movement stops.
Do you have any idea to do it so that it would be possible to have the movement data with fair precision? I need it for something like air-hockey game.
In order to determine horizontal position from the deviceMotion is not really doable. The horizontal sensors to use would be the accelerometers, but they measure changes in speed. Therefore, to find position from that, you would need to integrate the sensor data twice. Once to get speed, then a second time to get position.
That means that even the smallest inaccuracy in the acceleration data will make an error in the resulting speed, meaning that your program thinks the phone is moving at constant speed, while in reality it is standing still.
If you don't need to be accurate (for example, when you do not need it to come back to the same position when you move it back and forth), then you might get acceptable results if you force the speed to zero if the accelerometer data is quiet and close to zero.
If you are really set on trying this, then a Kalman filter is probably your best bet to derive speed and position from the accelerometer data. That means that your code will in the end look something like the following, where
acceleration
,speed
, andposition
are three variables you keep, andacc
is a data sample from the accelerometer.where
DT
is the time between data samples, and suitable values forFACTOR1..3
you will need to find out.By the way, Yaw does not give you horizontal motion, but rotation.
EDIT: Nowadays, you may get good results with ARKit's Motion Tracking, which combines accelerometer input with image analysis from the camera. It does all the hard work for you.