matlab report
Inverted Pendulum: Simulink Modeling
Contents
· Physical setup and system equations
· Building the nonlinear model with Simulink
· Generating the open-loop response
· Extracting a linear model from the simulation
In this page we outline how to build a model of our inverted pendulum system for the purposes of simulation using Simulink and its add-ons. A great advantage of simulation, as will be demonstrated in this example, is that it can generate numerical solutions to nonlinear equations for which closed-form solutions cannot be generated. The nonlinear simulation can then be employed to test the validity of a linearized version of the model. The simulation model can also be used to evaluate the performance of the control scheme designed based on the linearized model.
Physical setup and system equations
In this example we will consider a two-dimensional version of the inverted pendulum system with cart where the pendulum is constrained to move in the vertical plane shown in the figure below. For this system, the control input is the force that moves the cart horizontally and the outputs are the angular position of the pendulum
and the horizontal position of the cart
.
For this example, let's assume the following quantities:
(M) mass of the cart 0.5 kg
(m) mass of the pendulum 0.2 kg
(b) coefficient of friction for cart 0.1 N/m/sec
(l) length to pendulum center of mass 0.3 m
(I) mass moment of inertia of the pendulum 0.006 kg.m^2
(F) force applied to the cart
(x) cart position coordinate
(theta) pendulum angle from vertical (down)
Below are the two free-body diagrams of the system.
This system is challenging to model in Simulink because of the physical constraint (the pin joint) between the cart and pendulum which reduces the degrees of freedom in the system. Both the cart and the pendulum have one degree of freedom ( and
, respectively). We will generate the differential equations for these degrees of freedom from first principles employing Newton's second law (
) as shown below.
(1)
(2)
It is necessary, however, to include the interaction forces and
between the cart and the pendulum in order to fully model the system's dynamics. The inclusion of these forces requires modeling the
- and
-components of the the translation of the pendulum's center of mass in addition to its rotational dynamics. In the Inverted Pendulum: System Modeling tutorial, the interaction forces
and
were solved for algebraically.
In general, we would like to exploit the modeling power of Simulink to take care of the algebra for us. Therefore, we will model the additional - and
-component equations for the pendulum as shown below.
(3)
(4)
(5)
(6)
However, the position coordinates and
are exact functions of
. Therefore, we can represent their derivatives in terms of the derivatives of
. First addressing the
-component equations we arrive at the following.
(7)
(8)
(9)
Then addressing the -component equations gives us the following.
(10)
(11)
(12)
These expressions can then be substituted into the expressions for and
from above as follows.
(13)
(14)
We can now represent these equations within Simulink. Simulink can work directly with nonlinear equations, so it is unnecessary to linearize these equations as was done in the Inverted Pendulum: System Modeling page.
Building the nonlinear model with Simulink
We can build the inverted pendulum model in Simulink employing the equations derived above by following the steps given below.
· Begin by typing simulink into the MATLAB command window to open the Simulink environment. Then open a new model window in Simulink by choosing New > Model from the File menu at the top of the open Simulink Library Browser window or by pressing Ctrl-N.
· Insert four Fcn Blocks from the Simulink/User-Defined Functions library. We will build the equations for ,
,
, and
employing these blocks.
· Change the label of each Fcn block to match its associated function.
· Insert four Integrator blocks from the Simulink/Continuous library. The output of each Integrator block is going to be a state variable of the system: ,
,
, and
.
· Double-click on each Integrator block to add the State Name: of the associated state variable. See the following figure for an example. Also change the Initial condition: for (pendulum angle) to "pi" to represent that the pendulum begins pointing straight up.
· Insert four Multiplexer (Mux) blocks from the Simulink/Signal Routing library, one for each Fcn block.
· Insert two Out1 blocks and one In1 block from the Simulink/Sinks and Simulink/Sources libraries, respectively. Then double-click on the labels for the blocks to change their names. The two outputs are for the "Position" of the cart and the "Angle" of the pendulum, while the one input is for the "Force" applied to the cart.
· Connect each output of the Mux blocks to the input of the corresponding Fcn block.
· Connect the output of the and
Fcn blocks to two consecutive integrators to generate the cart's position and the pendulum's angle. Your current model should now appear as follows.
Now we will enter each of the four equations (1), (2), (13), and (14) into a Fcn block. Let's start with equation (1) which is repeated below.
(15)
· This equation requires three inputs: ,
, and
. Double-click on the corresponding Mux block and change the Number of inputs: to "3".
· Connect these three inputs to this Mux block in the order prescribed in the previous step.
· Double-click on the first Fcn block and enter the equation for xddot as shown below.
Now, let's enter equation (2) which is repeated below.
(16)
· This equation also requires three inputs: ,
, and
.
· Enter the above equation into the Fcn block, change the number of inputs of the Mux block, and connect the correct signals to the Mux block in the correct order.
· Repeat this process for equations (13) and (14) repeated below.
(17)
(18)
When all of these steps are completed, the resulting model should appear as follows.
Save as screen capture for lab report.
In order to save all of these components as a single subsystem block, first select all of the blocks, then select Create Subsystem from the Edit menu. Your model should appear as follows:
Generating the open-loop response
We will now simulate the response of the inverted pendulum system to an impulsive force applied to the cart. This simulation requires an impulse input. Since there is no such block in the Simulink library, we will use the Pulse Generator block to approximate a unit impulse input. Follow the steps given below.
· Open the inverted pendulum model generated above.
· Add a Pulse Generator block from the Simulink/Sources library. Double-click on the block and change the parameters as shown below. In particular, change the Period: to "10". Since we will run our simulation for 10 seconds, this will ensure that only a single "pulse" is generated. Also change the Amplitude to "1000" and the Pulse Width (% of period): to "0.01". Together, these settings generate a pulse that approximates a unit impulse in that the magnitude of the input is very large for a very short period of time and the area of the pulse equals 1.
· Add a Scope block from the Simulink/Sinks library.
· In order display two inputs on the scope, double-click on the Scope block, choose the Parameters icon, and change the Number of axes: to "2".
Connect the blocks and label the signals connected to the Scope block as shown.
Save this system as Pend_Openloop.mdl.
Before we start the simulation, we would like to enable the visualization of the inverted pendulum system. From the menus at the top of the model window choose Simulation > Configuration Parameters.
Save as screen capture for lab report.
Notice that the pendulum repeatedly swings through full revolutions where the angle rolls over radians. Furthermore, the cart's position grows unbounded, but oscillates under the influence of the swinging pendulum. These results differ quite a bit from the results of the open-loop simulation shown in the Inverted Pendulum: System Analysis page. This is due of course to the fact that this simulation employed a fully nonlinear model, while the previous analysis had relied on a linear approximation of the inverted pendulum model. In order to compare the results of the simulation model more directly to the prior results, we will extract a linear model from our simulation model.
Extracting a linear model from the simulation
Aside from comparing our simulation model to our prior results, it may also be desirable to extract a linear model for the purposes of analysis and design. Much of the analytical techniques that are commonly applied to the analysis of dynamic systems and the design of their associated control can only be applied to linear models. Therefore, it may be desirable to extract an approximate linear model from the nonlinear simulation model. We will accomplish this from within Simulink.
· To begin, open the Simulink model generated above, Pend_Model.mdl.
· If you generated your simulation model using variables, it is necessary to define the physical constants in the MATLAB workspace before performing the linearization. This can be accomplished by entering the following commands in the MATLAB command window.
M = 0.5;
m = 0.2;
b = 0.1;
I = 0.006;
g = 9.8;
l = 0.3;
· Next choose from the menus at the top of the model window Tools > Control Design > Linear Analysis. This will cause the Linear Analysis Tool window to open.
· In order to perform our linearization, we need to first identify the inputs and outputs for the model and the operating point that we wish to perform the linearization about. First right-click on the signal representing the Force input in the Simulink model. Then choose Linearization Points > Input Point from the resulting menu. Similarly, right-click on each of the two output signals of the model (pendulum angle and cart position) and select Linearization Points > Output Point from the resulting menu in each case. The resulting inputs and outputs should now be identified on your model by arrow symbols as shown in the figure below.
· Next we need to identify the operating point to be linearized about. From the Operating Point: menu choose Linearize At... > Trim model... as shown in the figure below. This will open the TRIM MODEL tab. Within this tab, select the Trim button indicated by the green triangle. This will create the operating point op_trim1.
· Since we wish to examine the impulse response of this system, return to the EXACT LINEARIZATION tab and choose New Impulse from the Plot Result: drop-down menu near the top window as shown in the figure below.
· Finally, choose op_trim1 from the Operating Point: drop-down menu and click the Linearize button indicated by the green triangle. This automatically generates an impulse response plot and the linearized model linsys1.
· In order to compare the results to those plots generated in the Inverted Pendulum: System Analysis page, it is necessary to change the -axis scaling. This can be achieved from by choosing Properties from the right-click menu. The resulting window should then appear as follows, where the top plot is response of the pendulum angle and the bottom plot is the response of the cart position.
Save as screen capture for lab report.
We can also export the resulting linearized model into the MATLAB workspace for further analysis and design. This can be accomplished by simply right-clicking on the linsys1 object in the Linear Analysis Workspace to copy the object. Then right-click within the MATLAB Workspace to paste the object.
Inverted Pendulum: Simulink Controller Design
Problem setup and design requirements
In this problem, the cart with an inverted pendulum, shown below, is "bumped" with an impulse force, .
For this example, let's assume that
(M) mass of the cart 0.5 kg
(m) mass of the pendulum 0.2 kg
(b) friction of the cart 0.1 N/m/sec
(l) length to pendulum center of mass 0.3 m
(I) inertia of the pendulum 0.006 kg*m^2
(F) force applied to the cart N
(x) cart position coordinate m
(theta) pendulum angle from vertical radians
In the design process we will develop a PID controller and apply it to a single-input, single-output plant. More specifically, the controller will attempt to maintain the pendulum vertically upward when the cart is subjected to a 1-Nsec impulse. The cart's position will be ignored. Under these conditions, the design criteria are:
· Settling time of less than 5 seconds
· Pendulum should not move more than 0.05 radians away from the vertical
Implementing PID control for the nonlinear model
In the Inverted Pendulum: PID Controller Design page a PID controller was designed with proportional, integral, and derivative gains equal to 100, 1, and 20, respectively. To implement this closed-loop system, we will start with one of our plant models from the Inverted Pendulum: Simulink Modeling page. Following the steps below, we will build a closed-loop model with reference input of pendulum position and a disturbance force applied to the cart.
· To begin, open either of the Simulink models generated previously, Pend_Model.mdl.
· Insert two Add blocks from Simulink/Math Operations library.
· Change the List of signs: of one of the Add blocks to "+-".
· Insert a Constant block from Simulink/Sources library. Change its value to 0. This is the reference input that corresponds to the pendulum vertically upward. Note, the model (and the rest of the pages in this example) define the pendulum angle to equal pi when pointing straight up.
· Insert a PID Controller block from the Simulink/Continuous library.
· Edit the PID block by doubleclicking on it. Change the Proportional (P): gain to "100", leave the Integral (I): gain as "1", and change the Derivative (D): gain to "20".
· Now connect the blocks as they appear in the following figure:
Save as screen capture for lab report.
Nonlinear closed-loop response
We can now simulate the closed-loop system. Be sure that the physical parameters are set as follows.
M = 0.5;
m = 0.2;
b = 0.1;
I = 0.006;
g = 9.8;
l = 0.3;
Now, start the simulation (select Start from the Simulation menu or enter Ctrl-T). As the simulation runs, an animation of the inverted pendulum will visualize the system's resulting motion. Recall that the Show animation during simulation option must be checked under the Simulation > Configuration Parameters menu. After the simulation has run, double-click on the scope and hit the Autoscale button. You should see the following response.
This response is almost identical to the closed-loop response obtained in the MATLAB tutorials (for example, in the Inverted Pendulum: PID Controller Design page). Note that the PID controller handles the nonlinear system very well because the deviation of the angle from the operating point is very small (approximately .05 radians).
Save as screen capture for lab report.
1