# Align smartphone and vehicle using Acceleration and Orientation from Matlab Mobile

11 views (last 30 days)
David Stefan on 10 Jun 2022
Answered: William Rose on 8 Aug 2022
Dear,
I acquired data during driving with a car. The data I collected was:
• Acceleration (X,Y,Z)
• Orientation (Azimuth, Pitch, Roll)
I placed my phone in an arbitrary position within my car, hoping to later align the acceleration using the acceleration. I'm aware that I need to use Euler angles of some kind, but not sure of the exact implementation. The main question is, what rotation matrix do I need to make and how I should multiply this with my unaligned accelerations and aligned accelerations. One of the main difficulties for me is that I think I would need Yaw instead of Azimuth, but I'm not sure.
In stationary position, my aligned accelerations should be (0,0,9.81). Whilst momentarily, the accelerations are more as (2,2,4.81)
Many thanks for any help.
David

William Rose on 7 Aug 2022
Edited: William Rose on 7 Aug 2022
The information provided is not sufficient to determine the rotation matrix.
Also, the data you gave is not consistent with a simple rotation. Rotations preserve length. Therefore the length of your rotated acceleration vector should be 9.81. But vector v1=[2,2,4.81] has magnitude 5.58.
Let us multiply v1 by a constant so that it has length 9.81: v1=[3.52,3.52,8.46].
This is still not sufficient to determine R. You can think of it this way: The rotation matrix R describes the orientation of the phone relative to the car. We know that R*w1=v1, where w1=[0,0,9.81]. w is in car coordinates, v is in phone coordinates. You need at least one more independent vector in both coordinate systems. With that information, you can compute R as follows:
R*[w1,w2,cross(w1,w2)]=[v1,v2,cross(v1,v2)].
Therefore R= [v1,v2,cross(v1,v2)]*inv([w1,w2,cross(w1,w2)])
Because of measurement errors, the R computed above will not be exactly a rotation matrix - in other words, its columns will not be three vectors which are exactly orthogonal and have length=1. Another way of putting it is that it will not be exactly true that R*R'=I, where R'=transpose of R and I=identity matrix. You deal with that by a singular value decomposition approach. Once you have R, you use inv(R) to convert your measured vectors from phone coordinate system to car coordinate system:
w=inv(R) * v
##### 1 CommentShowHide None
William Rose on 8 Aug 2022
For the SVD appoach to obtaining the rotation matrix, see here and here.

William Rose on 8 Aug 2022
Try the following for the rotation matrix R= : where [0.375561 -0.385512 0.023676].
The matrix Rxyz is derived in the attached notes, equaiton 37. I realized that, since [0;0;9.81] gets transformed to [3.5161; 3.5161; 8.4563], then it must also be the case that [0;0;1] gets transformed to [0.3584; 0.3584; 0.8620], by simple scaling. And that means the last column of Rxyz must be [0.3584; 0.3584; 0.8620]. So we have three equaitons and three unknowns - where the unknowns are the angles . Then we must find the values of that will make the last column of Rxyz, shown above, have those values. I solve this system of 3 equtions and 3 unknowns using Matlab's solve(). I called the angles x,y,z for simplicity.
x=optimvar('x');
y=optimvar('y');
z=optimvar('z');
eq1 = -cos(x)*sin(y)*cos(z)+sin(x)*sin(z)==0.3584;
eq2 = cos(x)*sin(y)*sin(z)+sin(x)*cos(z)==0.3584;
eq3 = cos(x)*cos(y)==0.8620;
prob = eqnproblem;
prob.Equations.eq1 = eq1;
prob.Equations.eq2 = eq2;
prob.Equations.eq3 = eq3;
x0.x = 0; x0.y=0; x0.z=0; %initial guess
sol = solve(prob,x0) %solve the system of equations
Solving problem using fsolve. Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = struct with fields:
x: 0.3756 y: -0.3855 z: 0.0237
Use the values of x,y,z above for the angles, when evaluating the formulas for the elements of the rotation matrix Rxyz, using the formulas in the image above.
The matrix Rxyz describes how the phone is rotated relative to the car. Matrix Rxyz times a vector w1, in car coordinates, gives the vector v1, in phone coordinates. To convert acceleration v2, measured in phone coordinates, to w2 (car coordinates), do w2=inv(Rxyz)*v2.
I said earlier that the information provided was insufficient. I was wrong.

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!