Shop OBEX P1 Docs P2 Docs Learn Events
Using 2 cogs for controlling 2 motors simultaneously — Parallax Forums

Using 2 cogs for controlling 2 motors simultaneously

Will EyeamWill Eyeam Posts: 16
edited 2009-03-19 08:35 in Propeller 1
I am trying to develop a code to control 2 stepper motors in their own cogs. I am using a sample code from a nuts n volts article, but it only controls one motor. Can anyone help me with starting 2 cogs and having a demo_file that can run methods from both cogs simultaneously? When I run the demo code(http://www.sendspace.com/file/xqd8x9),·motor1 will execute a forward rotation, then·a backward rotation; then the other motor will execute the rotations. I want to be able to have both motors run at the same time, not one after the other..

Comments

  • telluriantellurian Posts: 52
    edited 2009-03-13 20:57
    Can you post the code (I can't open the rar file)? There is no reason why the Propeller can't control both motors at the same time. Arrange the control pins so they are consecutive so you can easily write to both motors at the same time. This should be simple to do.

    -tellurian
  • Will EyeamWill Eyeam Posts: 16
    edited 2009-03-13 23:45
    There are 3 .spin files: stepper_demo_alternate, stepper, steppers. The stepper_demo file calls methods from stepper and steppers. PM me if you can't get the files...
  • hover1hover1 Posts: 1,929
    edited 2009-03-14 00:58
    This may help.
    tellurian said...
    Can you post the code (I can't open the rar file)? There is no reason why the Propeller can't control both motors at the same time. Arrange the control pins so they are consecutive so you can easily write to both motors at the same time. This should be simple to do.

    -tellurian
  • telluriantellurian Posts: 52
    edited 2009-03-14 04:22
    Hi Will,

    I don't have a demo file for you but I can give you some pointers. I looked over the spin code, everything that you need is there to run both motors simultaneously from a single COG. You do not need two COGS. All you have to do is rearrange the pins and the code (just one 'stepper' object that handles both motors). You don't say it but I assume you are using 2 L293D's, one for each motor.

    First rearrange the pins so that they are consecutive. For example the L293D's motor control pins ...
    You are using pins 1,2,3,4 and 17,18,19,20 , change that to 1,2,3,4,5,6,7,8 (for example)
    or 12,13,14,15,16,17,18,19 if you want then on either side of the propeller

    That way you can write something like:

    outa[noparse][[/noparse]8 .. 1] := ( Steps[noparse][[/noparse]stpIdx_left] << 4 ) | Steps[noparse][[/noparse]stpIdx_right]

    So that you can write to both motors in one shot. This will allow you to to move fwd, rev and turn left or right (if a bot is your application ... I don't know) with little or no delay. If you try to write to each motor separately the first one will always start just a fraction of a second before the other one and you always don't quite go 100% true FWD or REV from a stop. Writing to both motors in this fashion will eliminate that pain in the butt which can be difficult to correct for.

    Next rewrite the stepper.spin code keeping the above structure in mind and the demo to only issue a single FWD or REV (and Left or Right) command.

    I can't write it for you (got too much of my own stuff on the go) but you have working code you can use as the base to start from. A bit of a rewrite, and some simple wiring changes and voila! ... you can control both motors from a single COG ... and have a cool object to add to the Object Exchange if you document it a bit more closely. I think a lot of people would be interested in something like that for a skid steer robot.

    Also (FYI) check out the SN754410 it is a drop in (pin for pin and code compatible) replacement for the L293D but it will handle 1amp per channel (L293D = .6A) Basically it will handle higher transient spikes and burn out less often than the L293D (to which you glued a heat sink on to ... right?).


    I hope that helped a bit,
    -tellurian
  • Will EyeamWill Eyeam Posts: 16
    edited 2009-03-18 21:15
    thanks for the help, I will try to work on the code later today. I am developing the control for a robot so left and right turning is a goal
  • Will EyeamWill Eyeam Posts: 16
    edited 2009-03-19 08:35
    This is working:

    m8 := m1 + 7········································· ' calculate last pin
    · dira[noparse][[/noparse]m8..m1]~~······································· ' make outputs for this cog

    · timer~··············································· ' clear timer
    · c := cnt············································· ' sync with system counter
    · repeat··············································· ' run until cog unloaded
    ··· waitcnt(c += clkfreq / 1000)······················· ' wait 1 ms
    ··· if (mode & M_ENABLED) and (mode & M_RUN)··········· ' background action active?
    ····· timer := ++timer // stepTm
    ····· if (timer == 0)·································· ' ready for step?
    ······· if (mode & M_STEPS)
    ········· if (numSteps-- > 0)·························· ' any steps left?
    ··········· if (mode & M_REV)·························· ' check direction
    ············· stpIdx_left := ++stpIdx_left // 4
    ············· stpIdx_right := (stpIdx_right + 3) // 4·· ' point to previous step
    ············· outa[noparse][[/noparse]m8..m1] := ( Steps[noparse][[/noparse]stpIdx_left] << 4 ) | Steps[noparse][[/noparse]stpIdx_right]
    ··········· else
    ············· stpIdx_left := (stpIdx_left + 3) // 4········ ' point to next step
    ············· stpIdx_right := ++stpIdx_right // 4
    ············· outa[noparse][[/noparse]m8..m1] := ( Steps[noparse][[/noparse]stpIdx_left] << 4 ) | Steps[noparse][[/noparse]stpIdx_right]
    ······· else
    ········· if (mode & M_REV)···························· ' check direction
    ··········· stpIdx_left := (stpIdx_left + 3) // 4
    ··········· stpIdx_right := (stpIdx_right + 3) // 4
    ··········· outa[noparse][[/noparse]m8..m1] := ( Steps[noparse][[/noparse]stpIdx_left] << 4 ) | Steps[noparse][[/noparse]stpIdx_right]
    ········· else
    ··········· stpIdx_left := ++stpIdx_left // 4
    ··········· stpIdx_right := ++stpIdx_right // 4
    ··········· outa[noparse][[/noparse]m8..m1] := ( Steps[noparse][[/noparse]stpIdx_left] << 4 ) | Steps[noparse][[/noparse]stpIdx_right]


    DAT

    · Steps······ byte····· %1010, %0110, %0101, %1001

    Post Edited (Will Eyeam) : 3/19/2009 10:30:26 PM GMT
Sign In or Register to comment.