Shop OBEX P1 Docs P2 Docs Learn Events
PBASIC: Run Motors Simultaneously — Parallax Forums

PBASIC: Run Motors Simultaneously

lewisglewisg Posts: 3
edited 2010-01-12 01:05 in BASIC Stamp
Hi, everyone!

I am trying to run two motors simultaneously, but I can't figure out the code.

I am using BASIC Stamp Editor, PBASIC 2.5, and a Parallax Stamps In Class Homework Board.

Thanks!

Comments

  • $WMc%$WMc% Posts: 1,884
    edited 2010-01-11 01:49
    lewisg

    The Stamps just like most computers, Can only do one thing at a time. But they run really fast and appear to run things simultaneously.

    If You'll post some code, I/WE can help You make both motors run.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Truth is out there············___$WMc%___···························· BoogerWoods, FL. USA
  • FranklinFranklin Posts: 4,747
    edited 2010-01-11 02:48
    You are going to need more than the stamp to drive your motors and depending on what you want them to do you could opt for a smart motor driver that would run the motors without tying up the stamps processing power.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-11 03:16
    It depends on what kind of motors you're trying to drive. If you're using servos, they use intermittent control pulses and the Stamp can briefly do other things while a servo is between control pulses (like controlling a 2nd servo and reading a few sensors). If you're using stepper motors, you should be able to interleave two stepper motors' windings' pulses. If you're using a DC motor with an H-Bridge, you need some external PWM driver. Something as simple as a 555 timer with a digital pot controlling the on-time would be adequate.

    In all cases, you can buy external motor controllers that take much of the burden off the Stamp and allow it to handle sensors, make decisions, and set the state of whatever motors you're using.
  • lewisglewisg Posts: 3
    edited 2010-01-11 14:07
    This is the code

    a VAR Word
    FOR a = 0 TO 100
    PULSOUT 14,280
    NEXT
    b VAR Word
    FOR b = 0 TO 100
    PULSOUT 14,1280
    NEXT
    c VAR Word
    FOR c = 0 TO 200
    PULSOUT 14,790
    NEXT
    d VAR Word
    FOR d = 0 TO 100
    PULSOUT 15,280
    NEXT
    e VAR Word
    FOR e = 0 TO 100
    PULSOUT 15,1280
    NEXT
    f VAR Word
    FOR f = 0 TO 200
    PULSOUT 15,790
    NEXT
    END
  • ercoerco Posts: 20,256
    edited 2010-01-11 15:14
    a VAR byte' a<255
    FOR a = 0 TO 100
    PULSOUT 14,280
    pulsout 15,280' send 2 pulses together
    pause 10' full wait is 20 ms
    NEXT
    FOR a = 0 TO 100' reuse a
    PULSOUT 14,1280
    pulsout 15,280
    pause 10
    NEXT
    FOR a = 0 TO 200
    PULSOUT 14,790
    PULSOUT 15,790
    pause 10
    next
    end

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • W9GFOW9GFO Posts: 4,010
    edited 2010-01-11 19:26
    lewisg said...
    This is the code
    a VAR Word
    FOR a = 0 TO 100
    PULSOUT 14,280
    NEXT
    b VAR Word
    FOR b = 0 TO 100
    PULSOUT 14,1280
    NEXT
    c VAR Word
    FOR c = 0 TO 200
    PULSOUT 14,790
    NEXT
    d VAR Word
    FOR d = 0 TO 100
    PULSOUT 15,280
    NEXT
    e VAR Word
    FOR e = 0 TO 100
    PULSOUT 15,1280
    NEXT
    f VAR Word
    FOR f = 0 TO 200
    PULSOUT 15,790
    NEXT
    END
    



    It appears that the motors you are talking about are continuous rotation servos. If so there are a couple problems. The first is that there are no PAUSE statements. Your FOR..NEXT loop will execute many times faster than the servos need the pulses - they expect a pulse once every 20mS (50 times a second). Second, there is no reason why you cannot follow one PULSOUT with another inside each loop - both motors will be running at the same time with one receiving the PULSOUT command a tiny fraction of a second after the other. In most cases this would not be detectable.

    a VAR Byte          ' only use Word variables if you need values greater than 255, and it can be reused - don't need a new one for each loop
    
    FOR a = 0 TO 100
    PULSOUT 14, 280     ' takes about 1/4 mS for the command and 1/2 mS for the pulse length total ~ .750mS
    PULSOUT 15, 280     ' another ~ .750 mS
    PAUSE 18            ' The other lines of code use up a small amount of time so I use PAUSE 18 to keep the pulses close to every 20mS - because that's just the way I am. 
    NEXT                ' PAUSE 20 would be fine but be sure to reduce once you add more code within the loop or the servos may start stuttering
    
    FOR a = 0 TO 100
    PULSOUT 14, 1280    ' takes about 1/4 mS for the command and 2.5 mS for the pulse length total ~ 2.750mS
    PULSOUT 15, 1280    ' another ~ 2.750mS
    PAUSE 14            ' 20.000mS - (2 * 2.750mS) = 14.500mS round down to account for execution time of FOR..NEXT
    NEXT
    
    FOR a = 0 TO 200
    PULSOUT 14, 790     ' takes about 1/4 mS for the command and 1.5 mS for the pulse length total ~ 1.750mS
    PULSOUT 15, 790     ' another ~ 1.750mS
    PAUSE 16
    NEXT
    
    END
    



    Just like erco said! But with formatting wink.gif

    One thing to add, if they are servos and they will always move together, simply tie both signal lines to just one pin. Then they truly will be getting the pulse at exactly the same moment.

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.

    Post Edited (W9GFO) : 1/11/2010 8:07:53 PM GMT
  • lewisglewisg Posts: 3
    edited 2010-01-12 01:05
    Thank you all soooo much, I fianally got the code to work
    Thanks Again
    lewisg
Sign In or Register to comment.