Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Lean Angle with Core Motion

I have a record session for my application. When user started a record session I start collecting data from device's CMMotionManager object and store them on CoreData to process and present later. The data I'm collecting includes gps data, accelerometer data and gyro data. The frequency of data is 10Hz.

Currently I'm struggling to calculate the lean angle of device with motion data. It is possible to calculate which side of device is land by using gravity data but I want to calculate right or left angle between user and ground regardless of travel direction.

This problem requires some linear algebra knowledge to solve. For example for calculation on some point I must calculate the equation of a 3D line on a calculated plane. I am working on this one for a day and it's getting more complex. I'm not good at math at all. Some math examples related to the problem is appreciated too.

like image 417
Cemal Eker Avatar asked Jul 20 '12 09:07

Cemal Eker


1 Answers

It depends on what you want to do with the collected data and what ways the user will go with that recording iPhone in her/his pocket. The reason is that Euler angles are no safe and especially no unique way to express a rotation. Consider a situation where the user puts the phone upright into his jeans' back pocket and then turns left around 90°. Because CMAttitude is related to a device lying flat on the table, you have two subsequent rotations for (pitch=x, roll=y, yaw=z) according to this picture:

  • pitch +90° for getting the phone upright => (90, 0, 0)
  • roll +90° for turning left => (90, 90, 0)

But you can get the same position by:

  • yaw +90° for turning the phone left (0, 0, 90)
  • pitch -90° for making the phone upright (-90, 0, 90)

You see two different representations (90, 90, 0) and (-90, 0, 90) for getting to the same rotation and there are more of them. So you press Start button, do some fancy rotations to put the phone into the pocket and you are in trouble because you can't rely on Euler Angles when doing more complex motions (s. gimbal lock for more headaches on this ;-)

Now the good news: you are right linear algebra will do the job. What you can do is force your users to put the phone in always the same position e.g. fixed upright in the right back pocket and calculate the angle(s) relative to the ground by building the dot product of gravity vector from CMDeviceMotion g = (x, y, z) and the postion vector p which is the -Y axis (0, -1, 0) in upright position:

g • x = x*0 + y*(-1) + z*0 = -y = ||g||*1*cos (alpha)

=> alpha = arccos (-y/9.81) as total angle. Note that gravitational acceleration g is constantly about 9.81

To get the left-right lean angle and forward-back angle we use the tangens:

alphaLR = arctan (x/y)

alphaFB = arctan (z/y)


[UPDATE:]

If you can't rely on having the phone at a predefined postion like (0, -1, 0) in the equations above, you can only calculate the total angle but not the specific ones alphaLR and alphaFB. The reason is that you only have one axis of the new coordinate system where you need two of them. The new Y axis y' will then be defined as average gravity vector but you don't know your new X axis because every vector perpedicular to y' will be valid.

So you have to provide further information like let the users walk a longer distance into one direction without deviating and use GPS and magnetometer data to get the 2nd axis z'. Sounds pretty error prone in practise.

The total angle is no problem as we can replace (0, -1, 0) with the average gravity vector (pX, pY, pZ):

g•p = xpX + ypY + zpZ = ||g||||p||*cos(alpha) = ||g||^2*cos(alpha)

alpha = arccos ((xpX + ypY + z*pZ) / 9.81^2)


Two more things to bear in mind:

  • Different persons wear different trowsers with different pockets. So the gravity vector will be different even for the same person wearing other clothes and you might need some kind of normalisation
  • CMMotionManager does not work in the background i.e. the users must not push the standby button
like image 177
Kay Avatar answered Oct 12 '22 04:10

Kay