Yaw, Pitch and Roll composition I'm trying to understand composition of rotations using eulers angles and rotation matrices.
I am facing a counterintuitive situation performing two rotations about different angles.
My setting is the following:


*

*first rotation of an angle $\psi$ about the Z body-axis (yaw)

*second rotation of an angle $\theta$ about the Y body-axis (pitch)

*third rotation of an angle $\varphi$ about the X body-axis (roll)


Denoting with
$$R_X=\begin{bmatrix}1&0&0\\
0&\cos(\varphi)&-\sin(\varphi)\\
0&\sin(\varphi)&\cos(\varphi)\end{bmatrix}$$
$$R_Y=\begin{bmatrix}\cos(\theta)&0&\sin(\theta)\\
0&1&0\\
-\sin(\theta)&0&\cos(\theta)\end{bmatrix}$$
$$R_Z=\begin{bmatrix}\cos(\psi)&-\sin(\psi)&0\\
\sin(\psi)&\cos(\psi)&0\\
0&0&1\end{bmatrix}$$
the rotation matrices relative to elementary rotation about the three body axis, I obtain that a general rotation of eulers angles $(yaw,pitch,roll)=(\psi,\theta,\varphi)$ is given by:
$$R=R_X\cdot R_Y\cdot R_Z$$
Suppose now that I would like to perform this 2 consecutive rotations:


*

*Rotation of $(yaw,pitch,roll)=(0,\frac{\pi}{4},0)$

*Rotation of $(yaw,pitch,roll)=(\frac{\pi}{2},0,0)$
The matrices relative to these two rotations are the following:
$$R_1=\begin{bmatrix}\frac{\sqrt{2}}{2}&0&\frac{\sqrt{2}}{2}\\
0&1&0\\
-\frac{\sqrt{2}}{2}&0&\frac{\sqrt{2}}{2}\end{bmatrix}$$
$$R_2=\begin{bmatrix}1&0&0\\
0&0&-1\\
0&1&0\end{bmatrix}$$
and composing them I obtain the total rotation matrix 
$$R=R_2\cdot R_1 = \begin{bmatrix}0&-1&0\\
\frac{\sqrt{2}}{2}&0&\frac{\sqrt{2}}{2}\\
-\frac{\sqrt{2}}{2}&0&\frac{\sqrt{2}}{2}\end{bmatrix}$$
If I want to recover the euler angles sequence relative to $R$ I apply these equations:
$$\theta=\arcsin(R_{13})\\
\psi=-\arctan2\left(\frac{R_{12}}{\cos(\theta)},\frac{R_{11}}{\cos(\theta)}\right)\\
\varphi=-\arctan2\left(\frac{R_{23}}{\cos(\theta)},\frac{R_{33}}{\cos(\theta)}\right)$$
obtaining $(yaw,pitch,roll)=(\frac{\pi}{2},0,-\frac{\pi}{4})$.
However applying this sequence of rotation I do not recover the initial frame (sequence of rotations 1-2) unless the rotations 1-2 are performed on different axis respect the body ones.
Probably I miss something and I'm making confusion with these concepts.
Please, could you help me to understand where I miss?
 A: I believe the problem you are having is that if you multiply the matrices in the order $R_X\cdot R_Y\cdot R_Z$, then you are rotating first around the global $Z$ axis, then the global $Y$ axis, then the global $X$ axis.
To get the rotations applied to the local (body) axes instead, you simply have to reverse the order of multiplication.
This might seem like magic, so I'll give a short explanation. After applying the $Z$-axis rotation, the body reference frame is not the same as the global reference frame: It has been rotated by $R_Z$. So at this point, if $A$ is a matrix denoting any transformation according to the body frame, then in the global frame that same transformation has the matrix representation
$$
R_Z\cdot A\cdot R_Z^{-1}
$$
(This will take a vector expressed in the global basis, then $R_Z^{-1}$ is the same vector expressed in the body basis. We apply $A$, then translate back to the global basis with $R_Z$.)
So first applying $R_Z$, then after that applying $R_Y$ in the new local frame has matrix representation
$$
\left(R_Z\cdot R_Y\cdot R_Z^{-1}\right)\cdot R_Z = R_Z\cdot R_Y
$$
If you want to apply a rotation about the body $X$-axis after this again, then similarily, the global matrix representation of this transformation is
$$
(R_Z\cdot R_Y)\cdot R_X\cdot (R_Z\cdot R_Y)^{-1}
$$
and if we apply this after our initial rotation of $R_Z\cdot R_Y$, then the result becomes
$$
R_Z\cdot R_Y\cdot R_X
$$
