0% found this document useful (0 votes)
228 views

Inverted Pendulam

1. The document describes an inverted pendulum system mounted on a cart that can move horizontally. It provides the equations of motion for the linearized system and represents it as a transfer function and state-space model in MATLAB. 2. The open-loop response of the system is entirely unsatisfactory as it is unstable. Control methods like PID, root locus or state feedback need to be designed to stabilize the system and meet requirements. 3. Design requirements for PID/root locus control include settling time less than 5 seconds and pendulum angle within 0.05 radians of vertical. For state feedback, additional requirements constrain cart position and pendulum overshoot.

Uploaded by

Vi Jay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views

Inverted Pendulam

1. The document describes an inverted pendulum system mounted on a cart that can move horizontally. It provides the equations of motion for the linearized system and represents it as a transfer function and state-space model in MATLAB. 2. The open-loop response of the system is entirely unsatisfactory as it is unstable. Control methods like PID, root locus or state feedback need to be designed to stabilize the system and meet requirements. 3. Design requirements for PID/root locus control include settling time less than 5 seconds and pendulum angle within 0.05 radians of vertical. For state feedback, additional requirements constrain cart position and pendulum overshoot.

Uploaded by

Vi Jay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 34

INVERTED PENDULUM FOR SELF BALANCING IN ROBOTS.

Aim: The goal of this project was to build and implement an inverted pendulum balancer, in the vertical two dimensional plane, using Proportional-Integral-Derivative (PID) feedback control. Abstract: An inverted pendulum is a pendulum which has its mass above its pivot point. It is often implemented with the pivot point mounted on a cart that can move horizontally and may be called a cart and pole. Whereas a normal pendulum is stable when hanging downwards, an inverted pendulum is inherently unstable, and must be actively balanced in order to remain upright, either by applying a torque at the pivot point or by moving the pivot point horizontally as part of a feedback system. We return to the history of the seismographs themselves. In 1898, E. Wiechert of Gttingen introduced a seismograph with a viscously-damped pendulum as a sensor (Wiechert, 1899). The damping was added to lessen the effects of the pendulum eigen-oscillations. Wiechert's first seismograph was a horizontalpendulum instrument, which recorded photographically. After a trip to Italy to study seismometers used in that country, he decided to build a mechanically-recording seismograph. For a sensor, he used an inverted pendulum stabilized by springs and free to oscillate in any direction horizontally (Wiechert, 1904). The seismograph was completed in 1900.

The inverted pendulum is a classic problem in dynamics and control theory and is widely used as a benchmark for testing control algorithms (PID controllers, neural networks, fuzzy control, genetic algorithms, etc.). Variations on this problem include multiple links, allowing the motion of the cart to be commanded while maintaining the pendulum, and balancing the cart-pendulum system on a see-saw. The inverted pendulum is related to rocket or missile guidance, where the center of gravity is located behind the center of drag causing aerodynamic instability. The understanding of a similar problem can be shown by simple robotics in the form of a balancing cart. An inertia wheel pendulum or gyroscopic

pendulum is a pendulum with an inertia wheel attached. It can be used as a pedagogical problem in control theory. The Segway PT is a two-wheeled, self-balancing transportation machine invented by Dean Kamen. Computers and motors in the base of the device keep the Segway PT upright when powered on with balancing enabled. A user commands the Segway to go forward by shifting their weight forward on the platform, and backward by shifting their weight backward. The Segway notices, as it balances, the change in its center of mass, and first establishes and then maintains a corresponding speed, forward or backward. Gyroscopic sensors and fluid-based leveling sensors are used to detect the shift of weight. To turn, the user manipulates a control on the handlebar left or right. Segway PTs are driven by electric motors and can go up to 12.5 miles per hour (20.1 km/h). Problem setup and design requirements The cart with an inverted pendulum, shown below, is "bumped" with an impulse force, F. Determine the dynamic equations of motion for the system, and linearize about the pendulum's angle, theta = Pi (in other words, assume that pendulum does not move more than a few degrees away from the vertical, chosen to be at an angle of Pi). Find a controller to satisfy all of the design requirements given below.

For this example, let's assume that M m b l I F x theta mass of the cart 0.5 kg mass of the pendulum 0.2 kg friction of the cart 0.1 N/m/sec length to pendulum center of mass 0.3 m inertia of the pendulum 0.006 kg*m^2 force applied to the cart cart position coordinate pendulum angle from vertical

For the PID, root locus, and frequency response sections of this problem we will be only interested in the control of the pendulum's position. This is because the techniques used in these tutorials can only be applied for a single-input-single-output (SISO) system. Therefore, none of the design criteria deal with the cart's position. For these sections we will assume that the system starts at equilibrium, and experiences an impulse force of 1N. The pendulum should return to its upright position within 5 seconds, and never move more than 0.05 radians away from the vertical. The design requirements for this system are:

Settling time of less than 5 seconds. Pendulum angle never more than 0.05 radians from the vertical.

However, with the state-space method we are more readily able to deal with a multi-output system. Therefore, for this section of the Inverted Pendulum example we will attempt to control both the pendulum's angle and the cart's position. To make the design more challenging we will be applying a step input to the cart. The cart should achieve its desired position within 5 seconds and have a rise time under 0.5 seconds. We will also limit the pendulum's overshoot to 20 degrees (0.35 radians), and it should also settle in under 5 seconds. The design requirements for the Inverted Pendulum state-space example are:

Settling time for x and theta of less than 5 seconds. Rise time for x of less than 0.5 seconds. Overshoot of theta less than 20 degrees (0.35 radians).

Force analysis and system equations Below are the two Free Body Diagrams of the system.

Summing the forces in the Free Body Diagram of the cart in the horizontal direction, you get the following equation of motion:

Note that you could also sum the forces in the vertical direction, but no useful information would be gained. Summing the forces in the Free Body Diagram of the pendulum in the horizontal direction, you can get an equation for N:

If you substitute this equation into the first equation, you get the first equation of motion for this system: (1) To get the second equation of motion, sum the forces perpendicular to the pendulum. Solving the system along this axis ends up saving you a lot of algebra. You should get the following equation:

To get rid of the P and N terms in the equation above, sum the moments around the centroid of the pendulum to get the following equation:

Combining these last two equations, you get the second dynamic equation: (2) Since MATLAB can only work with linear functions, this set of equations should be linearized about theta = Pi. Assume that theta = Pi + ( represents a small angle from the vertical upward direction). Therefore, cos(theta) = -1, sin(theta) = - , and (d(theta)/dt)^2 = 0. After linearization the two equations of motion become (where u represents the input):

1. Transfer Function To obtain the transfer function of the linearized system equations analytically, we must first take the Laplace transform of the system equations. The Laplace transforms are:

Since we will be looking at the angle Phi as the output of interest, solve the first equation for X(s),

then substitute into the second equation, and re-arrange. The transfer function is:

where,

From the transfer function above it can be seen that there is both a pole and a zero at the origin. These can be canceled and the transfer function becomes:

2. State-Space After a little algebra, the linearized system equations equations can also be represented in state-space form:

The C matrix is 2 by 4, because both the cart's position and the pendulum's position are part of the output. For the state-space design problem we will be controlling a multi-output system so we will be observing the cart's position from the first row of output and the pendulum's with the second row. MATLAB representation and the open-loop response 1. Transfer Function The transfer function found from the Laplace transforms can be set up using MATLAB by inputting the numerator and denominator as vectors. Create an m-file and copy the following text to model the transfer function: M = .5; m = 0.2; b = 0.1; i = 0.006; g = 9.8; l = 0.3; q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input num = [m*l/q 0]; den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]; pend=tf(num,den) Your output should be: Transfer function: 4.545 s ---------------------------------s^3 + 0.1818 s^2 - 31.18 s - 4.455 To observe the system's velocity response to an impulse force applied to the cart add the following lines at the end of your m-file: t=0:0.01:5; impulse(pend,t) axis([0 1 0 60]) You should get the following velocity response plot:

As you can see from the plot, the response is entirely unsatisfactory. It is not stable in open loop. You can change the axis to see more of the response if you need to convince yourself that the system is unstable. Although the output amplitude increases past 60 radians (10 revolutions), the model is only valid for small . In actuality, the pendulum will stop rotating when it hits the cart ( =90 degree).

2. State-Space Below, we show how the problem would be set up using MATLAB for the state-space model. If you copy the following text into a m-file (or into a '.m' file located in the same directory as MATLAB) and run it, MATLAB will give you the A, B, C, and D matrices for the state-space model and a plot of the response of the cart's position and pendulum angle to a step input of 0.2 m applied to the cart. M = .5; m = 0.2; b = 0.1; i = 0.006; g = 9.8; l = 0.3; p = i*(M+m)+M*m*l^2; %denominator for the A and B matrices A = [0 1 0 0; 0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0; 0 0 0 1; 0 -(m*l*b)/p m*g*l*(M+m)/p 0] B = [ 0; (i+m*l^2)/p; 0; m*l/p] C = [1 0 0 0; 0 0 1 0] D = [0; 0]

pend=ss(A,B,C,D); T=0:0.05:10; U=0.2*ones(size(T)); [Y,T,X]=lsim(pend,U,T); plot(T,Y) axis([0 2 0 100]) You should see the following output after running the m-file:

The blue line represents the cart's position and the green line represents the pendulum's angle. It is obvious from this plot and the one above that some sort of control will have to be designed to improve the dynamics of the system. Several example controllers are included with these tutorials; select from below the one you would like to use. Note: The solutions shown in the PID, root locus and frequency response examples may not yield a workable controller for the inverted pendulum problem. As stated previously, when we put this problem into the single-input, single-output framework, we ignored the x position of the cart. The pendulum can be stabilized in an inverted position if the x position is constant or if the cart moves at a constant velocity (no acceleration). Where possible in these examples, we will show what happens to the cart's position when our controller is implemented on the system. We emphasize that the purpose of these examples is to demonstrate design and analysis techniques using MATLAB; not to actually control an inverted pendulum. Example: Solution to the Inverted Pendulum Problem Using PID Control Open-loop Representation Closed-loop transfer function Adding the PID controller What happens to the cart's position? The transfer function of the plant for this problem is given below:

where,

The design criteria (with the pendulum receiving a 1N impulse force from the cart) are:

Settling time of less than 5 seconds. Pendulum should not move more than 0.05 radians away from the vertical.

To see how this problem was originally set up, consult the inverted pendulum modeling page. Open-loop Representation The first thing to do when using PID control in MATLAB is to find the transfer function of the system and to check to see if it makes sense. The transfer function found from the Laplace transforms for the output Phi (the pendulum's angle) can be set up using MATLAB by entering the numerator and denominator as vectors. Create anm-file (or a '.m' file located in the same directory as MATLAB) and copy the following text to model the transfer function: M = .5; m = 0.2; b = 0.1; i = 0.006; g = 9.8; l = 0.3; q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input num = [m*l/q 0]; den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]; pend=tf(num,den); Closed-loop transfer function The control of this problem is a little different than the standard control problems you may be used to. Since we are trying to control the pendulum's position, which should return to the vertical after the initial disturbance, the reference signal we are tracking should be zero. The force applied to the cart can be added as an impulse disturbance. The schematic for this problem should look like the following.

It will be easier to determine the appropriate transfer function to enter into MATLAB if we first rearrange the schematic as follows:

Adding the PID controller This closed-loop transfer function can be modeled in MATLAB by copying the following code to the end of your m-file: Kd = 1; Kp = 1; Ki = 1; contr=tf([Kd Kp Ki],[1 0]); sys_cl=feedback(pend,contr); This transfer function assumes that both derivative and integral control will be needed along with proportional control. This does not have to be the case. If you wanted to start with a PI or PD controller, just replace the contr line with your choice of controller as shown below. contr=tf([Kp Ki],[1 0]); % PI control contr=tf([Kd Kp],1); % PD control Now we can begin the actual control of this system. First let's see what the impulse response looks like with the numbers we already have. Enter the following code to the end of your m-file: t=0:0.01:5; impulse(sys_cl,t) axis ([0 1.5 0 40]) You should get the following velocity response plot from the impulse disturbance:

This response is still not stable. Let's start by increasing the proportional control to the system. Increase the Kp variable to see what effect it has on the response. If you set Kp=100, and set the axis to axis([0, 2.5, -0.2, 0.2]), you should get the following velocity response plot:

The settling time is acceptable at about 2 seconds. Since the steady-state error has already been reduced to zero, no more integral control is needed. You can remove the integral gain constant to see for yourself that the small integral control is needed. The overshoot is too high, so that must be fixed. To alleviate this

problem, increase the Kd variable. With Kd=20, you should get a satisfactory result. You should now see the following velocity response plot:

As you can see, the overshoot has been reduced so that the pendulum does not move more than 0.05 radians away from the vertical. All of the design criteria have been met, so no further iteration is needed. What happens to the cart's position? At the beginning on this solution page, the block diagram for this problem was given. The diagram was not entirely complete. The block representing the the position was left out because that variable was not being controlled. It is interesting though, to see what is happening to the cart's position when the controller for the pendulum's angle is in place. To see this we need to consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:

The feedback loop represents the controller we have designed for the pendulum's. The transfer function from the cart's position to the impulse force, with the PID feedback controller which we designed, is given as follows:

Now that we have the transfer function for the entire system, let's take a look at the response. First we need the transfer function for the cart's position. To get this we need to go back to the Laplace transforms of the system equations and find the transfer function from X(s) to U(s). Below is this transfer function:

where,

For more about the Laplace transform please refer to the inverted pendulum modeling page. The pole/zero at the origin cancelled out of the transfer function for Phi, has been put back in. So that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the command window: M = .5; m = 0.2; b = 0.1; i = 0.006; g = 9.8; l = 0.3; q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input num1 = [m*l/q 0 0];

den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0]; G1=tf(num1,den1); num2 = [(i+m*l^2)/q 0 -m*g*l/q]; den2 = den1; G2=tf(num2,den2); kd = 20; kp = 100; ki = 1; contr=tf([kd kp ki],[1 0]); xpos=feedback(1,G1*contr)*G2; t=0:0.01:5; impulse(xpos,t)

As you can see, the cart moves in the negative direction with a constant velocity. So although the PID controller stabilizes the angle of the pendulum, this design would not be feasible to implement on an actual physical system. Modeling an Inverted Pendulum in Simulink Problem setup and design requirements Force analysis and system equation setup Building the model Open-loop response Extracting a linearized model Implementing PID control Closed-loop response

Problem setup and design requirements The cart with an inverted pendulum, shown below, is "bumped" with an impulse force, F. Determine the dynamic equations of motion for the system, and linearize about the pendulum's angle, theta = 0 (in other words, assume that pendulum does not move more than a few degrees away from the vertical, chosen to be at an angle of 0). Find a controller to satisfy all of the design requirements given below.

For this example, let's assume that M m b l I F x theta mass of the cart 0.5 kg mass of the pendulum 0.2 kg friction of the cart 0.1 N/m/sec length to pendulum center of mass 0.3 m inertia of the pendulum 0.006 kg*m^2 force applied to the cart cart position coordinate pendulum angle from vertical

In this example, we will implement a PID controller which can only be applied to a single-input-singleoutput (SISO) system, so we will be only interested in the control of the pendulums angle. Therefore, none of the design criteria deal with the cart's position. We will assume that the system starts at equilibrium, and experiences an impulse force of 1N. The pendulum should return to its upright position within 5 seconds, and never move more than 0.05 radians away from the vertical. The design requirements for this system are:

Settling time of less than 5 seconds. Pendulum angle never more than 0.05 radians from the vertical.

Force analysis and system equation setup Below are the two Free Body Diagrams of the system.

This system is tricky 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 (X and theta, respectively). We will then model Newton's equation for these two degrees of freedom.

It is necessary, however, to include the interaction forces N and P between the cart and the pendulum in order to model the dynamics. The inclusion of these forces requires modeling the x and y dynamics of the pendulum in addition to its theta dynamics. In the MATLAB tutorial pendulum modeling example the interaction forces were solved for algebraically. Generally, we would like to exploit the modeling power of Simulink and let the simulation take care of the algebra. Therefore, we will model the additional x and y equations for the pendulum.

However, xp and yp are exact functions of theta. Therefore, we can represent their derivatives in terms of the derivatives of theta.

These expressions can then be substituted into the expressions for N and P. Rather than continuing with algebra here, we will simply represent these equations in Simulink. Simulink can work directly with nonlinear equations, so it is unnecessary to linearize these equations as it was in the MATLAB tutorials. Building the Model in Simulink First, we will model the states of the system in theta and x. We will represent Newton's equations for the pendulum rotational inertia and the cart mass.

Open a new model window in Simulink, and resize it to give plenty of room (this is a large model). Insert two integrators (from the Linear block library) near the bottom of your model and connect them in series. Draw a line from the second integrator and label it "theta". (To insert a label, double-click where you want the label to go.) Label the line connecting the integrators "d/dt(theta)". Draw a line leading to the first integrator and label it "d2/dt2(theta)". Insert a Gain block (from the Linear block library) to the left of the first integrator and connect its output to the d2/dt2(theta) line. Edit the gain value of this block by double clicking it and change it to "1/I". Change the label of this block to "Pendulum Inertia" by clicking on the word "Gain". (you can insert a newline in the label by hitting return). Insert a Sum block (from the Linear block library) to the left of the Pendulum Inertia block and connect its output to the inertia's input. Change the label of this block to Sum Torques on Pend. Construct a similar set of elements near the top of your model with the signals labeled with "x" rather than "theta". The gain block should have the value "1/M" with the label "Cart Mass", and the Sum block should have the label "Sum Forces on Cart". Edit the Sum Forces block and change its signs to "-+-". This represents the signs of the three horizontal forces acting on the cart.

Now, we will add in two of the forces acting on the cart.


Insert a Gain block above the Cart Mass block. Change its value to "b" and its label to "damping". Flip this block left-to-right by single clicking on it (to select it) and selecting Flip Block from the Format menu (or hit Ctrl-F). Tap a line off the d/dt(x) line (hold Ctrl while drawing the line) and connect it to the input of the damping block. Connect the output of the damping block to the topmost input of the Sum Forces block. The damping force then has a negative sign. Insert an In block (from the Connections block library) to the left of the Sum Forces block and change its label to "F". Connect the output of the F in block to the middle (positive) input of the Sum Forces block.

Now, we will apply the forces N and P to both the cart and the pendulum. These forces contribute torques to the pendulum with components "N L cos(theta) and P L sin(theta)". Therefore, we need to construct these components.

Insert two Elementary Math blocks (from the Non-linear block library) and place them one above the other above the second theta integrator. These blocks can be used to generate simple functions such as sin and cos. Edit upper Math block's value to "cos" and leave the lower Math block's value "sin". Label the upper (cos) block "Vertical" and the lower (sin) block "Horizontal" to identify the components. Flip each of these blocks left-to-right. Tap a line off the theta line and connect it to the input of the cos block. Tap a line of the line you just drew and connect it to the input of the sin block. Insert a Gain block to the left of the cos block and change its value to "l" (lowercase L) and its label to "Pend. Len." Flip this block left-to-right and connect it to the output of the cos block. Copy this block to a position to the left of the sin block. To do this, select it (by single-clicking) and select Copy from the Edit Menu and then Paste from the Edit menu (or hit Ctrl-C and CtrlV). Then, drag it to the proper position. Connect the new Pend. Len.1 block to the output of the sin block.

Draw long horizontal lines leading from both these Pend. Len. blocks and label the upper one "l cos(theta)" and the lower one "l sin(theta)".

Now that the pendulum components are available, we can apply the forces N and P. We will assume we can generate these forces, and just draw them coming from nowhere for now.

Insert two Product blocks (from the Non-linear block library) next to each other to the left and above the Sum Torques block. These will be used to multiply the forces N and P by their appropriate components. Rotate the left Product block 90 degrees. To do this, select it and select Rotate Block from the Format menu (or hit Ctrl-R). Flip the other product block left-to-right and also rotate it 90 degrees. Connect the left Product block's output to the lower input of the Sum Torques block. Connect the right Product block's output to the upper input of the Sum Torques block. Continue the l cos(theta) line and attach it to the right input of the left Product block. Continue the l sin(theta) line and attach it to the right input of the right Product block. Begin drawing a line from the open input of the right product block. Extend it up and the to the right. Label the open end of this line "P". Begin drawing a line from the open input of the left product block. Extend it up and the to the right. Label the open end of this line "N".

Tap a line off the N line and connect it to the open input of the Sum forces block.

Next, we will represent the force N and P in terms of the pendulum's horizontal and vertical accelerations from Newton's laws.

Insert a Gain block to the right of the N open ended line and change its value to "m" and its label to "Pend. Mass". Flip this block left-to-right and connect it's to N line. Copy this block to a position to the right of the open ended P line and attach it to the P line. Draw a line leading to the upper Pend. Mass block and label it "d2/dt2(xp)". Insert a Sum block to the right of the lower Pend. Mass block. Flip this block left-to-right and connect its output to the input of the lower Pend. Mass block. Insert a Constant block (from the Sources block library) to the right of the new Sum block, change its value to "g" and label it "Gravity". Connect the Gravity block to the upper (positive) input of the newest Sum block. Draw a line leading to the open input of the new Sum block and label it "d2/dt2(yp)".

Now, we will begin to produce the signals which contribute to d2/dt2(xp) and d2/dt2(yp).

Insert a Sum block to the right of the d2/dt2(yp) open end. Change the Sum block's signs to "--" to represent the two terms contributing to d2/dt2(yp). Flip the Sum block left-to-right and connect it's output to the d2/dt2(yp) signal. Insert a Sum block to the right of the d2/dt2(xp) open end. Change the Sum block's signs to "++-" to represent the three terms contributing to d2/dt2(xp). Flip the Sum block left-to-right and connect it's output to the d2/dt2(xp) signal. The first term of d2/dx2(xp) is d2/dx2(x). Tap a line off the d2/dx2(x) signal and connect it to the topmost (positive) input of the newest Sum block.

Now, we will generate the terms d2/dt2(theta)*lsin(theta) and d2/dt2(theta)*lcos(theta).


Insert two Product blocks next to each other to the right and below the Sum block associated with d2/dt2(yp). Rotate the left Product block 90 degrees. Flip the other product block left-to-right and also rotate it 90 degrees. Tap a line off the l sin(theta) signal and connect it to the left input of the left Product block. Tap a line off the l cos(theta) signal and connect it to the right input of the right Product block. Tap a line off the d2/dt2(theta) signal and connect it to the right input of the left Product block. Tap a line of this new line and connect it to the left input of the right Product block.

Now, we will generate the terms (d/dt(theta))^2*lsin(theta) and (d/dt(theta))^2*lcos(theta).


Insert two Product blocks next to each other to the right and slightly above the previous pair of Product blocks. Rotate the left Product block 90 degrees. Flip the other product block left-to-right and also rotate it 90 degrees. Tap a line off the l cos(theta) signal and connect it to the left input of the left Product block. Tap a line off the l sin(theta) signal and connect it to the right input of the right Product block. Insert a third Product block and insert it slightly above the d/dt(theta) line. Label this block "d/dt(theta)^2". Tap a line off the d/dt(theta) signal and connect it to the left input of the lower Product block. Tap a line of this new line and connect it to the right input of the lower Product block. Connect the output of the lower Product block to the free input of the right upper Product block. Tap a line of this new line and connect it to the free input of the left upper Product block.

Finally, we will connect these signals to produce the pendulum acceleration signals. In addition, we will create the system outputs x and theta.

Connect the d2/dt2(theta)*lsin(theta) Product block's output to the lower (negative) input of the d2/dt2(yp) Sum block. Connect the d2/dt2(theta)*lcos(theta) Product block's output to the lower (negative) input of the d2/dt2(xp) Sum block. Connect the d/dt(theta)^2*lcos(theta) Product block's output to the upper (negative) input of the d2/dt2(yp) Sum block. Connect the d/dt(theta)^2*lsin(theta) Product block's output to the middle (positive) input of the d2/dt2(xp) Sum block. Insert an Out block (from the Connections block library) attached to the theta signal. Label this block "Theta". Insert an Out block attached to the x signal. Label this block "x". It should automatically be numbered 2.

Now, save this model as pend.mdl. Open-loop response To generate the open-loop response, it is necessary to contain this model in a subsystem block.

Create a new model window (select New from the File menu in Simulink or hit Ctrl-N). Insert a Subsystem block from the Connections block library. Open the Subsystem block by double clicking on it. You will see a new model window labeled "Subsystem". Open your previous model window named pend.mdl. Select all of the model components by selecting Select All from the Edit menu (or hit Ctrl-A). Copy the model into the paste buffer by selecting Copy from the Edit menu (or hit Ctrl-C). Paste the model into the Subsystem window by selecting Paste from the Edit menu (or hit Ctrl-V) in the Subsystem window Close the Subsystem window. You will see the Subsystem block in the untitled window with one input terminal labeled F and two output terminals labeled Theta and x. Resize the Subsystem block to make the labels visible by selecting it and dragging one of the corners.

Label the Subsystem block "Inverted Pendulum".

Now, we will apply a unit impulse force input, and view the pendulum angle and cart position. An impulse cannot be exactly simulated, since it is an infinite signal for an infinitesimal time with time integral equal to 1. Instead, we will use a pulse generator to generate a large but finite pulse for a small but finite time. The magnitude of the pulse times the length of the pulse will equal 1.

Insert a Pulse Generator block from the Sources block library and connect it to the F input of the Inverted Pendulum block. Insert a Scope block (from the Sinks block library) and connect it to the Theta output of the Inverted Pendulum block. Insert a Scope block and connect it to the x output of the Inverted Pendulum block. Edit the Pulse Generator block by double clicking on it. You will see the following dialog box.

Change the Period value to "10" (a long time between a chain of impulses - we will be interested in only the first pulse). Change the Duty Cycle value to ".01" this corresponds to .01% of 10 seconds, or .001 seconds. Change the Amplitude to 1000. 1000 times .001 equals 1, providing an approximate unit impulse.

Close this dialog box. You system will appear as shown below.

We now need to set an appropriate simulation time to view the response.


Select Parameters from the Simulation menu. Change the Stop Time value to 2 seconds. Close this dialog box

You can download a version of the system here. Before running it, it is necessary to set the physical constants. Enter the following commands at the MATLAB prompt. M = .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 hit Ctrl-t). If you look at the MATLAB prompt, you will see some error messages concerning algebraic loops. Due to the algebraic constraint in this system, there are closed loops in the model with no dynamics which must be resolved completely at each time step before dynamics are considered. In general, this is not a problem, but often algebraic loops slow down the simulation, and can cause real problems if discontinuities exist within the loop (such as saturation, sign functions, etc.) Open both Scopes and hit the auto scale buttons. You will see the following for theta (left) and x (right).

Notice that the pendulum swings all the way around due to the impact, and the cart travels along with a jerky motion due to the pendulum. These simulations differ greatly from the MATLAB open loop simulations because Simulink allows for fully nonlinear systems. Extracting the linearized model into MATLAB Since MATLAB can't deal with nonlinear systems directly, we cannot extract the exact model from Simulink into MATLAB. However, a linearized model can be extracted. This is done through the use of In and Out Connection blocks and the MATLAB function linmod. In the case of this example, will use the equivalent command linmod2, which can better handle the numerical difficulties of this problem. To extract a model, it is necessary to start with a model file with inputs and outputs defined as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as pend.mdl. In this model, one input, F (the force on the cart) and two outputs, theta (pendulum angle from vertical) and x (position of the cart), were defined. When linearizing a model, it is necessary to choose an operating point about which to linearize. By default, the linmod2 command linearizes about a state vector of zero and zero input. Since this is the point about which we would like to linearize, we do not need to specify any extra arguments in the command. Since the system has two outputs, we will generate two transfer functions.

At the MATLAB prompt, enter the following commands [A,B,C,D]=linmod2('pend') [nums,den]=ss2tf(A,B,C,D) numtheta=nums(1,:) numx=nums(2,:) You will see the following output (along with algebraic loop error messages) providing a state-space model, two transfer function numerators, and one transfer function denominator (both transfer functions share the same denominator). A= 0 0 1.0000 0 0 0 0 1.0000 31.1818 0.0000 0.0000 -0.4545 2.6727 0.0000 0.0000 -0.1818 B= 0 0 4.5455 1.8182 C= 1 0 D= 0 0 nums = 0 0 den = 1.0000 numtheta = 0 0.0000 4.5455 0.0000 0.0000 0.1818 -31.1818 -4.4545 0.0000 0.0000 0.0000 4.5455 1.8182 0.0000 0.0000 0.0000 -44.5455 0 1 0 0 0 0

numx = 0 0.0000 1.8182 0.0000 -44.5455 To verify the model, we will generate an open-loop response. At the MATLAB command line, enter the following commands. t=0:0.01:5; impulse(numtheta,den,t); axis([0 1 0 60]); You should get the following response for the angle of the pendulum.

Note that this is identical to the impulse response obtained in the MATLAB tutorial pendulum modeling example. Since it is a linearized model, however, it is not the same as the fully-nonlinear impulse response obtained in Simulink. Implementing PID control In the pendulum PID control example, a PID controller was designed with proportional, integral, and derivative gains equal to 100, 1, and 20, respectively. To implement this, we will start with our open-loop model of the inverted pendulum. And add in both a control input and the disturbance impulse input to the plant.

Open your Simulink model window you used to obtain the nonlinear open-loop response. (pendol.mdl) Delete the line connecting the Pulse Generator block to the Inverted Pendulum block. (singleclick on the line and select Cut from the Edit menu or hit Ctrl-X). Move the Pulse Generator block to the upper left of the window. Insert a Sum block to the left of the Inverted Pendulum block.

Connect the output of the Sum block to the Inverted Pendulum block. Connect the Pulse generator to the upper (positive) input of the Sum block.

Now, we will feed back the angle output.


Insert a Sum block to the right and below the Pulse Generator block. Change the signs of the Sum block to "+-". Insert a Constant block (from the Sources block library) below the Pulse generator. Change its value to "0". This is the reference input. Connect the constant block to the upper (positive) input of the second Sum block. Tap a line off the Theta output of the Inverted Pendulum block and draw it down and to the left. Extend this line and connect it to the lower (negative) input of the second Sum block.

Now, we will insert a PID controller.


Double-click on the Blocksets & Toolboxes icon in the main Simulink window. This will open a new window with two icons. In this new window, double-click on the SIMULINK extras icon. This will open a window with icons similar to the main Simulink window. Double-click on the Additional Linear block library icon. This will bring up a library of Linear blocks to augment the standard Linear block library. Drag a PID Controller block into your model between the two Sum blocks.

Connect the output of the second Sum block to the input of the PID block. Connect the PID output to the first Sum block's free input.

Edit the PID block by double clicking on it. Change the Proportional gain to 100, leave the Integral gain 1, and change the Derivative gain to 20. Close this window.

You can download our version of the closed-loop system here Closed-loop response We can now simulate the closed-loop system. Be sure the physical parameters are set (if you just ran the open-loop response, they should still be set.) Start the simulation, double-click on the Theta scope and hit the autoscale button. You should see the following response:

This is identical to the closed-loop response obtained in the MATLAB tutorials. Note that the PID controller handles the nonlinear system very well because the angle is very small (.04 radians).

You might also like