Continuous and Discrete Inverted Pendulum

This example shows a continuous and discrete inverted pendulum using Python and

QFIRE Studio

.

Introduction

The inverted pendulum is a classical model in control and automation engineering. This plant describes the behavior of a helicopter gliding.A classical inverted pendulum is represented by Figure 1.

Figure 1 - A car with a inverted pendulum on top

As can be seen, there are three letters in Figure 1 meaning variables of the system. Where:

  • M: Car mass.
  • m: Pendulum mass.
  • L: Pendulum length.

These variables are used in the following equations that define the model behavior.

Mlθ¨=(M+m)gθuMx¨=umgθ\begin{align}Ml\ddot{\theta} &= (M+m)g\theta-u\\M\ddot{x} &= u-mg\theta\end{align}

In this case,

θ\theta
means the angle between a perpendicular reference and the pendulum and
xx
means the position of the car.

Simulation

The Equation 1 and 2 can be converted to state space form.

[θ˙θ¨x˙x¨]=[0100M+mMlg0000001mMg000][θθ˙x x˙]+[01Ml01M]u\begin{bmatrix}\dot{\theta}\\ \ddot{\theta}\\ \dot{x}\\ \ddot{x} \end{bmatrix} = \begin{bmatrix}0 & 1 & 0 & 0\\ \frac{M+m}{Ml}g & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ -\frac{m}{M}g & 0 & 0 & 0\end{bmatrix} \begin{bmatrix}\theta\\\dot{\theta}\\x\\\ \dot{x}\end{bmatrix}+\begin{bmatrix}0\\ -\frac{1}{Ml}\\ 0\\ \frac{1}{M}\end{bmatrix}u

It is possible to use Python to execute the calculation for this model after defines the parameters. In this example, it will be used

M=5KgM = 5Kg
,
m=2Kgm = 2Kg
and
L=1mL = 1m
.

Running this code, the following system is returned:

[θ˙θ¨x˙x¨]=[010068.6700000013.924000][θθ˙x x˙]+[0100.2]u\begin{bmatrix}\dot{\theta}\\ \ddot{\theta}\\ \dot{x}\\ \ddot{x} \end{bmatrix} = \begin{bmatrix}0 & 1 & 0 & 0\\ 68.67 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ -3.924 & 0 & 0 & 0\end{bmatrix} \begin{bmatrix}\theta\\\dot{\theta}\\x\\\ \dot{x}\end{bmatrix}+\begin{bmatrix}0\\ -1\\ 0\\ 0.2\end{bmatrix}u

This system can be represented in block diagram as Figure 2 and 3 show. In the figures, the states will be represented by

xnx_n
.

Figure 2 - Matrix A

Figure 3 - Matrix B

This diagram was design for simulation in the Real World Workspace. Aiming to simulate it using a

QFIRE CTR-101

, the system was discretized using the following code:

As result of the code, the following discrete state space system was calculated.

[θk+1θ˙k+1xk+1x˙k+1]=[1.363450.11184007.680361.36345000.020770.0006810.10.438880.0207701][θkθ˙kxkx˙k]+[0.005290.111840.0010170.020677]u\begin{bmatrix}\theta_{k+1}\\ \dot{\theta}_{k+1}\\ x_{k+1}\\ \dot{x}_{k+1}\end{bmatrix} = \begin{bmatrix}1.36345 & 0.11184 & 0 & 0\\ 7.68036 & 1.36345 & 0 & 0\\ -0.02077& -0.00068 & 1 & 0.1\\ -0.43888& -0.02077 & 0 & 1\end{bmatrix}\begin{bmatrix}\theta_k\\ \dot{\theta}_k\\ x_k\\ \dot{x}_k\end{bmatrix}+\begin{bmatrix}-0.00529\\ -0.11184\\ 0.001017\\ 0.020677\end{bmatrix}u

This system represented in block diagram is shown in Figure 4 and 5.

Figure 4 - Matrix Phi

Figure 5 - Matrix Gamma

About MWF

MWF is a traditional Brazilian company that provides a wide range of electronic and mechatronic products for industry sectors such as automotive, agricultural machinery and aerospace.

Contact Us

Rua Doutor Siqueira, 139 / Sala 804 Campos dos Goytacazes - RJ, Brasil

contact@mwf-technologies.com

© 2018-2026 MWF. All rights reserved.