In this tutorial, we will show how to use Collimator's notebook to design a full-state feedback controller for an inverted pendulum system.

The inverted pendulum system is one of the well-known benchmarks in control systems. It has several different variations including the rotary pendulum and the two-wheeled inverted pendulum. In this tutorial, we will consider an inverted pendulum with a cart as shown in **Figure 1 **with the parameters given in **Table 1.**

**Table 1 **Inverted pendulum model parameters

\begin{array} {|l|l|}\hline Symbol & Description & Value & Unit \\ \hline m & \text{Rod mass} & 0.15 & kg \\ \hline M & \text{Cart mass} & 0.4 & kg \\ \hline L & \text{Rod length} & 0.05 & m \\ \hline J & \text{Rod intertia} & 0.005 & Kg.m^2 \\ \hline b & \text{Friction constant} & 0.8 & N.m.s \\ \hline \end{array}

Here, we will consider a linear model around the equilibrium point of interest at the balance position. The Inverted pendulum equations of motion are described in the model as a system of differential equations for the coupled dynamics of both the cart and rod.

$$\left(J+mL^2\right)\ddot{\theta}-mgL\theta=mL\ddot{x}$$

$$\left(M+m\right)x+b\dot{x}-mL\ddot{\theta}=F$$

This model represents perturbed dynamics around the vertical balance angle. The above equations can be casted into a state variable model as:

$$\left[\begin{matrix}\dot{x}\\\ddot{x}\\\dot{\theta}\\\ddot{\theta}\\\end{matrix}\right]=\left[\begin{matrix}0&1&0&0\\0&\frac{-\left(J+mL^2\right)b}{J\left(M+m\right)+MmL^2}&\frac{m^2gL^2}{J\left(M+m\right)+MmL^2}&0\\0&0&0&1\\0&\frac{-mLb}{J\left(M+m\right)+MmL^2}&\frac{mgL\left(M+m\right)}{J\left(M+m\right)+MmL^2}&0\\\end{matrix}\right]\left[\begin{matrix}x\\\dot{x}\\\theta\\\dot{\theta}\\\end{matrix}\right]\left[\begin{matrix}0\\\frac{J+mL^2}{J\left(M+m\right)+MmL^2}\\0\\\frac{mL}{J\left(M+m\right)+MmL^2}\\\end{matrix}\right]F$$

$$y=\left[\begin{matrix}1&0&0&0\\0&0&1&0\\\end{matrix}\right]\left[\begin{matrix}x\\\dot{x}\\\theta\\\dot{\theta}\\\end{matrix}\right]+\left[\begin{matrix}0\\0\\\end{matrix}\right]F$$

To begin analysis in Collimator's notebook, we import some useful libraries:

Then, we define the model parameters and the state variable matrices:

If we investigate the dynamics of the inverted poles, we will find an unstable pole as expected:

Before moving on with designing the control, we would like to check if this system is stabilizable and controllable. We can do so by investigating the rank of the controllability matrix:

As the controllability matrix for the system has a full rank, the system can be controlled through full-state feedback.

Our inverted pendulum controller will have these design requirements:

- The angle never exceeds \(0.05 rad\) for a position step command.
- Settling time for position and angle less than \(2\) seconds for a position step command.

The full-state feedback has the structure depicted in **Figure 2**

The state feedback can place the poles by altering the eigenvalues of the closed loop system matrix:

$$u=-Kx$$

$$A_c=A-BK$$

Consequently, one can select the gains feedback so that it places the poles with required time-domain characteristics. Another way is to select the gains by optimal solution of an unconstrained quadratic cost function problem. The cost function is defined as:

$$\mathcal{J}=\int\left(x^TQx+u^TRu\right)dt$$

The \(Q\) and \(R\) matrices are the relative weights for states and inputs to be optimized. The solution of this problem is conducted by the LQR algorithm. We will first try unity weights for the position, angle, and inputs:

We now try to close the feedback with the obtained gains and investigate a step response:

From the response above, we can see that the pendulum tracks the step position command. However, the settling time and overshoot requirements are not met.

Next, we try some other gains combination. The new combination gives more weight on position and angle relative to control action signal:

Now, we see that the overshoot and settling time are met. But there is a steady-state error at the position. We can eliminate this steady-state error by adding are scaling pre compensator \(N = 31.5\)

The final controller now meets the design requirements for our inverted pendulum system.