Inverted Pendulum : Simulink Approach

\bf {Simulink \ Approach}

There are two ways to model the system in Simulink. Using the standard Simulink library and the Simscape library. They differ in several respects as you will see in the following section.

The challenge in modelling an inverted pendulum in Simulink, is the physical constraint created by the pin joint between the cart and pendulum, which effectively reduces the degrees of freedom of the system to 1DOF each, the position of the cart x and \theta, respectively.

Once again taking an Inverted Pendulum system as illustrated in the Figure below.

Figure 1: Inverted Pendulum

We can use Newton’s Second law to generate the differential equations for those degrees of freedom such that:

M\ddot x = F-N-b\dot x

Where b is the friction coefficient and N the reaction force.

The sum of the moments acting on the Pendulum, in terms of the vertical and horizontal reaction forces P and N, is given:

I\ddot \theta = -NLcos\theta - PLsin\theta

These two equations can be rearranged to give expressions for the acceleration of the cart, resulting from this external impulsive force F, and resultant angular acceleration of the Pendulum.

\ddot x =\frac {1}{M} (F-N-b\dot x)

\ddot \theta =-\frac {1}{I} (NLcos\theta + PLsin\theta)

In order to model the dynamics of the system in Simulink, we need to consider the interactions of the forces between the cart and pendulum P and N by defining the two reaction forces.

We previously need to mathematically derive the equation of the system and resolve the resulting algebra in order to model the system in Matlab. However, when using Simulink, we can rely on Simulink to resolve the algebra for us, asking this approach less mathematically taxing.

m\ddot x_p=\Sigma F_x=N

m\ddot y_p=\Sigma F_y=P-mg

As the coordinates of x_p and y_p are exact functions of \theta we can represent their derivatives in terms of \theta.

For the x-component:

x_p=x + Lsin\theta

\dot x_p= \dot x + L\dot\theta cos\theta

\ddot x_p= \ddot x - L\dot \theta^2 sin \theta + L\ddot \theta cos \theta

The y-component:

y_p=Lcos\theta

\dot y_p=L\dot \theta sin\theta

\ddot y_p=L\ddot \theta sin\theta + L\dot \theta^2 cos\theta

The equations for the derivatives of the x and y components of the pendulum position can the be substituted into the equations of the reaction forces P and N to give the following:

N=m\ddot x_p=m(\ddot x - L\dot \theta^2 sin \theta + L\ddot \theta cos \theta)

P=m\ddot y_p=m(L\ddot \theta sin\theta + L\dot \theta^2 cos\theta +g)

We can use the reaction forces and accelerations above to define the dynamics of the system.

As you can see this approach, using Simulink, can be far less algebraically taxing than computing a transfer function or state-space form for the system and can be very convenient when testing a model.

We the need to build our Simulink model blocks. Starting with the derived differential equations for for acceleration of the cart and angular acceleration of the pendulum and the two reaction forces, we need to insert four function blocks one for each: \ddot x, \ddot \theta, P and N.

When defining the equation of each of the functions, we need to replace the variables (no-constants) as inputs e.g. u(1), u(2) etc.

For example \ddot x can be written in the form: (1/M)* (u(1)-u(2)-b*u(3))

where inputs u(1) is the impulsive force F, u(2) is the reaction force N and u(3) is the velocity \dot x, which is the integral of this same block in the previous time step.

Then we can use the integrator block to compute the \dot x and x from \ddot x and \theta and \dot \theta from the \ddot \theta function block.

The we can proceed to pass the \ddot x and \ddot \theta functions through their two integral blocks and connect the the variables to their desired input ports as shown indicatively n the figure below.

It is important to be careful when connecting the blocks, especially where the function blocks have multiple inputs, as the order in which the inputs are connected can fundamentally change the expression of the function. The number of the input port corresponds to the input number specified in the function’s expression i.e. u(1) represents the first input port.

We can the add an input block F ad two output blocks, x and Theta.

Figure below shows an indicative layout of what this might look like.

Note: The layout of the links will vary depending on how you have written the function expressions.

Figure 2: Simulink Sub-system

Once these blocks are in place, and inputs correctly linked to their respective sources, we can turn this into a subsystem, by simply highlighting all the blocks, right clicking and selecting ‘create subsystem from selection’ from the drop-down menu.

We can then test the response of the subsystem to an impulsive force on the cart. One way to achieve this is by connecting an pulse generator block to the subsystem input to simulate the impulsive force and a scope block to the output ports to measure the cart position and pendulum rotation, as shown in the figure below.

Figure 3: Simulink System

\bf {Basic \ Simulink \ Model}

When using the Simulink library we will still need to input the differential equations for the acceleration and reaction forces derived above. However, the Simscape method is a more literal translation of the system, making use of joints and virtual actuators. The Simscape approach has the advantage of giving us a visualization tool which can provide a visual representation of the system response, once we have run the simulation.

\bf {Simscape \ Model}

In this section we will cover how we can use the Simscape library extension of Simulink to model the nonlinear inverted Pendulum system. This involves the least amount of algebra of all the approaches as blocks in the Simscape library represent actual physical components. As such, we can build complex multi-body dynamic models without the need to build mathematical equations from basic principles as per the original Simulink case. In addition, this method benefits from the fact that using the blocks from the Simscape library allows us to produce a visual representation of the response of the system once we run the system.

Building the system using the Simscape library will require:

  • Two body blocks
  • 3 joint actuators;
  • Two Prismatic joint block;
  • One Revolute block;
  • Two Ground Blocks;
  • Two joint Sensors;
  • A Gain block;
  • One Machine environment block;
  • Two outputs and an input;
  • One Joint Initial Condition block.

Figure 4: Simscape Model

Within the Body block parameters we must also define the location of the centre of mass and the shape and dimension of the bodies by specifying the coordinates of each corner of the body, with respect to the centre of mass specified. For a 2D case, we only need to define 4 corners of the cart, the coordinate of the centre of mass, and specify what the coordinate frame used, either world or CG (centre of gravity) to define the position of these edges.

Figure 5: Cart Body Block Parameters

In addition to defining the body parameters, two further inputs are required to define the position at which the impulsive force and frictional force act on the cart. An example of the body block parameters for the cart and pendulum are shown below.

Figure 6: Pendulum Block Parameters

For the ‘Port Side’, we simply assign the centre of mass and the two force inputs, to the ‘Left side’ and the remaining entries to the ‘Right side’.

<p

We can simulate an impulsive force using the pulse generator block. Once the amplitude and period of the pulse is set, the block should be connected to the input of our subsystem.

\bf {Linearising \ In \ Simulink}

As per the Matlab approach, we can linearise the system created in Simulink allowing Simulink to resolve the algebra for us. This steps to linearise the system are the same whether using the simscape library or standard Simulink libraries.

Step 1: Set the input signal ‘Force’ to the subsystem to ‘Open-loop input’ in the linear Analysis Points on the drop down menu; Set the two output signals ‘Theta’ and ‘x’ to ‘Open-loop Output, as per above.

Step 2: Select Linear analysis in the Control Design drop down menu under the Analysis toolbar. This will automatically prompt open the linear analysis window, as shown below.

To start with, select the Trim model function under the ‘Operating Point’ menu to create a new operating point. As per default, we should find that only the ‘steady state’ state specifications are ticked, and the maximum and minimum values for each of the states are set to infinity and negative infinity. We can leave this a it is and Start Trimming. This will create the new operating point.

Step 4: Select the created operating point in the Linear analysis workspace, then run the Impulse Plot in the Linear toolbar. This should automatically linearise the response of the system and produce two graphs, one for the angular deviation (theta) of the Pendulum and the other for the displacement of the cart, resulting from the impulsive force on the cart simulated.

Figure 7: Linearising the Response of the system

Leave a comment