Shop OBEX P1 Docs P2 Docs Learn Events
Problems in pbasic. Simple boe-bot roaming routine fail — Parallax Forums

Problems in pbasic. Simple boe-bot roaming routine fail

asabotasabot Posts: 8
edited 2013-12-11 11:05 in Robotics
I'm trying to make my boe-bot go forward 5 seconds, do a 135, 5 seconds straight again and 135 in opposite direction from previous, unless the IR sensors detect something. The last routine was just to roam straight if the sensors were not detecting, and that worked fine. Now the sensors will not detect while the "else" part of the loop is being carried out. I have tried a million different things to fix it, but none of them worked, so i'm just going to post the basic code of what needs to happen, the best that I can understand it right now.
' {$STAMP BS2}
' {$PBASIC 2.5}


irdetectleft VAR Bit
irdetectright VAR Bit
pulsecount VAR   Byte
FREQOUT 5,2000,3000
DO
  FREQOUT 8,1,45500
  irdetectleft=IN9
  FREQOUT 2,1,45500
  irdetectright=IN0
  IF(irdetectleft=0) AND (irdetectright=0) THEN
    GOSUB back_up
    GOSUB turn_left
    GOSUB turn_left
  ELSEIF (irdetectleft=0) THEN
    GOSUB back_up
    GOSUB turn_right
  ELSEIF (irdetectright=0) THEN
    GOSUB back_up
    GOSUB turn_left
  ELSE
    GOSUB forward_pulse
    GOSUB onethirtyfive
    GOSUB forward_pulse
    GOSUB onethirtyfive2
  ENDIF
  LOOP
  forward_pulse:
  FOR pulsecount=0 TO 200
    PULSOUT 13,650
    PULSOUT 12,850
    PAUSE 20
    NEXT
    RETURN
  turn_left:
    FOR pulsecount=0 TO 20
      PULSOUT 13, 850
      PULSOUT 12, 850
      PAUSE 20
      NEXT
      RETURN
  turn_right:
     FOR pulsecount=0 TO 20
      PULSOUT 13, 650
      PULSOUT 12, 650
      PAUSE 20
      NEXT
      RETURN
  back_up:
    FOR pulsecount=0 TO 40
      PULSOUT 13, 850
      PULSOUT 12, 650
      PAUSE 20
      NEXT
      RETURN
  onethirtyfive:
     FOR pulsecount=0 TO 30
       PULSOUT 13, 850
       PULSOUT 12, 850
       PAUSE 20
       NEXT
       RETURN
  onethirtyfive2:
     FOR pulsecount=0 TO 30
       PULSOUT 13, 650
       PULSOUT 12, 650
       PAUSE 20
       NEXT
       RETURN

I tried making the main part inside the DO LOOP the routines, and then added the checks for the sensors in the actual routines, but that wasn't working either. Thanks in advance

Comments

  • Ken GraceyKen Gracey Posts: 7,392
    edited 2013-12-08 09:06
    Asabot,

    Welcome to the forums.

    The ELSE loop onethirtyfive executes for 30 cycles, taking 30 x 20ms = 600 ms + 30 1.5 ms pulses = 645 ms, more than half of a second. The forward_pulse routine does the same for 200 cycles, so you're committed to almost three seconds of straight driving. This could total to a few feet of travel. If you want to check the sensors more frequently you could reduce the number of loops in this routine. Also look at these loops in term of their time commitment.

    Let's see what others have to say, too.

    Ken Gracey
  • ercoerco Posts: 20,256
    edited 2013-12-08 09:11
    Way to jump in on a weekend, Ken!

    Welcome, Asabot. FYI, the president of Parallax just answered your question!
  • Mike GMike G Posts: 2,702
    edited 2013-12-08 09:11
    Now the sensors will not detect while the "else" part of the loop is being carried out
    The STAMP does one thing at a time. The way the code is written, the bot is either in a for...next loop or the main do...loop. While a for...next is executing, irdetectleft and irdetectRight are not read. Once the for...next loops exist, execution is passed back to the main do...loop and irdetectleft and irdetectRigh are checked.

    Try putting irdetectleft and irdetectRigh checks in the for...net loops or remove the for...next loops all together, and/or create a method where you pass a value to the for...next which tells the loop how many times to execute.

    Essentially you need to move the logic around to get the expected behavior.

    Edit: Ken beat me to it.
  • asabotasabot Posts: 8
    edited 2013-12-08 09:42
    Mike G wrote: »
    The STAMP does one thing at a time. The way the code is written, the bot is either in a for...next loop or the main do...loop. While a for...next is executing, irdetectleft and irdetectRight are not read. Once the for...next loops exist, execution is passed back to the main do...loop and irdetectleft and irdetectRigh are checked.

    Try putting irdetectleft and irdetectRigh checks in the for...net loops or remove the for...next loops all together, and/or create a method where you pass a value to the for...next which tells the loop how many times to execute.

    Essentially you need to move the logic around to get the expected behavior.

    Edit: Ken beat me to it.

    Thank you. I have only been programming for a year and it's been mainly cc,java, and it's harder for me to see how things are working in pbasic.
    Ken Gracey wrote: »
    Asabot,

    Welcome to the forums.

    The ELSE loop onethirtyfive executes for 30 cycles, taking 30 x 20ms = 600 ms + 30 1.5 ms pulses = 645 ms, more than half of a second. The forward_pulse routine does the same for 200 cycles, so you're committed to almost three seconds of straight driving. This could total to a few feet of travel. If you want to check the sensors more frequently you could reduce the number of loops in this routine. Also look at these loops in term of their time commitment.

    Let's see what others have to say, too.

    Ken Gracey

    Thanks for reply. I believe I did try some variation of what you're saying. I had the loop in forward_pulse go from 0 to 20, and then called it 10 times in the do loop, followed by the 135, 10 more times, and 135 again. That didn't work, so I made the forward_pulse routine actually contain the 135's also, and made the forward movement a series of 10, 0 to 20 for loops, but that didn't work either. I think after Mike's response maybe I can go back and make sense of it. I actually did this same routine with whiskers a few months ago, and I had this same exact problem and was able to figure it out. That program is gone now though, and this time i'm having more trouble than the first :blank:
    erco wrote: »
    Way to jump in on a weekend, Ken!

    Welcome, Asabot. FYI, the president of Parallax just answered your question!
    +rep for parallax
  • asabotasabot Posts: 8
    edited 2013-12-09 10:33
    I just don't get it. I don't know what i'm doing wrong. Not only do the sensors still not read when they're supposed to, when they do read the routines are that are copy/pasted from working program are acting weird. I know this is because of a logic error somewhere but it seems like NOTHING I DO changes the results. I have added tests in my forward routine, and it is going through all of my "20" intervals, but it will not read the sensors in between. I have been working on this program off and on for days, and have to finish it before I can even pick up my gripper kit from teacher and start my final project that is due by the last day of class on Friday.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    
    
    
    irdetectleft VAR Bit
    irdetectright VAR Bit
    pulsecount VAR   Byte
    FREQOUT 5,2000,3000
    DO
      FREQOUT 8,1,45500
      irdetectleft=IN9
      FREQOUT 2,1,45500
      irdetectright=IN0
    
    
        GOSUB forward_pulse
        GOSUB onethirtyfive
        GOSUB forward_pulse
        GOSUB onethirtyfive2
    
    
      LOOP
    
    
    
    
      ' FORWARD PULSE                 I HAVE BEEN USING 200 AS MY INITIAL FORWARD VALUE FOR TESTING
    
    
      forward_pulse:
    
    
      IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                   '20
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
      IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                           '40
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
       IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                   '60
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
      IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                           '80
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
     IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                  ' 100
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
      IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                           '120
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
     IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                   '140
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
      IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                           '160
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
     IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                   '180
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
      IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
        FOR pulsecount=0 TO 20
         PULSOUT 13,650                                           '200
         PULSOUT 12,850
         PAUSE 20
         NEXT
      ENDIF
    
    
    
    
     'TURN LEFT
      turn_left:
        FOR pulsecount=0 TO 20
          PULSOUT 13, 850
          PULSOUT 12, 850
          PAUSE 20
          NEXT
          RETURN
      turn_right:
         FOR pulsecount=0 TO 20
          PULSOUT 13, 650
          PULSOUT 12, 650
          PAUSE 20
          NEXT
          RETURN
      back_up:
        FOR pulsecount=0 TO 40
          PULSOUT 13, 850
          PULSOUT 12, 650
          PAUSE 20
          NEXT
          RETURN
      onethirtyfive:
         FOR pulsecount=0 TO 30
           PULSOUT 13, 850
           PULSOUT 12, 850
           PAUSE 20
           NEXT
           RETURN
      onethirtyfive2:
         FOR pulsecount=0 TO 30
           PULSOUT 13, 650
           PULSOUT 12, 650
           PAUSE 20
           NEXT
           RETURN
    
  • asabotasabot Posts: 8
    edited 2013-12-09 15:28
    this doesn't work either
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    
    
    
    irdetectleft VAR Bit
    irdetectright VAR Bit
    pulsecount VAR   Byte
    FREQOUT 5,2000,3000
    DO
      FREQOUT 8,1,45500
      irdetectleft=IN9
      FREQOUT 2,1,45500
      irdetectright=IN0
    
    
        GOSUB forward_pulse
        GOSUB onethirtyfive
        GOSUB forward_pulse
        GOSUB onethirtyfive2
    
    
      LOOP
    
    
    
    
      ' FORWARD PULSE                 I HAVE BEEN USING 200 AS MY INITIAL FORWARD VALUE FOR TESTING
    
    
      forward_pulse:
    
    
    
    
        FOR pulsecount=0 TO 200
        IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
         PULSOUT 13,650                                   '20
         PULSOUT 12,850
         PAUSE 20
          ENDIF
      NEXT
      RETURN
    
    
    
    
     'TURN LEFT
      turn_left:
        FOR pulsecount=0 TO 20
          PULSOUT 13, 850
          PULSOUT 12, 850
          PAUSE 20
          NEXT
          RETURN
      turn_right:
         FOR pulsecount=0 TO 20
          PULSOUT 13, 650
          PULSOUT 12, 650
          PAUSE 20
          NEXT
          RETURN
      back_up:
        FOR pulsecount=0 TO 40
          PULSOUT 13, 850
          PULSOUT 12, 650
          PAUSE 20
          NEXT
          RETURN
      onethirtyfive:
         FOR pulsecount=0 TO 30
           PULSOUT 13, 850
           PULSOUT 12, 850
           PAUSE 20
           NEXT
           RETURN
      onethirtyfive2:
         FOR pulsecount=0 TO 30
           PULSOUT 13, 650
           PULSOUT 12, 650
           PAUSE 20
           NEXT
           RETURN
    
  • asabotasabot Posts: 8
    edited 2013-12-09 18:09
    Fixed it. I feel like an idiot. It probably would have worked 100 different ways that i've had it over the past few days had I initialized the sensors in the sub-routine..
  • robjeffreyrobjeffrey Posts: 1
    edited 2013-12-10 05:33
    I just read your reply, Could you post code for the "fix" so that I can compare. I am in a similar position with my BOE.

    Thanks

    Rob
  • asabotasabot Posts: 8
    edited 2013-12-11 11:05
    robjeffrey wrote: »
    I just read your reply, Could you post code for the "fix" so that I can compare. I am in a similar position with my BOE.

    Thanks

    Rob

    Here ya go!


    The way it is below is actually my first attempt at this behavior (minus the commented lines obviously). All of the ugly code above was desperate attempts at getting it to work..
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    irdetectleft VAR Bit
    irdetectright VAR Bit
    pulsecount VAR   Byte
    
    FREQOUT 5,2000,3000
    
    DO
      FREQOUT 8,1,45500
      irdetectleft=IN9
      FREQOUT 2,1,45500
      irdetectright=IN0
    
    
        GOSUB forward_pulse
        GOSUB onethirtyfive
        GOSUB forward_pulse
        GOSUB onethirtyfive2
    
    LOOP
    
    
    
      forward_pulse:
    
      FOR pulsecount=0 TO 200       
    
      FREQOUT 8,1,45500             '
      irdetectleft=IN9              '   these lines were my whole problem.  hours of messing with loops
      FREQOUT 2,1,45500             '   because of not initializing my sensors inside the routine.
      irdetectright=IN0             '
    
    
      IF(irdetectleft=0) AND (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        GOSUB turn_left
        PAUSE 100
      ELSEIF (irdetectleft=0) THEN
        GOSUB back_up
        GOSUB turn_right
        PAUSE 100
      ELSEIF (irdetectright=0) THEN
        GOSUB back_up
        GOSUB turn_left
        PAUSE 100
      ELSE
         PULSOUT 13,650                                   
         PULSOUT 12,850
         PAUSE 20
          ENDIF
      NEXT
      RETURN
    
    
    
      turn_left:
        FOR pulsecount=0 TO 20
          PULSOUT 13, 850
          PULSOUT 12, 850
          PAUSE 20
          NEXT
          RETURN
    
      turn_right:
         FOR pulsecount=0 TO 20
          PULSOUT 13, 650
          PULSOUT 12, 650
          PAUSE 20
          NEXT
          RETURN
    
      back_up:
        FOR pulsecount=0 TO 40
          PULSOUT 13, 850
          PULSOUT 12, 650
          PAUSE 20
          NEXT
          RETURN
    
      onethirtyfive:
         FOR pulsecount=0 TO 30
           PULSOUT 13, 850
           PULSOUT 12, 850
           PAUSE 20
           NEXT
           RETURN
    
      onethirtyfive2:
         FOR pulsecount=0 TO 30
           PULSOUT 13, 650
           PULSOUT 12, 650
           PAUSE 20
           NEXT
           RETURN
    
Sign In or Register to comment.