Shop OBEX P1 Docs P2 Docs Learn Events
Boebot project problem — Parallax Forums

Boebot project problem

MikeTorenoMikeToreno Posts: 3
edited 2010-12-04 08:40 in Robotics
Hello everyone,
I'm Mike and I'm a student.

This month we had to create a boebot program using photoresistors and ir led. The robot should follow an electrical tape stripe with the ir led on a table. The robot must also run his program only when the photoresistor detects a high level of light, in order to avoid failing down from the table.
In my program ( a mix of stripefollowingboebot and a modified flashlightcontrolledboebot) the robot run perfectly until the border of the table. Normally it should completely stop itself, but in my case it continues to produce little movements forward and backward): I 've also tried to add a counter out of the DO-LOOP, but uselessly. What I've to do?
Thanks,:smilewinkgrin:
sorry for my bad english.

This is the program:
' {$STAMP BS2}
' {$PBASIC 2.5}


DEBUG "Program Running!"

'----------------[Constantes]---------------------------------------------
Kpl            CON    25
Kpr            CON    -25
SetPoint       CON     3
CenterPulse    CON    750

LeftAmbient         CON   156
RightAmbient        CON   160
LeftBright          CON   59
RightBright         CON   53
LeftThreshold       CON   LeftBright + LeftAmbient / 2    * 5 / 8
RightThreshold      CON   RightBright + RightAmbient / 2  * 5 / 8
'----------------[Variables]------------------------------------------------

freqSelect        VAR   Nib
irFrequency       VAR   Word
irDetectRight     VAR   Bit
irDetectLeft      VAR   Bit
distanceLeft      VAR   Nib
distanceRight     VAR   Nib
pulseleft         VAR   Word
pulseright        VAR   Word

timeLeft       VAR     Word
timeRight      VAR     Word

counter        VAR     Nib


'----------------[Initialization]---------------------------------------------

FREQOUT  4, 2000, 3000
counter = 1


'----------------[Main Routine]--------------------------------------------

IF (counter < 5) THEN
  DO
  GOSUB Test_Photoresistors
  GOSUB Navigate
  LOOP
ELSE
 PULSOUT 13,750
 PULSOUT 12,750
ENDIF
END


'----------------[Subroutines]---------------------------------------------

Get_Ir_Distances:

 distanceLeft = 0
 distanceRight = 0


 FOR freqSelect = 0 TO 4

  LOOKUP freqSelect,[37500,38250,39500,40500,41500], irFrequency

  FREQOUT 8,1, irFrequency
  irDetectLeft = IN9
  distanceLeft = distanceLeft + irDetectLeft

  FREQOUT 2,1, irFrequency
  irDetectRight = IN0
  distanceRight = distanceRight + irDetectRight

  NEXT
RETURN

Send_Pulse:

PULSOUT 13,pulseleft
PULSOUT 12,pulseright
PAUSE 5
RETURN

Test_Photoresistors:

   HIGH 6
   PAUSE 3
   RCTIME 6,1, timeLeft

   HIGH 3
   PAUSE 3
   RCTIME 3,1, timeRight

RETURN


Navigate:

  IF (timeLeft > LeftThreshold)AND (timeRight > RightThreshold) THEN
  counter = counter + 1
  PULSOUT 13,750
  PULSOUT 12,750


  ELSEIF (timeLeft > LeftThreshold) THEN
  counter = counter + 1
  PULSOUT 13,750
  PULSOUT 12,750


  ELSEIF (timeRight > RightThreshold) THEN
  counter = counter + 1
  PULSOUT 13,750
  PULSOUT 12,750


  ELSE

  GOSUB Get_Ir_Distances
  pulseleft  = SetPoint - distanceLeft  * Kpl + CenterPulse
  pulseright = SetPoint - distanceRight * Kpr + CenterPulse
  GOSUB Send_Pulse

  ENDIF

  PAUSE 20

RETURN

Comments

  • ercoerco Posts: 20,256
    edited 2010-11-27 09:26
    At least one problem is that you don't have any "PAUSE 20" statements after your PULSOUTS to your servos. You need to add those after EVERY group of PULSOUTs and retry your program. For instance, your main routine should look like this:

    IF (counter < 5) THEN
    DO
    GOSUB Test_Photoresistors
    GOSUB Navigate
    LOOP
    ELSE
    PULSOUT 13,750
    PULSOUT 12,750
    PAUSE 20
    ENDIF
    END

    You don't need one in between two consecutive pulsouts (as shown).

    Also, are you certain that your servos are stopped at pulsout 750? That's a general average number, yours may be 738 and 761. Experiment!
  • MikeTorenoMikeToreno Posts: 3
    edited 2010-11-27 09:46
    thanks,but sfotunately the program seem the same even with thoose corrections......I added the pause and controlled the servomotors ( you were right, they are stopped only at 754 ad 751.....) but the program doesn't end at the table border....it continues little movements foreward and backward....
  • W9GFOW9GFO Posts: 4,010
    edited 2010-11-27 12:18
    erco wrote: »
    At least one problem is that you don't have any "PAUSE 20" statements after your PULSOUTS to your servos. You need to add those after EVERY group of PULSOUTs and retry your program.

    That's not quite right. You want to put in a PAUSE statement with a value such that the time from the beginning of one pulse to the beginning of the next is 20 milliseconds (as seen by each servo).

    Your first subroutine, Test_Photoresistors, takes a minimum of six and probably more like eight milliseconds to execute. The next subroutine, Navigate, is going to take 3.0 milliseconds just to send the pulses. Add in a couple more milliseconds for executing the other commands and you have about 13 milliseconds total. The appropriate PAUSE time would then be about 7.

    When your counter reaches five those subroutines will be skipped and a longer PAUSE statement is in order. The PAUSE 20 is fine there but a Pause 17 would be more precise because the two PULSOUTS consume at least three milliseconds.

    The servos may be hunting at the end because they are not receiving any pulses and will be sensitive to other stray signals. Use a loop to keep refreshing the pulses.

    Or, more likely, it could be because there is no way to exit from the first loop. Once the counter is tested and found to be less than five you become stuck in the loop, counter is never tested again.

    Here is how I would do it, remove the PAUSE 20 from Navigate, remove the PAUSE 5 from Send_Pulse and then change your main loop to this;
    DO
      IF (counter < 5) THEN
        GOSUB Test_Photoresistors
        GOSUB Navigate
        PAUSE 7
      ELSE
        PULSOUT 13,750
        PULSOUT 12,750
        PAUSE 17
      ENDIF
    LOOP
    
    END
    

    Rich H
  • ercoerco Posts: 20,256
    edited 2010-11-27 12:59
    @Rich: Thanks for correcting me to the millisecond, OM! 73s :)

    @Mike: Do you have any idea what section of the program is causing the twitching problem? It may be different than you suspect.... Try adding some different DEBUG statements and run the program while hooked up to a laptop.
  • W9GFOW9GFO Posts: 4,010
    edited 2010-11-27 13:17
    erco wrote: »
    @Rich: Thanks for correcting me to the millisecond, OM! 73s :)

    @Mike: Do you have any idea what section of the program is causing the twitching problem? It may be different than you suspect.... Try adding some different DEBUG statements and run the program while hooked up to a laptop.

    You are welcome.

    Check his DO..LOOP, there is no exit.

    Rich H
  • MikeTorenoMikeToreno Posts: 3
    edited 2010-12-04 07:05
    Thanks a lot to everyone.
    The solution proposed by Rich worked perfecty......the robot avoided the table edge perfecty and also it didn't twitch. :)
    I've only to hope it will works correctly for the test, too.

    Thanks, Mike
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-04 08:40
    Glad to hear it Mike, good luck on the test!

    Rich H
Sign In or Register to comment.