EE 712 Embedded Systems Design, Lab Project Report, EE Dept. IIT Bombay, April 2006. ANGULAR POSITION CONTROL OF DC MOTOR USING SHORTEST PATH ALGORITHM Group Number: 17 Rupesh Sonu Kakade (05323014) <rupeshsk@sc.iitb.ac.in> LtCdr S Saravanan (05323407) <sarva@sc.iitb.ac.in> Supervisor: Prof. P. C. Pandey Abstract Position control finds wide applications in industry, especially in aerospace, automotive and mechatronics applications. A feedback driven, proportional control for a 12 V DC motor was implemented using microcontroller AT 89C2051, to achieve fast response with no overshoot. The set point for the controller was given using 4 DIP switches. The position of the motor shaft was monitored using a Gray coded disc fixed to the shaft of the motor. A 4-bit feedback was provided by using an array of Infrared LEDs and photo transistors. The motor was controlled using the PWM technique. Variable speed, consequent to proportional control action, was achieved by varying the duty cycle of the pulses. The motor was driven using H bridge driver IC L 293 D. The program was written in Assembly Languauge of 8051. The program converted Gray code corresponding to the present position of the shaft to binary code, used it to compute the error and generated pulses with duty cycle proportional to the magnitude of error. An algorithm to enable the motor to take the shortest path to reach the set point was also implemented. 1. Introduction A DC motor position control finds wide applications in servo systems and mechatronics. The speed of a DC motor can be controlled by controlling the Armature Voltage or Field Voltage. The following are few of the simple methods of controlling a DC motor shaft position. (a) Open loop - without feedback of current position, method is ineffective and inaccurate in the presence of load disturbance. (b) On-off controller - motor is turned on with maximum torque till it reaches set point and switches off, this may result in overshoots and oscillations. (c) Single directional control - reaches set point in a single direction only, EE 712 Lab Project Report Page 1 of 15
resulting in larger settling time in 50% of the cases. The angular position of a DC motor can also be controlled by varying the torque generated by varying the armature voltage or field voltage. In most digital applications, PWM (Pulse Width Modulation) is generally used to control the speed of the DC motor. It is relatively easier to generate pulses of varying duty cycles with a microcontroller or microprocessor. The pulses with varying duty cycle when applied to the armature will result in variable torque proportional to the duty cycle. The control methodology used in this project is to apply a average voltage proportional to the error between actual position and the set point and reduce the average voltage as the current position approaches the set point. This kind of control is very effective in systems with high inertia as an inherent property, so that no control effort is essential when the set point is reached. 2. Problem statement The objective was to control the angular position of the DC motor using a microcontroller. The total angle of 360 degrees was equally divided into 16 sectors. The sectors were numbered from '0' to '15'. The set point or the desired position is conveyed to the microcontroller as a 4 bit number - binary equivalent of the sector number. The microcontroller should move the motor shaft to the desired angular position or sector with the following features. (a) (b) (c) (d) Using feedback data of current position By taking the shorter path With minimum settling time Without any overshoot 3. Design approach The control action had to achieve the set point when motor was working against a load. Hence, a 12 V DC, permanent magnet, geared motor was chosen for the project. The motor had a maximum speed of 15 RPM when full armature voltage of 12 V was applied across it. Microcontroller AT 89C2051 was used to control the motor shaft position. Since the total number of sectors was 16, shaft position could be represented with 4 bits of binary number. The desired sector was set with 4 DIP switches that were interfaced with the micro controller. The data of present shaft position was fed back to the controller using an array of 4 infra red (IR) LEDs and 4 Photo transistors. Optical sensor MOC 7811 was used to assemble the array of LEDs and sensors. The datasheet of the sensor is placed at appendix A. The biasing circuit for the IR LEDs (D0, D1, D2 and D3) and the sensors (S0, S1, S2 and S3) is shown in Fig. 2. The Gray coded disc, fixed to the motor shaft, was positioned between the LED array and the sensor array. As the motor shaft rotates IR light is blocked/allowed depending on the position of the shaft. Thus the position of the shaft is converted into Gray code and transmitted to the port 1 of the micro controller. EE 712 Lab Project Report Page 2 of 15
MICRO CONTROLLER DIP SWITCH PWM D0 D1 BIASING CIRCUIT H BRIDGE DC MOTOR CODED DISC Fig.1: General block diagram of angular position control of DC motor The program converts the Gray code received by the microcontroller to binary code and computes the offset/error from the set point. The microcontroller generates pulses of frequency 50 Hz. The duty cycle was varied from 95% to about 0% (as shown in Table 1) depending on the magnitude of the error. The microcontroller also generates two direction signals (A0 and A1) for taking the shortest path to reach the set point. The direction signals and pulses are given to the driver IC L293D which has an H Bridge circuit shown in Fig 3. The datasheet of L293D is placed at appendix A. The H Bridge was used to achieve bi- directional control. The driver circuit generates pulses of 12 V peak and at the same frequency as input. These pulses were applied to the motor s armature. T = Ton + Toff Ton Duty Cycle = Ton+ Toff EE 712 Lab Project Report Page 3 of 15
Fig.2: H-bridge circuit diagram Error (bitwise) Table 1: Look up Table for Timer registers value (Duty Cycle) Ton Percent Duty Cycle TH0 (in Hexadecimal) 0 0.0 ms 0.0 % (0.36 %) FF 99 1 4.0 ms 20.0 % F1 99 2 6.5 ms 32.5 % E8 99 3 9.0 ms 45.0 % DF 99 4 11.5 ms 57.5 % D6 99 5 14.0 ms 70.0 % CD 99 6 16.5 ms 82.5 % C4 99 7 19.0 ms 95.0 % BB 99 TL0 (in Hexadecimal) 4. Design of circuit Biasing Circuits for IR LED and IR Phototransistor: To detect the actual position of the shaft (and hence the encoder disk attached to the shaft) four pairs of Infrared (IR) LEDs and Photo transistors were used. Few specifications required for designing the biasing circuits are described below: IR LED: 1. Forward Current: 50 ma (max) Typical value: 20mA EE 712 Lab Project Report Page 4 of 15
2. Forward Voltage: 1.0 to 2.3 V Typical value: 1.2V 3. Radiant Power: 10mW 4. Wavelength of radiation: 740nm IR Phototransistor: 1. Dark Current: 100nA (max), 5nA (typ) 2. Light current: 0.4 to 1.24mA, 1mA (typical) 3. V CE (sat): 0.4 V (max), 0.15 V (typical) 4. Maximum collector current: 40mA 5. Response time: 6 microseconds. Based on the above data it was decided to design biasing circuits to have following specifications: IR LED: Forwards Current: 25 ma. IR Phototransistor: V CE (sat): 0.4 V (max) I C : around 0.5 to 1 ma. Fig. 3 IR LED and Sensor Biasing Circuit EE 712 Lab Project Report Page 5 of 15
For IR LED: Vcc Vf R = If 5 1.24 R = 20mA R = 150.4Ω Used standard value resistor of R = 150 Ω. With R = 150 Ω, I F = 25.067mA For IR Phototransistor: Vcc Vce( sat) R = Ic 5 0.2 R = 0.5mA R = 9600Ω Used a standard value resistor of R = 10 kω. With R = 10 kω, I C = 0.48 ma. 5. Algorithm The following algorithm was used to generate the assembly language program of the controller. Step 1 : Initialize the Microcontroller Port1 as input port Step 2 : Initialize the Timer 0 and Timer 1in mode 1 Step 3 : Set PWM output to low Step 4 : Read port 1. Lower nibble gives set point and upper nibble gives Gray code of current position Step 5 : Convert Gray code to binary code Step 6 : Compute error. Step 7 : Generate pulses of 50 Hz with duty cycle proportional to error at Pin 3.2 Step 8 : Generate direction signals (A0 and A1) at Pin 3.3 and 3.4. Step 9 : Go to step 4 6. Test procedure to show how the design achieves the requirement of the problem The microcontroller with the motor was initially tested without feedback. The current position was provided with DIP switches. For every set point, the microcontroller generated pulses of duty cycle proportional to the magnitude of error. This enabled the measurement of the speed of the motor for different values of error, i.e. the speed at EE 712 Lab Project Report Page 6 of 15
Fig 4: Array of four IR LED and Photo transistor pair which the controller will try to nullify error. Then the sensors were assembled and mounted. When biased, the assembly generated Gray code corresponding to the position of the motor shaft. Then various set points were set in the input DIP switches and after the motor settled in its final position, data was recorded. EE 712 Lab Project Report Page 7 of 15
Motor drive circuit: Direction Signal from Microcontroller, A0 PWM Pulses from Microcontroller + 5 V Pin 7 To Motor (Red) To Motor (Black) GND L293D Pin 6 GND Pin 3 GND Pin 2 + 12 V Direction Signal from Microcontroller, A1 Fig 5: Motor Drive Circuit 7. Test results The motor was found to track the set point in shortest path. The following test results were achieved with the feedback control. Current position : 1100b Set point : 0000b Settling time : 4.5 seconds Overshoot : 0 A step change to 1110 is given Current position : 1100b Set point : 1110b Settling time : 1 second Overshoot : 0 8. Discussion of the results The results obtained were satisfactory. The microcontroller was able to move the shaft to the desired position with no overshoot. For a change in a set point it always EE 712 Lab Project Report Page 8 of 15
followed the shortest path. Even when sensors were moved from their original position the disk was moved within no time, so that desired position got locked to the sensor position. 9. Conclusion and suggestions for further improvement The DC motor position control that was realized using AT 89C2051 microcontroller showed satisfactory results. The control system was shown to be effective for systems with high inherent inertia. An additional module to generate proportional plus integral control action instead of proportional only, needs to be developed so as to maintain the shaft at the desired position against load. 10. References [1] Jonathan W. Valvano. Embedded Micocomputer Systems - Real time interfacing, Thomson Brooks/Cole, 2005. [2] Muhammad Ali Mazidi, The 8051 Microcontroller and Embedded Systems, Pearson Education, 2005. [3] K.J. Ayala, "The 8051 Microcontroller: Architecture, Programming, and Applications", Penram International, 1996. [4] Fairchild Semiconductor Corporation. Phototransistor optical interrupter switch, 2001. Datasheet, available on the web at http://www.datasheet.in/datasheethtml/m/o/c/moc70p_fairchildsemiconductor.pdf.html. [5] Texas Instruments. Quadruple half H bridge driver, 1990. Datasheet, available on the web at http://www.datasheet.in/datasheet- html/l/2/9/l293dne_texasinst ruments.pdf.html. EE 712 Lab Project Report Page 9 of 15
Appendix A: Photographs of Various Assemblies Ph.1 Array of IR LEDs Ph.2 Array of Phototransistors EE 712 Lab Project Report Page 10 of 15
Ph. 3 Coded Disc of Shaft Encoder Ph. 4 Optical Shaft Encoder Assembly fixed to the Motor EE 712 Lab Project Report Page 11 of 15
Ph. 5 IR LED and Sensor Biasing Circuit Ph. 6 Microcontroller and Drive Circuit EE 712 Lab Project Report Page 12 of 15
Ph. 7 Full Assembly EE 712 Lab Project Report Page 13 of 15
Appendix B: Assembly Language Program org 0000h sjmp start org 0030h start: mov tmod,#11h ; Both timers are in mode 1 mov p3,#00h ; PWM output low mov p1,#0ffh ; Port 1 as a input port. Upper 4-bits = Gray, ; Lower 4-bits = Set point (Binary) clr a mov 20h,a mov 21h,a back: mov a,p1 ; Read both input and set point mov b,a anl a,#0f0h ; Mask upper 4-bits i.e., Gray code swap a acall conv ; Gray to binary conversion mov r2,a ; r2 = Y mov a,b anl a,#0fh ; Mask lower 4-bits i.e., Set point mov r3,a ; r3 = Ysp clr c subb a,r2 mov 00h,c anl a,#0fh clr c subb a,#08h jb 00h,noinv cpl c ; Move Sign bit to Bit addressable ; location 00h of internal RAM ; Check if error magnitude is more than 08h noinv: mov 08h,c mov a,20h xrl a,21h jnb acc.0,forward clr p3.3 setb p3.4 sjmp reverse ; For reverse direction (clockwise) forward: clr p3.4 setb p3.3 EE 712 Lab Project Report Page 14 of 15
reverse: mov a,r3 ; a = Ysp (Set point) mov b,r2 ; b = Y (Output) jb p3.3,norev ; Or we can use jb p3.4 xch a,b ; Do the reverse subtraction norev: clr c subb a,b anl a,#0fh ; pwm generator mov dptr,#duty movc a,@a+dptr ; Get the duty cycle data into th0 mov th0,a mov tl0,#99h ; tl0 found to be constant = 99h mov th1,#0b7h ; Timer 1 to set frequency of 50Hz mov tl1,#0ffh setb tr0 setb tr1 setb p3.2 again: jnb tf0,again clr tr0 clr tf0 clr p3.2 wait: jnb tf1,wait clr tr1 clr tf1 sjmp back conv: org 0200h mov dptr,#grtbi movc a,@a+dptr ret ; Gray to Binary conversion ; Look up table for Gray to Binary conversion org 0300h grtbi: db 00h,01h,03h,02h,07h,06h,04h,05h,0fh,0eh,0ch,0dh,08h,09h,0bh,0ah ; Look up table for timer 0 register th0 org 0400h duty: db 0ffh,0f1h,0e8h,0dfh,0d6h,0cdh,0c4h,0bbh end EE 712 Lab Project Report Page 15 of 15