Lagrangian Dynamics of an inverted Spherical Cart Pendulum Introduction
I have to come up with a PD-controller for an inverted Spherical Cart Pendulum, therefore I tried to compute the Dynamics of such a Pendulum.

The Spherical Cart Pendulum is a hybrid between the Cart Pole and the Spherical Pendulum. The underlying Cart can move in the X-Y Plane.

Basics
$l$ is the length of the Pendulum, $m_p$ is the mass of the pendulum, $m_c$ is the mass of the cart, $\theta$ denotes the azimuthal and $\phi$ the polar
As generalized Coordinates I use the conversion between spherical Coordinates and cartesian Coordinates:
$$x=l\sin(\theta)\cos(\phi)$$
$$y=l\sin(\theta)\sin(\phi)$$
$$z=l\cos(\theta)$$
The generalized Coordinates for the Pendulum look like this:
$$x_p=l\sin(\theta)\cos(\phi)+x$$
$$y_p=l\sin(\theta)\sin(\phi)+y$$
$$z_p=l\cos(\theta)$$
and
$$\dot x_p = -l\sin(\phi)\sin(\theta)\dot\phi+l\cos(\phi)\cos(\theta)\dot\theta+\dot x$$
$$\dot y_p = l\sin(\phi)\cos(\theta)\dot\theta+l\sin(\theta)\cos(\phi)\dot\phi+\dot y$$
$$\dot z_p = -l\sin(\theta)\dot \theta$$
The Lagrangian Equation is defined by:
$$L=T-V$$
with
$$T=T_c+T_p$$
$$T_c=\frac{1}{2}m_c(\dot x^2+\dot y^2)$$
$$T_c=\frac{1}{2}m_p(\dot x_p^2+\dot y_p^2 + \dot z_p^2)$$
and
$$V=m_p\cdot g \cdot z_p$$
results in:
$$L=- g l m_{p} \cos{\left(\theta \right)} + 0.5 l^{2} m_{p} \sin^{2}{\left(\theta \right)} \dot{\theta}^{2} + 0.5 m_{c} \dot{x}^{2} + 0.5 m_{c} \dot{y}^{2} + 0.5 m_{p} \left(- l \sin{\left(\phi \right)} \sin{\left(\theta \right)} \dot{\phi} + l \cos{\left(\phi \right)} \cos{\left(\theta \right)} \dot{\theta} + \dot{x}\right)^{2} + 0.5 m_{p} \left(l \sin{\left(\phi \right)} \cos{\left(\theta \right)} \dot{\theta} + l \sin{\left(\theta \right)} \cos{\left(\phi \right)} \dot{\phi} + \dot{y}\right)^{2}$$
and after $$\frac{\partial L}{\partial q_j}-\frac{d}{dt}\frac{\partial L}{\partial \dot q_j}=0$$
I get a set of differential equations$A=(\ddot \phi, \ddot \theta, \ddot x, \ddot y)^T$. They are all looking good, except one:
Now my Problem:
The equilibrium point of the inverted Pendulum I want to control is at $\theta=0$. Therefore the differential equation for
$$
\ddot{\phi} =\frac{- 2 l \cos{\left(\theta \right)} \dot{\phi} \dot{\theta} + \sin{\left(\phi \right)} \ddot{x} - \cos{\left(\phi \right)} \ddot{y}}{l \sin{\left(\theta \right)}}
$$
$$
\lim_{\theta \to 0}\ddot{\phi}=\infty
$$
means that I cannot compute $\theta$ for very small angles. I know that for $\ddot x=0$ and $\ddot y=0$, $\ddot \phi$ is a cyclic Coordinate and displays the angular momentum.
How can I interpret $\ddot \phi$ in a way that my differential equation makes sense and does not explode into the infinity?
 A: Imagine the pendulum swinging along $y=0$, with $x$ changing. As the pendulum swings directly below the cart, $\phi$ instantly changes from 0 to $\pi$, so you would expect $\ddot{\phi}$ to be infinite - when $\theta=0$, $\phi$ can take any value without the mass moving. It's similar to gimbal lock (https://en.wikipedia.org/wiki/Gimbal_lock).
You probably need to rephrase in a different coordinate system or reference frame - for example, set $\theta$ to be the angle from the z-axis in the yz-plane, and $\phi$ to be the angle from the z-axis in the xz-plane, and redo the analysis, that shouldn't lead to either blowing up with the pendulum vertical with $\theta=\phi=0$ (but will have discontinuities with it horizontal if aligned to the axes, so at $\theta=\pi/2$, $phi$ will be similarly undefined).
A: You are modelling this using a simple pendulum.  $\phi$ describes the angular displacement of the point about the $z$-axis.  This only makes sense if $\theta\neq0$.
As $\theta\to0$, using conservation laws it makes sense that $\ddot\phi$ increases.
Your problem is that you are trying to interpret the $\theta\to0$ case by considering a real-life physical pendulum bob that can spin about an axis passing through it (i.e. it's not point-like). Since you used a point in your analysis, spinning about an axis passing through the point does not make mathematical sense.  Therefore, for the $\theta=0$ case, one cannot even talk about $\phi$ because a point-like particle does not have angular displacement about an axis passing through that same point (whereas an object that occupies space would). As such, your equation breaks down for $\theta=0$.
For small angles $\theta$ angles, the physical size of the pendulum bob does matter, and, if accounted for mathematically, will give you the correct expression for $\ddot \phi$ (which, as you correctly stated, will be related to the angular momentum of the physical pendulum).
The takeaway is that your equations are approximations, and in particular the $\ddot\phi$ expression becomes considerably less accurate for small angles of $\theta$.

@sqek 's answer is true -- by redefining your coordinate system, you get to 'displace,' if you will, the problem of $\phi$ 'losing its meaning' in your coordinate system.
