OK, but there is still potentially several routines involved in this so I tried to pair it all down to just the required stuff. The errant variable currpan is in the Scan_With_Turret routine called by Scan_Turret.
[code]’
’ Pan/Tilt Turret Configuration
’
pan con p2 ’ Pan Servo Pin
minpan con -900
maxpan con 900
tilt con p3 ’ Tilt Servo Pin
mintilt con -100
maxtilt con 900
scanposinc con 10 ’ Turret Scan increment in degrees
’
’ IR Rangers
’
IR_Distance var word(5) ’ Distance object was detected at
IR_Detected var bit(5) ’ Was object detected? 0 = No, 1 = Yes
IR_Threshold var word(5) ’ Detection threshold for each IR Ranger
IR_Offset var sbyte(5) ’ Offset to threshold distance for detection
IR_Status var word ’ IR Detection status - 1 = Left Front, 2 = Right Front, 3 = BOTH Front, 4 = Turret IR
’ Expansion: 8 = Left Side (across wheel), 16 = Right Side (across wheel), 24 = BOTH
’ Sides (across wheels).
threshold var byte ’ Calculated detection threshold for IR Rangers
distance var byte ’ Shortest distance to any obstacle from any IR sensor
offset var byte ’ Largest of the IR Sensor Offsets
scanrange var word ’ A/D result variable
scanpath var sword ’ The bearing from the robot’s current forward facing direction of a clear path
scandir var sbyte ’ Scan direction, 1 = to the right, -1 = to the left
floating var long ’ Floating point math result storage
index var byte ’ Index into the IR Arrays
tfloat var float
power2 var long
too_close var bit ’ 1 = Too close to obstacle, 0 = Can get closer
’
’ Pan Tilt Turret
’
currpan var sword
currtilt var sword
savereps var word
’
’ Pan/Tilt Turrent Scanning
’
scanpos var sword
pathclear var bit ’ 1 = Path clear, 0 = obstructed
’ Scan using the Pan/Tilt Turret
Scan_Turret:
gosub Check_Sensors
gosub Check_Distance
if (IRPD_Status = 0) AND (IR_Status = 0) then
goto End_Scan
endif
if sendoutput then
serout S_OUT, I8N1_2400, "Scan_Turret 2: IRPD_Status = ", DEC IRPD_Status, ", IR_Status = ", DEC IR_Status, ", scandir = ", SDEC scandir, 10, 13]
endif
currtilt = 50
gosub Scan_With_Turret
if (pathclear = 0) OR (scanpos >= 90) OR (scanpos = 0) then
scandir = scandir * -1 ' Switch to try scanning the opposite direction
gosub Scan_With_Turret
endif
scanpath = scanpos ' Save the relative bearing of the path found
gosub Backup
End_Scan:
if (scanpath = 0) OR (pathclear = 0) then
scanpath = stdturn
scandir = 1
endif
return
Scan_With_Turret:
pathclear = 0
scanpos = scanposinc
gosub Check_Distance
gosub Home_Turret
do
currpan = scanpos * scandir * 10
if sendoutput then
serout S_OUT, I8N1_2400, "Scan_With_Turret 1: scanpos = ", DEC scanpos, ", scandir = ", SDEC scandir, ", currpan = ", SDEC currpan, 10, 13]
endif
' Position the Pan/Tilt Turret
servo pan, currpan
servo tilt, currtilt
' Check to see if the path is clear and set the flag
gosub Check_IR_Rangers
if IR_Detected(IR_TU) = 0 then
pathclear = 1
endif
if sendoutput then
serout S_OUT, I8N1_2400, "Scan_With_Turret 2: scanpos = ", DEC scanpos, ", scandir = ", SDEC scandir, ", scanpath = ", SDEC scanpath, ", pathclear = ", DEC pathclear, ", Detected = ", DEC IR_Detected(2), ", Distance = ", DEC IR_Distance(2), " cm, IR_Status = ", DEC IR_Status, 10, 13]
endif
scanpos = scanpos + scanposinc
while (scanpos < 90) AND (pathclear = 0)
return
’ Check the distance to the nearest obstacle detected - this should be called right after a call to
’ Roll_Forward or Creep_Forward.
Check_Distance:
if sendoutput then
serout S_OUT, I8N1_2400, "Check_Distance, scanpath = ", SDEC scanpath, ", scandir = ", SDEC scandir, 10, 13]
endif
gosub Check_Sensors
' Ignore hits on both sides
IR_Status = IR_Status min 2
IRPD_Status = IRPD_Status min 2
if (IR_Status = IR_RightFront) AND (IR_Distance(IR_RF) <= (IR_Threshold(IR_RF) + IR_Offset(IR_RF))) then
scandir = -1
elseif (IR_Status = IR_LeftFront) AND (IR_Distance(IR_LF) <= (IR_Threshold(IR_LF) + IR_Offset(IR_LF)))
scandir = 1
elseif IRPD_Status <> 0
gosub Home_Turret
if IR_Detected(IR_TU) AND (IR_Distance(IR_TU) < (IR_Threshold(IR_TU) + IR_Offset(IR_TU))) then
scanpath = stdturn
endif
if (IRPD_Status = IRPD_Right) then
scandir = -1
elseif (IRPD_Status = IRPD_Left)
scandir = 1
endif
else
scanpath = 0
endif
return
’ Check the range to any obstacle from any sensor
Check_Range:
gosub Check_IR_Rangers
' Get the shortest distance from an obstacle to any sensor
distance = IR_Distance(IR_LF) max IR_Distance(IR_RF)
distance = distance max IR_Distance(IR_TU)
' See if we are too close to an obstacle on any side
too_close = distance < (IR_Threshld_Dist + offset)
return[/code]
The really odd thing, is everything seems to behave perfectly as long as I have the robot up on the bench and have the serout stuff enabled (sendoutput = 1).
8-Dale