| SRF04 to OOPic Example Last Modified: 2006-10-26 | | |
| Acroname Robotics | |||
![]() Introduction In this example, an OOPic microcontroller uses a Devantech SRF04 Ultrasonic Range Finder to measures distances to obstacles. The OOPic tells the sonar module to emit a "ping" and measures the time it takes to receive an echo. It converts this time to a distance in inches. The OOPic controls 2 LEDs. It turns on LED1 and turns off LED2 when the distance exceeds a maximum threshold. It turns on both LEDs when the distance is within an acceptable window. It turns off LED1 and turns on LED2 when the distance goes below a minimum threshold. This is effective logic for implementing sonar-guided wall-following behavior in a robot with 2-motor on-off steering . By turning motors on or off instead of LEDs, such a robot can steer towards a wall, straight, or away from a wall. Circuit Schematic ![]() In the above schematic, the OOPic is powered by an external 9V battery. The OOPic voltage regulator also supplies power to the SRF04 . According to the Devantech specs,the SRF04 sonar module may require up to 50ma. The voltage regulator on a standard OOPic is rated at 100ma. This is sufficient for this circuit. However, if the OOPic must provide power to more than one sonar module or a sonar and several other devices, it will be necessary to upgrade the voltage regulator. Instructions for doing this may be found at the OOPic Web Site. Source Code This code uses BASIC syntax. It runs with version 3.0.1 of the OOpic compiler and version A.1.7 of the OOpic chip. '
' Devantech SRF04 Sonar Test Program
' Works with Ver. 3.0.1 of OOPIC Compiler
'
dim w1 as new oWord ' sonar reading
dim led1 as new Odio1 ' indicator lights
dim led2 as new Odio1
sub main()
led1.IOLine=6
led1.Direction=cvOutput
led2.IOLine=7
led2.Direction=cvOutput
call initSonar
do
call readSonar
if (w1>20) then
led2=1
led1=0
elseif (w1>16) then
led2=1
led1=1
else
led2=0
led1=1
end if
oopic.delay=20
loop
end sub
dim echoTimer as new oTimer
dim tmrControl as new oGate(1)
dim INIT as new oDio1
dim ECHO as new oDio1
sub initSonar()
' sound travles 1 inch every 73.746us
' timer uses 625KHz clock frequency (5MHz/8)
' 625KHz is 1.6us per clock tick
' maximum range of sonar is 9m (118 inches, 300cm)
' distance of echo round trip is 118*2=236 inches
' 236 * 73.746us / 1.6us = 10877 ticks at max range
' 10877 / (118) = 92 --> conversion factor for inches
' 10877 / (300) = 36 --> conversion factor for cm
echoTimer.ExtClock=cvOff
echoTimer.PreScale=3
INIT.IOLine=31
INIT.Direction=cvOutput
INIT.value=0
ECHO.IOLine=30
ECHO.Direction=cvInput
tmrControl.Input1.Link(ECHO.value)
tmrControl.Output.Link(echoTimer.Operate)
tmrControl.Operate=cvTrue
end sub
sub readSonar()
echoTimer.value=0 ' clear old result
INIT.value=1 ' pulse the sonar init input
INIT.value=0
oopic.delay=1 ' give echo chance to go high
' wait until echo goes low again to return reading
' ECHO may be low already, but could take up to 36ms
' (timer automatically runs while echo is high)
while (ECHO.value=1)
wend
w1=echoTimer.value/92 ' convert reading to inches
oopic.delay=1 ' give time for proper reset
end sub
The controller must raise and lower the INIT pin on the sonar module to begin a reading. After the INIT input pin goes low, the sonar module emits a ping. After the ping, the ECHO output goes high. It goes low again when the module receives an echo. An OOpic oTimer object runs while the ECHO output is high in order to measure the echo return time. A virtual circuit controls the timer. A 10ms delay after the INIT pulse ensures that the ECHO line has chance to go high. A while loop gives the ECHO line additional time to go low. (It may take up to 36 ms for the ECHO line to go low if the module does not detect an ECHO.) After recording the reading, the OOPic waits 10ms to give the module time to reset properly. Revision History:
| ||
Related Links: The Devantech Sonic Range Finders Comparison and Examples Related Examples: Example code and schematic for Devantech SRF04 sensor interface to BrainStem GP | ||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved. |