“The greatest glory in living
lies not in never falling,
but in rising every time we fall.”
Nelson Mandela

swing_up

The pendulum will now swing back upright after it fails and falls down.

I’ve managed to incorporate the swing-up controller to complement the stabilising controller when the pendulum is upright.  The trick was simple: the PID controller works by stabilising an inherently unstable system.  This is done with a combination of feedback of the error, integration of the error, and differentiation of the error, where error is the difference between the angle and the targeted angle (0 degrees if totally upright).  In control theory speak, the feedback works by moving the ‘poles’ of the system (natural frequency of the systems), which is originally positive, or on the right hand side, to the left hand side of the complex number plane.  In other words, the real part of the pole will be negative.

When the pendulum has fallen, the system then becomes inherently stable since for small moves the pendulum will just fall back down due to gravity and stay there.  In other words, the poles of that system is already negative.  To destabilise the system, I just made the gains of a PID controller negative, specifically the integrator and differentiator.  The trick is to keep the gain of the error term positive, which has the effect of keeping the pendulum near the center, rather than falling off the screen.

Link to demo here.