ECE4330 – Embedded Systems Design Lab 6 – PID Motor Controller Lab 6 – PID Motor Controller In most engineering applications, electric motors are controlled with some kind of feedback controller. In this lab, you will demonstrate why a feedback controller is better than an open loop controller. You will also build two kinds of feedback controllers to demonstrate why the PID variety is superior. Pre-Lab Assignments 1. Review PID without a PhD at http://www.embedded.com/2000/0010/0010feat3.htm. 2. Complete the Pre-Lab Questions at the end of the lab instructions. Open Loop Controller As you learned in lab 5, there are two kinds of control systems: open loop and closed loop. Your task in part 1 of this lab is to build an open loop DC motor controller. The ‘plant’ to be controlled in this lab is a DC motor. The speed of the motor is controlled by a power FET. The signal to the gate of the FET is the PWM output of the microprocessor (μP). The greater the duty cycle of the PWM output, the faster the motor turns. The circuit is shown in figure 1 below. VCC 5V A PWM_Output Q1 Figure 1 - DC Motor Speed Control with Power FET The RPM of the DC motor is sensed with the NTE3100 optical sensor that was used in lab 3. The DC motor spins a disk or paddle that interrupts the IR beam between the LED 6-1 ECE4330 – Embedded Systems Design Lab 6 – PID Motor Controller and the sensor in the NTE3100. The NTE3100 is then connected as the clock source of a counter in the μP. Each rotation of the DC motor thus registers as a count. By reading the count periodically, the RPM of the motor is determined. The circuit for the RPM sensor is shown in figure 2. Note that the circuit uses the CD4050 buffer as discussed in lab 3. VCC 5V VCC 5V U1 R1 E + 2.2kOhm + S NTE3100 To_AVR R2 1kOhm Figure 2 - DC Motor RPM Sensor In-lab Task 1 of 3 1. Build an open loop controller based on the circuits in figures 1 and 2. Use the various VTG pins for Vcc. 2. Use the switches on the STK500 board the vary the duty cycle of the PWM output from 0% to 100%. Record the data and use Excel to figure out a linear relationship between the number loaded into the PWM output compare register (OCR) and the RPM of the motor. 3. Program the switches on the STK500 to control the RPM of the DC motor. Make SW0 set the RPM to the low end of the range e.g. 2,000 RPM. Make SW7 set the RPM to the high end of the range e.g. 14,000 RPM. Have the switches between SW0 and SW7 set the RPM to increasing values that are integer multiples of 500 RPM e.g. SW1 sets the RPM to 2,500, SW2 sets the RPM to 4,000, SW3 sets the RPM to 5,000 and so forth. Note that the switches now command RPM, not a certain PWM duty cycle. 4. Use HyperTerminal to record actual RPM versus commanded RPM for various step changes in commanded RPM. HyperTerminal is the serial port available on all Windows PC’s. Open HyperTerminal by clicking on Start, then All Programs, Accessories, 6-2 ECE4330 – Embedded Systems Design Lab 6 – PID Motor Controller Communications, HyperTerminal. Configure HyperTerminal to the same settings as the UART in the P (probably 9600 baud, 8-N-1). Commanded RPM is the RPM set by the switch. Actual RPM is the RPM detected by the optical sensor. Output RPM data to HyperTerminal using the printf statement. A good period for data output is once a second. Record the data sent to HyperTerminal using the Capture Text function. In HyperTerminal click on Transfer, then Capture Text … Browse to a convenient directory and create a file for your test data. Run several step changes while recording data via HyperTerminal, then close out the data file by clicking on Transfer, then Capture Text …, then Stop. 5. Use Excel to graph the commanded RPM versus actual RPM data. Import the data into Excel by clicking on Data, then Get External Data, then Import Text File. Browse to the data file created by HyperTerminal and open it. In the Text Import Wizard - Step 1 of 3, select Delimited then Next >. In Step 2 of 3, select Space then Finish. Click on OK in the final screen and the data put out by the printf statement will be transferred to Excel. Use Excel’s charting functions to graph commanded RPM and actual RPM versus time for several step changes. Your graph should look something like figure 3. If actual RPM data is noisy, apply a single pole filter to the data. This can be done using code like the following: displayRPM = 0.1 * measuredRPM + 0.9 * displayRPM; where displayRPM is the data sent out in the printf statement and measuredRPM is the RPM measured in the last control cycle. 6-3 ECE4330 – Embedded Systems Design Lab 6 – PID Motor Controller RPM Response - Open Loop Controller 18000 Commanded RPM 16000 Actual RPM 14000 12000 RPM 10000 8000 6000 4000 2000 0 0 10 20 30 40 50 60 Time (s) Figure 3 - Example Graph of Commanded RPM versus Actual RPM for an Open Loop Controller Simple Feedback Controller With a feedback controller, plant output is sampled and used to correct control signals to the plant so that plant output adjusts to the desired output value. In this case, the plant output is DC motor RPM, the output is sampled by measuring RPM with the NTE3100 optical sensor and the control signal is the duty cycle of the PWM signal to the FET. In the second part of this lab, you will build a simple feedback controller that samples RPM and adjusts the PWM so that the motor turns at the commanded RPM. This controller is a simple feedback controller because it does not do any math to figure out the fastest way to achieve the commanded RPM. The simple controller just senses if actual RPM is too high or too low and then nudges the PWM by one increment in the correct direction. In-lab Task 2 of 3 1. Modify the software from part 1 so that the actual RPM is used to nudge the PWM output in the necessary direction. If measured RPM is too low, increment the PWM OCR value by one. If the measured RPM is too high, decrease the PWM OCR value by one. 2. An important consideration is how often to sample the RPM and correct the PWM OCR. This is the control frequency. A control frequency of 10 Hz will work well for this lab. 6-4 70 ECE4330 – Embedded Systems Design Lab 6 – PID Motor Controller 3. Using HyperTerminal, collect commanded RPM versus actual RPM data for several step changes in commanded RPM. Plot the data as in part 1. When compared to the open loop controller in part 1, the actual RPM should be closer to the commanded value. Proportional Feedback Controller In the simple feedback controller built in part 2, PWM output is modified as required to make actual RPM conform to commanded RPM. The result should have been a more accurate motor RPM. However, the response time is probably very long. That is, the actual RPM adjusts to the commanded RPM very slowly. The simple feedback controller is more accurate than the open loop controller, but its response time is very slow. In part 3, you will add proportionality to the controller to improve its response time. Like the simple feedback controller, the actual RPM will be very close to the commanded RPM. Unlike the simple controller, the response time should be very short. Commanded changes to RPM should very quickly be seen in actual RPM. In-lab Task 3 of 3 1. Modify the software from part 1 so that the amount by which the PWM OCR is adjusted depends on the size of the error between actual and commanded RPM. 2. Using HyperTerminal, collect commanded RPM versus actual RPM data for several step changes in commanded RPM. Plot the data as in part 1. Controller response time should be improved compared to the simple feedback controller in part 2. Parts List 1. 2. 3. 4. 5. DC motor, Radio Shack p/n 273-258 Power FET IRF510 Optical sensor, NTE3100 Buffer, CD4050BC Resistors Deliverables 1. Demonstrate the proportional controller of part 3 to the lab instructor. 2. In your lab report, include graphs of controller performance for all three kinds of controller. Compare their performance. 6-5 ECE4330 – Embedded Systems Design Lab 6 – PID Motor Controller Pre-Lab Questions for Lab 6 Name: ______________________________ 10 points total, 4 points for question 1 and 6 points for question 2. 1. According to the author of “PID without a PhD,” one of the three kinds of control is the most problematic. Which one is it and what makes it difficult to implement? 2. For Timer2, calculate two combinations of clock prescaler values (called Clock Value in CodeWizardAVR) and Timer2 values that will result in a Timer2 interrupt every 10 ms. 6-6