# 3D Transformation Between Two Cartesian Coordinate Systems Using Euler Angles

I am trying to transform any arbitrary vector $\bf{v_{1}}$ in a Cartesian frame $\mathscr{F_{1}}$ into a different Cartesian frame $\mathscr{F_{2}}$. Both $\mathscr{F_{1}}$ and $\mathscr{F_{2}}$ have the same origin. I have consulted Aircraft Flight Dynamics and Control by Wayne Durham, and understand that book's solution as follows:

Use the standard yaw-pitch-roll $z,y,x$ $/$ {$\theta_{z},\phantom{s} \theta_{y},\phantom{s} \theta_{x}$} (usually called {$\psi, \theta, \phi$} respectively) / $3 2 1$ order. There exist three transformation matrices, one for each axis. First, rotate about the z axis of $\mathscr{F_{1}}$. Call the transformation matrix $T_{\mathscr{F'},1}$ since it goes from $\mathscr{F_{1}}$ to an intermediate frame $\mathscr{F'}$. $T_{\mathscr{F'},1}$ is given as

$$T_{\mathscr{F'},1}=\begin{bmatrix} cos(\theta_{z}) & sin(\theta_{z}) & 0 \\ -sin(\theta_{z}) & cos(\theta_{z}) & 0 \\ 0 & 0 & 1 \\ \end{bmatrix}$$

To go from $\mathscr{F'}$ to $\mathscr{F''}$, you then rotate about the y' axis using $$T_{\mathscr{F''},\mathscr{F'}}=\begin{bmatrix} cos(\theta_{y}) & 0 & -sin(\theta_{y}) \\ 0 & 1 & 0 \\ sin(\theta_{y}) & 0 & cos(\theta_{y}) \\ \end{bmatrix}$$

Finally, to go from $\mathscr{F''}$ to $\mathscr{F_{2}}$, you then rotate about the x'' axis using

$$T_{\mathscr{F''},\mathscr{F'}}=\begin{bmatrix} 1 & 0 & 0 \\ 0 & cos(\theta_{x}) & sin(\theta_{x}) \\ 0 & -sin(\theta_{x}) & cos(\theta_{x}) \\ \end{bmatrix}$$

A composite transformation matrix $\bf{T}$ can be made by multiplying these transforms together. Individual transform steps on a generic vector $\bf{v}$ are given in the book (and I reproduced them below) to show order determination.

$$\bf{v'}=T_{\mathscr{F'},1} \bf{v}$$ $$\bf{v''}=T_{\mathscr{F''},\mathscr{F'}} \bf{v'}$$ $$\bf{v_{2}}=T_{\mathscr{F_{2}},\mathscr{F''}} \bf{v''}$$

$$\bf{v_{2}}=T_{\mathscr{F_{2}},\mathscr{F''}} T_{\mathscr{F''},\mathscr{F'}} T_{\mathscr{F'},\mathscr{F_{1}}} \bf{v}$$

$$\bf{v_{2}}=\bf{T}v_{1}$$

So $\bf{T} = T_{\mathscr{F_{2}},\mathscr{F''}} T_{\mathscr{F''},\mathscr{F'}} T_{\mathscr{F'},\mathscr{F_{1}}}$ (strictly in this order).

My problem arose when I coded this transformation into MATLAB and tested some examples which I could verify qualitatively (with some coordinate axes I made out of wood).

The transformation matrix $\bf{T}$ that works is $\bf{T} = (T_{\mathscr{F'},\mathscr{F_{1}}} T_{\mathscr{F''},\mathscr{F'}} T_{\mathscr{F_{2}},\mathscr{F''}})^{T}$. Thus, in order for the transformation to work the way I expect it should, I need to multiply the matrices in reverse order and take their transpose. What has gone wrong?

• I'm no expert, so this is just a comment. I suggest you first convert to Quaternions, rotate as a Quaternion, then convert back to whatever you need. Why? Euler Angles depend on the order that the angles are applied. They are also subject to Gimble Lock. Quaterions avoid both of these issues. Re order of application: It is possible to get different results if you apply yaw/pitch/roll versus applying one of the other combination, such as pitch/yaw/roll. Quaternions have a lot of software and documentation support because game programmers need them. – philologon May 12 '17 at 1:13
• Also, if you application will end up in software, look for already-done solutions in a package manager for the particular language you are working in. Here is one for Python: stackoverflow.com/a/4870905/1339950 – philologon May 12 '17 at 1:17
• Thank you for your comment. Unfortunately, I'm no expert either, and have only come across the word Quaternion today. It is not inconceivable that I could learn them, but I just need this transformation for angles less than 90 degrees anyway. I will keep it in mind for the future though. – Unique Worldline May 12 '17 at 1:18
• Often people feel like they have to show the math details before explaining how to use them. That often frightens people who are in a hurry. Don't let that dissuade you. Using them is easy. Most packages/modules/classes allow you to start a new one by passing in Euler Angles. Then doing what you need to, then passing in the points to be transformed and getting points in the new system out. I will be studying up on them soon myself (I hope I need to, anyway). – philologon May 12 '17 at 1:23
• This will probably get better, or more advanced, answers over at math.SE or Physics.SE. – Carl Witthoft May 12 '17 at 11:41

Suppose Frame 1 is a world coordinate frame and Frame 2 local robot frame.

The thing that's a little confusing here is that when you transform from Frame 1 to Frame 2, you are saying that you want to... transform frame 1 to frame 2. You want to take all of frame 1's points, and put them in frame 2. You want measure where points in frame 1 are located, but in frame 2's coordinate system.

This is a problem because what you probably want to do is measure/control the robot's frame (position) in the world frame coordinate system. You care if your vehicle is 10 degrees above the horizon, or the global coordinate of an end effector, etc. This means that (counter-intuitively for me at least), if you want to see what something reads in frame 1's coordinates, then you need to import it into frame 1.

So, if you want to use frame 1's coordinates to measure an object in frame 2, you need to take the transform from frame 1 to frame 2. To harp on this one more time, your math gives:

$$v_2 = Tv_1 \\$$

You can see that your input is $v_1$, you transform the inputs by $T$, and then your outputs are measured in frame 2's coordinates.

What you instead want to do is to take the inverse transform. This gives you the following:

$$v_2 = Tv_1 \\ T^{-1} \left(v_2\right) = T^{-1}\left(Tv_1 \right) \\ T^{-1}v_2 = v_1 \\$$

I'll point out briefly that you're using $T$ for a transform matrix, but the way you've defined it it's really a rotation matrix; all of your $T_F$ functions are 3x3 matrices.

In either event, transform or rotation, it doesn't matter! The rotation matrix is the upper-left 3x3 section of a 4x4 transform matrix, and taking the transpose of a rotation matrix is the same as taking the inverse of the same rotation matrix.

So your coordinates are coming out wrong because you're not measured the points you provided in the frame you think you are. It's "working" when you take the transpose because that is actually the same as the inverse, which means you are reversing the directionality of the transform.

• Thank you! Besides some other sloppiness on my part to do with negative signs, this is what the issue was. – Unique Worldline May 12 '17 at 21:38
• @UniqueWorldline. Yes so you modeled things upside down, that is exactly the same as having swapped the direction of your matrix to a different major order. If you really found the other way more intuitive. Go for it.. Nothing says have to model exactly this way. See matrix multiplication is symmetrical across the transpose in inverse multiplication order (which is also why a rotations inverse is its own transpose). Nothing else changes except direction of operations. – joojaa May 12 '17 at 22:20

You have actually not done anything wrong. Its just that Matrix algebra, much to the chagrin of some, does not actually define how you need to pack your data into the matrix. Your choice.

So the question is do you consider that vectors inside your matrix to be columns or rows. This is called column major or row major. What you simply get is a transposed solution, and a inverse calculation order. It happens, some literature is in row major and some is in column major notation. Its all a question of whether you you want to look form object out or form outside in, your choice. But you need to be aware of this or you end up with problems like this, or well they aren't problems just definitinons.

Using qaternions or any other method does get you the same issue.