Thanks robot dude, here is the code :
[code]’ ==============================================================================
’
’ File… navigation_beacon.bas
’ Purpose… Define a simple navigation based on IR beacon informations
’ Author… © 2010 Guillaume FRAPSAUCE – All Rights Reserved
’ E-mail… [email protected]
’ Started… 08/07/2010
’ Modified… 21/07/2010
’
’ ==============================================================================
’ ------------------------------------------------------------------------------
’ Program Description
’ ------------------------------------------------------------------------------
’ This is a simple program for the navigation of the robot based on the IR beacon
’ Direction informations and on the US sensors.
’ We get the direction information from the IR beacon to detect the leader and
’ Turn the robot toward this direction. Then we are going straight on toward the
’ Location of the leader until the US sensor indicates the leader’s proximity.
’ ------------------------------------------------------------------------------
’ Variables definition
West var Word ’ Beacon West output
South var Word ’ Beacon South output
East var Word ’ Beacon East output
North var Word ’ Beacon North output
dir_W var nib
dir_S var nib
dir_E var nib
dir_N var nib
i var byte ’ Incremental value
right var byte ’ Right US sensor value
left var byte ’ Left US sensor value
distanceL var byte ’ Distance from an obstacle to the left US sensor
distanceR var byte ’ Distance from an obstacle to the right US sensor
thresholdmin var byte ’ Distance min
thresholdmax var byte ’ Distance max
ltpwr var word ’ Left motors command
rtpwr var word ’ Right motors command
’ ltpwr < 2950 --> Backward
’ ltpwr = 2950 --> Stop
’ ltpwr > 2950 --> Forward
’ Variable definition
distanceL=0
distanceR=0
thresholdmin = 20 'cm
thresholdmax = 30 'cm
ltpwr=2950
rtpwr=2950
’ ------------------------------------------------------------------------------
’ Main Program Loop
’ ------------------------------------------------------------------------------
MAIN
ADIN P16, West
ADIN P17, South
ADIN P18, East
ADIN P19, North
’ serout s_out, i9600, “WEST :”,DEC West , " SOUTH :" , DEC South, " EAST :" , DEC EAST, " NORTH :" , DEC NORTH,13]
if West<500 Or South <500 or East <500 or North<500 then ; Detection of the Robot leader
gosub beacon
gosub ultrasonic
gosub calibration
gosub navigation
gosub motors
else
right=2950
left=2950
endif
gosub motors
goto main
’ ------------------------------------------------------------------------------
’ Beacon
’ ------------------------------------------------------------------------------
beacon
dir_W=0
dir_S=0
dir_E=0
dir_N=0
i=0
do ; Storage of the past 5 directions
if West < 200 then
dir_W=dir_W+1
i=i+1
endif
if South < 200 then
dir_S=dir_S+1
i=i+1
endif
if East < 200 then
dir_E=dir_E+1
i=i+1
endif
if North < 200 then
dir_N=dir_N+1
i=i+1
endif
pause 40
while i <5
; We take the dominant direction as the Leader’s direction
if dir_W > dir_S & dir_W > dir_E & dir_W > dir_N then
serout s_out, i9600, "WEST",13]
dir_W=1
dir_S=0
dir_E=0
dir_N=0
elseif dir_S > dir_W & dir_S > dir_E & dir_S > dir_N & dir_W > dir_E
serout s_out, i9600, "SOUTH-WEST",13]
dir_W=0
dir_S=0
dir_E=1
dir_N=0
elseif dir_S > dir_W & dir_S > dir_E & dir_S > dir_N & dir_W < dir_E
serout s_out, i9600, "SOUTH-EAST",13]
dir_W=1
dir_S=0
dir_E=0
dir_N=0
elseif dir_S > dir_W & dir_S > dir_E & dir_S > dir_N & dir_W = dir_E
serout s_out, i9600, "SOUTH",13]
dir_W=0
dir_S=1
dir_E=0
dir_N=0
elseif dir_E > dir_W & dir_E > dir_S & dir_E > dir_N
serout s_out, i9600, "EAST",13]
dir_W=0
dir_S=0
dir_E=1
dir_N=0
elseif dir_N > dir_W & dir_N > dir_S & dir_N > dir_E
serout s_out, i9600, "NORTH",13]
dir_W=0
dir_S=0
dir_E=0
dir_N=1
else
serout s_out, i9600, "PROBLEM",13]
return
endif
return
’ ------------------------------------------------------------------------------
’ Ultra Sonic Sensors
’ ------------------------------------------------------------------------------
ultrasonic
' Sensor RIGHT
high P15
input p12
SerIn P12, n9600, [WAIT("R"), dec right ] 'wait for "R" and get the data
Low P15
' Sensor LEFT
high P15
input p13
SerIn P13, n9600, [WAIT("R"), dec left ] 'wait for "R" and get the data
Low P15
return
’ ------------------------------------------------------------------------------
’ US sensor calibration
’ ------------------------------------------------------------------------------
calibration
;=====================================================================================
; RIGHT US sensor calibration
if right < 8 then
distanceR=15
endif
if right >= 8 and right < 9 then
distanceR=20
endif
if right >= 9 and right < 11 then
distanceR=25
endif
if right >= 11 and right < 12 then
distanceR=30
endif
if right >= 12 and right < 14 then
distanceR=35
endif
if right >= 14 and right < 17 then
distanceR=40
endif
if right >= 17 and right < 18 then
distanceR=45
endif
if right >= 18 and right < 20 then
distanceR=50
endif
if right >= 20 and right < 21 then
distanceR=55
endif
if right >=21 and right < 26 then
distanceR=60
endif
if right >= 26 and right < 29 then
distanceR=70
endif
if right >= 29 and right <33 then
distanceR=80
endif
if right >= 33 then
distanceR=100
endif
;=====================================================================================
; Left US sensor calibration
if left < 8 then
distanceL=15
endif
if left >= 8 and left < 9 then
distanceL=20
endif
if left >= 9 and left < 11 then
distanceL=25
endif
if left >= 11 and left < 12 then
distanceL=30
endif
if left >= 12 and left < 14 then
distanceR=35
endif
if left >= 14 and left < 17 then
distanceL=40
endif
if left >= 17 and left < 18 then
distanceL=45
endif
if left >= 18 and left < 20 then
distanceL=50
endif
if left >= 20 and left < 21 then
distanceL=55
endif
if left >=21 and left < 26 then
distanceL=60
endif
if left >= 26 and left < 29 then
distanceL=70
endif
if left >= 29 and left <33 then
distanceL=80
endif
if left >= 33 then
distanceL=100
endif
serout s_out,i9600,"RIGHT: ", dec distanceR,"cm", " LEFT: ", dec distanceL,"cm", 13] 'Display the distance
return
’ ------------------------------------------------------------------------------
’ Navigation
’ ------------------------------------------------------------------------------
navigation
if dir_S=1 then
ltpwr=2450
rtpwr=3450
endif
if dir_W=1 then
ltpwr=2450
rtpwr=3450
endif
if dir_E=1 then
ltpwr=3450
rtpwr=2450
endif
if dir_N=1 then
; Too close
if distanceR < thresholdmin then
rtpwr=2450
endif
if distanceL < thresholdmin then
ltpwr=2450
endif
; Good interval
if distanceR >= thresholdmin and distanceR <= thresholdmax then
rtpwr=2950
endif
if distanceL >= thresholdmin and distanceL <= thresholdmax then
ltpwr=2950
endif
; Too far
if distanceR > thresholdmax then
rtpwr=3450
endif
if distanceL > thresholdmax then
ltpwr=3450
endif
endif
return
’ ------------------------------------------------------------------------------
’ Motors
’ ------------------------------------------------------------------------------
motors
low p4
low p5
pulsout 5, ltpwr ; Left wheel
pulsout 4, rtpwr ; Right wheel
pause 10
return
[/code]