Shop OBEX P1 Docs P2 Docs Learn Events
Seperate contol of 2 servos at same time using one BS2px — Parallax Forums

Seperate contol of 2 servos at same time using one BS2px

KennyRKennyR Posts: 15
edited 2008-12-03 18:40 in BASIC Stamp
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-02 17:46
    1) You've got a "PAUSE 1" in your code. This should normally be "PAUSE 20" because the servos require a pulse every 20ms (roughly). More often can cause problems.

    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:
    DO
       PULSOUT 12,Position1
       PULSOUT 13,Position2
       PAUSE 20
    LOOP
    


    where you have "Position1 VAR Word" and "Position2 VAR Word".

    Think about these points, then see what questions you have.
  • KennyRKennyR Posts: 15
    edited 2008-12-02 19:24
    Im transmitting from my gui to the stamp using a wireless radio. The "15" in DO WHILE gui=15 is just a character being being sent from the VB6 GUI (when a button is pressed on the gui) to the BS2px telling the servo to move. It isnt the servo position. Each button on the gui has its own corresponding character, i want to press two buttons on the GUI at the same time to move two servos in different directions?
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-12-02 19:36
    Yes, we KNOW the "gui=15" doesn't control the servo.

    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".
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-02 19:38
    The simple way you're thinking about won't work because both the communications channel (wireless) and the Stamp (any Stamp) are "single threaded". They only can do one thing at a time. If you want the signal from the GUI to mean "start moving servo x", then you could quickly signal to start one servo, then the other. If you want the GUI to signal to start both servos at the same time, you'll need another code to send over the wireless link to say "start moving both servos".

    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.
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-12-02 19:39
    Oh and usually I put them in a subroutine:

    ' {$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
  • KennyRKennyR Posts: 15
    edited 2008-12-02 20:22
    Im using the aerocomm AC4790 as a wireless link
  • KennyRKennyR Posts: 15
    edited 2008-12-02 21:54
    Whats the reason for the GUI=15 in the SkipIt routine
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-12-02 22:00
    Folks -

    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
  • KennyRKennyR Posts: 15
    edited 2008-12-03 13:26
    Guys, thanks for your help. I read the manual and Im still sceptical of one thing. Is this servo controller capable of this: Say i have one servo moving continously (360 degrees of rotation) and then i decide i want to move another servo (while the other servo is still moving continously). Is this possible?
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-12-03 14:13
    Yes.

    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.
  • KennyRKennyR Posts: 15
    edited 2008-12-03 15:48
    Whats the reason for the GUI=15 in the SkipIt routine. If gui is 15 then the servo will spin
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-12-03 17:38
    Ah, well, if the "Timeout" occurs, the SERIN will "GOTO" the SkipIt label -- then GUI should be given a "default" value -- I think the time-out assigns it a zero.
    You might want to choose a different "default" value.
  • KennyRKennyR Posts: 15
    edited 2008-12-03 18:40
    Thats what i thought. I was just a little confused why "15" was choosen instead of "0". Thank You
Sign In or Register to comment.