Shop OBEX P1 Docs P2 Docs Learn Events
Need help in basic coding! — Parallax Forums

Need help in basic coding!

V360V360 Posts: 10
edited 2008-04-26 16:20 in Learn with BlocklyProp
So, i have been reading through examples of line follower codes. There's something i don't get.

' QtiLineFollow.bs2
' Boe-Bot follows electrical tape with 3 QTI modules.
'{$STAMP BS2}
'{$PBASIC 2.5}
qti VAR Nib
DO
Right: HIGH 5: PAUSE 1: qti.BIT0 = IN3: INPUT 5
Center: HIGH 6: PAUSE 1: qti.BIT1 = IN3: INPUT 6
Left: HIGH 7: PAUSE 1: qti.BIT2 = IN3: INPUT 7

SELECT qti
CASE %010 ' Forward
PULSOUT 13, 850
PULSOUT 12, 650
CASE %011 ' Pivot right
PULSOUT 13, 850
PULSOUT 12, 750
CASE %001 ' Rotate right
PULSOUT 13, 850
PULSOUT 12, 850
CASE %110 ' Pivot Left
PULSOUT 13, 750
PULSOUT 12, 650
CASE %100 ' Rotate Left
PULSOUT 13, 650
PULSOUT 12, 650
ENDSELECT
PAUSE 20
LOOP

WHat does the bolded part means?
1. Why do i have to set the·3 input pins to high initially?
2. qti.BIT0 = IN3 :· Why do all of the 3 bits of variable qti = IN3?·
3. What does the INPUT 5,6,7 behind indicates?



thanks in advance

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-24 14:32
    Pins 5,6, and 7 are the IR emitters of each of the sensors. They get turned on (supplied power) when needed with the HIGH statement and turned off when not needed using the INPUT statement. Since the QTI sensors have an "open collector" output setup, all of them can be connected together and attached to pin 3. Only the one with an active IR emitter should respond by making its output low (or zero). See the QTI documentation for details.
  • V360V360 Posts: 10
    edited 2008-03-25 12:16
    Thanks Mike, now i have another problem, how should i change the coding if i were to include ramping in it?
    Examples would be much appreciated.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-25 19:22
    I would first ask what you mean by ramping. Remember that there's no direct relationship between servo speed and the control pulse width other than the further the pulse width is from center, the faster the motor goes. It can be somewhat non-linear and will vary from servo to servo.

    The easiest way to do ramping is to use a FOR loop and to do something like:
    for i = 0 to 100 step 10
      pulsout <pin>, 750+i
      pause 20
    next i
    do
      pulsout <pin>,850
      pause 20
    loop
    


    In your case, you've got a more complicated situation. How would you do the same
    thing in your case? Remember, each time through your loop, you want to have the
    servos go a little faster until they reach the desired speed.
  • V360V360 Posts: 10
    edited 2008-04-15 13:27
    Hmm Mike,
    As my boebot jerks when it turns, so i have to use ramping to smoothen the turn. Well that's what i mean.
    I pretty much understand your example, but i have no idea how to apply it in my program so that my boebot would turn smoothly.
    Sorry to trouble you again, but can you give a detailed example of ramping when the boebot is turning?
    Lets say when only the right and middle LEDs detect the black line,

    CASE %011 ' Pivot right
    PULSOUT 13, 850
    PULSOUT 12, 750
    How do i modify the code above to smoothen the turning?

    Thanks in advance.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-15 14:31
    I don't think you can just modify a little piece of your program. You're going to have to change the way the whole program works.

    Add two variables, oldQti as a nibble and goal as a byte. Initialize oldQti to 15 and goal to 0.

    Before the SELECT, do something like:
    IF qti <> oldQti THEN
       goal = 0 : oldQti = qti
    ENDIF
    


    Before the PAUSE, do something like:
    IF goal < 100 THEN
       goal = goal + 10
    ENDIF
    


    Then, anywhere in your SELECT statement where you have 850, put 750+goal. Anywhere you have 650, put 750-goal.

    What this does is to set a goal anytime the QTI value changes. If QTI doesn't change, the program moves closer to its goal every 20ms. Once the program reaches its goal, it stays there until QTI changes again. The goal is used to set the speed of the servos. It takes about 200ms for the goal to be reached.
  • V360V360 Posts: 10
    edited 2008-04-15 16:22
    Thanks Mike,
    ·From my understanding, the modification you taught me allows the boebot to move from 750 to max speed, which means increase in speed.
    Do i have to include so that the boebot reduce its speed whenever it changes direction?
    I mean, moving from 750 to maximum speed is enough to prevent jerking?


    Post Edited (V360) : 4/15/2008 4:31:13 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-15 16:44
    I don't know. Try it and see. You may find that you need to move smoothly from one direction to another and that you do need to deccelerate as well so the program may get even more complicated. Ramping the speed up and down takes time and that may slow the response in the line following so that doesn't work as well. Keep in mind that the speed of the servos is not a linear function of the pulse width. It can vary from servo to servo. The step size (10) and maximum adjustment (100) may need to be different for each servo. Only experimentation will tell you.
  • V360V360 Posts: 10
    edited 2008-04-19 10:43
    Hey Mike,

    I tried the modification for ramping, and the boebot aint working at all. Can you please look through my code
    '{$STAMP BS2}
    '{$PBASIC 2.5}

    'left->5
    'right->7
    '0 -> detect
    '1 -> no detect
    left VAR Bit
    right VAR Bit
    old VAR bit
    goal VAR byte
    new VAR byte
    speed VAR bit
    old=15
    new=1
    speed=0
    DO
    IF new <> old THEN
    speed= 0 : old = new
    ENDIF
    '············ Forward
    IF (IN5=1) AND (IN6=0) AND (IN7=1) THEN
    GOSUB Forward
    '==========================================================
    '············ Right Turn
    ELSEIF (IN5=1) AND (IN6=0) AND (IN7=0) THEN
    GOSUB Pivot_right
    GOSUB Store
    ELSEIF (IN5=1) AND (IN6=1) AND (IN7=0) THEN
    GOSUB Rotate_right
    GOSUB clear
    '==========================================================
    '············ Left Turn
    ELSEIF (IN5=0) AND (IN6=0) AND (IN7=1) THEN
    GOSUB Pivot_left
    GOSUB Store
    ELSEIF (IN5=0) AND (IN6=1) AND (IN7=1) THEN
    GOSUB Rotate_left
    GOSUB clear
    '==========================================================
    ELSEIF (IN5=1) AND (IN6=1) AND (IN7=1) THEN
    ······· IF(left=0)AND (right=1)THEN
    ······· GOSUB Rotate_left
    ······· ELSEIF(left=1)AND(right=0)THEN
    ······· GOSUB Rotate_right
    ······· ENDIF
    '==========================================================
    ENDIF
    IF goal < 100 THEN
    ·· goal = goal + 10
    ENDIF
    'PAUSE 20
    LOOP

    Forward:
    PULSOUT 13, 750+speed
    PULSOUT 12, 750-speed
    new=1
    RETURN

    Pivot_right:
    PULSOUT 13, 750+speed
    PULSOUT 12, 750
    new=2
    RETURN

    Rotate_right:
    PULSOUT 13, 750+speed
    PULSOUT 12, 750+speed
    new=3
    RETURN

    Pivot_left:
    PULSOUT 13, 750
    PULSOUT 12, 750-speed
    new=4
    RETURN

    Rotate_left:
    PULSOUT 13, 750-speed
    PULSOUT 12, 750-speed
    new=5
    RETURN

    Store:
    left=IN5
    right=IN7
    RETURN

    clear:
    left=1
    right=1
    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-19 12:58
    I don't know why you've added "speed", particularly when it's defined as a bit. It should be "goal". This suggests to me that you're adding pieces before you understand previous pieces ... not a good idea. Always add new functionality a little piece at time, make sure it works and make sure you understand what it's doing before you add the next piece.
  • V360V360 Posts: 10
    edited 2008-04-20 10:54
    Hm.. okay, i will try it again, and will keep you updated. Thanks
  • V360V360 Posts: 10
    edited 2008-04-26 12:33
    Well mike,
    I have tried it over and over again in the past few days, and i am making no progress at all.
    First of all, the boebot cant follow the line if there's a sharp turn less than 90 degrees.

    Secondly, my boebot is still jerking. Can you teach me to slow the boebot down before each turn?

    Thank you.
  • FranklinFranklin Posts: 4,747
    edited 2008-04-26 15:55
    Can you ATTACH your code as it exists now?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • V360V360 Posts: 10
    edited 2008-04-26 16:20
    '{$STAMP BS2}
    '{$PBASIC 2.5}


    'left->5
    'right->7
    '0 -> detect
    '1 -> no detect

    store5 VAR Bit
    store7 VAR Bit
    '==========================================================
    DO
    ' Forward
    IF (IN5=1) AND (IN6=0) AND (IN7=1) THEN
    GOSUB Forward
    '==========================================================
    ' Right Turn
    ELSEIF (IN5=1) AND (IN6=0) AND (IN7=0) THEN
    GOSUB Pivot_right
    GOSUB Store
    ELSEIF (IN5=1) AND (IN6=1) AND (IN7=0) THEN
    GOSUB Rotate_right
    '==========================================================
    ' Left Turn
    ELSEIF (IN5=0) AND (IN6=0) AND (IN7=1) THEN
    GOSUB Pivot_left
    GOSUB Store
    ELSEIF (IN5=0) AND (IN6=1) AND (IN7=1) THEN
    GOSUB Rotate_left
    '==========================================================

    ELSE
    IF (store5=1) AND (store7=1)
    'stop
    ELSEIF(store7=0)AND (store5=1)THEN
    GOSUB Rotate_right
    ELSEIF(store7=1)AND(store5=0)THEN
    GOSUB Rotate_left
    ENDIF
    ENDIF
    'PAUSE 20
    LOOP


    Forward:
    PULSOUT 13, 800
    PULSOUT 12, 700
    RETURN

    Pivot_right:
    PULSOUT 13, 800
    PULSOUT 12, 750
    RETURN

    Rotate_right:
    PULSOUT 13, 800
    PULSOUT 12, 800
    RETURN

    Pivot_left:
    PULSOUT 13, 750
    PULSOUT 12, 600
    RETURN

    Rotate_left:
    PULSOUT 13, 700
    PULSOUT 12, 700
    RETURN

    Store:
    store7=IN7
    store5=IN5
    RETURN


    By the way the boebot is still jerking pretty badly. But the priority is to solve the sharp turns first.
    Anyway thanks.
Sign In or Register to comment.