photoresistor program
·· I'm designinig a light following robot using a combination of the "flashlight following robot" and
·"avoiding shadows" code from the robotics with the boe bot manual. The code works fine, however,
·when the robot gets within 2 feet of the light it jerks left and right and won't move towards the
·light. I think the problem is that the microprocessor is moving to quick through the code for
·the shawdow avoiding robot code.
·
Any help would be appreciated ?
·
'
[noparse][[/noparse] Title ]
' {$STAMP BS2} ' Stamp directive.' {$PBASIC 2.5} ' PBASIC directive.DEBUG "Program Running!"'
[noparse][[/noparse] Constants ]
LeftAmbient CON 132 ' Ambient left lightRightAmbient CON 152 ' Ambient Right lightLeftBright CON 24 ' Bright left lightRightBright CON 43 ' Bright light' Average Scale factorLeftThreshold CON LeftBright + LeftAmbient / 2 * 5 / 8 ' Threshold for leftRightThreshold CON RightBright + RightAmbient / 2 * 5 / 8 ' Threshold for right'
[noparse][[/noparse] Variables ]
' left & right photoresistors.timeLeft VAR Word 'Left light measureementtimeRight VAR Word 'Right light measurementmaneuver VAR Nib ' maneuver robotsensors VAR ByteRunStatus DATA 0average VAR Word ' average left and right light valuesdifference VAR Wordtemp VAR WordServoLeft PIN 15 ' Left servoServoRight PIN 14 ' Right servocounter VAR Byte'
[noparse][[/noparse]Bot maneuvers]
Forward CON 0 ' ForwardBackward CON 1 ' BackwardRotateLeft CON 2 ' RotateLeftRotateRight CON 3 ' RotateRightPivotLeft CON 4 ' Pivot to the leftPivotRight CON 5 ' Pivot to the rightCurveLeft CON 6 ' Curve to the leftCurveRight CON 7 ' Curve to the right' Servo pulse width rotationsFS_CCW CON 850 ' Full speed counterclockwiseFS_CW CON 650 ' Full speed clockwiseNO_ROT CON 750 ' No rotationLS_CCW CON 770 ' Low speed counterclockwiseLS_CW CON 730'
[noparse][[/noparse] Initialization ]
GOSUB Reset ' Uses reset button to start program'
[noparse][[/noparse] Subroutine - Main Routine ]
DOGOSUB Test_PhotoresistorsIF (timeLeft < LeftThreshold) AND (timeRight < RightThreshold) THEN 'Is light in view of bothmaneuver=Forward ' move forward if both photoresistors see lightGOSUB Servos_And_SensorsELSEIF (timeLeft < LeftThreshold) THEN ' Left detects flashlight beam,maneuver=RotateLeft ' rotate left if left detects lightGOSUB Servos_And_SensorsELSEIF (timeRight < RightThreshold) THEN ' Right detects flashlight beam,maneuver=RotateRight 'move right if right detects lightGOSUB Servos_And_SensorsELSEGOSUB Look_About ' if light is not found by either photo resistorENDIFPAUSE 20 ' Pause between pulses.LOOP'
[noparse][[/noparse] Subroutine - Reset]
Reset:READ RunStatus, temp ' Byte @RunStatus -> temptemp = temp + 1 ' Increment tempWRITE RunStatus, temp ' Store new value for next timeIF (temp.BIT0 = 1) THEN ' Examine temp.BIT0DEBUG CLS, "Press/release Reset", CR, ' 1 -> end, 0 -> keep going"button..."ENDELSEDEBUG CR, "Program running..."ENDIFRETURN'
[noparse][[/noparse] Subroutine -Servos_And_Sensors]
Servos_And_Sensors:GOSUB Pulse_Servos ' Call Pulse_Servos subroutine' Call sensor subroutine(s).Pulse_Servos:' Pulse to left servoLOOKUP maneuver, [noparse][[/noparse] FS_CCW, FS_CW, FS_CW, FS_CCW,NO_ROT, FS_CCW, LS_CCW, FS_CCW ], tempPULSOUT ServoLeft, temp' Pulse to right servoLOOKUP maneuver, [noparse][[/noparse] FS_CW, FS_CCW, FS_CW, FS_CCW,FS_CW, NO_ROT, FS_CW, LS_CW ], tempPULSOUT ServoRight, temp' Pause between pulses (remove when using IR object detectors + QTIs).' PAUSE 20RETURN'
[noparse][[/noparse] Subroutine -Look_About]
Look_About: ' Starting point labelGOSUB Test_Photoresistors ' Test photo resistors againaverage = timeRight + timeLeft / 2 ' Average both readingsdifference = average / 6 ' take difference' Shadow significantly stronger on left detector, turn right.IF (timeLeft > timeRight + difference) THENmaneuver = RotateRightGOSUB Servos_And_Sensors' Shadow significantly stronger on right detector, turn left.ELSEIF (timeRight > timeLeft + difference) THENmaneuver=RotateLeftGOSUB Servos_And_SensorsELSEmaneuver=ForwardGOSUB Servos_And_SensorsENDIFRETURN'
[noparse][[/noparse] Subroutine -Test_Photoresistors]
Test_Photoresistors:HIGH 2' Left RC time measurement.PAUSE 3RCTIME 2,1,timeleftHIGH 3 ' Right RC time measurement.PAUSE 3RCTIME 3,1,timeRightRETURN
·"avoiding shadows" code from the robotics with the boe bot manual. The code works fine, however,
·when the robot gets within 2 feet of the light it jerks left and right and won't move towards the
·light. I think the problem is that the microprocessor is moving to quick through the code for
·the shawdow avoiding robot code.
·
Any help would be appreciated ?
·
'
[noparse][[/noparse] Title ]
' {$STAMP BS2} ' Stamp directive.' {$PBASIC 2.5} ' PBASIC directive.DEBUG "Program Running!"'
[noparse][[/noparse] Constants ]
LeftAmbient CON 132 ' Ambient left lightRightAmbient CON 152 ' Ambient Right lightLeftBright CON 24 ' Bright left lightRightBright CON 43 ' Bright light' Average Scale factorLeftThreshold CON LeftBright + LeftAmbient / 2 * 5 / 8 ' Threshold for leftRightThreshold CON RightBright + RightAmbient / 2 * 5 / 8 ' Threshold for right'
[noparse][[/noparse] Variables ]
' left & right photoresistors.timeLeft VAR Word 'Left light measureementtimeRight VAR Word 'Right light measurementmaneuver VAR Nib ' maneuver robotsensors VAR ByteRunStatus DATA 0average VAR Word ' average left and right light valuesdifference VAR Wordtemp VAR WordServoLeft PIN 15 ' Left servoServoRight PIN 14 ' Right servocounter VAR Byte'
[noparse][[/noparse]Bot maneuvers]
Forward CON 0 ' ForwardBackward CON 1 ' BackwardRotateLeft CON 2 ' RotateLeftRotateRight CON 3 ' RotateRightPivotLeft CON 4 ' Pivot to the leftPivotRight CON 5 ' Pivot to the rightCurveLeft CON 6 ' Curve to the leftCurveRight CON 7 ' Curve to the right' Servo pulse width rotationsFS_CCW CON 850 ' Full speed counterclockwiseFS_CW CON 650 ' Full speed clockwiseNO_ROT CON 750 ' No rotationLS_CCW CON 770 ' Low speed counterclockwiseLS_CW CON 730'
[noparse][[/noparse] Initialization ]
GOSUB Reset ' Uses reset button to start program'
[noparse][[/noparse] Subroutine - Main Routine ]
DOGOSUB Test_PhotoresistorsIF (timeLeft < LeftThreshold) AND (timeRight < RightThreshold) THEN 'Is light in view of bothmaneuver=Forward ' move forward if both photoresistors see lightGOSUB Servos_And_SensorsELSEIF (timeLeft < LeftThreshold) THEN ' Left detects flashlight beam,maneuver=RotateLeft ' rotate left if left detects lightGOSUB Servos_And_SensorsELSEIF (timeRight < RightThreshold) THEN ' Right detects flashlight beam,maneuver=RotateRight 'move right if right detects lightGOSUB Servos_And_SensorsELSEGOSUB Look_About ' if light is not found by either photo resistorENDIFPAUSE 20 ' Pause between pulses.LOOP'
[noparse][[/noparse] Subroutine - Reset]
Reset:READ RunStatus, temp ' Byte @RunStatus -> temptemp = temp + 1 ' Increment tempWRITE RunStatus, temp ' Store new value for next timeIF (temp.BIT0 = 1) THEN ' Examine temp.BIT0DEBUG CLS, "Press/release Reset", CR, ' 1 -> end, 0 -> keep going"button..."ENDELSEDEBUG CR, "Program running..."ENDIFRETURN'
[noparse][[/noparse] Subroutine -Servos_And_Sensors]
Servos_And_Sensors:GOSUB Pulse_Servos ' Call Pulse_Servos subroutine' Call sensor subroutine(s).Pulse_Servos:' Pulse to left servoLOOKUP maneuver, [noparse][[/noparse] FS_CCW, FS_CW, FS_CW, FS_CCW,NO_ROT, FS_CCW, LS_CCW, FS_CCW ], tempPULSOUT ServoLeft, temp' Pulse to right servoLOOKUP maneuver, [noparse][[/noparse] FS_CW, FS_CCW, FS_CW, FS_CCW,FS_CW, NO_ROT, FS_CW, LS_CW ], tempPULSOUT ServoRight, temp' Pause between pulses (remove when using IR object detectors + QTIs).' PAUSE 20RETURN'
[noparse][[/noparse] Subroutine -Look_About]
Look_About: ' Starting point labelGOSUB Test_Photoresistors ' Test photo resistors againaverage = timeRight + timeLeft / 2 ' Average both readingsdifference = average / 6 ' take difference' Shadow significantly stronger on left detector, turn right.IF (timeLeft > timeRight + difference) THENmaneuver = RotateRightGOSUB Servos_And_Sensors' Shadow significantly stronger on right detector, turn left.ELSEIF (timeRight > timeLeft + difference) THENmaneuver=RotateLeftGOSUB Servos_And_SensorsELSEmaneuver=ForwardGOSUB Servos_And_SensorsENDIFRETURN'
[noparse][[/noparse] Subroutine -Test_Photoresistors]
Test_Photoresistors:HIGH 2' Left RC time measurement.PAUSE 3RCTIME 2,1,timeleftHIGH 3 ' Right RC time measurement.PAUSE 3RCTIME 3,1,timeRightRETURN
Comments
You are probably never executing the maneuver= Foward command
I dont understand what you mean by "gets within 2 feet of the light ", is the ligth at a constant location of are you movind it to direct the bot?