Suppose we have a mass attached to a cart depicted in following diagram:

cart mass system

We wish to control the system such that the $m2$ mass will remain in the upright and vertical position. To keep the $m2$ mass in the upright vertical position i.e. keeping $\theta = 0$, we will be applying some force $f(t)$ on the $m1$ mass in the horizontal direction. To determine the magnitude and direction of the force needed to keep the $m2$ mass balanced, we will be using a full state feedback controller.

In order create a state feedback controller to balance our $m2$ mass, we must first come up with a mathematical model of our system, a.k.a the equations of motion (EOM). For this exercise, we opted to use Lagrangian mechanics to derive our EOM.

Note: We could have derived the EOMs using Newton's 2nd law, but found the math to be much more simple when using the Lagrangian.

In order to use the Lagrangian, we must fully describe the kinetic and potential energies of the system. This is more easily achieved if we add a few position vectors to our system diagram: cart mass system with vectors

With the vectors $\vec{p_1}$ and $\vec{p_2}$ we can now describe the kinetic and potential energies as follows:

$$ KE = \frac{1}{2} m_1 \lvert \dot {\vec p_1} \rvert ^2 + \frac{1}{2} m_2 \lvert \dot {\vec p_2} \rvert ^2 \\ \tag{1} \label{keNotUseful} $$$$ PE = m_2 g l cos(\theta) \\ \tag{2} \label{pe} $$

While the equation listed at $\eqref{keNotUseful}$ is very true, it is not very helpful. When we go to implement, we will not be able to directly measure or approximate $\vec p_2$ or $\dot{\vec p_2}$. We can however solve for $\vec p_2$ and $\dot{\vec p_2}$ in terms of other variables we can measure and approximate such as $\vec p_1$ and $\dot {\vec p_1}$.

Note: In the controls world, we typically call the variables that we can measure and/or calculate in real-time 'states'.

Consider our system but while only looking at the following vectors: system with only vectors

We can say that $$ \vec{p_2} = \vec{p_1} + \Delta \vec{p} \\ \tag{3} $$

Using the vector diagram, we can substitute useful relationships for $\vec{p_1}$ and $\Delta \vec{p}$ to represent $\vec{p_2}$ in terms of states that we can measure or approximate.

We can now use these relationships for $\vec{p_1}$ and $\vec{p_2}$ to determine $\lvert \dot{\vec{p_1}} \rvert$ and $\lvert \dot{\vec{p_2}} \rvert$ in terms of known constants and measurable values.

Now that we have $\vec{p_1}, \lvert \dot{\vec{p_1}} \rvert, \vec{p_2}, \lvert \dot{\vec{p_2}} \rvert$ all in terms of useful states, we can express our Lagrangian entirely in terms of useful states.

Now that we have our Lagrangian, we can get our EOM by evaluating the following:

$$ \frac{d}{dt}\left\lgroup \frac{\partial L}{\partial \dot x}\right\rgroup-\frac{\partial L}{\partial x}=f(t) \\ \tag{4} \label{eom1} $$$$ \frac{d}{dt}\left\lgroup \frac{\partial L}{\partial \dot \theta}\right\rgroup-\frac{\partial L}{\partial \theta}=0 \\ \tag{5} \label{eom2} $$

Note: If you need a refresher of Lagrangian mechanics, we found the following videos to be useful:

$$ = \frac{d}{dt}\left\lgroup \frac{\partial L}{\partial \dot x}\right\rgroup-\frac{\partial L}{\partial x}=f(t) $$
$$ = \frac{d}{dt}\left\lgroup \frac{\partial L}{\partial \dot \theta}\right\rgroup-\frac{\partial L}{\partial \theta}=0 $$

Now that we have our EOM, we want to represent our system in a way that makes it easy to control. Since we have decided to use a full-state feedback controller, we want to represent our system in the following form:

$$ \dot{\vec{x}}= A \vec x + Bu \\ \tag{6} \label{stateEq} $$$$ y = C \vec x + Du \\ \tag{7} \label{outputEq} $$

Note: If the state equation $\eqref{stateEq}$ or the output equation $\eqref{outputEq}$ seem unfamiliar, then we highly suggest watching the following video:

In order to simplify our lives and be able to express A in terms of only constants, we decide to apply the following simplifications:

Note: The following approximations are justified for $-5 ^{\circ} \leq \theta \leq 5 ^{\circ} $: $$ cos\theta \approxeq1 \\ sin\theta\approxeq\theta \\ $$

Note: The following approximation is valid since $\dot\theta$ will be relatively small: $$ \dot\theta^2\approxeq 0 $$

Now that we have linearized our EOM, let us explicitly say what our states are: $$ \vec x = \left [ \begin{matrix} p_{1x} \\ \dot p_{1x} \\ \theta \\ \dot\theta \\ \end{matrix} \right ] \implies \dot{\vec x} = \left [ \begin{matrix} \dot{p_{1x}} \\ \ddot{p_{1x}} \\ \dot{\theta} \\ \ddot{\theta}\\ \end{matrix} \right ] $$

We will now solve our equations of motion for $\ddot{\theta}$ and $\ddot{p_{1x}}$. This gets us one step closer to be able to represent our system in the following form: $$ \dot{\vec{x}}= A \vec x + Bu \\ y = C \vec x + Du $$

Now that we have expressions for $\ddot{\theta}$ and $\ddot{p_{1x}}$ in terms of our states, we are ready to represent the system in terms of the state equation and the output equation.

Now is a good time to substitue some actual values for our constants and do some sanity checks. We want to plot the poles of the system and check that at least one is in the right half plane.

As seen in the figure above a pole is in the right half plane, meaning the system is inherently unstable. This makes sense, since the pendulum would not remain in the upright vertical position if we were not actively trying to balance the pendulum.

The objective of our statespace controller should be to move the pole in the right half plane to the left half.

In general, state feeback controllers take a plant as illustrated in the figure below: open loop system

The state is 'fed back' to the input while applying a individual gains K to each state variable. Applying these gains and feedback ultimately is what moves the poles of the overall system. A figure illustrating the state feedback controller being applied to the plant can be found below: closed loop system

One question you may be asking yourself is how do we determine each of the gains for K such that the system's poles get moved to the left hand plane?

Probably the best way to show how we determine the gains for K is through a little derivation. We will prove:

  1. The state equation and the output equation can be represented as a transfer function.
  2. The eigenvalues of the A matrix in the state equation represent the poles of our system.

Let's begin with proving the state equation $\dot{\vec{x}} = A \vec x + Bu$ and the output equation $y = C\vec x + Du$ can be represented as a transfer function.

First let's take the Laplace transfrom of both the state equation and the output equation:

$$ sX(s) = A X(s) + B U(s) \\ Y(s) = C X(s) + D U(s) $$

Now let's isolate for $X(s)$ in the state equation:

$$ s X(s) - A X(s) = B U(s) \\ X(s)(sI - A) = B U(s) \\ X(s) = (sI - A)^{-1}B U(s) $$

In many cases (including our inverted pendulum) we choose our $D = \mathbf{0}$. Let's express this simplification in our output equation:

$$ Y(s) = CX(s) $$

Let's substitute $X(s)$ for the expression we derived in the state equation:

$$ Y(s) = C(sI - A)^{-1}B U(s) $$

Since $U(s)$ is a 1x1, we can solve for the transfer function by dividing $U(s)$ to both sides of the equation:

$$ \frac{Y(s)}{U(s)} = G(s) \\ = C(sI - A)^{-1}B \\ $$$$ = C \frac{\mathrm{adj}(sI - A)}{\det(sI - A)} B \label{transferFunction} \tag{8} $$

As seen in equation $\eqref{transferFunction}$ the poles for the system can be determined by setting the $\det(sI -A) = 0$. As it turns out, solving for the values of $s$ that cause the denominator to be zero is the same as if we had determined the eigenvalues for A.

An aside: The relationship between the poles of our system and the eigenvalues of the A matrix become apparent if we look at the definition of an eigenvector and try to solve for the eigenvalues: $$ A\vec{v} = \lambda \vec{v} \\ \lambda \vec{v} - A\vec{v} = \mathbf{0} \\ =(\lambda I - A)\vec{v} = \mathbf{0} $$

We would then solve for the eigenvalues by finding the $\det(\lambda I - A) = 0$. Notice that the $\det(\lambda I -A) = 0$ would be identical to the denominator of equation $\eqref{transferFunction}$ if we substitute $\lambda$ for $s$.

If you are uncertain why we took the $\det(\lambda I - A)$ and set it equal to $\mathbf{0}$ then consider watching the following video:

Okay, we have proven that the state space representation our system can be converted into a transfer function and the poles of that transfer function are the eigenvalues of our $A$ matrix. Lets now look at applying feedback and how that affects the $A$ matrix of the closed-loop system. Looking back at our closed-loop model: closed-loop system

Let's now substitute $u=r-K\vec x$ into our state equation:

$$ \dot{\vec x} = A \vec x + B (r - K\vec x) $$

Let's see if we can expand and factor out our $\vec x$.

$$ \dot{\vec x} = A \vec x + Br - BK\vec x \\ = A \vec x - BK\vec x + Br $$$$ = (A - BK)\vec x + Br \label{aClosedLoop} \tag{9} $$

Equation $\eqref{aClosedLoop}$ should look very familiar. If we substitute $(A-BK)$ for $A_{closed loop}$, we will see equation $\eqref{aClosedLoop}$ is in the exact same form as our original state equation:

$$ \dot{\vec{x}} = A_{closed loop} \vec x + Br $$

We can now say that the eigenvalues for the overall system are at the eigenvalues of $A_{closed loop}$. We can solve for our $K$ matrix by setting the characteristic equation of $A_{closed loop}$ to our desired characteristic equation: $$ \det(A_{closed loop}) = \mathrm{desiredCharEq} $$

To find our gain values we must choose some performance criteria for the transient response, namely the maximum settling time and percent overshoot. This produces a 2nd-order characteristic polynomial to which we add two more terms to make the overall 'desired' polynomial a 4th-order. The added terms simply place then two remaining roots (poles) at 5x the distance of the other two on the real axis. We can then set this equal to our actual characteristic equatioin and solve for our gains by matching like-terms: $$ \omega_n=\frac{4}{\zeta T_s} \\ \zeta=\frac{-\ln(\frac{\%OS}{100})}{\sqrt{\pi^2+\ln^2(\frac{\%OS}{100})}} \\ \mathrm{desiredCharEq} = (s^2+2\zeta\omega_ns+\omega_n^2)(s+5\zeta\omega_n)^2 = \det(A_{closed loop}) $$

Now that we have solved for the gains that move the poles to the desired locations. We should plot where the poles are and confirm they are at their desired locations.

Okay, we have reason to believe the system is now stable. Let's see what happens to $\theta$ when we give it an intial displacement of $5^\circ$.

As seen in the plot above, $\theta$ will approch $0$ as $t \to \infty$. This gives us reason to believe our control theory is correct and we are ready to begin implementation.