Solution (source code)

= Solution

Take $\theta$ to be the angle between $\mathbf e_3$ and the upward vertical. From the supplied angular-velocity components,
$$
T=\frac I2(\dot\theta^2+\sin^2\theta\,\dot\phi^2)
+\frac J2(\dot\psi+\cos\theta\,\dot\phi)^2,
\qquad V=Mga\cos\theta.
$$
Therefore
$$
L=\frac I2(\dot\theta^2+\sin^2\theta\,\dot\phi^2)
+\frac J2(\dot\psi+\cos\theta\,\dot\phi)^2-Mga\cos\theta.
$$
Both $\psi$ and $\phi$ are cyclic. Their conserved conjugate momenta are
$$
p_\psi=J(\dot\psi+\cos\theta\,\dot\phi),
\qquad
p_\phi=I\sin^2\theta\,\dot\phi+p_\psi\cos\theta,
$$
and $p_\theta=I\dot\theta$. Solving for the velocities and taking the Legendre transform gives
$$
\boxed{H=\frac{p_\theta^2}{2I}
+\frac{(p_\phi-p_\psi\cos\theta)^2}{2I\sin^2\theta}
+\frac{p_\psi^2}{2J}+Mga\cos\theta}.
$$