Shop OBEX P1 Docs P2 Docs Learn Events
Any suggestions for how to make one cog wait for another to finish? — Parallax Forums

Any suggestions for how to make one cog wait for another to finish?

edited 2012-04-18 13:43 in Propeller 1
I'm building a stepper motor controller, and each motor runs in its own cog. Cog zero launches a motor controller in cog one that moves a motor to a specified position and speed. Cog one then finishes and shuts down, and is relaunched when I send a new command to the motor. The problem is that cog zero must wait for cog one to finish moving the motor and shut down before it sends the next command, otherwise the motor gets multiple instructions at once. I tried to use variables and results but couldn't get it right. Any ideas?

Comments

  • KyeKye Posts: 2,200
    edited 2012-04-15 20:14
    Hi Matthewjgingerich,

    Have the first cog set a variable to zero. The first cog then launches the second cog which knows the address of the variable. The first cog then spins in a repeat loop until the variable becomes not zero. The second cog sets the variable to not zero when finished.

    Thanks,
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-04-15 20:54
    Matthew,

    Welcome to the forum!

    I feel compelled to ask: why use separate cogs when their operation appears to be sequential? Is there a way that you can combine the command receipt and execution into a single cog? Then you would not have to worry about launching a new cog and waiting for it to finish.

    -Phil
  • Mark_TMark_T Posts: 1,981
    edited 2012-04-16 09:43
    Matthew,

    Welcome to the forum!

    I feel compelled to ask: why use separate cogs when their operation appears to be sequential? Is there a way that you can combine the command receipt and execution into a single cog? Then you would not have to worry about launching a new cog and waiting for it to finish.

    -Phil

    I suspect because, as stated by the OP, there are multiple motors with a cog for each - the controlling code runs in a separate cog so it can run all the motors at once - we can presume this is some sort of positioning system or CNC application. Another common reason for inter-cog communication is because one cog is Spin and the other is in PASM.
  • dbpagedbpage Posts: 217
    edited 2012-04-18 13:43
    Sounds like a perfect use of locks (semaphores).
Sign In or Register to comment.