Seperate contol of 2 servos at same time using one BS2px
KennyR
Posts: 15
Im trying to control two servos at the same time using one BS2px,··but i want to have individual contol of each servo. Im using a·VB6 GUI·w/ a touch screen to contol the servos, so pushing 2 buttons on the gui at once isnt a·problem·. My problem is if·i have one servo spinning and try to spin the other nothing happens.Not sure why? All I want is complete seperate contol of each servo, is this possible with one BS2px? Heres a bit of my code.
gui VAR Word
SERIN 16, 16780, [noparse][[/noparse]gui]
DO WHILE gui = 15
PULSOUT 12, 650
PAUSE 1
LOOP
DO WHILE gui =17
PULSOUT 13, 3050
PAUSE 1
LOOP
gui VAR Word
SERIN 16, 16780, [noparse][[/noparse]gui]
DO WHILE gui = 15
PULSOUT 12, 650
PAUSE 1
LOOP
DO WHILE gui =17
PULSOUT 13, 3050
PAUSE 1
LOOP
Comments
2) You have independent actions in your example. To control two servos, you need to merge these so a pulse is put out for each servo roughly every 20ms. One question is "how are you going to transmit from the GUI the different combinations of servo positions?"
3) In your example, you receive the value "gui" only once. Even though your loop says "DO WHILE gui = 15", "gui" will never change because you're never going back to read it again. If you do go back to read it again, that will interrupt the servo movement, at least for long enough to do the SERIN.
4) Once you have a servo position for each servo, controlling both of them is as easy as:
where you have "Position1 VAR Word" and "Position2 VAR Word".
Think about these points, then see what questions you have.
See the "Pause 1" in your code? That should be a "Pause 20". The Servo Control Signal is a PULSOUT of a certain duration, depending on the desired position, that MUST be repeated EVERY 20 milliseconds. No less than 20 milliseconds. Could be as much as 50 milliseconds, so there's some slop allowed there, but NO LESS than 20 milliseconds.
Thus, you MUST have "Pause 20", and MUST NOT have "Pause 1".
You're still faced by the problem that a Stamp can't receive a character and generate servo pulses at the same time. While it's generating servo pulses, it will ignore (and miss) anything sent via the wireless radio. Some wireless radios have buffers and respond to "flow control" and the Stamp's SERIN statement can deal with this properly. You didn't say what kind of wireless link you're using and your program sample didn't use flow control (or handshaking).
Without flow control or adding extra hardware to provide buffering, you'll have to alternate between looking for an input character with a SERIN statement with a timeout (see the Basic Manual under SERIN) and moving the servos for a short time. The servo movement will be a little jerky and, because the Stamp can't listen for an input character all the time, it may miss some of them. Possibly the GUI will need to send the same character over and over.
' {$STAMP BS2}
' {$PBASIC 2.5}
ServoPin1 CON 12
ServoPin2 CON 13
ServoPos1 VAR Word
ServoPos2 VAR Word
gui VAR Word
ServoPos1 = 650
ServoPos2 = 3050
MAIN:
· GOSUB CheckInput· ' Sets "GUI"
· GOSUB AdjustServo
· GOSUB RefreshServo
· PAUSE 10· ' 10 milliseconds, because we 'waited' the other
··········· ' 10 milliseconds in "CheckInput".
· GOTO MAIN
CheckInput:
· SERIN 16, 16780, 10, SkipIt, [noparse][[/noparse]DEC GUI]· ' 'SkipIt' is a timeout destination.
········································· ' Otherwise, SERIN waits forever.
· RETURN
SkipIt:
· GUI = 15
· RETURN
AdjustServo:
· IF Gui = 15 THEN
··· ServoPos1 = 650
· ENDIF
· IF Gui = 17 THEN
··· ServoPos2 = 3050
· ENDIF
· RETURN
RefreshServo:
· PULSOUT ServoPin1, ServoPos1
· PULSOUT ServoPin2, ServoPos2
· RETURN
Post Edited (allanlane5) : 12/2/2008 7:50:36 PM GMT
Here is one alternative that seems to have been missed:
http://www.parallax.com/Store/Accessories/MotorServoControllers/tabid/160/CategoryID/35/List/0/Level/a/ProductID/346/Default.aspx?SortField=ProductName,ProductName
The Parallax Servo Controller should solve all this hassle. Nice and easy, just feed it what you want to in the way of directions, and it does the rest. No interleaving of code, no fancy subroutines, or anything else. Best of all, in my opinion, it's priced right. Take a look and I think you'll agree.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When all else fails, try inserting a new battery.
Post Edited (Bruce Bates) : 12/2/2008 10:07:56 PM GMT
If you set "ServoPosition1" to 750, and "ServoPostion2" to 850, then one servo will move while the other holds still, assuming both are continuous rotation servos.
You might want to choose a different "default" value.