20120713-LTR01_en

Embed Size (px)

Citation preview

  • "Making Microcomputer Controlled Line Tracing Robot"

    Edited by Shibaura Institute of Technology,

    Center for Lifelong Learning and Extension Programs

    S.I.T.-LTR04 Line Tracing Robot Hardware & Software Manual

    Shibaura Institute of Technology

    Center for Lifelong Learning and Extension Programs

    Ver.0.2 for UCI Summer Session

  • 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 GroupShibaura Institute of Technology, College of Engineering, Dept. of Electrical Engineering Human Robot Interaction Laboratory Professor Makoto Mizukawa Robotics Laboratory Associate Professor Yoshinobu AndoShibaura Institute of Technology Professor Emeritus Chie KasugaFtech Co.,LTD 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 Professor Yoshinobu AndoShibaura Institute of Technology, College of Engineering, Dept. of Electrical Engineering Robot Task & System Laboratory Professor Takashi Yoshimi Micro-Mechatronics Laboratory Associate Professor Tadahiro Hasegawa Human Robot Interaction Laboratory Professor/Dean, College of Engineering

    Makoto Mizukawa

    July, 8th, 2012

  • Table of contents

    Chapter 1 Introduction 1 Chapter 2 Configuration of the line tracing robot kit 2 2.1 A circuit diagram, a printed circuit board, and a part list 2 2.2 Drawing aluminum bracket, figure aluminum mounting for cart 6 Chapter 3 Assembly8 3.1 Printed circuit board assembly procedure 8 3.2 Parts mounting procedure23 Chapter 4 Program development26 4.1 Port Assignments 26 4.2 Program development by assembly language27 4.2.1 Assembly 27 4.2.2 Flashing LED, switch operation 28 4.2.3 Straight, curve37 4.2.4 How to detect the line using the photo sensor 42 4.2.5 How to follow the line 43 4.3 Sample program using assembly language68 4.3.1 Turn on and off LEDs 68 4.3.2 Go straight 71 4.3.3 Line trace174 4.3.4 Line trace278 4.4 Program development by C language86 4.4.1 Flow of program development 86 4.4.2 Blinking of LED, Switch operation 86 4.4.3 Go straightTurn 92 4.4.4 Method of detecting the line by sensor 100 4.4.5 How to trace a line102 4.5 Example Program by C Language 110 4.5.1 Blinking LED 110 4.5.2 Go straight 112 4.5.3 Line Tracefor beginners113 4.5.4 Line Tracefor middle level116 4.5.5 Line Tracefor advanced level119

  • 1

    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.

    Figure 1.1 Line tracing robot S.I.T.-LTR04

  • 2

    Chapter 2 Configuration of the line tracing robot kit

    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 S.I.T.-LTR04 schematic diagram

  • 3

    Figure 2.2 S.I.T.-LTR04 printed circuit board (Top)

    Figure 2.3 S.I.T.-LTR04 printed circuit board (Bottom)

  • 4

    Figure 2.4 S.I.T.-LTR04 PCB pattern (Top)

    Figure 2.5 S.I.T.-LTR04 PCB pattern (Bottom)

  • 5

    Table2.1 S.I.T-LTR04 BOM No. NAME Component

    arrangement number Part name Part number Number

    1 Monolithic Ceramic Chip Capacitors

    C2,C3,C5,C6,C7,C8C9 0.1 72 Electrolytic Capacitor C4 10220(E) 13 Electrolytic Capacitor C1 50V10(E) 14 Diode D1,D2 1SS133 1SS133 25 Diode D3,D4 11EQS03L 11EQS03L 26 Transistor Q4,Q5 Tr 2SD2106 27 One-Chip

    Microcomputer IC1 PIC16F84 PIC16F84A-20/P 1

    8 LED(Green) LED1 LED GL3KG8 19 LED(RED) LED2,LED3,LED4 LED GL3PR8 310 IR LED LED2,LED3,LED4 TLRE180AP(F) TLRE180AP(F) 311 Photo transistor Q1,Q2,Q3 TPS615(F) TPS615(F) 312 Resistor(O,O,Bl,Bl,Br)* R8,R9,R10 100 100R0 313 Resistor(Br,Bl,Bl,R,Br)* R4,R19,R20 10K 1002 314 Resistor(O,O,Bl,Go,Br)* R6,R7 1K 1001 215 Resistor(B,G,Bl,Bl,Br)* R1 680 6800 116 Resistor(Br,Bl,Bl,Br,Br)* R3,R11,R12,R13,R14 1K 1001 917 Resistor(O,Bl,Bl,R,Br)* R2 30K 3002 118 Resistor(R,Bl,Bl,O,Br)* R5 200K 2003 119 Mechatronics Key

    Switch SW1,SW2 B3F-1052 B3F-1052 2

    20 Toggle Switch SW3 SW(POWER) 121 Pre-Set Variable

    Resistor VR1,VR2,VR3 50K 3

    22 Connector CN1 S5B-XH-A 123 CERALOCK X1 20MHz 124 IC Socket DIP-18P IC

    Socket(18P) 1

    25 Battery Holder Battery Box MC-304-3 126 Aluminum Chassis Aluminum

    Chassis 1

    27 NabeM3-10 screw thread 428 NabeM2-6 Brass screw thread 229 Sara M3-6 Brass Nickel screw thread 230 Nylon Spacer Spacer 33 431 Caster Dokodemo

    Caster 1

    32 circuit board F0278 133 Double Gear

    BOX NO.168 1

    34 Tire Track Tire Set NO.101 235 M2Nat 236 M3nat 637 3 Spring washer 4Resister 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,

  • 6

    2.2 Drawings of aluminum frame and robot assembly Figure 2.6 shows the drawing of aluminum frame and figure 2.7 shows assembly drawing of S.I.T.LTR04.

    Figure 2.6 Drawing of aluminum frame.

  • 7

    Figure 2.7 Assembly drawing

  • 8

    Chapter 3 Assembling

    3.1 Printed circuit board assembly procedure (a) Resistor20pcs Refer to the BOM in Table 2.1 for value of each resistor and resistor color code.

    (a)Enlarged view (b) A real resistor (c) Schematic symbol

    Figure 3.1 Resistor

    No polarity, no need to worry about orientation when assembling. Be careful with color code. Reference 1.

    Figure 3.2 Mounting position of resistor

  • 9

    (b) Diode4pcs D1, D22pcs D3, D42pcs

    (a) Enlarged (b) 1SS133 (c) 11EQS03L (d) PCB symbol

    Diode Diode Figure 3.3

    Cathode is marked by a yellow line. Be careful with the orientation of diodes.

    Figure 3.4 Mounting position of diodes

  • 10

    (c) Light-emitting diode4pcs LED1 (power indicator) green1pcs LED5~7 (sensor indicator) red3pcs

    (a) Enlarged view (b) Actual LED (c) PCB symbol

    Figure 3.5 Light-emitting diode (LED)

    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 Mounting position of LEDs

  • 11

    (d) Multilayer ceramic capacitor 7pcs C2, C3, C5, C6, C7, C8, C9 0.1[F]7pcs

    (a) Enlarged view (b) Actual capacitor (c) PCB symbol

    Figure 3.7 Multilayer ceramic capacitor

    Function: eliminating the noise from motors and other parts. No polarity, no need to worry about direction when assembling.

    Figure 3.8 Mounting position of multilayer ceramic capacitors.

  • 12

    (e) DIP-18P IC socket (18 pin socket)1pcs

    (a)Enlarged view (b) Actual socket (c) PCB symbol

    Figure 3.9 DIP-18P IC socket

    Fit the Notch in socket and the Notch in schematic symbol

    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.

    (3.10) Attaching socket (1) (3.11) Attaching socket (2) (3.12)Attaching socket (3)

    Time-saver

  • 13

    (f) CERALOCK 1pcs CERALOCK (ceramic resonator) 20[MHz]1pcs

    (a) Enlarged view (b) Actual ceralock (c) PCB symbol

    Figure 3.13 CERALOCK

    GND is the center pin. No polarity, no need to worry about orientation when assembling.

    Figure 3.14 Mounting position of CERALOCK

  • 14

    (g) Mechanical key switch 2pcs SW1 (Reset)1pcs SW2 (Start)1pcs

    (a) Actual switch (b) PCB symbol

    Figure 3.15 Mechanical key switch 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 Mounting position of mechanical key switches

  • 15

    (h) Semi-fixed variable resistor3pcs VR1~VR3 50[k]3pcs

    (a) Actual one (Top view) (b) PCB symbol

    Figure 3.17 Semi-fixed variable resistor 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. 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 Mounting position of semi-fixed variable resistor

  • 16

    (i) Electrolytic capacitor 2pcs C1 10[F] 1pcs C4 220[F] 1pcs (a) Enlarged view (b) Actual capacitor (c) PCB symbol

    Fig 3.19 Electrolytic capacitor 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 + .

    Fig 3.20 Position of electrolytic capacitors

    Pay attention to the polarity ! Marked by

    sign

    1

    C1C4

  • 17

    (j) Switching transistor Q4, Q5 2pcs

    (a) Actual transistor (b) PCB symbol (c) Schematic symbol

    Fig 3.21 Transistor From the front, left to right, the order of three leads is B (base), C (collector), and E (emitter).

    Fig 3.22 Positions of transistors.

    B

    EC

    B C E

    Heatsink

    B

    C

    E

    B

    C

    E

    2SD2106

    or 2SD560

    Q4Q5

  • 18

    (k) Toggle switch 3P 1pcs SW3 1pcs (a) Actual switch (b) PCB symbol (c) Position

    Fig 3.23 Toggle switch 3P (l) S5B-XH-A downloader connector 1pcs CN1 1pcs Caution) This connector must be soldered to PCBs bottom layer.

    Fig 3.24 Connector and attachment position

    Switchs terminals are symmetrical. It can be assembly i ith di ti

    Carefully check orientation!!

    Solder to bottom layer.

  • 19

    (m) Phototransistor 3pcs Q1, Q2, Q3 3pcs Caution) Phototransistors Q1, Q2, Q3 must be soldered to PCBs bottom layer.

    (a) Enlarged view (b) Side view (d) PCB symbol

    Fig 3.25 Phototransistor Phototransistors receive reflecting light from the floor. Be careful with the polarity of emitter and collector.

    Fig 3.26 Position of phototransistors

    Emitter Collector

    PCB

    Caution! Solder firmly to PCB.

    The longer l d

    The shorter l d

    Collector Emitter E C

    The shorter lead

    The longer lead

    The smaller one

    Q13 Solder to bottom layer.

    Caution! Push all the way down to PCB. Solder to bottom layer.

  • 20

    (n) Red LED 3pcs LED2, LED3, LED4 3pcs Caution) LED2-4 must be soldered to PCBs bottom layer. (a) Enlarged view (b) Side view (d) PCB symbol

    Fig 3.27 Red LED Emit red light to the floor under robot. Be careful with the polarity of anode and cathode.

    Fig 3.28 Position of red LEDs

    Anode Cathode

    Anode Cathode

    K A

    PCB

    Caution! Solder firmly to PCB.

    The larger one

    The longer lead

    The shorter lead

    K A

    The longer lead The shorter lead

    LED24 Solder to bottom layer.

    Caution! Push all the way down to PCB.Solder to bottom layer.

  • 21

    (o) Battery box 1pcs

    (a) (b) PCB symbol

    Fig 3.29 Battery box Caution) Thread the wires through the hole near battery connection points before soldering.

    Fig 3.30 Position of battery box connection

    PCB

    Solder Hole

    Wires

    Vcc GND

    Red wire Black wire

    BATTERY

    Thread wires through the hole

  • 22

    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.

    Fig 3.31 PCB symbol

    Fig 3.32 Position of motors power wires connection

    Black wire Red wire

    +-

    PCB

    Solder Hole

    Wires

    21

    Thread wires

    through this hole

  • 23

    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.

    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.

    For C typeDouble gear boxGear ratio of 114.7:1 Wheel diameter 36mmTamiya TRUCK TIRE SET

  • 24

    Fig 3.35 Assembly procedure (3)

    Fig 3.36 LTR04 photograph (side)

  • 25

    By the steps described above, the robot is complete.

    Fig 3.37 Complete picture

  • 26

    Chapter 4 Program development 4.1 Port assignment, motor control logic

    Table 4.1 a) and b) show pin assignment of PORT A and PORT B. Table 4.2 shows port assignment of sensor input and LED output. Table 4.3 shows motor control logic according to sensor input. Table 4.4 shows motor control logic.

    Table 4.1 a) PORT A

    A7 A6 A5 A4 A3 A2 A1 A0 Start Left motor Right motor Use

    Input Output Output Input and output1 1 1 1 1 1 0 0 Initial setting

    Table 4.1 b) PORT B

    B7 B6 B5 B4 B3 B2 B1 B0Writer Writer Right LED Middle LED Left LED Right sensor Middle sensor Left sensor UseInput Input Output Output Output Input Input Input Input and output

    1 1 0 0 0 1 1 1 Initial setting

    Table 4.2 Port assignment of sensor input and LED output

    B7 B6 B5 B4 B3 B2 B1 B0 PortRight sensor Middle sensor Left sensor Sensor input

    Right sensor Middle sensor Left sensor Shift to the leftRight sensor Middle sensor Left sensor Shift to the left

    Right sensor Middle sensor Left sensor Shift to the left Bit inversion

    Right LED Middle LED Left LED LED output

  • 27

    Table 4.3 Motor control logic according to sensor input

    Right sensor Middle sensor Left sensorB2 B1 B01 0 1 Normal Straight0 0 1 Right - leaning slightly Somewhat to the left1 0 0 Right - leaning slightly Somewhat to the right0 1 1 Right - leaning To the left1 1 0 Left-leaning To the right1 1 1 Derailment Derailment processing0 0 0 Impossible Straight0 1 0 Impossible Straight

    Situation Control logic

    Table 4.4 Motor control logic

    Left motor RightmotorA1 A00 0 Stop STP1 0 To the right R1R20 1 To the Left L1L21 1 Straight GOGOh

    Controllogic Subroutine name

  • 28

    4.4 Program development by C language 4.4.1 Flow of program development 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.

    Fig.4.30 Flow of making executable program from C program

    4.4.2 LED blinking, push button handling (a) LED blinking

    Figures 4.31(a) and (b) shows the hardware for LED driving.

    In Figure 4.31(a), when high level voltage (5V) is output from PORT, LED will turn on.

    In Figure 4.31(b), when low level voltage (0V) is output from PORT, LED will turn off. To output high level voltage (5V), we set value 1 for PORT: PORT = 0;

    To output low level voltage (0V), we set value 1 for PORT: PORT = 1;

    C program

    C program without directives

    Assembly program

    Machine code program

    Eg.Pre-processor (# directives are processed)

    Compiler (C language assembly language)

    Assembler (assembly language machine code)

    Executable machine code program

    Linker (Machine code and libraries are linked)

  • 29

    aLED on bLED off

    4.31 Hardware component for LED driving

    LED blinking example 1 All LEDs blink every 0.5 second.

    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.

    LED (PORTB) all LEDs on LED (PORTB) all LEDs off

    -----Repeat------ all LEDs offPORTB=0x00; all LEDs onPORTB=0x38;

    1.

    2.

    3.

    4.

    5.

    6.

    7.

    8.

    9.

    10.

    11.

    12.

    13.

    /************************************************************/ /* All LEDs blink in every about 0.5 second (C language) */ /* 2004.8.20 by AND */ /************************************************************/ #include __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

  • 30

    14.

    15.

    16.

    17.

    18.

    19.

    20.

    21.

    22.

    23.

    24.

    25.

    26.

    27.

    28.

    29.

    for(i=0;i

  • 31

    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 2for statement 3if statement Fig 4.32 Flow chart

    Description of input and output setting by TRISA and TRISB

    TRISA = 0xFC; /* A0,1:output, 2,3,4:input */ TRISB = 0xC7; /* B0,1,2:input, B3,4,5:LED output, other bits input */

    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.

    Fig 4.33 Input and output setting of PORTs

    RA2RA3RA4MCLRVssRB0RB1RB2RB3

    RA0RA1

    OSC1OSC2

    VddRB7RB6RB5RB4Output for LED(L)

    Input for Switch

    Output for Motor(L)Output for Motor(R)

    Input for Sensor(L)Input for Sensor(C)Input for Sensor(R)

    Output for LED(C)Output for LED(R)

  • 32

    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 A3 A2 A1 A0 START LEFT MotorRIGHT Motor Use

    Input Output Output In Out1 0 0 Value

    B7 B6 B5 B4 B3 B2 B1 B0

    Writer Writer Right LED Center LED Left LEDRight Sensor

    Center Sensor

    Left Sensor Use

    Input Input Output Output Output Input Input Input In Out1 1 0 0 0 1 1 1 Value

    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.

    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;

    (Value in hexadecimal: FC)

    (Value in hexadecimal: C7)

  • 33

    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) Middle LED on LED (PORTB) Side LEDs on -----Repeat------ (All LEDs off) PORTB= ; (All LEDs on) PORTB= ;

    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.

    /************************************************************/ /* All LEDs blink in every about 0.5 second (C language) */ /* 2004.8.20 by AND */ /************************************************************/ #include __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

  • 34

    (b) Push button handling Figure 4.34 (a) and (b) shows hardware circuit and logic to handle push button.

    In Figure 4.34 (a), when the push-button switch is not pressed, the corresponding pin will be at

    high level voltage (5V). Therefore logical value of that pin when read into program will be 1.

    In Figure 4.34 (b), when the push-button switch is pressed, the corresponding pin will be at low

    level voltage. Therefore its logical value when read into program will be 0.

    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.

    while(RA4!=0){} // next code

    a Button not pushed b Button pushed

    Fig4.34 Hardware configuration and circuit operation of push-button switch

    PIC16F84A

    5V

    5V(High)

    Return value = 1

    If do not push switch,

    switch

    PIC16F84A

    5V

    0V(Low)

    Return value = 0

    If push switch,

    switch

  • 35

    4.4.3 Going straight and turning 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: PORT = 1;

    To output low level voltage (0V), set PORT value to 0: PORT = 0;

    (a) Motor is running (ON state) (b) Motor is stopped (OFF state) 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.

    fast

    middle

    slow

    Fig.4.36 Motor speed control

    PIC16F84A

    If PORT= 1,

    5V(High)

    5V

    M

    Motor

    Transistor

    Rotation

    switch on

    PIC16F84A

    If PORT= 0,

    0V(Low)

    5V

    M

    Motor

    Transistor

    NotRotation

    switch off

    ON

    OFF

    ON ON ON

    OFF OFF OFF

    Average of ON Level

    ON

    OFF

    ON ON ON

    OFF OFF OFF

    Average of ON Level

    ON

    OFF

    ON ON ON

    OFF OFF OFF

    Average of ON Level

  • 36

    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)

    (Switching of the left motor)

    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.

    (Switching of the right motor)

    (Switching of the left motor)

    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.

    (Switching of the right motor)

    (Switching of the left motor)

    Fig.4.39 Switching periods of left and right motors (Going straight)

    ON ON

    OFF OFFOFF OFF

    ON

    ON

    OFF OFF OFFOFF OFF OFF

    ON ON

    OFF OFF OFFOFF OFF

    ON

    ON

    OFF OFF OFFOFF OFF

    ON

    OFF

    ON ON

    OFF OFF OFFOFF OFF

    ON

    OFF

    ON ON

    OFF OFF OFFOFF OFF

    ON

    OFF

  • 37

    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)

    PORTA bit 1 (Switching of left motor)

    Fig.4.40 Switching of right and left motors (Going straight)

    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.

    /*****************************************************/ /*motor drive program (C Language) */ /* straight */ /* 2004.8.23 by AND */ /*****************************************************/ #include __CONFIG(0xFFFA); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */

    wait00(short k) { /* Wait time (k0.01) msec.*/ short i; short j; /* 16 bit variable declaration */ for(j=0;j

  • 38

    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)

    PORTA bit 1 (Switching of left motor)

    Fig.4.41 Switching of right and left motor (Driving straight slowly)

    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.

    /*****************************************************/ /* motor drive program (C Language) */ /* straight */ /* 2004.8.23 by AND */ /*****************************************************/ #include __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

  • 39

    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.

    PORTA bit 0Switching of right motor

    PORTA bit 1Switching of left motor

    Fig4.42 Switching of right and left motors CCW turning with very short radius 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.

    /*****************************************************/ /* motor drive program (C Language) */ /* rotate right motor and stop left motor */ /* 2004.8.23 by AND */ /*****************************************************/ #include __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 variables */ for(j=0;j

  • 40

    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.

    PORTA bit 0Switching of right motor

    PORTA bit 1Switching of left motor

    Fig4.43 Switching of right and left motorCW turning with very short curvature radius 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.

    /*****************************************************/ /* motor drive program (C Language) */ /* rotate left motor and stop right motor */ /* 2004.8.23 by AND */ /*****************************************************/ #include __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

  • 41

    Motor driving 5 Turning left around an arc with short radius of curvature 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.

    PORTA bit 0Right motor

    PORTA bit 1Left motor

    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.

    /*********************************************************************/ /* Motor drive program (C language) */ /* Turn left around an arc with short radius of curvature */ /* 2004.8.23 by AND */ /*********************************************************************/ #include __CONFIG(0xFFFA); /* Initial settings CP:OFF,PT:OFF,WT:OFF,HS */

    wait00(short k) { /* Wait time about (k0.01)msec */ short i; short j; /* 16 bit variables declaration */ for(j=0;j

  • 42

    Motor driving 6 Turning left around an arc with long radius of curvature 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.

    PORTA bit 0Right motor

    PORTA bit 1Left motor

    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.

    /*******************************************************************/ /* Motor drive program (C language) */ /* Turn left around an arc with long radius of curvature */ /* 2004.8.23 by AND */ /*******************************************************************/ #include __CONFIG(0xFFFA); /* Initial settings CP:OFF,PT:OFF,WT:OFF,HS */

    wait00(short k) { /* Wait time about (k0.01) msec */ short i; short j; /* 16 bit variable declaration */ for(j=0;j

  • 43

    4.4.4 Line detection method by sensors Sensor input Figure 4.46 (a), (b) shows the sensor circuit for line detection, composing of a pair of Photodiode and

    Photo Transistor.

    Figure 4.46 (a) shows circuit operation when white surface is detected: Photo Transistor turns

    on, and the voltage of PORT becomes high level (5v). Therefore, logical value of PORT

    becomes 1 when sensor detects white surface.

    Figure 4.46 (a) shows circuit operation when black surface is detected: Photo Transistor turns

    off, and the voltage of PORT becomes low level (0v). Therefore, logical value of PORT becomes

    0 when sensor detects black surface.

    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 Bit3; left LED PORTB Bit0; left sensor PORTB Bit4; middle LED PORTB Bit1; middle sensor PORTB Bit5; right LED PORTB Bit2; right sensor

    (a) Sensor detects white surface

    5V(High)

    Return Value = 1

    5V

    PhotoTransistor

    switch on

    PIC16F84A

    5V

    PhotoDiode

    Reflect bywhite

    surface

    0V(Low)Return Value = 0

    5V

    PhotoTransistor

    switch off

    PIC16F84A

    5V

    PhotoDiode

    NotReflect by

    blacksurface

    (b) Sensor detects black surface

  • 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.

    /**************************************************************/ /* A program to indicate the states of the sensor */ /* 2002.8.18by AND */ /**************************************************************/ #include __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

  • 45

    1.

    2.

    3.

    4.

    5.

    6.

    7.

    8.

    9.

    10.

    11.

    12.

    13.

    14.

    15.

    /*************************************************/ /* A program to drive the motor by using states of the sensor */ /* 2004.8.24 by AND */ /*************************************************/ #include __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 /* Repetition Times */ wait0(short k) { /* Wait about (k0.01)sec */ short i; short j; /* 16 Bit; Variable declaration */ for(j=0;j

  • 46

    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.

    Fig 4.47 State transition of robot

    If robot is not tilted against the line.

    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.

    Sensors values are (White, Black, White)?

    Go straight

    Yes

    No

  • 47

    If robot is slightly tilted against the line

    Fig.4.49 Flow chart when robot is slightly tilted against the line

    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.

    0 0 1

    Sensor values are (Black,Black,White)?

    Turn left gently

    Yes

    No

  • 48

    If robot is tilted heavily to the right

    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.

    Sensor values are (Black,White,White)?

    Turn left quickly

    Yes

    No

    0 1 1

  • 49

    If robot lost the line while tilted heavily to the right

    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.

    Fig 4.54 Movement of robot when sensor is out of the line.

    Example Program for Line Tracing

    1.

    2.

    3.

    4.

    5.

    6.

    7.

    /*****************************************************/ /* a little fastline trace program (C language) */ /* with error recovery 2004.8.24 by AND */ /*****************************************************/ #include #define RIGHT_DOWN 1 #define LEFT_DOWN 2

    1 1 1and

    Whether the sensor is

    (White,White,White), and

    robot turned left the last

    time

    Turn left quickly

    Yes

    No

  • 50

    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); /* Initial setting CP:OFF,PT:OFF,WT:OFF,HS */ int last_time; /*A flag to retain the former state when robot cant detect the line */

    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

  • 51

    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.

    PORTA=0x03; /* both motor on */ wait00(20); /* 0.20msec wait */ PORTA=0x00; /* both motor off */ wait00(80); /* 0.80msec wait */ }

    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; /* PORTB clear */ PORTA = 0; /* PORTA clear */ last_time=STRAIGHT; while(RA4==1){ led_sens(); } while(1){/* infinite loop */ led_sens(); if(RB0==1 && RB1==1 && RB2==1 && last_time==LEFT_DOWN){ left_down(); /*"White White White" and "LEFT_DOWN last time"*/ last_time=LEFT_DOWN; /* turn left */ } 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 */ } else if(RB0==0 && RB1==0 && RB2==1){ /* Black Black White */ small_left_down(); /* turn left a little */ last_time=LEFT_DOWN; } else if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); /* turn left */ last_time=LEFT_DOWN; } else if(RB0==1 && RB1==0 && RB2==0){ /* White Black Black */ small_right_down(); /* turn right a little */ last_time=RIGHT_DOWN; } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); /* turn right */ last_time=RIGHT_DOWN; } else{ /* The other case */ straight(); /* go to straight */ last_time=STRAIGHT; } } }

  • 52

    Fig. 4.55 Flow chart of line trace programwith error recovery

    Is sensorAnd,was the last time CW

    (last_time=RIGHT_DOWN?)

    Display sensor information on LED

    Is sensor value

    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 isdetecting the black line.

    NO

    YES

    YES

    YES

    NO

    Is sensor value

    Is sensor value

    Turning CCW with long radius of curvature last_time = LEFT_DOWN

    Is sensor value

    NO

    NO

    YES

    Go straight last_time=STRAIGHT

    START

    YES

    NO

    When running to CCW direction, it is "last_time = LEFT_DOWN". When running to CW direction, itis "last_time = RIGHT_DOWN". When Go straight, it is "last_time = STRAIGHT". This is toremember the latest states.

    Turning CCW with short radius of curvature last_time = LEFT_DOWN

    Turning CWwith long radius of curvature

    last_time = RIGHT_DOWN

    Turning CW with short radius of curvature last_time = RIGHT_DOWN

    Is sensorAnd,was the last time CCW

    (last_time=LEFT_DOWN?)

    YES

    NO

    Turning CCW directionwith short radius of curvature last_time = LEFT_DOWN

    Turning CW direction with short radius of curvature last_time = RIGHT_DOWN

  • 53

    4.5 Example Program by C Language 4.5.1 Blinking LED 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) left LED on LED (PORTB) middle LED on LED (PORTB) right LED on

    -----repetition------ left LED onPORTB= ; middle LED onPORTB= ; right LED onPORTB= ; 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.

    /*************************************************/ /* blinking one by one every about 0.5 seconds of LEDC language */ /* 2004.8.20 by AND */ /*************************************************/ #include __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

  • 54

    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 LED (PORTB) all LEDs off

    -----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 __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

  • 55

    4.5.2 Go straight Motor drive slowly 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.

    PORTA Bit0switching of right motor

    PORTA Bit1switching of left motor

    Fig. 4.56 Switching of motor 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.

    /*************************************************/ /* program of motor drive(C language) */ /* slowly go straight */ /* 2004.8.23 by AND */ /*************************************************/ #include __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

  • 56

    4.5.3 Line Tracingfor beginners 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 beginners (with error recovery) 2004.8.24 by AND */ /*************************************************/ #include __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

  • 57

    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; /* PORTB clear */ PORTA = 0; /* PORTA clear */ last_time=STRAIGHT; while(RA4==1){ led_sens(); } while(1){/* infinite loop */ led_sens(); if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); /* turn left */ } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); /* turn right */ } else{ /* The other case */ straight(); /* go to straight */ } } }

    Fig. 4.57 Line tracing programfor beginners

    while(1){/* infinite loop */

    led_sens();

    if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */left_down(); /* turn left */

    }

    else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black*/

    right_down(); /* turn right */}

    else{ /* The other case */straight(); /* go to straight */

    }

    }

    (1)

    (2)

    (3)

    (4)

    (5)

    (6)

  • 58

    Fig. 4.58 Flow chart of line tracing programfor beginners

    Indicate sensor information on LED

    Left sensor detectsthe black line

    sensor value isblack 0white 1

    Turning CCW

    Right sensor detectsthe black line

    TurningCW

    Go straight

    NO

    YES

    YES

    NO

    (2)

    (3)

    (4)

    (6)(1)

    (5)

    START

  • 59

    4.5.4 Line Tracefor middle level 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 2004.8.24 by AND */ /*************************************************/ #include __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

  • 60

    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); /* 0.12msec wait */ PORTA=0x00; /* both motor off */ wait00(83); /* 0.83msec wait */ }

    int straight(void){ PORTA=0x03; /* both motor on */ wait00(20); /* 0.20msec wait */ PORTA=0x00; /* both motor off */ wait00(80); /* 0.80msec wait */ }

    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; /* PORTB clear */ PORTA = 0; /* PORTA clear */ while(RA4==1){ led_sens(); } while(1){/* infinite loop */ led_sens(); if(RB0==0 && RB1==0 && RB2==1){ /* Black Black White */ small_left_down(); /* turn left a little */ } else if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); /* turn left */ } else if(RB0==1 && RB1==0 && RB2==0){ /* White Black Black */ small_right_down(); /* turn right a little */ } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); /* turn right */ } else{ /* The other case */ straight(); /* go to straight */ } } }

  • 61

    Fig. 4.59 Flow chart of line tracing programfor middle level

    Indicate sensor information on LED

    Is sensor value

    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.

    Running to arcs of large radiusof curvature in the CCW

    NO

    YES

    YES

    YES

    NO

    NO

    Is sensor value

    Is sensor value

    Running to arcs of small radius of curvature in the CCW

    Running to arcs of large radius of curvature in the CW

    Is sensor value

    NO

    YES Running to arcs of small radius of curvature in the CW

    Go straight

    START

  • 62

    4.5.5 Line tracingfor advanced level

    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.

    /*************************************************/ /* line trace program (C language) */ /* for upper level(with error recovery ) 2004.8.24 by AND */ /*************************************************/ #include #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 */

    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

  • 63

    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.

    int small_right_down(void){ PORTA=0x03; /* both motor on */ wait00(5); /* 0.05msec wait */ PORTA=0x02; /* left motor on */ wait00(12); /* 0.12msec wait */ PORTA=0x00; /* both motor off */ wait00(83); /* 0.83msec wait */ }

    int straight(void){ PORTA=0x03; /* both motor on */ wait00(20); /* 0.20msec wait */ PORTA=0x00; /* both motor off */ wait00(80); /* 0.80msec wait */ }

    int led_sens(void) { RB3=RB0; /* LED */ RB4=RB1; /* LED */ RB5=RB2; /* LED */ }

    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; /* PORTB clear */ PORTA = 0; /* PORTA clear */ last_time=STRAIGHT; while(RA4==1){ led_sens(); } 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"*/ last_time=LEFT_DOWN; /* turn left */ } 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 */ } else if(RB0==0 && RB1==0 && RB2==1){ /* Black Black White */ small_left_down(); /* turn left a little */ last_time=LEFT_DOWN; } else if(RB0==0 && RB1==1 && RB2==1){ /* Black White White */ left_down(); /* turn left */ last_time=LEFT_DOWN; }

  • 64

    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(); /* turn right a little */ last_time=RIGHT_DOWN; } else if(RB0==1 && RB1==1 && RB2==0){ /* White White Black */ right_down(); /* turn right */ last_time=RIGHT_DOWN; } else{ /* The other case */ straight(); /* go to straight */ last_time=STRAIGHT; } } }

  • 65

    Fig. 4.60 Flow chart of line trace programfor advanced level

    Display sensor information on LED

    Is sensor value

    NO

    YES

    YES

    YES

    NO

    NO

    Sensors values are display from left to right. : sensor detects black. :sensor detects white. For example, ( ) indicates left and middle sensors are detectingwhite surface, right sensor is detecting the black line.

    Is sensor value

    Is sensor value

    Turning CCWwith large radius of curvaturelast_time=LEFT_DOWN

    with small radius of curvature Turning CCW

    last_time=LEFT_DOWN

    Is sensor value

    Is sensor value

    NO

    YESTurning CW

    last_time=RIGHT_DOWN

    YES

    NO

    START

    When running to CW direction, it is "last_time = RIGHT_DOWN".When running to CCW direction, it is "last_time = LEFT_DOWN".

    B

    last_time=RIGHT_DOWN

    Go straight

    with large radius of curvature

    with small radius of curvatureTurning CW

  • 66

    Fig. 4.61 Flow chart of line trace programfor advanced level part of error recovery

    Turning CCW with minimum radius of curvature.

    Was the last time, CCW

    last_time=LEFT_DOWN?

    NO

    YES

    YES

    START B

    Turning CCW with minimum radius of curvature.

    Was the last time, CWlast_time=RIGHT_DOWN?

  • 67

    "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 Professor Takashi Yoshimi

    Micro-Mechatronics Laboratory Associate Professor Tadahiro Hasegawa

    Human Robot Interaction Laboratory 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.

    Chapter1-3-TrungNLChapter4.4-TrungNL