Shop OBEX P1 Docs P2 Docs Learn Events
Problem with this code for BoeBot — Parallax Forums

Problem with this code for BoeBot

decoderdecoder Posts: 1
edited 2015-03-25 11:10 in Learn with BlocklyProp
Hi, i'm building an IR boe bot but i'm having problems with the code.
The robot is actually moving in spurts, i think that there's a problem between the pulses.
' {$STAMP BS2}
' {$PBASIC 2.5}

irDetectLeft   VAR     Bit                   ' Variable Declarations
irDetectRight  VAR     Bit
pulseLeft      VAR     Word
pulseRight     VAR     Word
pulseCount VAR Byte
timeleft VAR Word
timeright VAR Word
average VAR Word
difference VAR Word

FREQOUT 4, 2000, 3000
GOSUB forward_Pulse

  DO
GOSUB test_photoresistors
GOSUB average_and_difference
GOSUB navigate
LOOP

test_photoresistors:
HIGH 5
PAUSE 3
RCTIME 5,1,timeleft
HIGH 0
PAUSE 3
RCTIME 0,1,timeright
DEBUG HOME, "timeright =· ", DEC5 timeright ," timeleft =· ", DEC5 timeleft
RETURN

average_and_difference:
average = timeright + timeleft / 2
difference = average / 6
RETURN


navigate:


  FREQOUT 10, 1, 38500
  irDetectLeft = IN11

  FREQOUT 3, 1, 38500
  irDetectRight = IN4

  IF (timeleft < 300) THEN
PULSOUT 13, 750
PULSOUT 12, 0
PAUSE 10

 ELSEIF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
    pulseLeft = 650
    pulseRight = 850
  ELSEIF (irDetectLeft = 0) THEN
  DEBUG "Right"
    pulseLeft = 1300
    pulseRight = 1300
  ELSEIF (irDetectRight = 0) THEN
   DEBUG "left"
    pulseLeft = 650
    pulseRight = 650
  ELSE
   DEBUG "Forward"
    pulseLeft = 850
    pulseRight = 650
  ENDIF

  PULSOUT 13,pulseLeft
  PULSOUT 12,pulseRight
  PAUSE 15

'Sub routine
 Forward_Pulse:
PULSOUT 13,850
PULSOUT 12,650
PAUSE 20

Can anyone help me to fix it?
Thank you

Comments

  • edited 2015-03-25 11:10
    GOSUB foreard_pulse sends the code to a subroutine without a return statement. There should be a return at the end of forward_pulse (after pause 20). Since forward pulse is only called once, you could actually remove the GOSUB Forward_Pulse call and the entire forward_pulse subroutine.

    The Navigate subroutine is also missing a return statement after the pause 15.

    Without return statements, the code jumps back to the beginning after it gets to the last line. This restarts the program. That's probably why you see twitches instead of continuous motion.

    Also, since the Navigate subroutine is setting up pulseLeft and pulseRight values for a couple pulseout commands, this if block should also set values of pulseLeft and pulseRight. Make sure that they are in the 650 to 850 range. The value of 0 is not valid for pulseout going to servos.

    IF(timeleft < 300)
    PULSOUT 13, 750
    PULSOUT 12, 0
    PAUSE 10
Sign In or Register to comment.