Is it the SBYTE bug again?

I am not sure if this is the SBYTE bug again or not, but it seems to be acting in a similar manner. I have a variable that is defined as SWORD which can legitimately take positive or negative values. This variable holds the position to set a servo to, which can be from -900 (-90 degrees) through 900 (+90 degrees).

In my code, I can not see how this variable can possibly NOT be set correctly. It is set to the value of an SBYTE and an SWORD multiplied by a constant of 10. The other variables are both being set correctly according to what I see when I have the robot on the bench and have my debugging serout statements turned on, but the actual servo position variable is coming out zero.

I don’t know that posting a code snippet would provide enough information, so send me a PM if you need to see the code and I will give you the URL to download it. This is over 13k of compiled code, so it’s rather large and close to the limit of what I can stuff into a Basic ATOM.

I am using IDE 5.3.0.1 with the patch for the ADIN pin bug.

8-Dale

Really gona need to see some code here. And perferably not the whole 13k. You should be able to narrow it down a little bit.

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

I think I found what was causing this problem, but I am not sure it is a complete fix. I had a place in the Rotate_To_Bearing routine at the end where I set scandir and scanpath to zero. If scandir is 0, this would definitely through currpan out of whack. I am no longer setting scandir or scanpath to 0 at the end of the Rotate_To_Bearing routine.

I am NOT sure this is a complete fix for this problem though and there may be another cause lurking somewhere. I need to do some more testing, but I can’t seem to recreate the problem when the robot is up on the bench.

8-Dale