Is it possible to find the unit vector with:
Roll € [-90 (banked to right), 90 (banked to left)],
Pitch € [-90 (all the way down), 90 (all the way up)]
Yaw € [0, 360 (N)]
I calculated it without the Roll and it is
\begin{pmatrix}
cos(Pitch) sin(Yaw)\\
cos(Yaw) cos(Pitch)\\
sin(Pitch)
\end{pmatrix}.
How should it be with the Roll rotation and how can I get to this result?
My coordinate system is with +z up, +x right and +y forward
Many thanks!
[Math] Find unit vector given Roll, Pitch and Yaw
geometrymatricesrotationstrigonometryvectors
Related Solutions
I'll use the OMAF coordinate system which defines the viewer looking out the $x$-axis with $z$-axis up and the $y$-axis to the left. Given a yaw angle $\phi$ and a pitch angle $\theta$ we transform the direction vector $(u,v,w)$ onto the view axis $(1,0,0)$ by first performing a clockwise rotation about the $z$-axis by $\phi$ followed by a counter-clockwise rotation about the $y$-axis by $\theta:$ $$ \left[\begin{array}{c}1\\0\\0\end{array}\right] = R_y(\theta) \cdot R_z(-\phi) \left[\begin{array}{c}u\\v\\w\end{array}\right] $$
We then solve for $(u,v,w)$ and get $$ \left[\begin{array}{c}u\\v\\w\end{array}\right] = R_z(\phi) \cdot R_y(-\theta) \left[\begin{array}{c}1\\0\\0\end{array}\right] $$
$$ \left[\begin{array}{c}u\\v\\w\end{array}\right] = \left[\begin{array}{ccc}\cos\phi & -\sin\phi & 0 \\ \sin\phi & \cos\phi & 0\\ 0 & 0 & 1 \end{array}\right]\left[\begin{array}{ccc}\cos\theta & 0 & -\sin\theta \\ 0 & 1 & 0 \\ \sin\theta & 0 & \cos\theta \end{array}\right] \left[\begin{array}{c}1\\0\\0\end{array}\right] = \left[\begin{array}{c}\cos\phi\cos\theta \\ sin\phi \cos\theta \\ \sin\theta \end{array}\right] $$
which is your classical spherical to cartesian transformation where $\phi$ is the azimuthal angle and $\theta$ is the elevation angle. Note that the roll angle does not enter into the equation since it merely defines a rotation about the direction vector which does not alter the direction vector.
If you happen to be using OpenGL conventions where the viewer is looking out the $-z$-axis and the $y$-axis is "up" and the $x$-axis is to the right you will use the direction $(u',v',w') = (-v,w,-u).$
Let's say your three orthogonal unit vectors are $$\hat{i} = \left [ \begin{matrix} i_x \\ i_y \\ i_z \end{matrix} \right ], \quad \hat{j} = \left [ \begin{matrix} j_x \\ j_y \\ j_z \end{matrix} \right ], \quad \hat{k} = \left [ \begin{matrix} k_x \\ k_y \\ k_z \end{matrix} \right ]$$ The rotation matrix $\mathbf{R}$ that rotates $x$ axis to $\hat{i}$, $y$ axis to $\hat{j}$, and $z$ axis to $\hat{k}$ is $$\mathbf{R} = \left [ \begin{matrix} i_x & j_x & k_x \\ i_y & j_y & k_y \\ i_z & j_z & k_z \end{matrix} \right ]$$ Because $\mathbf{R}$ is orthonormal, its inverse is its transpose; and the matrix $\mathbf{R}^{-1}$ that rotates $\hat{i}$ to $x$ axis, $\hat{j}$ to $y$ axis, and $\hat{k}$ to $z$ axis, is $$\mathbf{R}^{-1} = \mathbf{R}^T = \left [ \begin{matrix} i_x & i_y & i_z \\ j_x & j_y & j_z \\ k_x & k_y & k_z \end{matrix} \right ]$$
Euler and Tait-Bryan rotations are combinations of the three axis rotations, $$\begin{aligned} \mathbf{R}_x &= \left [ \begin{matrix} 1 & 0 & 0 \\ 0 & \cos\phi & -\sin\phi \\ 0 & \sin\phi & \cos\phi \end{matrix} \right ] \\ \mathbf{R}_y &= \left [ \begin{matrix} \cos\theta & 0 & \sin\theta \\ 0 & 1 & 0 \\ -\sin\theta & 0 & \cos\theta \end{matrix} \right ] \\ \mathbf{R}_z &= \left [ \begin{matrix} \cos\psi & -\sin\psi & 0 \\ \sin\psi & \cos\psi & 0 \\ 0 & 0 & 1 \end{matrix} \right ] \end{aligned}$$ where $\phi$, $\theta$, and $\psi$ specify the intrinsic rotations around the $x$, $y$, and $z$ axis, respectively. The combined rotation matrix is a product of three matrices (two or three of the above; first one can be repeated) multiplied together, the first intrinsic rotation rightmost, last leftmost.
The hard part, in my humble opinion, is to determine exactly which the correct order of rotations is. I do believe that in OP's case it is $\mathbf{R}_z \mathbf{R}_y \mathbf{R}_x$, but I am not sure; since this is apparently for an articulated arm of some sort, I would definitely numerically verify this first.
Except that I would not, because Euler/Tait-Bryan angles are evil due to their inherent ambiguity; I would use unit quaternions (versors) to describe the orientation instead. They're easier, logical, and well defined.
The problem at hand, is that at "run time", we have the nine numerical components of $\mathbf{R}$, but need to find out the angles $\phi$, $\theta$, and $\psi$.
If the correct rotation formalism indeed is $\mathbf{R} = \mathbf{R}_z \mathbf{R}_y \mathbf{R}_x$ then $$\mathbf{R} = \left [ \begin{matrix} \cos\theta \cos\psi & \sin\phi \sin\theta \cos\psi - \cos\phi \sin\psi & \cos\phi \sin\theta \cos\psi + \sin\phi \sin\psi \\ \cos\theta \sin\psi & \sin\phi \sin\theta \sin\psi + \cos\phi \cos\psi & \cos\phi \sin\theta \sin\psi - \sin\phi \cos\psi \\ -\sin\theta & \sin\phi \cos\theta & \cos\phi \cos\theta \end{matrix} \right ]$$ and comparing to $\mathbf{R}$, we pick the five entries with the simpler terms (and all Euler and Tait-Bryan angles have five such entries; their position and exact terms vary), finding that $$\left\lbrace \begin{aligned} i_x &= \cos\theta \cos\psi \\ i_y &= \cos\theta \sin\psi \\ i_z &= -\sin\theta \\ j_z &= \sin\phi \cos\theta \\ k_z &= \cos\phi \cos\theta \end{aligned} \right .$$ If we choose $-90° \le \theta \le +90°$, then $\cos\theta \ge 0$, and we can solve the angles, getting: $$\left\lbrace \begin{aligned} \cos\phi &= \frac{ k_z }{\sqrt{1 - i_z^2}} \\ \sin\phi &= \frac{ j_z }{\sqrt{1 - i_z^2}} \\ \cos\theta &= \sqrt{1 - i_z^2} \\ \sin\theta &= -i_z \\ \cos\psi &= \frac{ i_x }{\sqrt{1 - i_z^2}} \\ \sin\psi &= \frac{ i_y }{\sqrt{1 - i_z^2}} \end{aligned} \right . $$ In other words, $$\left\lbrace \begin{aligned} \phi &= \arctan\left(\frac{j_z}{k_z}\right) \\ \theta &= \arctan\left(\frac{-i_z}{\sqrt{1-i_z^2}}\right) = \arcsin\left(-i_z\right) \\ \psi &= \arctan\left(\frac{i_y}{i_x}\right) \end{aligned} \right . $$ On a microcontroller without hardware floating-point operation support, one can use the numeric $\sin$ and $\cos$ values as an unit vector (noting that they might not be exactly unit length, due to rounding errors when calculating the original matrix $\mathbf{R}$), and apply e.g. CORDIC to obtain the angles at desired precision.
Best Answer
There are a lot of questions like this, all slightly different. My earlier answer to a different question is closely related to what you need, but the question is different enough that I thought it better to write an answer a little more applicable to your case.
I'll use right-handed $x,y,z$ Cartesian coordinates. I visualize roll, pitch, and yaw using the motion of someone's right hand, whose thumb and index finger are kept outstretched at right angles.
We start out with index finger of pointing in the direction $(0,1,0)^T$ (positive $y$ axis) and thumb pointing in the direction $(0,0,1)^T$ (positive $z$ axis). We turn the entire hand by the angle $\theta$ (the "yaw" angle) counterclockwise around the $z$ axis. We then rotate the hand in the plane now occupied by the index finger and thumb, turning it by the angle $\phi$ (the "pitch" angle) with the index finger moving in the direction of the thumb.
At this point the angles $\phi$ and $\theta$ describe the direction of the index finger in something like geographic coordinates, where $\phi$ corresponds to latitude and $\theta$ corresponds to longitude. (Note that these are not the kind of "mathematical" spherical coordinates where $\phi$ is the angle of a vector relative to the $z$-axis.)
Finally, we rotate the hand using the direction of the index finger as the axis of rotation, turning the thumb toward the palm of the hand by an angle $\psi$ (the "roll" angle).
The effect of these motions on a vector "attached" to the hand can be represented by a $3\times3$ matrix. We can decompose this matrix into a product of three much simpler matrices. Each of these three matrices will be a rotation around one of the principal axes ($x$, $y$, or $z$ axes), unlike the pitch and roll motions described above, which were performed around axes relative to an already-rotated hand.
To reproduce the result of the roll, pitch, and yaw on the orientation of the hand, using rotations only around the principal axes, we have to do the rotations in reverse order. This is so that we can still do pitch and roll around the correct axes relative to the hand, but do them while those axes are still aligned with the principal axes.
We do the "roll" through angle $\psi$ first, using the rotation matrix $$ R_y(\psi) = \begin{pmatrix} \cos\psi & 0 & -\sin\psi \\ 0 & 1 & 0 \\ \sin\psi & 0 & \cos\psi \end{pmatrix} $$ (a rotation around the $y$-axis), then the "pitch" through angle $\phi$, using the matrix $$ R_x(\phi) = \begin{pmatrix} 1 & 0 & 0 \\ 0 & \cos\phi & -\sin\phi \\ 0 & \sin\phi & \cos\phi \end{pmatrix} $$ (a rotation around the $x$-axis), and finally the "yaw" through angle $\theta$, using the matrix $$ R_z(\theta) = \begin{pmatrix} \cos\theta & \sin\theta & 0\\ -\sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{pmatrix} $$ (a rotation around the $z$-axis). These are much like the axis-rotation matrices used in several other places (including my earlier answer), but with a sequence of axes and signs of the matrix entries suitable to your system.
The best way to understand how this works may be to try several examples, using simple pitch, roll, and yaw angles such as $\pi/2$ or $\pi/4$, and confirm that this sequence of rotations around fixed principal coordinate axes has the same result as the desired sequence of rotations around axes defined by the orientation of the hand.
This sequence of rotations is equivalent to the single rotation performed by the matrix product $R_z(\theta)R_y(\phi)R_x(\psi)$. For example, here's what this rotation does to the direction of the index finger, $(0,1,0)^T$: \begin{align} R_z(\theta) R_x(\phi) R_y(\psi) \begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix} &= R_z(\theta) R_x(\phi) \begin{pmatrix} \cos\psi & 0 & -\sin\psi \\ 0 & 1 & 0 \\ \sin\psi & 0 & \cos\psi \end{pmatrix} \begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix} \\ &= R_z(\theta) R_x(\phi) \begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix} \\ &= R_z(\theta) \begin{pmatrix} 1 & 0 & 0 \\ 0 & \cos\phi & -\sin\phi \\ 0 & \sin\phi & \cos\phi \end{pmatrix} \begin{pmatrix} 0 \\ 1 \\ 0 \end{pmatrix} \\ &= R_z(\theta) \begin{pmatrix} 0 \\ \cos\phi \\ \sin\phi \end{pmatrix} \\ &= \begin{pmatrix} \cos\theta & \sin\theta & 0\\ -\sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} 0 \\ \cos\phi \\ \sin\phi \end{pmatrix} \\ &= \begin{pmatrix} \cos\phi \sin\theta \\ \cos\phi \cos\theta \\ \sin\phi \end{pmatrix}, \\ \end{align} which agrees with your result.
What this same rotation does to the direction of the thumb, $(0,0,1)^T$, is \begin{align} R_z(\theta) R_x(\phi) R_y(\psi) \begin{pmatrix} 0 \\ 0 \\ 1 \end{pmatrix} &= R_z(\theta) R_x(\phi) \begin{pmatrix} \cos\psi & 0 & -\sin\psi \\ 0 & 1 & 0 \\ \sin\psi & 0 & \cos\psi \end{pmatrix} \begin{pmatrix} 0 \\ 0 \\ 1 \end{pmatrix} \\ &= R_z(\theta) R_x(\phi) \begin{pmatrix} -\sin\psi \\ 0 \\ \cos\psi \end{pmatrix} \\ &= R_z(\theta) \begin{pmatrix} 1 & 0 & 0 \\ 0 & \cos\phi & -\sin\phi \\ 0 & \sin\phi & \cos\phi \end{pmatrix} \begin{pmatrix} -\sin\psi \\ 0 \\ \cos\psi \end{pmatrix} \\ &= R_z(\theta) \begin{pmatrix} -\sin\psi \\ -\cos\psi \sin\phi \\ \cos\psi \cos\phi \end{pmatrix} \\ &= \begin{pmatrix} \cos\theta & \sin\theta & 0\\ -\sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} -\sin\psi \\ -\cos\psi \sin\phi \\ \cos\psi \cos\phi \end{pmatrix} \\ &= \begin{pmatrix} -\sin\psi \cos\theta - \cos\psi \sin\phi \sin\theta \\ \sin\psi \sin\theta - \cos\psi \sin\phi \cos\theta \\ \cos\psi \cos\phi \end{pmatrix} \\ \end{align} if I haven't dropped a sign or made some other arithmetic error.