Shop OBEX P1 Docs P2 Docs Learn Events
Cogs launching Cogs, is it necessary? — Parallax Forums

Cogs launching Cogs, is it necessary?

TLCTLC Posts: 74
edited 2011-01-11 09:18 in Propeller 1
I've been trying to wrap my mind around this for hours (coding, reading and looking through the forum) and I'm still stumped, so sorry if it's an obvious question. I want two processes to operate independently (In their own cogs?) that need to launch their own cogs and update global variables that can be read by the top object.

For example, I would like an independent scan routine for two servos (with Ping modules) to continuously scan and update a set of variables. For this to be completely asynchronous to the main program I was going to assign a cog to each servo. I was also going to use PWM_32_v2 as the servo controller, but since the method is in assembly, I'm under the assumption that it needs to be launched in its own cog. Is that correct.

To do this will I have to launch a cog that does the scan routines and have that launch a cog that runs the PWM_32 code, taking up 4 cogs?

Thanks in advance,

Todd

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-01-09 21:40
    there is a better suited object for this in the obex. it is call Servo32v7
    Like the name says it CAN handle up to 32 servos.

    It is in assembler too. As long as code is in ONE spin-file (let*s call it FILE_A.spin all global variables of this SPIN-code-file is accessible. Even ACROSS cogs.

    Only global variables that are in another SPIN-file (let's call it FILE_B.spin) are not directly accessible from File_A.spin.
    In this case you have to define methods that give back the values of variables as the result

    MyMethod1_get_var

    and to set variables methods

    MyMethod1_Set_var

    So it will be enough to start the Servo32v7-object ONCE

    and then start two methods for two scan-routines and your main-program summing up in three cogs.

    best regards

    Stefan
  • TLCTLC Posts: 74
    edited 2011-01-10 08:10
    Stefan,

    Thanks so much for the response, I'll try it as soon as I get home this evening. I had an equally hard time figuring out how to double dereference a C pointer 30 years ago.

    Todd C
  • JonnyMacJonnyMac Posts: 9,235
    edited 2011-01-10 08:19
    I might be above your comfort zone now, but you could create a method that sweeps a servo and stores the ping results in an array. Through careful coding you can reuse this code and launch into multiple cogs, that way, if you decide to add a third scanner there would be no additional code required, just launching the existing code into its own cog.
  • TLCTLC Posts: 74
    edited 2011-01-11 08:03
    Jon,

    Thanks for the reply. I'm mostly doing this to stay out of my comfort zone. I learned everything I know about the SX (even a little interrupt programming) by reading your columns and doing some of the projects.

    I almost hated to ask the question because it seems like a huge advantage of the Propeller would be the ability to launch a cog running PWM that can handle 32 channels, thereby taking care of differential motor drive control as well as all servo control and being able to easily use that from any of the other cogs independently. Something that obviously useful seems like it would be very straightforward so it was just a little frustrating not to "just get it."

    Thanks again and keep up the good work with The Spin Zone.

    Todd C.
  • TLCTLC Posts: 74
    edited 2011-01-11 09:18
    Jon,

    I should listen to myself. I just read your January Spin Zone article and I had my "Aha!" moment. Best description of how variable scope/multiple COGs works that I've found. Now I have to wait until I get home to play (I need a demo board for my desk!)

    Todd C.
Sign In or Register to comment.