# Calibration of a 3-Axis Accelerometer for fixing effect of gravity

1. Jan 15, 2010

### electrophile

Hi all,

I've been trying to develop this excel macro for mapping out a train track and locating kinks.
It's been going well so far except that when the accelerometer is not exactly level with the ground, the acceleration due to gravity (9.8 m/s^2) is not only effective in the z-direction, but in the x and y as well since the whole co-ordinate system of the accelerometer is tilted.

Now, I have the x-tilt and the y-tilt angles and am trying to fix this problem.
Please have a look at the excel sheets I have attached. Starting from row 13 in B, C and D, I have the acceleration values in g (with 1g = 9.8m/s^w) and the tilt angles are in columns G and H.

new_x_acceleration = old_x_acceleration + sin(x_tilt)
new_y_acceleration = old_y_acceleration + sin(y_tilt)
new_z_acceleration = sqrt (1 - (new_x_acceleration)^2 - (new_y_acceleration)^2)

These seem right but when I apply these formulas (run the calibrator macro i programmed replaces the originals) the values seem very implausible. They are on the order of 10^-16 or less.

Any thoughts on what I'm doing wrong?

#### Attached Files:

• ###### Test 22 Aligned physforums.zip
File size:
144.7 KB
Views:
98
2. Jan 18, 2010

### electrophile

anyone?...

3. Jan 18, 2010

### Yitzach

The reason no one has helped is because it a difficult problem to be sure we got it right.
Your right angle trig is royally messed up.

Problem repeat and simplification:
Given:
x,y,z vector in rotated vector
x,y angle from true x and y
Find: x,y,z in true vertical

VM (g) in column G is total acceleration or rho.
new_z=sqrt(VM^2-new_x^2-new_y^2)

It is going to take a lot of thought to figure out new_x and new_y. I can tell you yours are wrong because you are adding an acceleration and a unitless ratio. You can multiply mismatched units together, but you can't add them.

4. Jan 19, 2010

### electrophile

Yes, I realized the solution couldn't be that simple.

I think the solution can be given using a rotation matrix. However, this is not my area of expertise. I was wondering if anyone here could suggest a possible solution.

So far, what I'm thinking is, I should find the rotation matrix according to the x and y tilts. Take the inverse of it and multiply it by the matrix of xyz accelerations I have to find the original axis value (before the rotation). I've tried this but it's still not working out as I'm getting values that are even farther from 0 for x and y acceleration.

5. Jan 19, 2010

### Yitzach

How far off? Let's what else you have come up with. We may say that the "off" numbers are in fact correct. Or we might realize what the correct solution is because you can so close.

6. Jan 19, 2010

### electrophile

Hey,

Okay, they are very far off. I have attached the excel sheet containing the rotation matrices and the final calibrated answers.

P1:R3 (yellow cells) contain the Rz matrix since (x-tilt is the rotation about the z-axis). T1:V3 (green cells) contain the Rx matrix (y-tilt is the rotation about the x-axis). X1:Z3 (red cells) contain the multiplication of the two (Rx * Ry). X4:Z6 (blue) contain the inverse of this multiplication and AD1:AD3 (purple) contain the final calibrated answers (inverse*xyz).

These values are calculated for the row 13 values. I have written a VBA code for this calculation [Sub pitchroll2()]

#### Attached Files:

File size:
28.5 KB
Views:
108
• ###### Test 22 physforums rotmat nonmacro xls.xls
File size:
88.5 KB
Views:
112
Last edited: Jan 19, 2010
7. Jan 19, 2010

### Yitzach

You need a course in linear algebra or matrix algebra. That matrix in red is a singular matrix, it has no inverse. But that is a correct multiplication of matrices.
X-tilt is rotation about the y-axis. Rotation about the z-axis is yaw. x-axis goes right and y-axis goes up on the plane of the paper with the z-axis coming out of the paper. If you wish to draw the three axis on the paper, you must maintain this relationship. The two common ways to do are y right, z up, x down and left; and x right, y up, z down and left.

I didn't poke your algorithm real hard but it looks like you are calculating roll and pitch and then multiplying them together. The problem with this is that that playing with roll and pitch in this manner works best when there is only roll or only pitch.
The rotation matrix for 2 dimensions is as follows:
$$\begin{pmatrix}\bar{A_y}\\\bar{A_x}\end{pmatrix}=\begin{pmatrix}\cos\phi&\sin\phi\\-\sin\phi&\cos\phi\end{pmatrix}\begin{pmatrix}A_y\\A_x\end{pmatrix}$$
It certainly looks like you used it in your algorithm.
That looks a lot like the conversion matrix for going from Cartesian $$\hat{x}, \hat{y}, \hat{z}$$ to cylindrical $$\hat{s}, \hat{\phi}, \hat{z}$$ which is as follows:
$$\begin{pmatrix}\hat{s}\\\hat{\phi}\\\hat{z}\end{pmatrix}=\begin{pmatrix}\cos\phi&\sin\phi&0\\-\sin\phi&\cos\phi&0\\0&0&1\end{pmatrix}\begin{pmatrix}\hat{x}\\\hat{y}\\\hat{z}\end{pmatrix}$$
The conversion matrix for going from Cartesian $$\hat{x}, \hat{y}, \hat{z}$$ to cylindrical $$\hat{r}, \hat{\theta}, \hat{\phi}$$ is as follows:
$$\begin{pmatrix}\hat{r}\\\hat{\theta}\\\hat{\phi}\end{pmatrix}=\begin{pmatrix}\sin\theta\cos\phi&\sin\theta\sin\phi&\cos\theta\\\cos\theta\cos\phi&\cos\theta\sin\phi&-\sin\theta\\-\sin\phi&\cos\phi&0\end{pmatrix}\begin{pmatrix}\hat{x}\\\hat{y}\\\hat{z}\end{pmatrix}$$
Theta is the angle from the z axis to the vector. Phi is the angle from the x axis to the projection of the vector onto the xy-plane. The trick to using this as a conversion matrix for this case is figuring out what theta and phi is, neither of which you posses, but you can find them.

8. Jan 19, 2010

### Yitzach

$$\theta=arctan(\frac{y_{tilt}}{x_{tilt}})$$
I can do this because the two angles produce a unitless ratio which is what arc trig functions take as arguments.
Make sure that the angle is in the right quadrant as arctangent is rather ambiguous.

Last edited: Jan 20, 2010