Documente Academic
Documente Profesional
Documente Cultură
Extension Programs
Shibaura Institute of Technology Center for Lifelong Learning and Extension Programs
Preface
Recently, various topics on robot are frequently appearing in TV. Though people are increasingly interested in the robot, there is a little chance to learn how to make robots by themselves. Since Shibaura Institute of Technology (SIT) has experiences in the making of micro mouse robot, we developed the educational robotics workshop program using the new line tracing robot (S.I.T.-LTR01) with digital logic circuit for celebrating the 70th anniversary of Shibaura Institute of Technology in 1997. After this, we developed variety of robots and programs for the workshops. Based on these experiences, we began to develop the microprocessor controlled line tracing robot again in 2000, and developed the line tracing robot (S.I.T.-LTR02) in 2001. It was a robot with a simple structure for easy understanding of electronics, computer system and control mechanism, and the line tracing robot (S.I.T.-LTR03) was improved based on the experiences of the workshops over several times in 2002, and started a line tracing robot workshops every year. After this, robot has been improved and completed as the S.I.T.-LTR04 course. This lecture will use a line tracing robot that contains all three elements that make up robot Sensor, Microcomputer processing, Motor drive control. And this purpose is to get a deep understanding of the mechanism by making every student make a robot mechanism and electronics circuit as well as writing and debugging control programs to control the robot actually. The robot is composed of as few parts as possible for the purpose of understanding the principle of control program as well as the configuration of the circuit behavior. Therefore, you also will notice principle of the device you are using in the circuit. The one-chip microprocessor is used for computer which provides intelligence to the robot. The microprocessor contains all the features of the computers, and is an affordable material to understand the principle of operation of the computer and programming. We think you will have many difficulties in the workshop, however are confident that you will reach the confidence that I can control anything using a microcomputer by the end. Finally, we would express our gratitude for manual editing to many members of each laboratory including Mr. Koji Noda. July 25, 2005
Shibaura Institute of Technology, Center for Lifelong Learning and Extension Programs Robot Seminar Group Shibaura Institute of Technology, College of Engineering, Dept. of Electrical Engineering Human Robot Interaction Laboratory Robotics Laboratory Shibaura Institute of Technology Ftech Co.,LTD Professor Makoto Mizukawa Associate Professor Yoshinobu Ando Professor Emeritus Chie Kasuga Yasuo Ogawa
English version was prepared for UC Summer Session in S.I.T. in 2012 with the support from following professors.
Shibaura Institute of Technology, College of Design Engineering, Dept. of Design Engineering Robotics Laboratory Robot Task & System Laboratory Micro-Mechatronics Laboratory Human Robot Interaction Laboratory Professor Yoshinobu Ando Professor Takashi Yoshimi Associate Professor Tadahiro Hasegawa Professor/Dean, College of Engineering Makoto Mizukawa Shibaura Institute of Technology, College of Engineering, Dept. of Electrical Engineering
Table of contents
Chapter 1 Chapter 2 2.1 2.2 Chapter 3 3.1 3.2 Chapter 4 4.1 4.2 Introduction 1 Configuration of the line tracing robot kit 2 A circuit diagram, a printed circuit board, and a part list 2 Drawing aluminum bracket, figure aluminum mounting for cart 6 Assembly8 Printed circuit board assembly procedure 8 Parts mounting procedure23 Program development26 Port Assignments 26 Program development by assembly language27 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.3 4.3.1 4.3.2 4.3.3 4.3.4 4.4 4.4.1 4.4.2 4.4.3 4.4.4 4.4.5 4.5 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 Assembly 27 Flashing LED, switch operation 28 Straight, curve37 How to detect the line using the photo sensor 42 How to follow the line 43 Turn on and off LEDs 68
Sample program using assembly language68 Go straight 71 Line trace174 Line trace278 Flow of program development 86 Blinking of LED, Switch operation 86 Go straightTurn 92 Method of detecting the line by sensor 100 How to trace a line102 Blinking LED 110 Go straight 112 Line Tracefor beginners 113 Line Tracefor middle level 116 Line Tracefor advanced level 119
Chapter 1 Introduction
Background and features of production kit In 1997, the line tracing robot S.I.T.-LTR01 was developed to celebrate the 70th Anniversary of Shibaura Institute of Technology (SIT). Afterwards, we have been working on the development of various robots, such as multi-legged walking robot for the robot seminar. In 2001, we designed line tracing robot again with the S.I.T.-LTR02. From this experience, we continued to make S.I.T.-LTR03 in 2002. It was awarded the Good Design Award in 2003. After that, S.I.T.-LTR04 was developed with improvements to increase robots speed. S.I.T.-LTR series are designed with a minimum number of elements that make up the robot, so it can be easily assembled in a short time even by beginners and can be used as the material for introduction to microcomputer. Moreover, it carefully supports the creation of robot for beginners. For reference in case you cannot participate in the seminar, the content of the lectures is included in CD-ROM provided with this textbook. Kit Contents Main components of S.I.T.-LTR04: 1. Microcomputer board: 1x PIC16F84 (20MHz) microcomputer and peripheral circuits. 2. Detection sensor line: 3x LED and phototransistor pairs. 3. Gear motor: 1x double gear box. 4. Power supply circuit and battery box.
Chapter 2
2.1 Circuit diagram, printed circuit board (PCB), and part list
The following figures and table show configuration of S.I.T.-LTR04 robot kit. Figure 2.1: circuit diagram. Figure 2.2: PCB top layer. Figure 2.3: PCB bottom layer. Figure 2.4: PCB pattern (top layer). Figure 2.5: PCB pattern (bottom layer). Table 2.1: part list (bill of material BOM).
Figure 2.1
Figure 2.2
Figure 2.3
Figure 2.4
Figure 2.5
No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
NAME Monolithic Ceramic Chip Capacitors Electrolytic Capacitor Electrolytic Capacitor Diode Diode Transistor One-Chip Microcomputer LED(Green) LED(RED) IR LED Photo transistor Resistor(O,O,Bl,Bl,Br)* Resistor(Br,Bl,Bl,R,Br)* Resistor(O,O,Bl,Go,Br)* Resistor(B,G,Bl,Bl,Br)* Resistor(Br,Bl,Bl,Br,Br)* Resistor(O,Bl,Bl,R,Br)* Resistor(R,Bl,Bl,O,Br)* Mechatronics Key Switch Toggle Switch Pre-Set Variable Resistor Connector CERALOCK IC Socket DIP-18P Battery Holder Aluminum Chassis NabeM3-10 NabeM2-6 Brass Sara M3-6 Brass Nickel Nylon Spacer Caster circuit board
Table2.1 S.I.T-LTR04 BOM Component Part name arrangement number C2,C3,C5,C6,C7,C8C9 0.1 C4 C1 D1,D2 D3,D4 Q4,Q5 IC1 LED1 LED2,LED3,LED4 LED2,LED3,LED4 Q1,Q2,Q3 R8,R9,R10 R4,R19,R20 R6,R7 R1 R3,R11,R12,R13,R14 R2 R5 SW1,SW2 SW3 VR1,VR2,VR3 CN1 X1 10220(E) 50V10(E) 1SS133 11EQS03L Tr PIC16F84 LED LED TLRE180AP(F) TPS615(F) 100 10K 1K 680 1K 30K 200K B3F-1052 SW(POWER) 50K S5B-XH-A 20MHz IC Socket(18P) Battery Box Aluminum Chassis screw thread screw thread screw thread Spacer Dokodemo Caster Double Gear BOX Track Tire Set
Part number
Number 7 1 1 2 2 2 1 1 3 3 3 3 3 2 1 9 1 1 2 1 3 1 1 1
1SS133 11EQS03L 2SD2106 PIC16F84A-20/P GL3KG8 GL3PR8 TLRE180AP(F) TPS615(F) 100R0 1002 1001 6800 1001 3002 2003 B3F-1052
MC-304-3
1 1 4 2 2 4 1 1 1
33
F0278 NO.168
34 Tire NO.101 2 35 M2Nat 2 36 M3nat 6 37 3 Spring washer 4 Resister is 5-digit display, with tolerance of 1%. In case of 4-digit display, see p.150 of reference book [1]. *Resistor color code: Black Bl, Brown Br, Red R, Orange O, Blue B, Grey G, Gold Go,
2.2
Figure 2.6 shows the drawing of aluminum frame and figure 2.7 shows assembly drawing of S.I.T.LTR04.
Figure 2.6
Figure 2.7
Assembly drawing
Chapter 3
Assembling
(a)Enlarged view
<Characteristics > No polarity, no need to worry about orientation when assembling. Be careful with color code. Reference 1.
Figure 3.2
(a) Enlarged
<Characteristics > Cathode is marked by a yellow line. Be careful with the orientation of diodes.
Figure 3.4
(c) Light-emitting diode4pcs LED1 (power indicator) green1pcs LED5~7 (sensor indicator) red3pcs
<Characteristics > By lead length: the longer lead is +, the shorter lead is -. By electrode size: the smaller one is +, the larger one is -. Checking with multimeter: when the minus rod (black) is attached to LEDs anode (+) and the plus rod (red) is attached to LEDs cathode, multimeters needle will swimg.
Figure 3.6
(d) Multilayer ceramic capacitor 7pcs C2, C3, C5, C6, C7, C8, C9 0.1[F]7pcs
Figure 3.7
<Characteristics > Function: eliminating the noise from motors and other parts. No polarity, no need to worry about direction when assembling.
Figure 3.8
(a)Enlarged view
<Characteristics > Fit the Notch in socket and the Notch in schematic symbol
Time-saver
1. When soldering a socket, firstly fix it with some tape (Figure 3.10). 2. Temporary solder from pin 1 to pin 10 (Figure 3.11). 3. With your finger pressing the socket, use the soldering iron to thoroughly melt the solder at each pin.
12
20[MHz]1pcs
<Characteristics > GND is the center pin. No polarity, no need to worry about orientation when assembling.
Figure 3.14
13
No polarity, no need to worry about orientation when assembling. Press the switchs shoulders to plug it in a parallel basis(3.15(a)).
Figure 3.16
<Characteristics > Resistance value changes from 0[k] to 50[k] by turning the knob in the center. Mounting is uniquely determined because of special pin arrangement. <Tips for mounting> Solder one pin first to fix the parts position and keep it from floating. Then solder the last two pins when position is fixed.
Figure 3.18
15
<Characteristics > Attach + of part to + on schematic symbol. In actual capacitor, the polarity of - marked lead is minus. In case of no - mark found, the polarity of longer lead is + .
C1C4
16
2SD2106 or 2SD560
E C B
B C E
<Characteristics> From the front, left to right, the order of three leads is B (base), C (collector), and E (emitter).
Q4Q5
17
Switchs
terminals
(c) Position
(l) S5B-XH-A downloader connector 1pcs CN1 1pcs Caution) This connector must be soldered to PCBs bottom layer.
18
(m) Phototransistor 3pcs Q1, Q2, Q3 3pcs Caution) Phototransistors Q1, Q2, Q3 must be soldered to PCBs bottom layer.
The longer The longer l d The shorter l d Caution! Emitter Collector Solder firmly to PCB. PCB lead Emitter E The shorter lead C Collector
<Characteristics> Phototransistors receive reflecting light from the floor. Be careful with the polarity of emitter and collector.
Q13
Solder layer. to bottom
Caution!
Push all the way down to PCB. Solder to bottom layer.
19
(n) Red LED 3pcs LED2, LED3, LED4 3pcs Caution) LED2-4 must be soldered to PCBs bottom layer.
The shorter lead Cathode The longer lead The shorter lead Caution! Anode Cathode Solder firmly to PCB. K A PCB K The longer lead A Anode
<Characteristics> Emit red light to the floor under robot. Be careful with the polarity of anode and cathode.
LED24
Solder to bottom layer.
Caution!
Push all the way down to PCB. Solder to bottom layer.
20
Vcc
GND
Solder Thread wires the Hole
Red wire
(a)
Fig 3.29 Battery box Caution) Thread the wires through the hole near battery connection points before soldering.
BATTERY
Fig 3.30
21
Motors wiring Perform wiring and solder motor power wires to PCB as below. Be careful with colors and polarity of the wires. Sometimes it is necessary to widen the hole for easier wire threading.
+
Wires
Thread wires through this hole
PCB
Black wire
Red wire
Solder
Hole
1 Fig 3.32
22
3.2 Parts assembly procedure This section describes the assembly procedure of robots body. After soldering all parts to PCB, attach gearbox and PCB to the provided aluminum frame as in Fig 3.33.
For C typeDouble gear boxGear ratio of 114.7:1 Wheel diameter 36mmTamiya TRUCK TIRE SET
Fig 3.33 Assembly procedure (1) Then attach battery box to robots frame as in Fig. 3.34.
Fig 3.34 Assembly procedure (2) Wiring of motors should perform as shown in Fig 3.35. Be sure not to mistake wiring on motor.
23
24
25
A7
A6
A5
A4 Start Input
A3
A2
A1
Writer Writer Right LED Middle LED Left LED Right sensor Middle sensor Left sensor Input 1 Input 1 Output 0 Output 0 Output 0 Input 1 Input 1 Input 1
Right sensor Middle sensor Left sensor Right sensor Middle sensor Right sensor Middle sensor Right sensor Middle sensor Right LED Middle LED Left sensor Left LED Left sensor Left sensor
26
Right - leaning slightly Somewhat to the right Right - leaning Left-leaning Derailment Impossible Impossible To the left To the right Derailment processing Straight Straight
27
4.4
4.4.1
In this section you will learn about programming of the line tracing robot LTR-04 using C language. The detailed description in assembly language cannot be done in C language. However, C language can describe similar function in just a few lines of code. In addition, it is easier to describe high level operation such as math expression or algorithm in C than in assembly. Development of a program in C language (C program) is done in following order. First of all, C program is created by using an editor with personal computer. Then the C program is converted into machine program composed of machine codes that can be understood and executed by microcomputer. The set of software to convert a C program into machine codes includes compiler, assembler, and linker. Fig.4.30 shows the process to compile and build an executable program from C program with pre-processor, compiler, assembler, and linker. C program Eg.Pre-processor (# directives are processed) C program without directives Compiler (C language assembly language) Assembly program Assembler (assembly language machine code) Machine code program Linker (Machine code and libraries are linked) Executable machine code program Fig.4.30 Flow of making executable program from C program
4.4.2
(a)
Figures 4.31(a) and (b) shows the hardware for LED driving.
28
0V(Low)
Below is sample program to control the three LEDs connecting to bit 3, bit 4, and bit 5 of PORTB. When the program is executed, all LEDs turn on and off every 0.5 second.
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 0 0 0 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 1 1 1
/************************************************************/ /* /* #include <pic.h> __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ wait0(short k) { /* wait time about (k0.01 sec.) */ short i; short j; /* declaration of 16 bit variables */ for(j=0;j<k;j++){ /* (k3000) times iteration */ All LEDs blink in every about 0.5 second (C language) 2004.8.20 by AND */ */
/************************************************************/
29
14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. } } main(void) { } }
for(i=0;i<3000;i++){ }
TRISA = 0xFC; /* A0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LED output, other bits input */ while(1){ /* infinite loop */ /* LEDs off */ /* wait time = 0.5 sec. */ /* LEDs on */ /* wait time = 0.5 sec. */ PORTB=0x00; wait0(50); PORTB=0x38; wait0(50);
Description of LED blinking program #include <pic.h> Header file pic.h contains setting definition of PIC microcontroller. __CONFIG(0xFFFA); As initial setting, configuration bits (1)~ (4) are set up as below. 1Code protection is turned off. 2Power up timer is turned off. 3Watch-dog timer is turned off. 4High-speed oscillator mode is selected. (420MHz) wait0(short k) Sub-routine to do nothing but to wait for a specified period of time. For example, wait0(20) means do nothing in 0.2 sec. short i,j; Declaration of two 16 bit variables. for(i=0;i<3000;i++){} 3000 iterations. main(void) Main function declaration. This is the function that will be executed first. TRISA = 0xFC; Configuration of PORTA: Bit 0, 1 are set to output, bit 2~4 bit are set an input. TRISB = 0xC7; Configuration of PORTA: Bit 0, 1, 2 are set to output, bit 3, 4, 5 are set to input. while(1){ Definition of infinite loop within two brackets. PORTB=0x00; All PORTB bits are off. 30
wait0(50); Wait for 0.5 sec. PORTB=0x38; Bit 3, 4, 5 of PORTB on, all LEDs on. wait0(50); Wait for 0.5 sec. The flow charts of the typical syntax in C language, (1) for statement, (2) while statement, and (3) if statement are shown below.
1while statement
3if statement
The two lines of code above set the operational input/output setting of PICs PORTA and PORTB. Detailed setting is shown in Figure 4.33 below.
Input for Switch Input for Sensor(L) Input for Sensor(C) Input for Sensor(R) Output for LED(L)
If input is defined as 1 and output is defined as 0, input and output configuration of PORTB and PORTA are specified as below.
A7
A6
A5
A4 START Input 1
A3
A2
A1
A0 Use In Out
Value
Output 0
B0 Left Sensor Input 1
B7 Writer Input 1
B6 Writer Input 1
For PORTA, the remaining pins are set to input. For PORTB, two remaining pins RB6 and RB7 are used by the program writer (downloader), so they are all set to input. Below are configuration values of each PORT and their corresponding values in hexadecimal. (Value in hexadecimal: FC)
(Value in hexadecimal: C7) In C language, hexadecimals are prefixed with 0x. Moreover, PORTA and PORTB input/output configuration are controlled by TRISA and TRISB. When setting port configuration, the above hexadecimal values are written to TRISA and TRISB. Also, semicolon ; is used to end C language statements. Therefore, C codes for port configuration for PIC microcontroller are as below: TRISA=0xFC; TRISB=0xC7;
32
LED blinking example2It is both-ends blink every 0.5 second Below is program to blink all three LEDs in a different pattern. When the program is executed, LEDs
blinks as below. LED (PORTB) LED (PORTB) Middle LED on Side LEDs on ; ;
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 0 0 1 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 1 0 1
*/ */
/************************************************************/ /* /* #include <pic.h> __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ wait0(short k) { /*Wait time about (k0.01 sec.) short i; short j; /* Declaration of 16 bit variables */ for(j=0;j<k;j++){ /*(k3000) times iteration */ for(i=0;i<3000;i++){ } */ All LEDs blink in every about 0.5 second (C language) 2004.8.20 by AND
/************************************************************/
33
(b)
Figure 4.34 (a) and (b) shows hardware circuit and logic to handle push button.
Example: the following C code waits until the push-button switch connected to RA4 is pushed. After that the next line of code will be executed.
5V
5V
PIC16F84A
switch
switch
Button pushed
34
4.4.3
Motor drive Figure 4.35 shows the hardware for motor driving. In Figure 4.35 (a), when high level voltage (5V) is output from PORT, motor rotates. In Figure 4.35 (b), when low level voltage (0V) is output from PORT, motor stops. To output high level voltage (5V), set PORT value to 1: To output low level voltage (0V), set PORT value to 0:
5V If PORT= 1, 5V(High) Rotation PIC16F84A switch on Motor M If PORT= 0, 0V(Low)
PORT = 1; PORT = 0;
5V Motor Not M Rotation
PIC16F84A
Transistor
Transistor
switch off
Fig.4.35 Hardware and circuit operation for motor driving Principal of the motor speed control 1. Controlling motor speed In Figure 4.36, when motor is turned ON and OFF repeatedly in short period of time as shown on the left, the average current flowing through it can be seen on the right. Therefore, by changing the percentage of ON and OFF period, it is possible to control the speed of motor.
ON ON ON ON
Average of ON Level
fast
OFF OFF OFF OFF
ON
ON
ON
ON
Average of ON Level
middle
OFF OFF OFF OFF
ON
ON
ON
ON
Average of ON Level
slow
OFF OFF OFF OFF
2. Turning left at short radius curvature By increasing the ON period of the right motor as in Figure 4.37, it is able to control robot to make a sharp turn left at short radius curvature. (Switching of the right motor)
ON ON ON
ON
Fig.4.37 Switching periods of left and right motors (Turning left at the short radius curvature) 3. Turning left at long radius curvature In a program that turn left in the long curvature radius, increase the straightness of the robot compared to that in Fig.4.37 by approximating the ON time of the two motors as Fig.4.38. As a result, robot makes a left turn at the curvature radius long.
ON
ON
ON
ON
ON
Fig.4.38 Switching periods of left and right motors (Turning left at the long radius curvature) 4. Go straight By applying the same switching period on both two motors, robot will go straight.
ON ON
ON
ON ON
ON
36
Motor Driving (1) Going straight The program that controls robot to go straight is described below. As shown in Figure 4.40, left and right motors are connected to bit 1 and bit 0 of PORTA. Current through both motors are switched on at the same time. Therefore, the robot goes straight. PORTA bit 0 (Switching of right motor)
ON ON ON ON
ON ON
ON
ON
for(i=0;i<3;i++){
37
Motor Driving (2) Going straight slowly The program that controls robot to go straight is described below. As shown in Figure 4.40, left and right motors are connected to bit 1 and bit 0 of PORTA. Current through both motors are switched on at the same time. Therefore, the robot goes straight. PORTA bit 0 (Switching of right motor)
ON ON
ON ON
for(i=0;i<3;i++){
38
Motor driving3 Right side wheel drivingCCW turning with very short curvature radius The program below controls robots right side wheel. As in Figure 4.42, electrical current is applied at a constant frequency only to the right motor which is connected to bit 0 on PORTA. No current is applied to the left motor. As a result of this program, robot turns left along an arc with very short curvature radius.
ON ON ON
Fig4.42
39
Motor driving4 Left side wheel drivingCW turning with very short curvature radius The program below controls robots left side wheel. As in Figure 4.43, electrical current is applied at a constant frequency only to the left motor which is connected to bit 1 on PORTA. No current is applied to the right motor. As a result of this program, robot turns right along an arc with very short curvature radius.
ON
ON
ON
Fig4.43
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. } } main(void){ } }
Switching of right and left motorCW turning with very short curvature radius
/*****************************************************/ /* motor drive program (C Language) /* /* rotate left 2004.8.23 motor and stop right motor by AND */ */ */
/*****************************************************/ #include <pic.h> __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ wait00(short k) { /* Wait time (k0.01) msec.*/ short i; short j; /* declaration of 16 bit variable */ for(j=0;j<k;j++){ /* repeat (k3) times */ for(i=0;i<3;i++){ } return 0;
/* ---- rotate left motor and stop right motor ---- */ /* A0,1:output, 2,3,4:input */ /* B0,1,2:input, B3,4,5:LED output, other bits input */ /* motor off */ /* infinite loop */ /* left motor on */ /* wait time 0.30 sec. */ /* both motor off */ /* wait time 0.70 sec. */
TRISA = 0xFC; TRISB = 0xC7; PORTA=0x00; while(1){ PORTA=0x02; wait00(30); PORTA=0x00; wait00(70);
40
Motor driving
The program below controls both motors of robot. As in Figure 4.44, electrical current is applied at a constant frequency only to both motors. Speed of the left motor is slowed down by applying a smaller current and the robot turns left around an arc with short radius of curvature.
ON
ON
ON
Fig. 4.44 Switching pattern for both motors Turn left around an arc with short radius of curvature
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. } } } main(void){ /* ---- left_down ---- */ /* A0,1:output, 2,3,4:input */ /* B0,1,2:input, B3,4,5:LED output, other bits input */ /* Motor OFF */ /* Infinite loop */ /* both motor on */ /* 0.15msec wait */ /* right motor on */ /* 0.12msec wait */ /* both motor off */ /* 0.73msec wait */ TRISA = 0xFC; TRISB = 0xC7; PORTA=0x00; while(1){ PORTA=0x03; wait00(15); PORTA=0x01; wait00(12); PORTA=0x00; wait00(73); } return 0; wait00(short k) { /* Wait time about (k0.01)msec */ short i; short j; /* 16 bit variables declaration */ for(j=0;j<k;j++){ /* repeat (k3) times */ for(i=0;i<3;i++){ } /*********************************************************************/ /* /* /* Motor drive program (C language) Turn left around an arc with short radius of curvature 2004.8.23 by AND */ */ */
41
Motor driving
The program below controls both motors of robot. As in Figure 4.45, electrical current is applied at a constant frequency only to both motors. Speed of the left motor is slowed down by applying a smaller current and the robot turns left around an arc with long radius of curvature.
ON
ON
ON
ON
ON
Fig. 4.45 Switching pattern for both motorTurn left around an arc with long radius of curvature
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. } } main(void){ /* ---- small_left_down ---- */ /* A0,1:output, 2,3,4:input */ /* B0,1,2:input, B3,4,5:LED output, other bits input */ /* Motor OFF */ /* Infinite loop */ /* both motor on */ /* 0.19msec wait */ /* right motor on */ /* 0.12msec wait */ /* both motor off */ /* 0.69msec wait */ TRISA = 0xFC; TRISB = 0xC7; PORTA=0x00; while(1){ PORTA=0x03; wait00(19); PORTA=0x01; wait00(12); PORTA=0x00; wait00(69); } } return 0; wait00(short k) { /* Wait time about (k0.01) msec */ short i; short j; /* 16 bit variable declaration */ for(j=0;j<k;j++){ /* repeat (k3) times */ for(i=0;i<3;i++){ } /*******************************************************************/ /* /* /* Motor drive program (C language) Turn left around an arc with long radius of curvature 2004.8.23 by AND */ */ */
42
5V
5V Photo Transistor
5V
5V Photo Transistor
PIC16F84A
PIC16F84A
Photo Diode
Photo Diode
Fig.4.46 Hardware structure and circuit operation of line detecting sensor o Displaying states of sensors Below is a program for displaying states of line detecting sensors. Bits 3, 4, 5 of PORTB are output to LEDs. Bits 0, 1, 2 of PORTB are used for sensor input. Therefore, input values of bits 0, 1, 2 are used to drive the LEDs 0, 1, 2. In order to indicate input number of the sensor by using LED, the assignment expression of variable of PORTB is written in the program. If a sensor detects white (reflection), it returns value 1 and the corresponding LED will be turned on accordingly.
PORTB Bit4; middle LED PORTB Bit1; middle sensor PORTB Bit2; right sensor
43
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35.
/**************************************************************/ /* A program to indicate the states of the sensor */ /* 2002.8.18by AND */ /**************************************************************/ #include <pic.h> __CONFIG(0xFFFA); /* Initial settings CP:OFF,PT:OFF,WT:OFF,HS */ #define T_MAX 30 /* Turn on the motor every 300msec in a cycle */ #define COUNT 3 /* Iteration Count */ wait0(short k) { /* Wait time about (k0.01) sec */ short i; short j; /* 16 Bit Variable declaration */ for(j=0;j<k;j++){ /* repetition of (k3000) times */ for(i=0;i<3000;i++){ /* wait */ } } } main(void) { TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTA=0x00; /* Turn off the motor */ PORTB=0x00; /* All LEDs off */ while(1){/* Infinite loop */ RB3=RB0; /* Left sensor Left LED*/ RB4=RB1; /* Middle sensor Middle LED*/ RB5=RB2; /* Right sensor Right LED */ wait0(3); /* Wait about 30msec */ } }
o Motor driving by using sensor states The program to drive the motor is as follow. When the right sensor detects white (reflection), the right motor is driven. When the left sensor detects white (reflection), the left motor is driven. Otherwise, motor is not driven. PORTA Bit1; The left motor is driven PORTB Bit0; the left sensor detects white (reflection) PORTA Bit0,1; The right and left motor are drivenPORTB Bit1; the middle sensor detects white (reflection) PORTA Bit0; The right motor is driven PORTB Bit2; the right sensor detects white (reflection)
44
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45.
/*************************************************/ /* A program to drive the motor by using states of the sensor */ /* #include <pic.h> __CONFIG(0xFFFA); #define T_MAX 30 #define COUNT 3 wait0(short k) { /* Wait about (k0.01)sec */ short i; short j; /* 16 Bit; Variable declaration */ for(j=0;j<k;j++){ /* repetition of (k3000) times */ for(i=0;i<3000;i++){ wait } } } main(void) { TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTA=0x00; /* MOTOR OFF */ PORTB=0x00; /* LED ALL OFF */ while(1){/* infinite loop */ RB3=RB0; /* Sensor input LED display */ RB4=RB1; /* Sensor input LED display*/ RB5=RB2; /* Sensor input LED display */ if(RB0==1){ PORTA=0x02; } else if(RB2==1){ PORTA=0x01; } else if (RB1==1){ PORTA=0x03; } else{ PORTA=0x00; } } } /* if the other case */ /* then both motor off */ /* if center sensor on */ /* then both motor on */ /* if right sensor on */ /* then right motor on */ /* if left sensor on */ /* then left motor on */ /* Initial settings CP:OFF,PT:OFF,WT:OFF,HS */ /* Turn on the motor every 300msec in a cycle */ /* Repetition Times */ 2004.8.24 by AND */ /*************************************************/
45
4.4.5
Line tracing
Concept of state transition As shown in Figure 4.47, the state of robot will change by the value of its sensors. In this way, the changing state of the robot is called state transition. You must consider the difference between states of this robot to design a line tracing program. In the worst case, if robot is out of line, values of three sensors are (white, white, white). In this case, state of robot will change in deviated direction from the line. If robot is out of line, it can return to the line by considering previous states.
Yes
Go straight No Fig.4.48 Flow chart when robot is not tilted against the line As shown in Figure 4.48, if robot is not tilted against the line, the robot will go straight. 46
Yes
Fig.4.49
Description of this case like figure 4.49 and 4.50 are shown below. Each pin RB0, RB1 and RB2 of PORTB is connected with a sensor of Left, Center and Right. If floor is white, return value is 1. If floor is black, return value is 0. When each sensor of Left, Center and Right detects (Black, Black, white) = (0, 0, 1), robot is considered to be slightly tilted right. So in order to be back on the line, robot must turn left along arc in long radius of curvature by slightly slowing down its left motor.
Fig4.50 Movement of robot when robot is tilted to a little right from the line.
47
Yes
Turn left quickly No Fig4.51 Flow chart when robot is tilted heavily to the right
Description of this case like figure 4.51 and 4.52 are shown below. Each RB0, RB1 and RB2 is PORT connected sensor of Left, Center and Right. If floor is white, return value is 1. If floor is black, return value is 0. When each sensor of Left, Center and Right detects (Black,White, white) = (0, 1, 1), robot is considered to list to the large right from line. Then to run on the line, robot must be turned left along arc in short radius of curvature by large slowing down of left motor.
Fig 4.52
Movement of robot when robot lists to right hard from the line.
48
Whether the sensor is (White,White,White), and robot turned left the last time
Yes
Turn left quickly No Fig 4.53 Process of the case of a robot is out of the line when robot is tilted right
Description of this case like figure 4.53 and 4.54 are shown below. Each RB0, RB1 and RB2 is PORT connected sensor of Left, Center and Right. If floor is white, return value is 1. If floor is black, return value is 0. When each sensor of Left, Center and Right detects (white, white, white) = (1, 1, 1) and robot turned left the last time, robot is considered to list to the large right from line. Then to run on the line, robot must be turned left along arc in short radius of curvature by large slowing down of left motor.
1
and
Fig 4.54
49
8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60.
#define STRAIGHT 0 __CONFIG(0xFFFA); int last_time; wait00(short k) { /* wait time about (k 0.01) msec. (k0.01msec wait)*/ short i; short j; for(j=0;j<k;j++){ } } return 0; } int left_down(void){ PORTA=0x03; wait00(3); PORTA=0x01; wait00(18); PORTA=0x00; wait00(79); } int small_left_down(void){ PORTA=0x03; wait00(5); PORTA=0x01; wait00(12); PORTA=0x00; wait00(83); } int right_down(void){ PORTA=0x03; wait00(3); PORTA=0x02; wait00(18); PORTA=0x00; wait00(79); } int small_right_down(void){ PORTA=0x03; wait00(5); wait00(12); wait00(83); } int straight(void){ /* both motor on */ /* 0.05msec wait */ /* 0.12msec wait */ /* 0.83msec wait */ /* both motor on */ /* 0.03msec wait */ /* left motor on */ /* 0.18msec wait */ /* both motor off */ /* 0.79msec wait */ /* both motor on */ /* 0.05msec wait */ /* right motor on */ /* 0.12msec wait */ /* both motor off */ /* 0.83msec wait */ /* both motor on */ /* 0.03msec wait */ /* right motor on */ /* 0.18msec wait */ /* both motor off */ /* 0.79msec wait */ /* declaration of 16 Bit variables /* (k 3) times repetition */ */ /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ /*A flag to retain the former state when robot cant detect the line */
for(i=0;i<3;i++){
50
61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. } } }
int led_sens(void) { RB3=RB0; /* sensor inputLED indicate */ RB4=RB1; /* sensor inputLED indicate */ RB5=RB2; /* sensor inputLED indicate */
int main(void) { TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTB = 0; PORTA = 0; while(RA4==1){ led_sens(); } while(1){/* infinite loop */ led_sens(); if(RB0==1 && RB1==1 && RB2==1 && last_time==LEFT_DOWN){ left_down(); } else if(RB0==1 && RB1==1 && RB2==1 && last_time==RIGHT_DOWN){ right_down(); /*"White White White" and "RIGHT_DOWN last time"*/ last_time=RIGHT_DOWN; } else if(RB0==0 && RB1==0 && RB2==1){ /* Black Black White */ small_left_down(); last_time=LEFT_DOWN; } else if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); last_time=LEFT_DOWN; } else if(RB0==1 && RB1==0 && RB2==0){ /* White Black Black */ small_right_down(); last_time=RIGHT_DOWN; } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); last_time=RIGHT_DOWN; } else{ straight(); last_time=STRAIGHT; } } /* The other case */ /* go to straight */ /* turn right */ /* turn right a little */ /* turn left */ /* turn left a little */ /* turn right */ /*"White White White" and "LEFT_DOWN last time"*/ /* turn left */ last_time=LEFT_DOWN; /* PORTB clear */ /* PORTA clear */
last_time=STRAIGHT;
51
START
Is sensor And,was the last time CCW (last_time=LEFT_DOWN?) NO Is sensor And,was the last time CW (last_time=RIGHT_DOWN?) NO Is sensor value NO Is sensor value NO Is sensor value NO Is sensor value NO YES YES YES YES
YES
Go straight last_time=STRAIGHT Sensors values are display from left to right. : sensor detects black. : sensor detects white. For example, ( ) indicates left and middle sensors are detecting white surface, right sensor is detecting the black line. When running to CCW direction, it is "last_time = LEFT_DOWN". When running to CW direction, it is "last_time = RIGHT_DOWN". When Go straight, it is "last_time = STRAIGHT". This is to remember the latest states.
Fig. 4.55 Flow chart of line trace programwith error recovery 52
4.5
4.5.1
Example Program of blinking LED Blinking one by one every 0.5 seconds A program which blinks LED connected to bits 3, 4, 5 of PORTB one by one is shown. After running
the program, the LED will blink as follows. LED (PORTB) LED (PORTB)
left LED on middle LED on right LED on
LED (PORTB)
RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 0 0 1 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 0 1 0 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 1 0 0
about 0.5 seconds of LEDC language */
/*************************************************/ /* /* #include <pic.h> __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ blinking one by one every 2004.8.20 by AND */
/*************************************************/
/* wait time about (k 0.01) sec. */ short i; short j; /* declaration of 16 Bit variables */ for(j=0;j<k;j++){ /* (k 3000) times repetition */ for(i=0;i<3000;i++){ }
TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTA=0x00; while(1){ PORTB= PORTB= PORTB= /* motor OFF */ /* infinite loop */ ; ; ; /* left LED on */ /* middle LED on*/ /* right LED on*/
wait0(50); /* wait 0.5 seconds */ wait0(50); /* wait 0.5 seconds */ wait0(50); /* wait 0.5 seconds */
53
Example Program of Blinking LED All blinking quickly every 0.2 seconds A program which blinks LEDs connected to PORTB quickly is shown. After running the program, the
LED will blink as follows. LED (PORTB) all LEDs on all LEDs off
LED (PORTB)
-----repetition-----wait time 0.5 sec. wait0(50); /* wait time 0.5 sec. */ wait time 0.2 sec. wait0(10); /* wait time 0.2 sec. */
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. /*************************************************/ /* blinking quickly every 0.2 seconds of LEDC language */ /* 2004.8.20 by AND */ /*************************************************/ #include <pic.h> __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ wait0(short k) { /* wait time about (k 0.01) sec. */ short i; short j; /* declaration of 16 Bit variables */ for(j=0;j<k;j++){ /* (k 3000) times repetition */ for(i=0;i<3000;i++){ } } } main(void) { TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTA=0x00; /* motor OFF */ while(1){ /* infinite loop */ PORTB=0x00; /* LED off */ wait0(20); /* wait time 0.2 sec. */ PORTB=0x38; /* LED on */ wait0(20); /* wait time 0.2 sec. */ } }
54
4.5.2
Go straight
A program driving robot go straight slowly is shown. Electric current is applied to both motors at the same time as in Figure 4.56. Robot will go straight slowly by reducing the amount of current.
ON ON
ON ON
55
4.5.3
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. } } } }
/*************************************************/
int left_down(void){ PORTA=0x03; /* both motor on */ wait00(3); wait00(18); wait00(79); /* 0.03msec wait */ /* 0.18msec wait */ /* 0.79msec wait */ PORTA=0x01; /* right motor on */ PORTA=0x00; /* both motor off */
int right_down(void){ PORTA=0x03; /* both motor on */ wait00(3); wait00(18); wait00(79); /* 0.03msec wait */ /* 0.18msec wait */ /* 0.79msec wait */ PORTA=0x02; /* left motor on */ PORTA=0x00; /* both motor off */
int straight(void){ PORTA=0x03; /* both motor on */ wait00(20); wait00(80); /* 0.20msec wait */ /* 0.80msec wait */ PORTA=0x00; /* both motor off */
int led_sens(void) { RB3=RB0; /* sensor inputLED indicate */ RB4=RB1; /* sensor inputLED indicate */ RB5=RB2; /* sensor inputLED indicate */
56
51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75.
} int main(void) { TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTB = 0; PORTA = 0; while(RA4==1){ led_sens(); } while(1){/* infinite loop */ led_sens(); if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); } else{ straight(); } } } /* The other case */ /* go to straight */ /* turn right */ /* turn left */ /* PORTB clear */ /* PORTA clear */
last_time=STRAIGHT;
(1) (2)
(3)
if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); /* turn left */ }
(4)
else{
(5)
}
straight();
(6)
57
(6)
(3)
YES
Turning CCW
Turning CW
Go straight
58
4.5.4
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50.
/*************************************************/ /* line trace program (C language) /* for middle level #include <pic.h> __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ wait00(short k) { /* wait time about (k 0.01) msec. (k0.01msec wait)*/ short i; short j; /* declaration of 16 Bit variables */ for(j=0;j<k;j++){ /* (k 3) times repetition */ for(i=0;i<3;i++){ } } return 0; } int left_down(void){ PORTA=0x03; /* both motor on */ wait00(3); wait00(18); wait00(79); } int small_left_down(void){ PORTA=0x03; /* both motor on */ wait00(5); wait00(12); wait00(83); } int right_down(void){ PORTA=0x03; /* both motor on */ wait00(3); wait00(18); wait00(79); } int small_right_down(void){ PORTA=0x03; /* both motor on */ wait00(5); /* 0.05msec wait */ /* 0.03msec wait */ /* 0.18msec wait */ /* 0.79msec wait */ PORTA=0x02; /* left motor on */ PORTA=0x00; /* both motor off */ /* 0.05msec wait */ /* 0.12msec wait */ /* 0.83msec wait */ PORTA=0x01; /* right motor on */ PORTA=0x00; /* both motor off */ /* 0.03msec wait */ /* 0.18msec wait */ /* 0.79msec wait */ PORTA=0x01; /* right motor on */ PORTA=0x00; /* both motor off */ 2004.8.24 */ by AND */
/*************************************************/
59
51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. } } } } } }
PORTA=0x02; /* left motor on */ wait00(12); wait00(83); /* 0.12msec wait */ /* 0.83msec wait */ PORTA=0x00; /* both motor off */
int straight(void){ PORTA=0x03; /* both motor on */ wait00(20); wait00(80); /* 0.20msec wait */ /* 0.80msec wait */ PORTA=0x00; /* both motor off */
int led_sens(void) { RB3=RB0; /* sensor inputLED indicate */ RB4=RB1; /* sensor inputLED indicate */ RB5=RB2; /* sensor inputLED indicate */
int main(void) { TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTB = 0; PORTA = 0; while(RA4==1){ led_sens(); } while(1){/* infinite loop */ led_sens(); if(RB0==0 && RB1==0 && RB2==1){ /* Black Black White */ small_left_down(); } else if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); } else if(RB0==1 && RB1==0 && RB2==0){ /* White Black Black */ small_right_down(); } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); } else{ straight(); /* The other case */ /* go to straight */ /* turn right */ /* turn right a little */ /* turn left */ /* turn left a little */ /* PORTB clear */ /* PORTA clear */
60
START
Is sensor value
NO
Is sensor value
NO
Is sensor value
NO
Is sensor value
YES
NO
YES
Go straight
In the case sensor (black 0)is ,(white 1)is ,and sensor values are indicated in order of left ,middle , and the right. For example , when it is( ), left and middle sensors are detecting the white color, right sensor is detecting the black line.
61
4.5.5
This is a program that allows robot returns even in case sensors lost the line.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. } int right_down(void){ PORTA=0x03; /* both motor on */ wait00(3); wait00(18); wait00(79); /* 0.03msec wait */ /* 0.18msec wait */ /* 0.79msec wait */ PORTA=0x02; /* left motor on */ PORTA=0x00; /* both motor off */ } int small_left_down(void){ PORTA=0x03; /* both motor on */ wait00(5); wait00(12); wait00(83); /* 0.05msec wait */ /* 0.12msec wait */ /* 0.83msec wait */ PORTA=0x01; /* right motor on */ PORTA=0x00; /* both motor off */ } } int left_down(void){ PORTA=0x03; /* both motor on */ wait00(3); wait00(18); wait00(79); /* 0.03msec wait */ /* 0.18msec wait */ /* 0.79msec wait */ PORTA=0x01; /* right motor on */ PORTA=0x00; /* both motor off */ } return 0; wait00(short k) { /* wait time about (k 0.01) msec. (k0.01msec wait)*/ short i; short j; /* declaration of 16 Bit variables */ for(j=0;j<k;j++){ /* (k 3) times repetition */ for(i=0;i<3;i++){ } /*************************************************/ /* line trace program (C language) */ 2004.8.24 by AND */ /* for upper level(with error recovery ) #include <pic.h> #define RIGHT_DOWN 1 #define LEFT_DOWN 2 #define STRAIGHT 0 __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ int last_time; /* A flag to retain the former state when it cant detect the line */
/*************************************************/
62
49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. } } else if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); last_time=LEFT_DOWN; /* turn left */ } else if(RB0==0 && RB1==0 && RB2==1){ /* Black Black White */ small_left_down(); last_time=LEFT_DOWN; /* turn left a little */ } else if(RB0==1 && RB1==1 && RB2==1 && last_time==RIGHT_DOWN){ right_down(); /*"White White White" and "RIGHT_DOWN last time"*/ last_time=RIGHT_DOWN; /* turn right */ } while(1){/* */ led_sens(); if(RB0==1 && RB1==1 && RB2==1 && last_time==LEFT_DOWN){ left_down(); /*"White White White" and "LEFT_DOWN last time"*/ /* turn left */ last_time=LEFT_DOWN; int main(void) { TRISA = 0xFC; /* A 0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LEDoutput, other bits input */ PORTB = 0; PORTA = 0; while(RA4==1){ led_sens(); /* PORTB clear */ /* PORTA clear */ } int led_sens(void) { RB3=RB0; /* LED */ RB4=RB1; /* LED */ RB5=RB2; /* LED */ } int straight(void){ PORTA=0x03; /* both motor on */ wait00(20); wait00(80); /* 0.20msec wait */ /* 0.80msec wait */ PORTA=0x00; /* both motor off */ } int small_right_down(void){ PORTA=0x03; /* both motor on */ wait00(5); wait00(12); wait00(83); /* 0.05msec wait */ /* 0.12msec wait */ /* 0.83msec wait */ PORTA=0x02; /* left motor on */ PORTA=0x00; /* both motor off */
last_time=STRAIGHT;
63
102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. } }
else if(RB0==1 && RB1==0 && RB2==0){ /* White Black Black */ small_right_down(); last_time=RIGHT_DOWN; } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); last_time=RIGHT_DOWN; } else{ straight(); last_time=STRAIGHT; } /* The other case */ /* go to straight */ /* turn right */ /* turn right a little */
64
START
Is sensor value NO Is sensor value NO Is sensor value NO Is sensor value NO Is sensor value NO
YES B
YES
Turning CW with large radius of curvature last_time=RIGHT_DOWN Turning CW with small radius of curvature last_time=RIGHT_DOWN
YES
Go straight
Sensors values are display from left to right. : sensor detects black. : sensor detects white. For example, ( ) indicates left and middle sensors are detecting white surface, right sensor is detecting the black line. When running to CW direction, it is "last_time = RIGHT_DOWN". When running to CCW direction, it is "last_time = LEFT_DOWN".
65
START
YES
YES
Fig. 4.61 Flow chart of line trace programfor advanced level part of error recovery
66
"Making Microcomputer Controlled Line Tracing Robot" Course Text (English Version) Hardware & Software Manual for Making of S.I.T.-LTR04 Line Tracing Robot Issued on July 16, 2012. This textbook was translated from the original Japanese textbook. It was prepared for UCI Summer Session in S.I.T. in 2012 with the support from following professors. Shibaura Institute of Technology, College of Design Engineering, Dept. of Design Engineering Robotics Laboratory Professor Yoshinobu Ando
Shibaura Institute of Technology, College of Engineering, Dept. of Electrical Engineering Robot Task & System Laboratory Micro-Mechatronics Laboratory Human Robot Interaction Laboratory Professor Takashi Yoshimi Associate Professor Tadahiro Hasegawa Professor/Dean, College of Engineering Makoto Mizukawa
This textbook was translated with cooperation of students from Ando, Hasegawa and Yoshimi Lab.
"Making Microcomputer Controlled Line Tracing Robot" Course Text (Japanese Version) Hardware & Software Manual for Making of S.I.T.-LTR04 Line Tracing Robot Issued on July 25, 2005 Written and edited by Professor Makoto Mizukawa (Human Robot Interaction Laboratory) and Professor Yoshinobu Ando (Robotics Laboratory), Shibaura Institute of Technology, College of Engineering, Dept. of Electrical Engineering Professor emeritus Chie Kasuga, Shibaura Institute of Technology Yasuo Ogawa, Ftech Co.,LTD. Planned by Shibaura Institute of Technology, Center for Lifelong Learning and Extension Programs, Robot Seminar Group Published by S.I.Tech Co. Ltd. Copyright (C) 2005 Shibaura Institute of Technology, All Rights Reserved. 67