Simultaneous Servo Control
Fnordcorps
Posts: 8
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
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
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.
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