In Orientation Representations Part 1 and Part 2, we explore some of the mathematical ways to represent the orientation of an object. Now we’re going to apply that knowledge to build a virtual gyroscope using data from a 3-axis accelerometer and 3-axis magnetometer. Reasons you might want to do this include “cost” and “cost”. Cost #1 is financial. Gyros tend to be more expensive than the other two sensors. Eliminating them from the BOM is attractive for that reason. Cost #2 is power. The power consumed by a typical accel/mag pair is significantly less than that consumed by a MEMS gyro. The downside of a virtual gyro is that it is sensitive to linear acceleration and uncorrected magnetic interference. If either of those is present, you probably still want a physical gyro.
So how do we go from orientation to angular rates? It’s conceptually easy if you step back and consider the problem from a high level. Angular rate can be defined as change in orientation per unit time. We already know lots of ways to model orientation. Figure out how to take the derivative of the orientation and we’re there!
In our prior postings, we’ve discussed a number of ways to represent orientation. For this discussion, we will use the basic rotation matrix. Jack B. Kuipers has a nice derivation of the derivative of direction cosine matrices in his “Quaternions and Rotation Sequences” text – one of my most used textbooks. It makes a good starting point. Paraphrasing his math:
Let:
Then at any time t:
Differentiate both sides (use the chain rule on the RHS):
Our restrictions on no linear acceleration or magnetic interference imply that:
Then:
We know that:
Plugging this into (8) yields
In a previous posting (Accelerometer placement — where and why) , we learned about the transport theorem, which describes the rate of change of a vector in a moving frame:
Those who take the time to check will note that we have inverted the polarity of the ω in Equation 11 from that shown in the prior posting. In that case ω was the angular velocity of the body frame in the fixed reference frame. Here we want it from the opposite perspective (which would match gyro outputs).
And again,
Equating equations 10 and 13:
where:
0 | -ω_{z} | ω_{y} | |
ω X = | ω_{z} | 0 | -ω_{x} |
-ω_{y} | ω_{x} | 0 |
Going back to the fundamentals in our first calculus course and using a one-sided approximation to the derivative:
where Δt = the time between orientation samples
Recall that for rotation matrices, the transpose is the same as the inverse:
Equation 15 is a truly elegant equation. It shows that you can calculate angular rates based upon knowledge of only the last two orientations. That makes perfect intuitive sense, and I’m ashamed when I think how long it took me to arrive at it the first time.
An alternate form that is even more attractive can be had by carrying out the multiplications on the RHS:
For the sake of being explicit, let’s expand the terms. A rotation matrix has dimensions 3×3. So both left and right hand sides of Eqn. 22 have dimensions 3×3.
0 | W_{1,2} | W_{1,3} | |
W = RM_{t+1} RM_{t}^{T} – I_{3X3 }= | W_{2,1} | 0 | W_{2,3} |
W_{3,1} | W_{3,2} | 0 |
The zero value diagonal elements in W result from small angle approximations since the diagonal terms on RM_{t+1} RM_{t}^{T} will be close to one, which will be canceled by the subtraction of the identity matrix. Then:
0 | -ω_{z} | +ω_{y} | 0 | W_{1,2} | W_{1,3} | ||
ω X = | +ω_{z} | 0 | -ω_{x} | = (1/Δt) | W_{2,1} | 0 | W_{2,3} |
-ω_{y} | +ω_{x} | 0 | W_{3,1} | W_{3,2} | 0 |
and we have:
Once we have orientations, we’re in a position to compute corresponding angular rates with
at time each point. Sweet!
Some time ago, I ran a Matlab simulation to look at outputs of a gyro versus outputs from a “virtual gyro” based upon accelerometer/magnetometer readings. After adjusting for gyro offset and scale factors, I got pretty good correlation, as can be seen in the figure below.
You will notice that we started with an assumption that we already know how to calculate orientation given accelerometer/magnetometer readings. There are many ways to do this. I can think of three off the top of my head:
Whichever technique you use to compute orientations, you need to pay attention to a few details:
This is one of my favorite fusion problems. There’s a certain beauty in the way that nature provides different perspectives of angular motion. I hope you enjoy it also.
References