ECE-320 Labs 7 and 8: Utlzng a dspic30f6015 to control the speed of a wheel Overvew: In ths lab we wll utlze the dspic30f6015 to mplement a PID controller to control the speed of a wheel. Most of the ntal code wll be gven to you (see the class webste), and you wll have to modfy the code as you go on. One of the thngs you wll dscover s that you often need to be aware of lmtatons of both hardware and software when you try to mplement a controller. The dspic30f6015 has been mounted on a carrer board that allows us to communcate wth a termnal (your laptop) va a USB cable. In what follows you wll need to make reference to the pn out of the dspic30f6015 (shown n Fgure 1) and the correspondng pns on the carrer (shown n Fgure 2) Fgure 1. dspic30f6015 64-PIN pnout. 1
Fgure 2. dspic30f6015 carrer. Note that the pn numbers are not consecutve. 2
Part A (Copyng the ntal fles) Set up a new folder for ths lab, and copy the fles for ths Lab from the class webste to ths folder. Part B (Downloadng and nstallng MPLAB) You wll need to go to the webste http://www.mcrochp.com/stellent/dcplg?idcservce=ss_get_page&nodeid=1406&ddocname=en 019469 and download the newest verson of MPLAB (MPLAB IDE v8.83+). If you have an older of MPLAB t wll probably work (mne s v8.76) and you may be able to skp ths step. It s probably easest to download a zpped verson and the unzp t. Once the fle has been unzpped, run setup.exe. Accept all of the defaults. Do not ntall any C complers yet. Part C (Downloadng and nstallng MPLAB C30 compler) You wll need to go to the webste http://www.mcrochp.com/stellent/dcplg?idcservce=ss_get_page&nodeid=1406&ddocname=en 536656 and download the MPLAB C compler for the dspic. Ths s the academc verson. You wll need to regster your use (clck on regster now). Once the fle has been downloaded, you need to run mplab30_v3_30c_wndows.exe to nstall t. I used the Versoned Drectory Name. Select the Evaluaton Compler. The devces we want are the dspic DCSs Part D (Settng up Communcatons wth Secure CRT) In ths step we wll make sure all of our systems are workng before we go on. Connect the PICkt3 to the carrer board and your computer (the whte arrow goes near the whte spot on the carrer board.) Connect the separate communcaton cable to the carreer board and your computer. Connect power to the power board and toggle to on swtch (the green LED should then be on) Wat a few mnutes (see f your system s tryng to fnd any devces). The go to Programs->Control Panel and select Dervce Manager. Expand the Ports opton as shown below. The followng fgure shows that the USB s connected to my COM5 port. You wll need ths nformaton shortly. 3
My USB seral port s COM5, yours may be dfferent We are gong to want to be able to record and plot data, so we wll be wrtng to the screen. In order to do ths we wll usng Secure CRT because t s nstalled on your computer by default. Go to Programs All Programs SecureCRT SecureCRT6.2 (t s ok f you don t have 6.2). You should get a screen lke ths (although the lst of Sessons wll probably be shorter for you): 4
Clck on New Sesson You should then get a screen that looks lke ths Choose Seral from ths menu, then clck on Next at the bottom of the page 5
You wll next get the followng screen. You should already know whch port to use (t may not be COM5 on your computer). Select a Baud rate of 115200 and be sure everythng s flled n as below. The clck on Next. Fnally, you need to create a sesson name so you do not have to go through these steps agan. My choce n names was pretty ntutve. Once you have named your sesson select Fnsh. 6
Fnally, you wll get a screen lke that below, and you need to select Connect. Now you should have a termnal lke that shown below From ths screen, the opton we wll use most s Fle-> Log Sesson. Ths wll allow us to wrte data collected to a fle to be processed later. 7
Part E (Puttng t all together) Now we are ready to start. Note that at some pont n the followng process you may need to let the system download frmware. Be sure you have selected the correct devce (or t wll do ths twce). Start MPLAB Select Project and open the project lab7.mcp Select Confgure, then Select Devce and choose dspic30f6015 Select Debugger, then Select Tool and choose PICkt 3 You may need to clck on lab7.c (under Source Fles) to see the C code. You should get a screen (more or less) lke the followng: Run comple Download (to dspic) You should comple the exstng program, download t to the dspic, and run t. You should see a sequence of numbers on your screen (the secure CRT screen). These correspond to sample tmes. 8
Part F (Turnng one LED on and off) In ths step we wll make one LED turn on and off. Locate pn 1 (RE5). Connect a resstor and an LED to ths pn as shown below: We need to get ths ready for output, so n the fle lab7.c, nsert the lnes (before the man loop) TRISEbts.TRISE5 = 0; You wll see n the man loop we are wrtng all 1 s or all zeros to all of port E every other tme nterval. Comple your code and run t. Your LED should turn on and off regularly. Part G (Runnng Two LEDs) Connect your other LED to pn2 (RE6). Modfy you code so the LED s alternate (one goes on and the other off). Once ths has been checked off (see the last page), comment out the code that turns the LEDs on and off n the man code (do not delete t). Have ths verfed by your nstructor. Part H (Utlzng PWM to run your LEDs) We wll now utlze the PWM functon to drve the LED. Note that RE6 corresponds to PWM4L.(Refer to Fgures 1 and 2 to be sure to get the correct pn. Note that not all of the dspic pns are on the carrer.) In the routne pwm_nt, change PWM_PDIS4L to PWM_PEN4L. Ths allows us to use ths pn as a PWM sgnal. In the man code, uncomment the functon call pwm_nt(); Set the varable AD_value = 512. 9
Uncomment the code: dutycycle = (unsgned nt) ((double) AD_value)*scale; dutycyclereg = 4; SetDCMCPWM( dutycyclereg, dutycycle, updatedsable); Ths code determnes the duty cycle value and wrtes t to dutycycle regster 4. Comple and run your code. A sngle LED should lght (constantly). Modfy the values n AD_value from 0 to 512 to verfy that the brghtness of the LEDs changes as the dutycycle changes. Note that at some pont the LED stops gettng brghter, so don t worry about that. Have the nstructor verfy ths (see the last page). Part I (Readng n an A/D value) Now we want to be able to connect a potentometer so we can have a varable reference. The software s currently set up for A/D nput on AN3/RB3. You wll need to do the followng thngs: 1) Connect the potentometer as shown below 2) Set the approprate TRIS bts for an nput sgnal 3) Comment out the part of the code that sets the AD_value = 0; 4) Modfy the prntf statement to allow you to also prnt out the A/D value from the pot Part J (Changng the dutycycle value by readng from the potentometer) Modfy your code so the value of the dutycycle n the PWM routne s set by readng n a value from the potentometer. As you turn the potentometer the LED should change ntensty. Have ths verfed by your nstructor (see the last page). Ths s the end of Lab 7 10
Part K: Readng n the sensor transducer In order to determne how fast the motor s spnnng, we wll need to use the QEI nterface. The sensor s connected to the center of the wheel, and the other end of the sensor plugs nto the breadboard. The pns for the nterface are shown n Fgure 3. You need to connect ths nterface to +5 volts (red ), ground (blue), and the QEA Channel A nput (pn 12), and the QEA Channel B nput (pn 11). Fgure 3. Encoder pn outs. Part L: Connectng the power entry Plug n both the 5 volt (green) and 12 volt (red) power supples, as shown n Fgure 4. Do not turn on the supples yet (the LEDs should be off). Fgure 4. Power entry module. 11
Part M: Connectng the H Brdge Plug n the H-brdge, shown n Fgure 5. You are gong to need access to the dfferent pns, so be sure t s located n a place you can easly get wres to. Fgure 5. H Brdge connectons Startng on the left, connect the ground and +5 volt supples. Next, connect IN1 to E6 (pn 2) and IN2 to E7 (pn 3). (Note that f the motor speed s negatve you may want to reverse these connectons.) Fnally connect PWM to PWM 3 hgh (pn 1). Startng on the rght, connect (wth the twsted wres) the 12 volt power from the power entry module to the 12 volt nput. Be sure to connect ground to ground and +12 to +12. Next, connect M1 and M2 (usng the twsted wres) to the motor nput. At ths pont all of your wrng should be done. You should check t over before you go on. Two mportant thngs to keep n mnd as you do the remander of ths lab: Use the red swtch to shut off power to the motor. Sometmes stoppng the dspic does not stop the motor, n whch case you wll need to use the swtch. Unless you are told otherwse (and later n the lab you wll be), turn the pot farly slowly. PART N: Determnng Intal Scalng We need a new program now to control the motor. Make sure there are four bolts attached to the wheel. Select Project and open the project lab8.mcp. You wll modfy lab8.c for the remander of ths lab. Now we wll start to determne some of our parameters. Wth the system at rest start the program and slowly turn the pot so the motor s spnnng (less than 30 rad/sec). Let the motor come to a reasonably steady value (t wll lkely keep gettng faster). Wat untl the motor speed s farly constant and the nput 12
readng from the pot s also farly constant. Do ths for three dfferent postons of the pot where the steady state speed s less than 80 rad/sec. (Note that t s lkely to be easest to use the sesson log fle than look at the screen output.) We now want to determne the proportonalty constant between the A/D value read from the pot and the motor speed n rad/sec. Compute the three scalng factors and average them (mne was approxmately 0.219). Change the parameter AD_scale (n your code) to ths value. Run the system agan at varous nput values. It may take a whle for the system to reach steady state. Compare your reference nput to the actual output. They should be farly close for speeds below around 80 rad/sec, but they wll not be exact. Ths s open loop control. PART O: Proportonal Control We now want to start wth our frst control scheme, proportonal control. You wll need to declare the varable kp as a double (at the top of the man routne). Outsde of the man loop set kp = 1.0, and nsde the man loop, after both the speed and reference nput are determned, compute the error as error = R-speed. Here R s the reference nput and speed s the measured output speed of the wheel n rad/sec. The control effort s then proportonal to ths error, so u = kp *error. Recomple and download the code onto the mcrocontroller. Now start the system agan. Move the pot to a number of dfferent set ponts, and wat untl the system comes to steady state. Most lkely the steady state value wll be approxmately one half of the set pont. Stop the system and change the value of kp to 5, and then to 10. (Be sure to recomple and download after each tme). Run the system agan for each value of kp. You should notce that the steady state error gets smaller each tme. PART P: Lmtng the slope of the control effort One of the frst lmtatons we wll have to deal wth s the fact that f you try and draw too much power all at once, the power supply assumes you have screwed up. Ths leads to some strange behavor. Starrng wth kp = 10.0, start the system and turn the pot (settng the reference nput) as quckly as you can (but don t worry about turnng t all the way). Mostly lkely you hear strange sounds from the motor lke t s startng and stoppng (or worse). What we need to do n ths case s lmt the rate at whch we allow the control sgnal to change. At the top of the code there s a constant MAX_DELTA_U whch ndcates the maxmum change n control effort u from one tme step to the next. Ths constant s gong to need to be changed (but we wll get to that). Declare a new (double) varable last_u, and set ths varable equal to zero outsde the man loop. Insde the man loop, add statements to the code that do not allow the control effort to ncrease by more than ts current value + the maxmum allowed ncrement. Insert ths lmt on the control effort after 13
convertng to a pwm sgnal but before we check that the control effort s not too large. Also be sure to update the value of last_u. However, do not update ths value untl the other statements n the code that nsure that the value of u s wthn acceptable lmts. Fnally, you wll need to set the value for MAX_DELTA_U. I would start at around 800, and change ths as necessary. Try to get ths value as large as possble. Run your code and try to change the pot (the reference nput) as quckly as possble. Once you thnk your values are ok, set kp=100.0 and be sure your system runs acceptably (the motor does not shut off and on). PART Q: Proportonal controllers wth large gans Make sure kp s set to 100 for ths part. Comple the code f you need to and start t runnng. Vary the reference sgnal to a few set ponts (such as 30 rad/sec, 60 rad/sec, and 90 rad/sec) and allow the system to try and reach steady state at each of these set ponts. You should notce that as the set pont ncreases n value, the speed of the wheel starts to vary sgnfcantly from the reference pont. Part of ths s due to the fact that the motor s only capable of one drecton, but a larger part s due to the fact that small errors are amplfed (by 100) and ths causes the motor to contnually oscllate. PART R: Integral control Recall that usng a preflter to control steady state error can sometmes be problematc snce the preflter s outsde the feedback loop. An alternatve, and generally better, soluton s to nclude some form of ntegral control. Recall that an ntegral controller has the form G ( z) = c k U( z) z) = 1 1 z E( Rearrangng ths we get k Ez ( ) = Uz ( ) z 1 U(z) In the tme-doman ths becomes k en ( ) = un ( ) u( n 1) Snce we want the control effort as our output, we wll wrte ths as un ( ) = u( n 1) + ke( n) If we assume the ntal control effort s zero, we can wrte ths as follows: 14
u(1) = k e(1) + u(0) = ke(1) u(2) = ke(2) + u(1) = k[ e(2) + e(1)] u(3) = ke(3) + u( 2) = k[ e(3) + e(2) + e(1)] un ( ) = k ek ( ) n k = 1 Hence to mplement the ntegral control, we need to sum the error terms and then scale them by k. Declare two new (double) varables Isum and k. Set the ntal value of Isum to zero outsde the man loop. Wthn the man loop update the error summaton usng somethng lke Isum = Isum + error. Wthn the man loop mplement a PI controller as follows: u = kp*error + k*isum; Set kp = 0.0 and k = 0.5. Modfy the prnt statement at the end of your code so the value of Isum s also prnted out. Recomple and download your code. Start the system and set the reference sgnal to a few reference ponts (30, 60, 90 rad/sec, for example), and let the system run for a whle. Your system wll probably exhbt some pretty strange behavor on ts way to the correct steady state value (t wll bounce around the steady state value due to samplng). Look at what s happenng to the value of Isum durng ths strange behavor. PART S: Integrator problems The frst problem we need to fx s that our motor s only spnnng n one drecton. When Isum becomes large and negatve, we would expect the motor to spn n the other drecton, but t can t. One way to mnmze ths effect s to check to be sure that the value of Isum s greater than or equal to zero. Modfy your code to do ths, recomple, and run t agan. The second problem we have s called ntegrator wndup. Bascally, the accumulated error s becomng too large and causes the system to overshoot, and then undershoot. One way to fx ths s to lmt the value of Isum to a maxmum value. There s a defned varable at the begnnng of the code MAX_ISUM. You need to set a reasonable value for ths varable and lmt the value of Isum be less than ths max. Modfy your code to do ths, recomple, download, and run t agan. You wll have to use some tral and error to fnd a good value for MAX_ISUM snce t s also a functon of the value of k. I choose a value somewhere between 10 and 800 (I m not tellng you where, but I am suggestng someplace to start lookng). Do not move on untl you have what you thnk s a reasonable value of MAX_ISUM and your code seems to be workng. Note that you wll stll have some oscllatons, and that to get to a large steady state value MAX_ISUM wll need to be farly large. 15
PART T: Includng a dervatve term Fnally we need to nclude a dervatve term to mplement a full PID controller. You wll need to defne three new (double) varables, kd, Derror, and last_error. Outsde of the man loop set Derror and last_error equal to zero and set kd equal to 0.01. Insde the man loop nclude the lnes Derror = error-last-error; Last_error = error; Fnally, to mplement the full PID controller defne the control error u to be u = kp*error+k*isum+kd*derror; Comple and download your code, and try a few reference ponts. Your system may stll oscllate, but we can work on that soon. PART U: Desgnng a PID controller At ths pont, we want to hardcode the value of the reference sgnal so the step response wll start as soon as we turn on the system. Just after the statement where we assgn R based on the A/D value, nsert the statement R = 90.0; // set for 90 rad/sec A general plan for desgnng a PID controller usng a tral and error method (we have no model for the plant) s the followng: Frst, set k = kd = 0, and try to get a good response for a step nput usng only kp. Next, adjust k to get a good steady state error. Snce the ntegral control tends to slow the system down, don t make ths any larger than you need to. However, you may need to also change MAX_ISUM to get a good response. Fnally, adjust kd to speed up the response. Once you have a good desgn, you need to log your data so you can make a Matlab plot. If you have saved your data n a fle named (for example) play3.log, then n Matlab type data = load( play3.log ); t = data(:,1); y = data(:,4); The plot I have for one of my results s shown n Fgure 6. 16
Desgn PID controllers to meet the specfcatons shown below. Include a Matlab generated fgure of your step response results n your memo, and be sure to nclude both the samplng nterval and the values of kp, k, and kd. Also attach your fnal code to your e-mal. () () () For a samplng nterval of T = 0.025 sec (the default) our desgn crtera s a settlng tme of less than or equal to 1.2 seconds, and a percent overshoot less than 10%. For a samplng tme of 0.1 sec, our desgn crtera s a settlng tme of less than or equal to 4.0 seconds, and a percent overshoot less than 10%. For a samplng tme of 0.1 sec, desgn a PID controller so there s at least a 10% overshoot and the settlng tme s less than 10 seconds. 100 90 80 70 Speed (rad/sec) 60 50 40 30 20 10 0 0 0.5 1 1.5 2 2.5 3 3.5 4 Tme (sec) Fgure 6: Step response for system wth T = 0.025 seconds. I am not tellng you my parameter values. Fnally, I would apprecate any comments you may have on ths lab, and how to mprove t. Also, do you thnk t was useful and we should do more labs lke ths n the future or not? 17
Instructor Verfcaton Names Part G (2 LEDs) Part J (Pot controls LED) Part J (Pot values wrtten to termnal) 18