Shop OBEX P1 Docs P2 Docs Learn Events
Need Help With Encoder Please — Parallax Forums

Need Help With Encoder Please

crd15crd15 Posts: 12
edited 2009-04-28 20:08 in BASIC Stamp
I am trying to design an encoder that will count the amount of times the wheel rotates and then have the robot turn around and go back exactly the amount of times the encoder counted.· I am having trouble incrementing and then saving that value.· There are four painted lines on the·wheel so it should pulse 4 times per rotation.··· If someone could look at my code and see what I am doing wrong, I would appreciate that much.· The code is attached

*** Update.· I have updated the attached file to some changes I have made on my own.·

The problem with this is that it stops after every 3 pulses I give the all_forward command and then checks the encoder which is not want a I want.· I want it to continously check the encoder while the motor is in the allforward command.

Post Edited (crd15) : 4/27/2009 11:55:58 PM GMT

Comments

  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-28 00:26
    It is difficult to advise you on program lacking any comments.
    Your Main: routine / function does not have an END statement – it “ends” with RETURN. That is a not a good programming style.
    I question the necessity of having PAUSE after PULSEOUT, but again, without comments it is hard to judge.
    Also – PULSEIN will read the duration of pulse (into quarter) or 0 if it times out.
    Why are you incrementing the· “quarter “ variable by 1?
    You should be checking for 0 first than add quarter to running pulse width if that is what you want. (running pulse width = running pulse width + quarter)·
    ·It is unclear if you want just event count or total pulse width.
    Cheers Vaclav

    Sorry we "doubled", you were editing and I was writing.
    After reading your edit - I recomend you draw yourself an·old fashioned flow chart and than write your code.




    Post Edited (vaclav_sal) : 4/28/2009 12:33:09 AM GMT
  • FranklinFranklin Posts: 4,747
    edited 2009-04-28 00:52
    Remember the stamp can only do one thing at a time so if you go off into a subroutine that is what it will do until it completes that task. Flowcharts are a good idea.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • crd15crd15 Posts: 12
    edited 2009-04-28 02:49
    Yeah it actually does end but i pulled it out of my main code to test it.
    I really want to just count how many times it goes by while its going forward that way i can figure how many times the wheel has turned.
    The basic pattern is go forward, turn 180 degrees, and go forward back to the starting place.
    I need to do it with an encoder because later i will be doing it with another type of loop to get to a certain location in a maze and then return so a for loop with a set time would not be as accurate as the encoder.

    I added comments but it is not letting me attach the code so here it is

    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}

    encoderpulse PIN 10
    quarter VAR WORD
    duration CON 1
    amount VAR WORD

    motor PIN 14
    turn PIN 15

    forward CON 1680 ' forward time
    backward CON 2050 'reverse time
    break CON 1875

    counter VAR BYTE
    main_counter VAR BYTE

    main:

    GOSUB all_stop ' stop robot
    PAUSE 1000

    FOR main_counter = 1 TO 20 'go forward and check how many times wheel
    ' rotates
    GOSUB all_forward ' go forward
    GOSUB update ' update the encoder

    NEXT

    FOR main_counter = 1 TO 14 ' turn counter clockwise 180 degrees
    GOSUB ccw
    NEXT

    DO
    GOSUB all_forward ' go forward the amount of times
    quarter = quarter - 1 ' the amount the encoder incremented
    LOOP UNTIL (quarter = 0) ' loop this until it quarter is 0

    GOSUB all_stop

    END

    '
    ' check the encoder
    '
    update:
    PULSIN encoderpulse, 1, quarter
    PAUSE 10
    quarter = quarter + 1
    RETURN

    '
    'motor controls
    '

    all_forward:
    FOR counter = 1 TO 3
    PULSOUT 14, forward
    PULSOUT 15, forward
    PAUSE 20
    NEXT

    GOSUB all_stop
    RETURN

    all_stop:
    FOR counter = 1 TO 9
    PULSOUT 14, break
    PULSOUT 15, break
    PAUSE 20
    NEXT
    RETURN

    CC:
    FOR counter = 1 TO 5
    PULSOUT 14, 2050
    PULSOUT 15, 1700
    PAUSE 20
    NEXT

    RETURN

    CCW:
    FOR counter = 1 TO 5
    PULSOUT 14, 1700
    PULSOUT 15, 2050
    PAUSE 20
    NEXT

    RETURN
  • FranklinFranklin Posts: 4,747
    edited 2009-04-28 03:18
    Your code makes little sense. could you write out what you are trying to accomplish as a flow chart with each step as detailed as you can. What are you using as an encoder and why are you sending it a pulsout? Maybe I'm missing something here but you seem to be going forward, stopping and then checking an encoder, incrementing a count and returning.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • crd15crd15 Posts: 12
    edited 2009-04-28 05:18
    yeah i am kind of new at this. The pulsout is for the motors. The pulsin is for the encoder. as far as a flow chart goes.

    Go forward

    count how many times wheel rotates

    stop and turn around

    go back the same amount of times the wheel rotated and stop

    I just can't find any good examples and I am trying my best to get this to happen. Sorry if it doesnt make the best of sense.
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-28 14:16
    Here us my interpretation of your task
    ·
    Forward loop·
    Start motion (forward)
    ·········· Read encoder pulse width using pulsein.
    ·········· (This gives length of travel to some degree of accuracy. )
    Count revolution··
    Evaluate loop ( count and or distance travelled)
    ·
    Stop motion
    ·
    ·
    Reverse· loop
    Start motion (reverse)
    ·
    Evaluate loop·(count and or distance travelled)

    It appears that you may be using stepper motor - my "Start motion" ·will definetly be more complex.
    Keep in mind that both· PULSEIN and PULSEOUT do not complete until the set or measured pulse width time.
    You could look at them as a loop statements.

    If you want to accompl;ish your task you need to restructure your code accordingly.













    ·
  • LilDiLilDi Posts: 229
    edited 2009-04-28 17:03
    crd15,
    Before you get to involved in this project, you need to realize that your assumption that the robot will be able to return to the point of origine after several turns and forward distance traveled and maybe more turns by counting wheel rotation is a mistaken assumption. Even though you can accurately count the number of revolutions the wheels turn, you will not be able to measure wheel slippage with each turn and even wheel slippage moving in a straight line. Wheel slippage is significant even on hard surfaces like wood. On carpet, wheel slippage is overwhelming.
  • crd15crd15 Posts: 12
    edited 2009-04-28 20:08
    For vaclav. I am using DC motors. Thanks for the logic though. I will work on it today and get back to you

    For LIlDi. It does not have to be to specific. The objective is to find an object along the wall in a room about 2 feet by 3 feet and a maximum of 4 feet by 3 feet. This is why I need to get a general idea of how far it has traveled. It is not that important to get a very accurate reading. Just enough to get back to where the doorway is. And then I will no longer need to use the encoder. The arena is actually the same as the Trinity Fire Fighting Competition arena. To give you an idea. Thanks though
Sign In or Register to comment.