Infrared Object Detection & Navigation


 Back To Index

This section shows how to construct a simple, yet very effective IR object detection circuit to help Micro-Bot navigate in his environment, and work his way around obstacles. Due to the intense amount of IR energy present in sunlight, infrared is best suited for indoor applications. Infrared will still work outdoors, but you'll find you get very limited range due to decreased sensitivity of the IR detector when used in sunlight. For operation outdoors see the Sonar experiments page.


Infrared Object Detection

As shown above in the illustration, an object in the path of the IR beam will reflect the IR energy back onto the face of the infrared detector module. We simply turn on the PWM carrier, take the LED cathode low, and sample the output of the IR detector. A logic 0 indicates the IR energy is striking the detector, and an object is in our path. A logic 1 on the detectors output indicates there is no object present to reflect IR energy back onto the face of the detector. Simple enough..!

Parts List:

bullet

2 x 430Ω resistors [YELLOW, ORANGE, BROWN]

bullet

2 x OED-EL-1L2 Infrared LEDs with heat shrink

bullet

1 x 10uF Electrolytic Capacitor

bullet

1 x 10KΩ resistor [RED, BLACK, ORANGE]

bullet

1 x 100Ω resistor [BROWN, BLACK, BROWN]

bullet

1 x TSOP1740 40KHz Infrared Photo Module


Figure #1: Infrared LED Drive Circuit
 

Place two pieces of heat shrink over the both of the infrared LEDs as shown below in Figure #1a on the left LED, and wire both infrared LEDs to PortB as shown above in figure #1. The heat shrink prevents IR energy from exiting the rear by covering the LED leads, and also keeps IR energy from spreading sideways. Since we'll be detecting the IR beams being reflected from objects, we want to concentrate the infrared energy in a forward direction.

Note: Be careful not to overheat the LED during the process. Use a candle or lighter to slowly apply heat to the heat shrink until it closes around the LED leads and lens as shown below on the left LED.


Figure #1a: Infrared LEDs With Heat Shrink

Port Pins Used & Configuration:

bullet PortB.0 = Left IRLED output
bullet PortB.1 = Right IRLED output
bullet PortB.2 = IR detector input
bullet PortB.3 = PWM Drive output


Figure #2: Infrared Detector Circuit

Place the infrared detector in the center area of the bread board or solder it to the center prototyping solder pads similar to this photo HERE. Wire in the TSOP1740 infrared photo detector circuit shown in Figure #2.

Note: The Micro-Bot parts kit includes two of the TSOP1740 IR detector modules. These detector modules will work at baud rates up to 2400 bps, and provide better noise rejection than most off-the-shelf, and less expensive IR detectors.

This routine is very simple. First we jump to two sub routines in the hardware.inc file named Check_Right & Check_Left to check for obstacles on both the left and right sides. On return from these sub routines we check the values returned in Left_IR and Right_IR bits. Yes & No have been defined in the hardware.inc file with constants. No = 1 & Yes = 0. A return value of 0 = Yes, and indicates there was an obstacle detected. A return value of 1 = No, and indicates there was no obstacle detected. These bit values are then used to initiate right or left turns.

Notice that we call the Check_Left and Check_Right sub routines from within each turn routine. Checking for obstacles during the turn allows us to continue turning away from the obstacle until it's no longer in our path.

'****************************************************************
'*  Name    : IR_NAV.BAS                                        *
'*  Author  : Bruce Reynolds                                    *
'*          : Micro-Bot Infrared Navigation                     *
'****************************************************************
INCLUDE "HARDWARE.INC"

Begin:
    GOSUB Check_Right    ' Check right side for obstacle
    GOSUB Check_Left     ' Check left side for obstacle
    IF (Left_IR=No) AND (Right_IR=No) THEN Continue ' If no contact skip further checking 
    IF (Left_IR=Yes) AND (Right_IR=Yes) THEN Back ' If contact on both sides then reverse
    IF (Left_IR=Yes) AND (Right_IR=No) THEN Right ' If contact on left then turn right
    IF (Left_IR=No) AND (Right_IR=Yes) THEN Left  ' If contact on right then turn left

Continue:
    FOR X=1 TO 2    ' Begin forward motion loop here
     PULSOUT Left_Servo,ForwardLeft   ' Forward left servo
     PULSOUT Right_Servo,ForwardRight ' Forward right servo
     PAUSE 20		
    NEXT
    GOTO Begin

Back:               ' Backwards
    FOR X=1 TO 4	 
     PULSOUT Left_Servo,ReverseLeft   ' Reverse left servo
     PULSOUT Right_Servo,ReverseRight ' Reverse right servo
     PAUSE 20     
    NEXT           
    GOTO Begin      ' Return to Begin routine

Right:              ' Turn right
    FOR X=1 TO 4   
     PULSOUT Left_Servo,ForwardLeft    ' Forward left servo
     PULSOUT Right_Servo,ReverseRight  ' Reverse right servo
     PAUSE 20
    NEXT            
    GOSUB Check_Right
    IF Right_IR=Yes THEN Right
    GOTO Begin      ' Return
    
Left:               ' Turn left
    FOR X=1 TO 4    
     PULSOUT Left_Servo,ReverseLeft   ' Reverse left servo
     PULSOUT Right_Servo,ForwardRight ' Forward right servo
     PAUSE 20
    NEXT            
    GOSUB Check_Left
    IF Left_IR=Yes THEN Left
    GOTO Begin      ' Return
    
    END

Note: To change the time Micro-Bot stays in a left or right turn, increase or decrease the loop values in each For Next loop in the Right & Left turn routines. You can adjust the value of "X" in the FOR X=1 TO 4 loop to increase or decrease the number of times through each loop.

Important Note: You'll notice in all of the code examples & experiments that we use the INCLUDE directive to include most of the initial hardware setup, port assignments, and variable declarations. This allows you to setup variables and port assignments in a single file, then simply include it to remove the large number of program code lines displayed on-screen while you're working on your code. This makes things much easier to read & manage as your code grows. You can view the include file HERE.

If you need to make changes to the I/O-pins, default servo speeds, etc, just modify the HARDWARE.INC file. One option is to save it with another name, and simply include your modified version in the beginning of your code instead.

Example:

Assume you have made changes to the original hardware.inc file, and saved it with the name myhardware.inc. Just include it like this;
INCLUDE
"myhardware.inc"

Fuse Configuration Explanations:

@ DEVICE HS_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF
DEFINE    OSC 20
DEFINE    NO_CLRWDT 1

The @ DEVICE line tells PBP to embed PIC configuration fuse settings in the .HEX file on compile.

The HS_OSC option sets the PIC internal fuse for external High-Speed crystal or oscillator. Any external oscillator with a speed greater than 4MHz requires us to use the HS_OSC fuse setting.

When using 4MHz oscillators, we would change this entry to XT_OSC. If we were using the 16F62x series internal 4MHz oscillator, we would substitute INTRC_OSC in place of the XT_OSC or HS_OSC options.

Notes: You can also set configuration fuses with your PIC programmer software before burning your code into the PIC microcontroller, but the @ DEVICE option allows you to see each fuse setting, make quick & easy changes, helps to reduce the chance of forgetting to set them with your programmer software, and can really save you a lot of time & headaches. If you prefer to do this manually, using your PIC programmer software, be sure to remember before you program the PIC.

The MCLR_OFF option turns off the internal reset function on the RA.5 or /MCLR pin. This allows us to use the RA.5 pin as an input only pin. Without this entry, you would need an external pull-up resistor of approximately 4.7K to 10KW connecting RA.5 or /MCLR to +5V. A low going or logic 0 signal on RA.5 would force a hardware reset. If we haven’t disabled the /MCLR function internally, RA.5 will function only as reset input by default. With the /MCLR function disabled, RA.5 can be used as an input only since it does not have the internal output drivers for this pin to be used as an output pin.

The LVP_OFF option turns off the PORTB.4 low-voltage programming fuse option. If you forget to turn off this option, you can’t use RB.4, and might have problems trying to program the 16F62x series in-circuit.

The WDT_OFF option turns off the internal watchdog timer to prevent watchdog timer resets during long pause commands. While PBP automatically handles resetting the watchdog timer for us, it’s still an option I prefer to turn off if I’m not using it.

The PROTECT_OFF option turns code protect off.

The DEFINE OSC 20 line tells PBP we’re using a 20MHz oscillator, and to adjust all timing for this oscillator speed when we compile our code. While PBP will use 4MHz by default unless we instruct if to use something else, it’s good to get in the habit of using this DEFINE since you'll know at a glance exactly what PBP will base its timing on. If you’re using a 4MHz oscillator, simply change it to DEFINE OSC 4. The point is “there’s no guessing”. You’ll know at a glance. This is very handy when you’re chasing down bugs in your code.

The DEFINE NO_CLRWDT 1 line prevents PBP from automatically generating assembly code to handle resetting the watchdog timer. If we have the watchdog timer disabled, we don’t need additional code generated by the compiler to attempt resetting it.

TIP Remember this option. It reduces the amount of code that PBP produces when you compile if you’re not using the WDT. Try commenting this line out when you compile the sample code. You’ll see a reduction in compiled code size by 4 words just by turning this default option off. This can save you a decent amount of program code space as your programs grow, and they will, so every little bit helps.

Using 4MHz Oscillators With Micro-Bot:

At some point you may prefer to use a 4MHz oscillator with Micro-Bot to slow things down or maintain compatibility with the PicBasic Standard compiler. Below are the modifications you’ll need to make to the example code shown previously for a 4MHz oscillator or crystal.

@ DEVICE XT_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF
DEFINE    OSC 4
DEFINE NO_CLRWDT 1
 
'** Define Constants For Motor Directions **'

FWDL  CON 200        ‘ 200 x 10uS = 2mS Pulsout

RVSL  CON 100        ‘ 100 x 10uS = 1mS Pulsout

FWDR  CON 100
RVSR  CON 200
 
'** Setup for 40KHz PWM IRLED drive **'
CCPR1L = 5          ' 5=20%, 12=50% PWM Duty-Cycle
PR2 = 24            ' Set PWM for approximately 40KHz
T2CON = 4           ' Timer2 ON + 1:1 pre-scale    

Changing to XT_OSC sets the internal configuration fuse for oscillator speeds from DC up to 4MHz. We also need to change our constant values used with PULSOUT to control our servo motors, and the hardware PWM control register values to load for hardware PWM at the lower oscillator speed.

Servo Control Pulses

The PicBasic Pro PULSOUT command has a resolution or period of 10uS when the PIC is operating at 4MHz, and 2uS when operating at 20MHz. The table below shows oscillator speeds with corresponding pulse timing for the PBP PULSOUT command.
 

PBP PULSOUT Command Timing & Constant Values
Pulse Time CON stant Value
x Pulse Period
Pulse Width =
Oscillator Speed
1000
2uS
2mS
20MHz
500
2uS
1mS
20MHz
200
10uS
2mS
4MHz
100
10uS
1mS
4MHz


Figure #3: Servo Motor Control Pulses

 

Standard hobby servo motors require pulses from 1.5mS for center position, 1mS for left position, and 2mS for right position as shown above in Figure #3.

Micro-Bot uses servo motors that have been pre-modified for continuous rotation, but they still require the same position pulses for movement. Assuming the center servo in Figure #3 is on the left or A side of Micro-Bot, it’s easy to see how a pulse of 1mS will cause forward rotation of the motor, while a pulse of 2mS would cause reverse motion.

Note: To have Micro-Bot travel in a straight line, small adjustments may help correct veering to one side or the other when Micro-Bot is moving forward. Slight adjustments are often necessary for different servo motors.

Example:
 

'** Define Constants For Motor Directions **'

FWDL  CON 180        ‘ Slight adjustment to forward left servo pulsout value

RVSL  CON 100        ‘ 100 x 10uS = 1mS Pulsout

FWDR  CON 100
RVSR  CON 200

You may need to make slight adjustments to the motor constants to adjust for various different servos, and battery voltages as they drop. If Micro-Bot tends to veer to the left or right when moving forward, simply edit the servo constants defined in the hardware.inc file to compensate. Setting the minimum and maximum pulse times will also effect overall speed.

For example. We have used 800 instead of 1000 [2mS pulse], and 600 instead of 500 [1mS pulse] to slow the Grand Wing servos down. Edit the hardware.inc file, and change 800 to 1000, and 600 to 500 to see the speed increase.

ForwardLeft  CON 800   ' 200 For 4MHz
ReverseLeft  CON 600   ' 100 For 4MHz
ForwardRight CON 600   ' 100 For 4MHz
ReverseRight CON 800   ' 200 For 4MHz

You can make adjustments to motor constants to best suit your own application or different servo motors. This is something you'll need to experiment with to find the best values, but for most applications, using the servos supplied with Micro-Bot, the default values in the hardware.inc file should work fine.


 Back To Index

Copyright © 2007 Reynolds Electronics
http://www.rentron.com