You are on page 1of 10
Jonathan W. Valvano 147 Chapter 13 Solutions (6811 Solution 13.1. Part a) The range of w(a) (500ns each) is 0 0 40000. As explained in Chapter 6, for simplicity reasons we will iit the range from about 100 to about 39900. This will allow enough time to service the output compare interrupt. The relationship is in) = 400 - Ue) Pat b) Fist we convert the continuous integral into discrete time UO) = Uead) + RP-RO)At [Next we use the relation found in pat 2) ua) = u(t) + 400%(R*-R(n))rAt At 50 He, At = 0.02 seconds, UGA) = U(n-l) + Be(R*-R(O)) art o) The ULN2074 has sufficient output current to drive the motor. The motor current will be SV/102=0.5A. The 74HC14 Schmit-‘tigger will “clean up" the ugly waveform from the tachometer. The output on OC4/PA4 is used to generate the pulse-width modulated actuator control. The input on IC2PAL will use input capture to measure frequency. The desired frequency resolution is 100 Hz, whieh converts to @ motor speed resolution of { ps. The Input Device is connected to IC3/PA0 so that the rising edge of Ready can generate an 1C3 interrupt Poxc| Boe 6811 R 1c3PA0 Re aout Devi PA3 ee o¢4pas- —>—* rs = eee 1N4003 |= ae ‘T4HC14 = Ic2PA <——o<¢}+- + Tachometer Part d) Ritual software including data structures. The main program executes the Hiwal, then performs other unrelated tasks. Le, all processing occurs under interrupt contro} unsigned short count; // raw count ducing frequency measurement unsigned short Retar: // desized motor speed in rps unsigned shore “1 current motor speed in rps unsigned short ur 7/ actuator command 100 to 39900 counts unsigned short low: // utlow=40000, void ritual (void) { asm(™ sei") U1 make atonic DDRC=0%007 71 Bort © is desired speed ‘TMSKL |=0%137 11 Rem 102, 13, OC4 Tocs=neNt+20000; // First 16 20 ms TOTLI|= 0x0c; | // oca/ené set on next interrupt TOTL2 = (1CTL260xF0} [0x08 /* 102k, IC3F both set on rising edges */ count = 07 U1 Set vp for first 35100; U1 initially off Retar-0; Set on the subsequent meaourements */ ‘TELGI=0x137 U] clear 1C2F, TC3R, OCA asm(* cli"); } atte) Show the interrupt software. pragma interrupt handler T1C2handier() void TTc2handler (oid) ( Jonathan W. Valvano 148 ‘Solutions Manual for Real Time Embedded Systems, 2" Edition countess // number of rising edges TELG1M0x02;} // ack, clea 102° spragna interrupt_handier 71¢3handler () void Trcshandler (oid) ( PORTA | 0x08; // Done(PAS)=1 Retar=PORTC: PORTA == 0x57; // Done(PA3)=0 TELGLe0x01;) // ack, clear 1038 #pragna intercupt_handler Tocdhandler () void Tocdhandler (void) { long news Ae(rereLadx04)(// PAL is now high R= counto>1; —// R(njacount/2 100 Hz unite newu =u + 4*(2*Retar - count); iftnewu39900) u=39990 else uenews ‘ow=40000- eount = 07 U/ Setup for next TOcterocé'u; —// PAM is 1 for u cycles TOTLIe=0x9D;}// clear on next else ( U1 BR is now low TocéeTocK+iow // PA4 is 0 for low cycles TOPE |=Ox0c;)// set on next interrupt TELGI= 0x10;)| _// Acknowledge (6812 Solution 13.1. Part) Same as 6811 solution. Part b) Same as 6811 solution. Parte) The ULN2074 has sufficient output current to drive the motor. The motor curent will be SV/I0Q~0.5A. The 74H1C14 Schmoitrigger will clean up” the ugly wavefoum from the tachometer. The output on OCA/PTA is used to generate the pulse-width modulated actuator control. The input on ICY/PT1 wil use input capture to measure frequency. The desired frequency resolution is 100 Hz, which convers to a motor speed resolution of 1 ps. The Input Device is connected to ICO/PTO so that the ising edge of Realy can generate an ICO interupt 8 6gizPOnAD rs ceeeTy Ready 5 out Devied Done PT3 35 re + bre ocapr4,-—>— | 10Q Gee ings Motor 74HC14 ICLP} o<}+——_j Tachometer =] Put d) Ritual software Including data structures. The main program executes the ritual, then performs other unrelated tasks. Ie. all processing occurs under interrupt control unsigned short count; // raw count during frequency measurement unsigned shozt Rstaz7 // desixed motor speed in zpe bneigned short Ry 17 current rotor speed in zps unsigned snort uy // actuator command 100 to 39900 counts unsigned short low; // welow=40000, void ritual (vod) ( asm sei 1/ wake atonic RIDDIEN = OXF; // enable AD digital DDRAD = 02007 17 Port AD is desired speed Jonathan W. Valvano 149 108 |= 0x10; // enable ocd Tos s= ~0%03; W/ enable Ici, 100 DORI = 03385 77 BU, 223 are outpute DORE Gx ~0x037 7/ Snputs on ICL, TCO TSCRI = 0x80; 11 enable SCR? = 0x0 71 800 ns clock ME |= 0213; “1 Bem 160, TC1, OC4 Tea = ToNT+40000;// First ih 20 ms TCTLL = (TCTL1¢OxF0)10x01; // PT4 toggle on each interrupt TCTLA = (ICTLEcOxEO) 10x05; // ICOE, ICLE both set on rising edges count = 07 17 Set up for first = 100; 1] Anitianly off Retar = 17 Bet on the subsequent measurements TFLGI = Ox137 7/ clear ICOF, TCLE, OCA asm cli } Part @) Show the interrupt software. void interrupt 9 TClhandier (void) { count t+; /] munber of rising edges TEUG\ = 0x02;) // ack, clear IC1F void interrupt 8 TOhandler (void) ( PET I= 0x08; — // Done(PT3)=2 Rstar = PTAD: PET s= ~0x08; // done (PT3)=( TELGL = 0x01;) // ack, clear ICOF void interrupt 12 Tethandler(void) { long newur Sf (PTTEOx0a) ( U7 P14 is now high R= count/2; 1/ estimated speed 100 Hz units newa = u + 4*(2eRstaz = count); 2 (newu<100)¢ w= 1007 W) ynder slow ) elset Le newy>39900) ( y= 39990; // overfion } else( : } low = 49000-w: count = 0; // Setup for next Teds TCatu; // PFA is i for u cycles ) else { 1) 34 38 now lox Ch = BCaslow; // PTA is 0 for low cycles ) TELGI = 0x10; // Acknowledge b Solution 13.2. Let Xstar be the fixed-point desired value witha resolution of 0.01V. Le, ~500 $00) 1509. Le{r < ~800) t=-800; Jonathan W. Vaivano 150 Solutions Manual for Real Time Embedded Systems, 2™ Edition Solution 133. Ifthe controler is executed too infrequently, then the error will be very large because the controller ‘can not respond quickly enough to changes in the physical plant Ifthe controller is executed too frequently, then the system will revert to bang-bang, (6812 Solution 13.4 org $3800 ts mb 2 Desired speed in 3.9 zpm units ro mbt Current Speed in 3.9 rpm units Told mm 1 Previous Speed in 3.9 rpm unite Domb Change in Speed in 3.9 rpm/tine units 5 om i Error in Speed in 3.3 rpm units 7 input menbership variables furvari ds 07 inpute Fast: ds 1} speed too fast > as 1; speed oK ds 1} speed too siow a3 1 7 speed decreasing Constant: do 1 7 speed constant Up: ds 1} speed increasing fuzout: ds 0; outputs Increase: ds 1; add power to system sane: ds. 1; Jeave power ao se, Decrease: ds 1 7 subtract some power from system } input membership variebles relative offsets FAST: equ 0 7 speed too fast OK: equ 1 7 speed ok Beer oss eoeeey esc sae Dom equ 3? speed decreasing constawr: equ 2 7 speed constant oe: equ 5 7 speed increasing soutput membership variebles relative offsets INCREASE: equ 6 7 add power to systen i Same: equ 7; leave power as is \ DECREASE: equ § 7 subtract some pover from system dorisp outputs an: as. 1 2 main program variables Rate equ 2000; kHz controller rate org $4000 + format is Point, Point?2, Slopel, Slope? Stab: deb 0,108,0/13 Feast, slope 13+256/20 deb 108,148,13,13 ok deb 128,255/13,0 Slow ated: deb 0,108,0,29 Down, slope 13+256/20 dc 108,148,13,13 constant deb 128,255/13,0 7UP rules ‘de.b OK CONSTANT, SEE, SAME, SEE ; if ok and constant then same eb OK,UP, SEE, DECREASE, SFE; if ok and up then decrease Go.b FAST, CONSTANT, SEE, DECREASE, SEE ; if fast and constant then decrease eo.b FAST, UP, SEE, DECREASE, SFE |; if fact and up then decrease e.b OK,DOWN, SEE, INCREASE, SEE j if ok and down then increase cb SLOW, CONSTANT, SEE, INCREASE, SEE ; if slow and constant then increase dc. SLOW, DOWN, SFB, INCREASE, SEB’ ; if slow and down then increase acd $F addsingleton: deb 108,128,148 7128 subtracted, ~20,0,20 + Input ms M pronots seeeeasupteact#*Aeriatarsey Rega is 8 bit unsigned Reg ie @ bit unsigned M + output Regs is 8 bit signed equ 0 N-M bounded to -128 to #127 fed to 16 bit Jonathan W. Valvano 151 Subtract pshx Allocate tex stab u2602,X Set least significant byte of M16 clr M16,x" Pronote M to M6 tab Set least significant byte of Ni6 clza Promote N to Regben16 subd M16,X -N16-MI6 cpa #127 bgt Seei27 copa 4-128, bge bone set228 ldap 4-128 bea Done set12? dab #127 Done pulx Deallocare CalceD*tiseeesseeee + Inputs: globals 7s, 1, Told + outputs: globale £'p fold calceD ldaa te laa > bsr Subtract ;EeTs-7 stab E lasa 7 dab Toa bsr Subtract ;D=t-Teld stab D ldaa ‘staa Told Setup Told for next time stessssvatnitializet*heetsunenes * Inputs: none + outputs: none Initialize set #$20,7105 005 movb §$80,1SCRI ";enable, no fast clr nov §$02,7SCK2 7500n6 clk + Bnalog signal connected to PADO FRIDCTES Register ($0065) 7 7 0 Not impienented 7 § seco 4 conversions $$ SCAN=0 one set of 4 } 4 WoT =0 Single channel 3-0 CD-A =0000 tox addr EADO mov #$80,ATDCTL2 ; A/D pover up ele PORTS } Motor stopped movb $$FF,DDRB D/A connected to Port clr ® } actuator clr Told ov 4128, 76 + initial setpoint at 500 xem rts 2B clk timing sessebetsganpletseesnnstens » Enputs: none * outputs: Global I is A/D sample from channel 0, units 3.9 rpm Sample movb #980, ATDCTLS pStart A/D Loop brelr ATDSTAI+1, $01,Loop wait for first one, CcEO idseATDDRL itizet result stea 7 ste main: lds #54000 Jer initialize ; initiatize control: ldaa #520 ielear C5 Jonathan W. Valvano 152 Solutions Manual for Real Time Embedded Systems, 2™ Edition staa TIGL dd Tes addd grate pment in 1 ms sta 75. broly TELG1, #820," + wait for 1 me clock jsr Sample } measuze T jsr_calce } caleolate B, D dae & adda #128 ixeg A is speed 0 to 255 ade #s_tab lay #fozvar em caleulate £ast ren 7 calculate OK rem > ealeulate slow Adee D adda #126 preg A is acceleration 0 to 255 ade Fete nem 2 caleulate UP mem > caleulate CONSTAND nen, + ealevlate Com ldap #3 cloop: clr l,y* + clear INCREASE, SAME, DECREASE done d,cloop lox rules ley Afuzvar daa 45°F day #tuzout lax faddsingieton c dab 43 ( eaiv ter yd subb #128 stab ak change dab aN ppl incr decr — aadb Newson bee sett) clr underflow bra set incr addb N Neate bee sett) 4255 overflow N PORTS actuator output control ‘Solution 13.5. The complement creates the opposite of the input. So the logically complement is appropriate for fuzzy logic too. See table setw Input Cupar ‘S00, compiciely ise | SFP. completely ae $20, mos alse SDF, mostly tue ‘$80, hal true half ise | S7F, half ove hal ee ‘SPO, very ue SOF, very alse 'SFF, completely mae | $00, completely als output = 2000) ud = 10007) // anesczeset windup U= Up + vi ig(u<200) 0 = 200: J/ minimum duty cycle iz(u>49800) 0 = 49800; // maximum duty cycle High = U: 11 denote Low = 50000-High? 71 maintain High+ow always equal to 50000 » void interrupt 9 OChandler (voie) { ‘THLGL = 0x02; 1 ack CIF 4 (PTTEORO2) { 71 PIL 43 now high Controller()> TCL = TClHigh; // 1 for Righ eye i else ( // RTL 38 now low TCL = 7C1+t0w; —// 0 for Low cycles » Jonathan W. Valvano 156 Solutions Manual for Real Time Embedded Systems, 2™ Edition void interrupt 8 Ichandler (void) | Period = Te0-First; First = 100; // Setup for next TELS] = 0x01; // ack by clearing COE Speed = 200000L/Period? Tine = 07 , yoid interrupt 16 TOFHan (void) { THLG2 = 0x80; 1 Acknowledge by clearing TOF Tame+ts Lf(Tine>2)(_// period too big ‘Speed = 0; // motor stopped Time = 07 , i Extra Queston 13.12, The followin integral equation wil be sed to control the temperature of a presison oven ‘U(®)is the actuator command in watts. E(¢) is the measured error in °C. The constant 12 34 has units of watt/sec!*C. Ue) = 1234") E(t) dt ‘The digal state estimator will measre the error every 1 ms, creating a decimal-fixed point representation, e with a reolation of 001°C. The diatal actustor will urpu the acwator command every I'ms, also using a decimal-ixed point representation, (a), but with a resolution of 0 O01 watt Part a) What is the relationship between E(t) and e(n)? Part b) What is the relationship between U(t) and u(m)? Part ) Rewrite the controler equation in cial form using fixed-point math. Te, ealeulate w(n) a8 faction of (a), No software required, just the equation Extra Solution 13.12. Rewrite the controller equation in digital form using fixed-point math arta) B(*100 ~e{n) oF EG) = (0/100 Part b) U(t)*1000= u(n) or U(t)= u(ny/1000 Parte) U()= 12 344] E(O at U = 1234F EE*0 001566 (91000 = 1234*5,e(ny100"0 001see ‘u(n) = 0 1234*F e(m) u(n) = u(n-1)+ 0.1234* e(n) where u(0)=0 exact answer is 160) = ula 1254" e(n)/10000 where m()=0 approximate (012590 1234) answers ‘u)= w(aeI}+ (ny where (00 Two approaches to a problem “The hard way" "The easy way" WwW

You might also like