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:
The design criteria (with the pendulum receiving a 1N impulse force from the cart) are:
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 an m-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);
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:
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 controlNow 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.
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:
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.