Shop OBEX P1 Docs P2 Docs Learn Events
How to run multiple cogs and have multiple error checking strings? — Parallax Forums

How to run multiple cogs and have multiple error checking strings?

turbosupraturbosupra Posts: 1,088
edited 2010-07-17 19:56 in Propeller 1
I'm doing my first experiment where I'm combining different pieces of code and trying to dedicate cogs to the different methods.

When I use a CogNew command, and call a method with some stack space, is that sufficient to start experiments with running multiple cogs, or should I be aware of something else? Can I use the basic format below for my methods? (the example code was done without an IDE from memory, so forgive some of the minor syntax error)



PUB Main



Cognew(Method1, @method1stack)

Cognew(Method2, @method2stack)

Cognew(Method3, @method3stack)

Cognew(Method4, @method4stack)


repeat

  if something
     option1 := option2
  else
     option1 := option3






PUB Method1

  if ina[noparse][[/noparse]8] == 1
    test1 := test2
    debug.str(string("test 1 equals test 2",13))
  else 
    test1 := 4
    debug.str(string("test 1 equals 4",13))


PUB Method2

  if ina[noparse][[/noparse]8] == 1
    test1 := test2
    debug.str(string("test 1 equals test 2",13))
  else 
    test1 := 4
    debug.str(string("test 1 equals 4",13))


PUB Method3

  if ina[noparse][[/noparse]8] == 1
    test1 := test2
    debug.str(string("test 1 equals test 2",13))
  else 
    test1 := 4
    debug.str(string("test 1 equals 4",13))


PUB Method4

  if ina[noparse][[/noparse]8] == 1
    test1 := test2
    debug.str(string("test 1 equals test 2",13))
  else 
    test1 := 4
    debug.str(string("test 1 equals 4",13))



Post Edited (turbosupra) : 7/16/2010 6:23:11 PM GMT

Comments

  • heaterheater Posts: 3,370
    edited 2010-07-16 16:52
    You can do that. Only problem is that if the object used for debug is something like FullDuplexSerial it will get totally confused by having multiple threads accessing it at the same time.

    At best you will get an output all scrambled like "ttteeesssttt 123 eeeqqq...."

    To fix that put the debug.str(....) into a method with a lock around the debug.str call.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • turbosupraturbosupra Posts: 1,088
    edited 2010-07-16 18:22
    Hey,

    Thanks for the response, do you mean to put the debug.str in its own method? Also how do I put a lock around the debug.str call?



    heater said...
    You can do that. Only problem is that if the object used for debug is something like FullDuplexSerial it will get totally confused by having multiple threads accessing it at the same time.

    At best you will get an output all scrambled like "ttteeesssttt 123 eeeqqq...."

    To fix that put the debug.str(....) into a method with a lock around the debug.str call.
  • heaterheater Posts: 3,370
    edited 2010-07-16 18:30
    Have a look in the Propeller Manual. Look for locknew, lockret, lockset, lockclr. There are some nice Spin examples. I've never actually used locks so can't advise much.

    Yes put the debug.str(...)) into a common method used by all your concurrent methods.
    Something like:

    PUB printstr(astring)
        ' Do the lock thing here
    
         debug.str(astring)
    
        ' Do the lock release thing here
    
    



    I guess you will have to do the locknew some where at the beginning of your program prior to starting the cogs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • jazzedjazzed Posts: 11,803
    edited 2010-07-16 18:36
    Peter's MultiCogSerial object can do the locking for you: obex.parallax.com/objects/232/
    Still, using @heater's suggested method may be easier. Nice to explore all opportunities.

    Cheers,
    --Steve

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Pages: Propeller JVM
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-16 18:46
    Also keep in mind that your various methods do something, then they return. When you start a cog, you can't return to the COGNEW. The return is actually to a piece of code in the Spin interpreter that just stops the cog. Usually, when you start up a routine in a cog, it initializes itself, then drops into a REPEAT loop so it continually executes, perhaps with some waits so the cog drops into low power mode when it's not needed.
  • turbosupraturbosupra Posts: 1,088
    edited 2010-07-16 19:23
    @jazzed/heater, thanks!


    Mike, if I understand what you are saying correctly, you are saying that once the cog is started in PUB Main, before the repeat loop, the cog continues to run until it is told to stop, or forced to stop through a chip reset? That is what I want, and I'd just like to make sure I understand the operation of multiple cogs correctly. I need a couple of dedicated cogs at the moment.




    Mike Green said...
    Also keep in mind that your various methods do something, then they return. When you start a cog, you can't return to the COGNEW. The return is actually to a piece of code in the Spin interpreter that just stops the cog. Usually, when you start up a routine in a cog, it initializes itself, then drops into a REPEAT loop so it continually executes, perhaps with some waits so the cog drops into low power mode when it's not needed.
  • heaterheater Posts: 3,370
    edited 2010-07-16 19:38
    turbosupra: The methods you are starting in cogs, like "PUB Method4" will do their "if" or "else" parts and then they run out of code and exit, the cog they are running on stops.

    If you want them to continue again from the "if" you will have to put a "repeat" at the beginning of each method and indent the "if" - "else" parts.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • turbosupraturbosupra Posts: 1,088
    edited 2010-07-16 20:37
    Ok,

    So if I call a newcog and have a repeat loop inside of the method, they cogs will run as dedicated cogs repeating the code in that loop?

    Then if I wanted to share a cog that didn't use very many system cycles I could just put more code into the method that cog is calling?

    heater said...
    turbosupra: The methods you are starting in cogs, like "PUB Method4" will do their "if" or "else" parts and then they run out of code and exit, the cog they are running on stops.

    If you want them to continue again from the "if" you will have to put a "repeat" at the beginning of each method and indent the "if" - "else" parts.
  • heaterheater Posts: 3,370
    edited 2010-07-16 20:43
    turbosupra: "So if I call a newcog and have a repeat loop inside of the method, they cogs will run as dedicated cogs repeating the code in that loop?"

    Yep, you got it.

    You can then put more code into any one or all of those methods that you have started in cogs. You can call other new methods from those cogs as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • turbosupraturbosupra Posts: 1,088
    edited 2010-07-16 21:54
    Thank you Heater, I'll try to implement this, this weekend
  • turbosupraturbosupra Posts: 1,088
    edited 2010-07-17 19:42
    Wow that object has a lot of lines of code!

    How much of the system resources does this use? It has ~10 methods.

    I can see by the comments in the top of it that it is pretty powerful, might take me a few months to figure out how to use it though [noparse]:)[/noparse]


    jazzed said...
    Peter's MultiCogSerial object can do the locking for you: obex.parallax.com/objects/232/
    Still, using @heater's suggested method may be easier. Nice to explore all opportunities.

    Cheers,
    --Steve
  • jazzedjazzed Posts: 11,803
    edited 2010-07-17 19:56
    turbosupra said...
    Wow that object has a lot of lines of code!

    How much of the system resources does this use? It has ~10 methods.
    Yah it does, but if you're doing lots of string and number output (formatting) it will pay for itself. You can load choose the MultiCogSerial.spin file and hit F8 in Propeller Tool or BST to get the file size. If nothing else it gives you a concrete example of how to use locks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Pages: Propeller JVM
Sign In or Register to comment.