Shop OBEX P1 Docs P2 Docs Learn Events
Simultaneous Servo Control — Parallax Forums

Simultaneous Servo Control

FnordcorpsFnordcorps Posts: 8
edited 2008-12-15 03:36 in BASIC Stamp
Hi, I am controlling two servos on a basic stamp 2 and can sucessfully step them using the FOR/NEXT command, I was wondering if there
is any way of stepping the two servos in this way but simultaneously. I only seem to be able to get them to move one
at a time due to the FOR/NEXT breaking up the program. Any suggestions for a command I should be using? I can't seem to find a
way to combine the two under one command.

The code I am using is as follows

opening VAR byte
forward VAR word


FOR opening = 1 TO 140
PULSOUT 8, 1000 - opening
PAUSE 20
NEXT

FOR forward = 1 TO 400 STEP 10
PULSOUT 12, 500 + forward
PAUSE 20
NEXT


Thanks

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-14 03:50
    You do have to have the two PULSOUT statements in the same loop. In that case, the number of steps has to be the same. Something like this:
    FOR opening = 1 to 140
    PULSOUT 8, 1000 - opening
    PULSOUT 12, 500 + ((opening-1)*400)/140
    PAUSE 20
    NEXT
    


    This takes the range from 500 to 900 used by the "forward" loop and approximates it with the 140 steps of the "opening" loop combining the two. Note that the maximum value in the above expression is less than 16-bits.
  • Don BuczynskiDon Buczynski Posts: 31
    edited 2008-12-15 03:36
    Try something like this. Loop timing will effect servo slew rate and precision. Can leave out PAUSE if· 'other stuff'·> 20 ms.



    Pos1····· VAR···· Byte············· ' Working location
    New1····· VAR···· Byte············· ' Desired position; 250 >= New1 >= 0
    Pos2····· VAR···· Byte············· ' Working location
    New2····· VAR···· Byte············· ' Desired position; 250 >= New2 >= 0
    Incr····· CON···· 1················ ' Higher value will move servos faster
    Servo1··· PIN···· 0
    Servo2··· PIN···· 1

    MainLoop:
    ·· GOSUB Move1····················· ' Update servo1 position
    ·· GOSUB Move2····················· ' Update servo2 position

    ·· ' do other stuff; like change new values.

    ·· PAUSE <time>···················· ' Set <time> as needed. Total loop
    ··································· ' time should be 20 - 30 ms.
    ·· GOTO MainLoop

    Move1:
    ·· IF Pos1 <> New1 THEN············ ' Change position if not equal
    ····· IF Pos1 < New1 THEN
    ········ IF (Pos1 + Incr) > New1 THEN
    ··········· Pos1 = New1
    ········ ELSE
    ··········· Pos1 = Pos1 + Incr
    ········ ENDIF
    ····· ELSE
    ········ IF (Pos1 - Incr) < New1 THEN
    ··········· Pos1 = New1
    ········ ELSE
    ··········· Pos1 = Pos1 - Incr
    ········ ENDIF
    ····· ENDIF
    ·· ENDIF
    ·· PULSOUT Servo1, 1000 + (Pos1 * 2)· ' Set servo position
    ·· RETURN

    Move2:
    ·· ' like Move1··

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don Buczynski

    http://www.buczynski.com
Sign In or Register to comment.