Transcript Day 3

PHY 235 Robotics Workshop
Day 3
IR Distance Sensing
IR = Infrared Light
Infrared Headlights
• IR LED emits IR light
• Light is reflected off object
• Reflected light is detected by IR Detector
IR Detector
IR LED
IR Sensor Issues
• Ambient IR: Our environment is flooded with IR
radiation from: sun, lights, bodies, heat sources.
• Impact on IR Detection: This IR “pollution” makes it
difficult to isolate a specific IR signal. Much like trying
to hear one voice in a room full of noisy people!
• Solution: Send out a modulated IR signal. That is, a
signal at a precise frequency which is not prevalent
naturally.
IR Modulation
• The Boe-Bot IR detectors are tuned to detect IR at a
modulated frequency of 38.5 khz.
Khz = 1000 Herz = 1000 cycles per second
• Thus, the Boe-Bot IR detectors will respond only to an
IR signal that flashes on/off about 38,500 times a
second.
• This will filter out: sunlight and bodies (0 khz) and
electric lights (100 or 120 hz).
• RF systems and FM and AM radio work the same way
– they ignore frequencies they don't care about.
Boe-Bot IR Circuit
IR Detector
Parts and Assembly
(Clear)
LED= Light-Emitting Diode
• A Diode is a one-way current device -- current can
flow in only one direction. An LED is a diode that
emits light as current is passed through it.
• Note the connections on the LED:
Anode: Connected to + side of
voltage. Typically has a longer lead.
Cathode: Connected to – side
of voltage. Typically has a
shorter lead AND a flat portion
on the lens.
LED– Current Limiting Problem
• LEDs have minimal resistance to current flow. A
5 volt source can destroy an LED if current is not
restricted. From Ohm's Law, if an LED has R of 1
, how much current will try to flow? An LED drops
approximately 1.4V, leaving 3.6V.
I = (5V-1.4V)/1 = 3.6 A
The maximum current a typical LED can handle is
around 30mA, or .030 A.
3.6 A will fry an LED in seconds!!
LED– Current Limiting Problem
• So, to use an LED, we must also use a resistor
to limit the current passing through. In our circuit,
we use a 1K  resistor. Then, the current I will be
I = (5V-1.4V)/1000 = .0036 A
• This is well below the LED limit
of 0.30 A.
• IMPORTANT NOTE: Always use a current
limiting resistor with LED’s.
Boe-Bot IR Circuit
• Build the circuit as shown in the previous
slides (or on pp. 238-239 in the text)
• Make sure all of the connections are correct
before moving on.
Programming
• To use the sensor:
– We need to send a square wave (on/off) signal to
the IR LED at 38.5KHz
5v
– One cycle takes
1/38500 = .00002597 s
13 13
= 26 microsec
26microsec
– This requires a 13
microsec pulse of 5V
Followed by a 13 microsec pulse of 0V.
OutputCaptureEx
• To create a waveform we use the ZBasic
subroutine OutputCaptureEx.
• Call OutputCaptureEX(pin, intervals, count, flags,
repeatCount)
• Note: Each entry in the intervals array specifies a time
interval, in units of the I/O Timer clock period (by default,
about 67.8ns for devices running at 14.7MHz), for each
segment of the waveform.
OutputCaptureEx
• Our waveform has two parts – an up and down. To
calculate the two entries in the intervals array, we
can use (CPUfreq/freq)/2) * CPUclockTicTime
• Why?
(CPUfreq/freq)/2) * CPUclockTicTime =
(CPUfreq/freq)/2) * (1/CPUFreq) = ((1/freq)/2)
= length of one-half cycle
• The CPU’s clock frequency (as an integer) is stored in
a special constant named register.cpufrequency
OutputCaptureEx
• Thus, if we want to output a signal of frequency freq
we use
CUint(csng(register.cpufrequency) / (freq * 2.0) + 0.5)
• Here, csng makes register.cpugrequency into a single
value and Cuint converts the answer to an integer.
Also, we add 0.5 to round up to the nearest integer.
Programming
• Here is the code for a subroutine to output a signal
of frequency = freq on pin = pin:
dim pulseState as Byte = 1 ' Output pulses at state 1 (5 Volts)
dim cycles as integer = 20 ' Number of cycles to be emitted
sub sendSignalOnPin(byVal freq as single, byVal pin as byte)
dim pd(1 to 2) as unsignedinteger ' Array to store waveform
call putpin(pin, startstate)
pd(1) = CUint(csng(register.cpufrequency) / (freq * 2.0) + 0.5)
pd(2) = pd(1)
call outputcaptureex(pin, pd, 2, pulsestate, cycles)
end sub
IR Object Avoidance
• Our IR detectors have outputs that are just like the
whiskers.
• When no object is detected, the output is high; when
an object is detected, the output is low.
• We can use this to avoid objects, just as we did when
we used whiskers.
Today’s Assignment
• Download the sample code named “IRHeadlight”
from the code page of the website. We will discuss
the code and then you should load the code into
your Boe-bot and try it out.
• Then, try out at least one of the projects on the next
two slides. Each team should be ready to demo their
results at the beginning of class on Thursday!
Today’s Assignment
• Project 1: Catch Me if you Can!
– Design a Boe-Bot program that has your robot
slowly rotate in place until it detects a nearby
object. As soon as it detects the object, it locks
onto and chases the object.
Today’s Assignment
• Project 2: Mama Dog and Puppy
– Find another group to team up with. Have one
robot move randomly, sending out an infrared
signal using the IR LED (like a beacon). (Mama
dog)
– The other robot should roam around, using its IR
detectors to look for the first robot. Once it finds
the robot, it should follow it without losing it. (Like
a puppy dog!)