Shop OBEX P1 Docs P2 Docs Learn Events
Help with simultaneous control of 2 stepper motors using Propeller — Parallax Forums

Help with simultaneous control of 2 stepper motors using Propeller

kotsoskotsos Posts: 12
edited 2011-03-22 20:30 in Propeller 1
Hi,
What I basically aim to do is to control many (4 in total) stepper motors SIMULTANEOUSLY using the propeller. I wrote an object that successfully controls 1 stepper motor, but when I try to launch the object multiple times, it doesn't work!
This is the code of the object:
_xinfreq = 5_000_000                     
_clkmode = xtal1 + pll1x

DAT

Steps byte %1100, %0110, %0011, %1001

VAR

long cntr, stpIdx
long stack[200]
byte cog

PUB Start(P1, P4, nxt, prev) : success

Stop
success := (cog := cognew(gostep(P1, P4, nxt, prev), @stack)+1)    

PUB Stop

if cog
  cogstop(cog~ -1)
  
PUB gostep(P1, P4, nxt, prev)

    dira[19..22]~~
    outa[19..22]~~
    dira[28..31]~~
    outa[28..31]~~
    
  if prev < nxt
    stepFwd(P1,P4,(nxt-prev))
    
  elseif prev > nxt
    stepBwd(P1,P4,(prev-nxt))

    
PUB stepFwd(P1,P4,stps)
     
    repeat  cntr from 1 to stps
       stpIdx := ++stpIdx // 4
       outa[P1..P4] := Steps[stpIdx]
       waitcnt(clkfreq/200 + cnt)

PUB stepBwd(P1,P4,stps)

    repeat  cntr from 1 to stps
       stpIdx := (stpIdx + 3) // 4
       outa[P1..P4] := Steps[stpIdx]
       waitcnt(clkfreq/200 + cnt)
This is the script that calls the object:
_xinfreq = 5_000_000                     
_clkmode = xtal1 + pll1x

VAR

long cntr, stpIdx 

OBJ

stepper : "Stepper_motor_trials"

PUB launchStepper

    
   stepper.Start(28,31, 200, 0)
   stepper.Start(19,22, 200, 0)
Any suggestions on how to launch the object "stepper" on multiple cogs?
Right now all it excecutes is what "stepper.Start(19,22, 200, 0)" does. I suspect that for some reason when I try to launch a second cog, I stop the previous one.

Thanks in advance!
-kotsos

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-03-22 18:43
    _xinfreq = 5_000_000                     
    _clkmode = xtal1 + pll1x
    
    VAR
    
    long cntr, stpIdx 
    
    OBJ
    
    stepper[COLOR="red"][2][/COLOR] : "Stepper_motor_trials"
    
    PUB launchStepper
    
        
       stepper[COLOR="red"][0][/COLOR].Start(28,31, 200, 0)
       stepper[COLOR="red"][1][/COLOR].Start(19,22, 200, 0)
    
    You do need two instances of the same object. Previously you simply re-started the one stepper object you had. Also, the hardwired dira/outa assignments in the gostep method look suspicious.
  • kotsoskotsos Posts: 12
    edited 2011-03-22 20:04
    I tried your correction and nothing moves! Without defining two instances of the object, the last motor moves as it should. Any suggestions?
  • kuronekokuroneko Posts: 3,623
    edited 2011-03-22 20:16
    Are you sure you want to play around with pins 28..31? Those are usually occupied by the EEPROM and the PC link. Also, did you clean up the gostep method? Starting the first object will set pins 19..22 and 28..31 to high. Only 28..31 are subsequently changed. Same with the second object, all 8 pins are set high, then 19..22 are changed. The thing with high outputs is that they block any attempt of other cogs to change the pins (wired OR). IOW, the first object is blocking the second's outputs and vice versa. So you should change your code to only touch the pins you are actually driving.
  • kotsoskotsos Posts: 12
    edited 2011-03-22 20:26
    That's the correction that I did to the method "launchStepper":
    PUB launchStepper
    
       dira[28..31]~~
       outa[28..31]~~ 
       stepper[0].Start(28,31, 200, 0)
    
       dira[19..22]~~
       outa[19..22]~~ 
       stepper[1].Start(19,22, 200, 0)
    

    Still nothing moves.
    How do I clean the gostep method, as you mentioned?

    Thanks!
  • kotsoskotsos Posts: 12
    edited 2011-03-22 20:30
    Wait!
    I got it!!!

    I re-corrected the dira/outa thing. Here is the code:
    _xinfreq = 5_000_000                     
    _clkmode = xtal1 + pll1x
    
    DAT
    
    Steps byte %1100, %0110, %0011, %1001
    
    VAR
    
    long cntr, stpIdx
    long stack[200]
    byte cog
    
    PUB Start(P1, P4, nxt, prev) : success
    
    Stop
    success := (cog := cognew(gostep(P1, P4, nxt, prev), @stack)+1)    
    
    PUB Stop
    
    if cog
      cogstop(cog~ -1)
      
    PUB gostep(P1, P4, nxt, prev)
    
       dira[P1..P4]~~
       outa[P1..P4]~~
       
      if prev < nxt
        stepFwd(P1,P4,(nxt-prev))
        
      elseif prev > nxt
        stepBwd(P1,P4,(prev-nxt))
    
        
    PUB stepFwd(P1,P4,stps)
         
        repeat  cntr from 1 to stps
           stpIdx := ++stpIdx // 4
           outa[P1..P4] := Steps[stpIdx]
           waitcnt(clkfreq/200 + cnt)
    
    PUB stepBwd(P1,P4,stps)
    
        repeat  cntr from 1 to stps
           stpIdx := (stpIdx + 3) // 4
           outa[P1..P4] := Steps[stpIdx]
           waitcnt(clkfreq/200 + cnt)
    

    And
    _xinfreq = 5_000_000                     
    _clkmode = xtal1 + pll1x
    
    VAR
    
    long cntr, stpIdx 
    
    OBJ
    
    stepper[2]: "Stepper_motor_trials"
    
    PUB launchStepper
    
       stepper[0].Start(28,31, 200, 0)
    
       stepper[1].Start(19,22, 200, 0)
    


    Thanks A lot! You solved my problem!!!!!
Sign In or Register to comment.