From the AHRS sensor fusion orientation filter to a 3d graphical rotating cube

Submitted by fabio on Fri, 2011-01-28 12:52.

In a comment from Ciskje on my sensor fusion implementation, I've been asked to explain how the Processing graphical uses rotations to visualize the orientation informations coming from the sensor fusion algorithm.

The question was basically, what's the reason about using the Yaw, Pitch and Roll in a "strange" order in the following lines of code in my Processing graphical cube example:

rotateZ(-Euler[2]); // phi - roll
rotateX(-Euler[1]); // theta - pitch
rotateY(-Euler[0]); // psi - yaw

The following explanation assumes some basic knowledge of rotation matrices and 3d coordinate systems. For a good introduction on the topics, I strongly suggest reading Chapter 2 of "Robot Modeling and Control" by Mark W. Spong.

Demonstration

The first thing you have to understand is that we have 3 coordinate systems here.

  • OsXsYsZs attached to the sensor array
  • OwXwYwZw attached to the real world, with Xw pointing to the Hearth North and Zw pointing to the sky
  • OmXmYmZm the coordinate system of the graphics program, usually it is attached on the top left corner of the monitor with Xm pointing right, Ym pointing down and Zm pointing forward to the monitor.

Note that when Xw and Zm are aligned (you point the monitor to the Earth north in your location) there is a simple relationship between OwXwYwZw and OmXmYmZm: a point pw defined in the coordinate system W having coordinates pw = [pw_x, pw_y, pw_z] can be expressed in the coordinate system M doing pm = [-pw_y, -pw_z, pw_x]

The sensor fusion algorithm running on Arduino computes a quaternion representing the orientation of OsXsYsZs with respect to OwXwYwZw, and from the quaternionToEuler function in the Processing code we can get the Euler angles expressed in the aerospace sequence, so they are the yaw (ψ - psi), pitch (θ - theta) and roll (φ - phi) angles.

We can use them in the following way: supposing that OsXsYsZs and OwXwYwZw are aligned in the beginning we can rotate OsXsYsZs so that it would assume the orientation described in the quaternion or yaw, pitch, roll angles by:

  1. rotate by ψ around Zs
  2. rotate by θ around Ys
  3. rotate by φ around Xs

After doing the above we will obtain that OsXsYsZs is oriented with respect to OwXwYwZw according to the orientation described by the output quaternion from the sensor fusion algorithm.

The same result could have been produced by rotations relative to the W coordinate system, but using the reverse order:

  1. rotate by φ around Xw
  2. rotate by θ around Yw
  3. rotate by ψ around Zw

However, we are interested in rotating the graphical cube which is defined in the OmXmYmZm coordinate system so a slightly different approach is needed. From the relationship between OwXwYwZw and OmXmYmZm described above we can see that there is a relationship between the rotations made in the W and M coordinate system:

  • a rotation φ around Xw corresponds to a rotation of -φ around Zm
  • a rotation θ around Yw corresponds to a rotation of -θ around Xm
  • a rotation ψ around Zw corresponds to a rotation of -ψ around Ym

Given the above and remembering that in Processing the rotations are made in respect to the monitor frame we can conclude that, in order to align a graphical cube defined in the OmXmYmZm to the orientation described by the quaternion and then Euler angles coming from the sensor fusion algorithm of the sensors coordinate system OsXsYsZs, we have to do the following rotations:

  1. a rotation of -φ around Zm
  2. a rotation of -θ around Xm
  3. a rotation of -ψ around Ym

as we wanted to demonstrate.

the real execute order

Submitted by JG (not verified) on Wed, 2012-10-31 03:42.

Excuse me,but i notice that when you write" rotateZ();rotateX();rotateY();" in Processing,the real execute order is Y - X -Z, is there someting wrong? thx 4 reply

replicate

Submitted by Anonymous (not verified) on Tue, 2012-10-30 04:55.

i have tried to rweplicate your work, but have been unable.

can you help, and if yes, tell please as if i were child, and unknowing on hhow to do this stuff

thank you

I'm sorry but this is as

Submitted by fabio on Tue, 2012-10-30 11:36.

I'm sorry but this is as simple as I can do it.. I know, it's not easy, but these are complex concepts which takes time to be understood.. it's like that. I strongly suggest you to study more about the geometric concepts behind this.. the book I suggest is a good one but there are a lot more.

Good luck

Hello Fabio!! why the angle

Submitted by Agostino (not verified) on Mon, 2012-09-17 20:50.

Hello Fabio!!
why the angle of Pitch never exceeds 90 °, but it always stops at about 82-83 °? .... and why it never reaches the 180° like roll and yaw???
Thank you!!

I trust you at 100%! What's

Submitted by avenue33 (not verified) on Fri, 2011-01-28 22:21.

I trust you at 100%!

What's important for me, the library based on those formulas actually works!

:-) .. and that's not the

Submitted by fabio on Fri, 2011-01-28 22:42.

:-) .. and that's not the sensor fusion.. just the rotating cube stuff! ;-)

Simple! :)

Submitted by Ciskje (not verified) on Fri, 2011-01-28 15:37.

Simple! :)

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a personal or company website insert its address in the form http://www.example.com/ .
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <small> <del> <img> <h2> <h3> <h4> <b> <video> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options