Help with accelerometer trig calculations, its driving me nuts. - Page 1
Forum section: electronics / general

author: Psi
author_rank: Super Contributor
timestamp: November 08, 2012, 10:08:33 am
content: Hey guy,I'm being driven crazy by a trig problem that should be simple.What i'm trying to do is take the X and Z g-force value from an accelerometer and correct for the sensor being mounted at a fixed angle in the X plane (other planes are mounted level).The signal i want to measure gets detected, along with gravity, by the X and Z sensors.Since i know the mount angle in degrees, gravity and X Z g-force it should be possible to calculate, without losing resolution, the real valueHowever i just cant seem to figure out the math, nothing i try seems to work.Has anyone done this before and can give me a nice formula? That would really be greatNotes:- With the accelerometer at 45degrees X is 0.7G and Z is 0.7G (not 0.5/0.5)- Excel sin/cos/tan are in radians, i wasted time trying to figure that one out, hehe- Here's a few urls i found which kind of help but still couldn't get anything workinghttp://classes.cecs.ucf.edu/seecsseniordesign/su2006fa2006/g09/files/patrick/measuring%20tilt%20with%20low-g%20accelerometers.pdfhttp://www.kionix.com/sites/default/files/AN005%20Tilt%20Sensing.pdf

author: Rerouter
author_rank: Super Contributor
timestamp: November 08, 2012, 10:39:31 am
content: X and Z isnt the most informative bit of info but i'll take a shot at it,because your working with 45 degrees it makes things a tad easier,when moving exactly along the new axis's, both sensors will record 0.707 of the signal, in this case gravity will be superimposed as a value of 0.707 of gravity on both,as for a vector calculation, both have to be solved at the same time as both measure the same signal just in different ratios depending on the angle,for the angle, well its possible, but it is far too late for that kind of math,

author: tom66
author_rank: Super Contributor
timestamp: November 08, 2012, 11:12:06 am
content: Hint: sin(45deg) = cos(45deg) = 0.707Find out which one works in the direction you need and use that as your multiplier.

author: Psi
author_rank: Super Contributor
timestamp: November 08, 2012, 11:52:59 am
content: hm.. i think you're right,Depending on which is larger X or Z either cos or sin the angle and 1 over that is the multiplier to correct the G value.example, at 14deg most of the X acceleration will be detected on the X axis so multiply detected X gforce by 1/cos(15) = 1.035.Have to subtract the gravity component for 15deg first, but yeah, just multiply whats left by 1.035.Will test this tomorrow in excel but that does seem to work. I think i was trying to make it far more complex that it really is.

author: mrflibble
author_rank: Super Contributor
timestamp: November 08, 2012, 12:27:56 pm
content: If you just want to rotate your reference frame:http://en.wikipedia.org/wiki/Rotation_matrix

author: Kremmen
author_rank: Super Contributor
timestamp: November 08, 2012, 12:35:57 pm
content: I quickly made a diagram that hopefully clarifies.The thin lines labeled X and Y are the sensor axis directions.Green arrow lableled g is the gravity vectorYellow arrow labeled a is the acceleration vectorRed arrow is the vector sum of a and gGravity vector g produces projected signals Xg and Yg to X and Y axes respectively, with amplitudes sin(w) and cos(w) respectively, where w is the angle of g vector relative to positive Y axis (not shown).An acceleration in direction a introduces a signal component that projects to X and Y axes as the vector sum of g and a as shown in yellow and red. To get the direction and magnitude of vector a you need to calculate backwards from the signals Xaxg and Yaxg, first subtracting the gravity vector components Xg and Yg to get Xa and Ya, and then calculating the vector a from these remaining components.

author: Psi
author_rank: Super Contributor
timestamp: November 08, 2012, 09:52:22 pm
content: Thanks for thatIt's much clearer now

author: Psi
author_rank: Super Contributor
timestamp: November 10, 2012, 12:10:45 am
content: One more thing, could someone check my assumptions and see if you can find the problem.Due to gravity if i have the accelerometer at 45deg in X and 45deg in Y the sensors produce (sqrt(3) / 3)X = 0.57735Y = -0.57735Z = -0.57735AndDue to gravity if i have the accelerometer at 45deg in only one axis the sensor produces (sqrt(2) / 2)X = 0.70711Y = 0Z = -0.70711If i take the first values and matrix rotate them by 45deg, then by my reasoning i should get the second values.However i don't. Instead of 0.707 for both i get 0.816 and 0.577Attached to this post is the excel spreadsheet im working in.HereThe first rotation is the 45deg example above. (Ideally i'm trying to get it rotated back from 45deg X & 45deg Y to flat using two rotations.)I'm using these matrix rotationsAny ideas?

author: IanB
author_rank: Super Contributor
timestamp: November 10, 2012, 12:27:49 am
content: This question involves what is fairly straightforward linear algebra (when you know it), but I'm scared to jump in right now since I can't just give an off-the-cuff answer.However, on reading your first post I saw mention of the x plane. There is, in fact, no x plane, but there are x, y and z axes. These axes define three intersecting planes x-y, x-z and y-z, each at right angles to the others.It is not possible to tilt one of the planes while leaving the others undisturbed, since they have a fixed relationship to each other. If you try to rotate one plane, than at least one other plane must be rotated as well.I think what happens with an accelerometer is that it outputs the components of the acceleration vector in each of the x, y and z directions. These three components can be used to define the three dimensional direction and magnitude of the overall acceleration (or force) vector.What I imagine you would do in a real system is wait until things stop changing and treat the resulting steady force vector as being the gravitational acceleration. Then to detect motion you would subtract the constant gravitational vector from the dynamic reading (a vector subtraction), and this would give the the current acceleration vector relative to the device coordinates. If you remember the gravity vector and use it as the reference, you can tell if the current movement is up, down, or sideways relative to earth.

author: IanB
author_rank: Super Contributor
timestamp: November 10, 2012, 02:33:51 am
content: Briefly, is this what you are trying to achieve?You have a two axis sensor mounted at 45 degrees. Given what the sensor sees, you want to subtract gravity and get back the actual acceleration?

author: Psi
author_rank: Super Contributor
timestamp: November 10, 2012, 02:41:16 am
content: Pretty much, yes except ideally id like to be able to correct for angles in both X and Y.(Icouldget away with ignoring side-to-side since it "should" be level, but id like to do both if i can)So the device could be mounted tilted forward/back or from side to side.The device will have an option to 'calibrate' it's position. When this happens it will record the X mount angle and Y mount angle and or X/Y G values then calculate and store in EEPROM the sin/cos multipliers needed to correct for those angles during normal operation.(That way it doesn't need to do sin/cos in realtime. I can do a lookup table though, if sin/cos are needed in realtime, but i don't think they are)

author: IanB
author_rank: Super Contributor
timestamp: November 10, 2012, 02:52:19 am
content: The easiest way to calibrate is to make sure the device is stationary and then press the calibrate button. On calibration, you can just read off the x, y and z outputs and store those. Later on, all you have to do is subtract x(cal) from x(current) to get x(true), and similarly for y and z.Once you have x(true), y(true) and z(true), then the fun happens. What do you need to do with these readings? What happens next depends on exactly what you need to determine about the acceleration detected. In some cases you may not need to do any sin/cos calculations at all.

author: Psi
author_rank: Super Contributor
timestamp: November 10, 2012, 03:27:46 am
content: Quote from: IanB on November 10, 2012, 02:52:19 amI need to know the flat X and Y acceleration.As the mount angle increases from flat the sensitivity of the accelerometer decreases (since it sees less of the acceleration). So this needs to be corrected for using a multiplier.Ignoring gravity, with a tilt of 45deg in only one axis an acceleration of 1G in that direction will be read as 0.707G so it needs a multiplier to correct for that.You can cheat and just multiply that axis by 1/sin(45) but doing it that way you lose accelerometer resolution.The correct way afaik is to combine the change detected on both axis. (this is what i cant get working)Then it gets more complicated if you introduce a tilt in the other axis at the same time as they interact with each other. At 45deg in both angles 1G is no longer 0.707, its 0.577

author: IanB
author_rank: Super Contributor
timestamp: November 10, 2012, 03:41:33 am
content: OK, but unfortunately if you tilt in two axes you will introduce inaccuracy into the x and y directions apart from any offset due to gravity (you will no longer be sure which way is "x" and which way is "y"). To correct for this you will need to know or measure the exact angles of rotation in two different axes, and it will be nearly impossible to get the calculations right without a diagram showing the reference x, y and z axes and the angles that have to be measured relative to these axes.

author: IanB
author_rank: Super Contributor
timestamp: November 10, 2012, 04:03:58 am
content: Suppose your true directions are north/south, east/west and up/down. (Doesn't have to be true north, just what you want to call north in your frame of reference.)Then, given how your sensor is mounted, possibly at some skewy angle, you need to determine by calculation the unit N vector in the x, y, z coordinates of the sensor, by measuring angles and doing trigonometry. You can determine the unit E vector in similar manner. You store the (xN, yN, zN) and (xE, yE, zE) coordinates of these vectors for later use.On calibration you can check for error by projecting the measured gravity vector onto these unit reference vectors. Since gravity should be perpendicular to N and E the result should be zero in each case. If not zero you know something is wrong.What you now do in operation is measure the actual acceleration experienced, subtract the stored gravity vector from it, and then project the resulting vector onto N and E to get the magnitudes in those directions. (You don't strictly have to subtract gravity first since it is perpendicular, but I have a feeling the results will be better if you do so.)The operation of projecting one vector onto another vector is described in various linear algebra texts. It is a fairly straightforward calculation to perform.

author: Psi
author_rank: Super Contributor
timestamp: November 10, 2012, 04:05:18 am
content: ok, i'll give that a go. ThanksDo you know of any software which can be used to easily simulate/visualize vectors/rotation matrix and these sort of problems?

author: IanB
author_rank: Super Contributor
timestamp: November 10, 2012, 04:10:50 am
content: Quote from: Psi on November 10, 2012, 04:05:18 amSorry, I don't. I've never had to do these kinds of calculation in the field. It's possible that something like Mathematica might be of use, but I've never tried it.

author: IanB
author_rank: Super Contributor
timestamp: November 10, 2012, 04:39:27 am
content: Here's a diagram that illustrates my earlier post:

author: HackedFridgeMagnet
author_rank: Super Contributor
timestamp: November 10, 2012, 06:54:06 am
content: this robotics course has a lot of 3d vector maths.Quotethe course notes are a bit hard to digest without watching the videos and the videos take a while, but its all good stuff.The maths for your problem ie. Axes translations, comes up in the first 2-3-4 lectures.Diagrams are so important in this sort of thing, as is the notation.