University of Utah Electrical & Computer Engineering Department ECE 3510 Lab 5b Position Control Using a Proportional - Integral - Differential (PID) Controller Note: Bring the lab-2 handout to use as a dspace tutorial. M. Boson, A. Stolp, 2/19/08 rev,2/24/09, 2/23/17 B. Mukunda Objectives! Analyze a type of PID control system.! Modify the dspace programming to implement he PID control law.! Observe the step response with different zero locations (as set by the feedforward gain).! Compare the tuned PID response to the simple proportional response. Equipment and materials from stockroom:! DC Permanent-magnet Motor (If you can, get the same motor you used last time.)! Dual Power Amp! DSPACE kit Experiment 3, PID Position Control In this experiment you will construct a position controller, so you ll need to modify the model of the DC motor to be angular position over voltage rather than angular velocity over voltage. Write your motor transfer-function as: θ(s) k P(s) = = 1 V(s) s(s + a )(s + a ) 1 2 In lab 3 you used a proportional control law to control the motor position. v = k ( r θ) p In this experiment you will use a variation of the proportional-integral-derivative control law. d v = k ( r θ) + k r dt k r P I ( θ) + ( θ) D dt The integral term will minimize the steady state error as you saw with velocity control in experiment 2. The derivative term can be viewed as a proportional feedback acting on the velocity error. Responding to this additional error can increase the speed of the response-- a good thing. Ah, would that life were so simple... Think what would happen if you input a step response? What is the derivative of a step? Mathematically no big deal, the control system delivers an impulse to the motor which certainly speeds up the response. The problem arises with the limits of a system implemented with real parts. No real amplifier can deliver an impulse, so instead, it rails for a very short time and has a minimal impact on the p1 ECE 3510 PID Lab
response. (Actually, this might be a blessing in disguise, given that an impulse would mean an infinite voltage probably not too safe.) In practice, the control law is often modified in two ways. First, to avoid the problem with step inputs. the derivative action is applied only to the output (θ), and not to the reference input (r). This also eliminates a zero in the overall (closed-loop) transfer function. (No, I don t expect you to just see that, it takes a bit of math to show that that is the case.) Second, the reference input to the proportional gain is modified by a feedforward gain (k F ). This is not done to adjust the DC gain (as in experiment 1), but rather to gain control over the placement of the remaining zero of the closed-loop transfer function. Again, this is not obvious, but you will see it come out in the math. The modified control law is given by: d v = k ( k r θ) + k r dt k P F I ( θ) θ D dt Verify that the resulting closed-loop transfer function for the system is: H CL k I k1k Pk F( s + ) θ( s) k Pk F ( s) = = 4 3 2 r( s) s + ( a + a ) s + ( a a + k k ) s + k k s + k k 1 2 1 2 1 D 1 P 1 I Now again, set the poles somewhat like those of critical damping, that is, make all the poles the same, say at -p. That would make: 4 3 2 s + ( a + a ) s + ( a a + k k ) s + k k s+ k k = ( s + p) 1 2 1 2 1 D 1 P 1 I = s + 4ps + 6p s + 4p s+ p 4 3 2 2 3 4 Find p, k D, k P, and k I. They should be about 180, 0.2, 30-42 and 1000-1900 respectively. Notice that the closed-loop transfer function has a zero at -k I /(k F k P ). With a neutral feedforward gain (k F = 1) the zero may have a small magnitude compared to the closed-loop poles, yielding overshoot in the step response even if the closed-loop poles are welldamped. (See page 37 in the text.) Find the position of the zero if k F = 1 then show that if k F <1, the zero will be further in the left-half plane and the step response will be better. Simulink Model Modifications Now you will need to modify the Simulink model and then the dspace layout to implement the PID controller. Open the Simulink model that you had previously downloaded for lab 5a. The following are the changes you will have to make: 4 p2 ECE 3510 PID Lab
1. Introduce kd (k D ) in the PI Controller block: >> Double click on the PI Controller block. You will see the function that was used for lab 5a. Consult the figure above and make the indicated changes to the input variable declaration and the function itself. Here dy will be the derivative of your reference signal y, y being the position and consequently dy being the speed. Once done, save and exit the function. You should be able to see an additional post for an input signal on the PI Controller block. 2. Create the section on model shown here. Note that the Switch block shown (found in the Simulink Library Browser, in the Signal Routing section has a >= threshold of 0.5. Here s how to set that: Once the block is dragged and dropped from the signal routing section, >> double click on the block >> in the Criteria for passing first input:, choose u2 >= Threshold >> in the Threshold section, enter 0.5 and hit OK. The kd, PID Controller and Contant3 blocks pictured below are just constant blocks you can find in the Commonly used blocks section of the Simulink library browser. You will need to double-click these blocks and enter the desired value (0 here) in the Constant value section to change their values. Name them (it is important to name kd and PID Controller) by clicking on the text under the blocks and changing it. 3. Once the section above is built, the output (red dotted connection) will be fed to the kd input on the PI Controller block. With this done, you will need to make a few minor modifications as shown. The Sum block can be found in the Simulink library browser under the Commonly used blocks, the Math Operations section or you may just type Sum into the search bar of the Simulink library browser. 4. For the final few edits, recall that lab 5a was designed with the intention of controlling velocity and not position. So the reference input signal (R) and the actual signal (y) were both velocity inputs. You will need to change this into position inputs. The dy input is then the velocity input. Shown below is a section of the PI Controller block with the changes explained. Note that the reference input (r) is a constant block named Pos_ref passed into a Gain block (found among the commonly used blocks) with its value changed to pi/180 (double click the dragged and dropped gain block and change the gain value to pi/180. The input you will be specifying reflects the Pos_ref p3 ECE 3510 PID Lab
block. Since you will be entering the reference position in degrees, the pi/180 gain block converts it into radians before it enters the PI Controller block. You probably noted that the y and dy inputs are fed by from blocks. Every Goto block can have one or more complementary From blocks. These can be found in the Signal Routing section and make the models much cleaner by eliminating winding connections. Note that there are already two Goto blocks with the tags th_rad and wm on the top right-hand section of the model. Now drag and drop two From blocks from the Simulink library. Double click the first one, under the Parameters, in the Goto dropdown list, where you can see all the tags assigned to Goto blocks. Select th_rad and click on update tags. Now this from block collects the signal from the Goto block with the same tag, in this case the position information in radians. Click on OK and connect the output of this block into y port of the PI Controller. Similarly, assign the tag wm to the second From block and connect the output to the dy port of the PI Controller block. The final form of the edited PI Controller block is provided for your reference in the appendix. Ensure that Ts, nd are defined in the model properties. Note that a Ts of 2e-3 s gives you the same resolution in data as a Ts of 1e-4 with a downsampling of 20 (in dspace). You can choose to do either. Make sure all the Model Configuration Parameters are defined appropriately, save and then build the model. Now in dspace: >> Open dspace > Click on Open Project+Experiment. This will allow you to open and existing project. Go on ahead and open the project that was created for lab 5a. You will be making a few adjustments to this layout. But before that, since new changes were built into the Simulink model, you will need to reload the variables into dspace. In the Project tab on the left side of the window, right click on the.sdf file and click on Reload Variable Description. All your new variables and changes should carry over to this experiment. You will now need two additional instruments, one for a control type selection (PID) and one to enter the value for kd. 1. Click on the Instrument Selector tab on the right side of the layout window. Choose a check button, click p4 ECE 3510 PID Lab
and drag it on the Control Type panel such that you now have three check boxes in the panel. In the Model Root, click on PID Controller, drag and drop the value into the new check box. 2. In the Instrument Selector again, select a Numeric Input instrument and draw it onto a comfortable spot on the layout. In the Model Root, click on kd, drag and drop the value into the new numeric input instrument. 3. Draw an On/Off button instrument for the encoder reset variable. You will notice that by default, there are two buttons. Right click on the instrument, and under the On/Off button settings, click on the browse option. Select button 2 on the left and click on the red X mark to remove it. Then, click on OK. The On/Off button window can be seen above. Map the RESET ENCODER value to this button. 4. In the Model Root, click on Pos_ref and map the value to the numeric instrument that contained Vel_ref in lab 5a. That instrument should now be blank since the model no longer has a constant named Vel_ref, but if there were a variable already mapped, to replace it with a new one, simply drag the new variable and drop it on the instrument. The previously mapped variable will be removed automatically (but the settings on that instrument will be retained, so make sure you reduce the increment size from1000 back to 1). The display instrument that sits beside this numeric input instrument will now be mapped to the Out1 signal from theta_deg, since this is what we will need to keep an eye on. 5. Also drag and drop Pos_ref and theta_deg on the small colored squares in the plotter, one in each. Your layout should now look something like the figure. Make sure you set all the data capture settings as instructed to in the previous labs. Set the data capture length to the same as that used in lab 3. Check your plots if you need to be sure. p5 ECE 3510 PID Lab
Proportional-Integral-Derivative (PID) Control Make sure to set up your recorder as you see fit. Once online, turn ON the simulation, hit Start, check the PID Controller in the Control Type panel and set the Feed-forward gain to 1 and all the other gains to your calculated values. The reference input is now the Pos_ref (degrees) and is angular position. Set this to 90 degrees. As soon as you enter this ref value, the motor should spin to 90 degrees and stop, giving you a step response with considerable overshoot due to the zero. The settling time should be about 100ms. You may run it again by simply unchecking the PID controller, the "start button", checking on the RESET ENCODER button to make sure the encoder position is at 0. Now check the start and PID controller buttons and re-enter the Pos_ref value. To shut the experiment down, uncheck the PID controller, zero out your Pos_ref, Uncheck Start, turn off the simulation and go offline to shut the experiment down. Adjust the parameter k F to get a better response (make k F smaller). Try several values of k F to get less than 5% overshoot and the fastest time to within 5% of the final position value. (Having trouble or don t have numbers yet? Use conservative values of 0.2, 30 and 1000 for k D, k P, and k I.) Use Matlab to plot the results for a few values of k F on a single graph (don t print it yet). If you still have the data from lab 3 (critical damp.) add that to the same plot. If not, make another run with k F =1, k P =3.66, and both k I =0 and k D =0. This should recreate that data and you can add it to the plot. Print the plot. Indicate what value of k F gives the best response and compare the PID response to the simple proportional response. Reset all your gains to your best values and make one last run, only this time don t hit Stop. Grab the coupler of the motor in such a way that if it unexpectedly begins to spin, it won t hurt you. If this worries you, temporarily remove the motor coupler. The naked shaft is pretty harmless. Try to turn the motor shaft. Can you feel (or see) the integral control fight the change and eliminate the steady-state error? Don t use too much force or do this too long. It seems that this can force the system into some weird state where it just keeps running. Conclusion Check-off and conclude as always. p6 ECE 3510 PID Lab