Shop OBEX P1 Docs P2 Docs Learn Events
Quickstart Demo 5 Multiple Cogs — Parallax Forums

Quickstart Demo 5 Multiple Cogs

g3cwig3cwi Posts: 262
edited 2012-02-10 09:11 in Propeller 1
Hi all

I just downloaded the Quickstart demo from:

http://www.parallaxsemiconductor.com/quickstart5

This is supposed to show multiple cogs doing different stuff - but it does not on my Quickstart board. It flashes 3 LEDs at the rate determined by the last cognew statement. I was expecting to see three LEDs flashing at different rates - but I don't. Have I misunderstood the intention of the program?
{{
QuickStart 5: MulticogTwinkleDemo.spin
www.parallaxsemiconductor.com
}}

CON

  _clkmode = xtal1 + pll16x         'Establish speed
  _xinfreq = 5_000_000              '80Mhz

OBJ

  led: "E555_LEDEngine.spin"        'Include LED methods object

VAR

  byte Counter                      'Establish Counter Variable                                    
  long stack[90]                    'Establish working space

PUB Main

  cognew(Twinkle(16,clkfreq/50), @stack[0])    'start Twinkle cog 1
  cognew(Twinkle(19,clkfreq/150), @stack[30])  'start Twinkle cog 2
  cognew(Twinkle(22,clkfreq/100), @stack[60])  'start Twinkle cog 3

PUB Twinkle(PIN,RATE)                  'Method declaration 

  repeat                               'Initiate a master loop
  
    repeat Counter from 0 to 100       'Repeat loop Counter
      led.LEDBrightness(Counter, PIN)  'Adjust LED brightness 
      waitcnt(RATE + cnt)              'Wait a moment
      
    repeat Counter from 100 to 0       'Repeat loop Counter
      led.LEDBrightness(Counter,PIN)   'Adjust LED brightness 
      waitcnt(RATE + cnt)              'Wait a moment
      

Puzzled.


Edit: actually all three Leds flash the same rate but that rate seems to vary with time. Nothing is obviously being done independently.

Regards

Richard

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-09 13:14
    That's kind of a strange program.


    As it is now, the variable Counter is being changed by three different cogs. Not a good way to do this (IMO).

    Here's my suggestion.

    Comment out the global "Counter" variable.
    VAR
    
      byte Counter                      'Establish Counter Variable                                    
    

    And make it local.
    PUB Twinkle(PIN,RATE)  | counter                'Method declaration 
    
    

    It should then behave as expected.

    With these changes, the each cog uses its own variable "counter" so the other cogs aren't affecting it like the original program.

    (I think.)
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-02-09 13:23
    The underlying object, E555_LEDEngine has a warning: " For each included copy of this object only one spin interpreter should access it at a time. " Teh demo used the same object and same variables for all the COGs.

    I fixed the demo code to be multi-cog so nobody shares any variables in the object or in the main program.
    {{
    QuickStart 5: MulticogTwinkleDemo.spin
    www.parallaxsemiconductor.com
    }}
    
    CON
    
      _clkmode = xtal1 + pll16x         'Establish speed
      _xinfreq = 5_000_000              '80Mhz
    
    OBJ
    
      led[3]: "E555_LEDEngine.spin"        'Include LED methods object
    
    VAR
                                        
      long stack[90]                    'Establish working space
    
    PUB Main
    
      cognew(Twinkle(16,clkfreq/50,0), @stack[0])    'start Twinkle cog 1
      cognew(Twinkle(19,clkfreq/150,1), @stack[30])  'start Twinkle cog 2
      cognew(Twinkle(22,clkfreq/100,2), @stack[60])  'start Twinkle cog 3
    
    PUB Twinkle(PIN,RATE,INSTANCE)| counter                  'Method declaration 
    
      repeat                               'Initiate a master loop
      
        repeat Counter from 0 to 100       'Repeat loop Counter
          led[INSTANCE].LEDBrightness(Counter, PIN)  'Adjust LED brightness 
          waitcnt(RATE + cnt)              'Wait a moment
          
        repeat Counter from 100 to 0       'Repeat loop Counter
          led[INSTANCE].LEDBrightness(Counter,PIN)   'Adjust LED brightness 
          waitcnt(RATE + cnt)              'Wait a moment
          
    
    

    Now it's a happy and productive member of the Demo world!
  • g3cwig3cwi Posts: 262
    edited 2012-02-09 13:27
    Duane

    That's much better thanks. It now looks like the LEDs are being independently controlled (which is what was presumably intended). That's a change Parallax might do well to consider!

    Rick - thanks too. I like demos that demonstrate what they suggest they will!

    Regards

    Richard
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-09 13:40
    Rick,

    I haven't tried running this program, but I don't see a need to include multiple instances when run from different cogs. It makes use of the cogs counters which shouldn't interfere with one another (as long as they're be run from different cogs).
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-02-09 14:03
    I was mostly going from Kye's comment in the object code.
    // For each included copy of this object only one spin interpreter should access it at a time.


    I just updated it to use all 8 COGs, so I'lltry it with just one object instance......stay tuned.
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-02-09 14:12
    @Duane,

    I guess you're right. It appears to work with the localized counter variable and a single object instance. The object itself doesn't have any variable space and everything it does is with the passed variable (on the stack?). I need to learn my SPIN better!! :smile:

    Here's and 8 COG version with a single object instance.....it's running on my desk now and is very relaxing!
    {{
    QuickStart 5: MulticogTwinkleDemo.spin
    
    Eight COGs, Eight LEDs, Eight different Twinkle Rates  
    
    www.parallaxsemiconductor.com
    
    Demo is now multi-thread friendly.
    
    Counter variable was moved to a local variable belonging to the Twinkle
    routine
    
    
    It's fun to watch if you need to space out for a while.
    
    Modified by Rick Post /Duane Degn - 2/9/2012
    
    }}
    
    CON
    
      _clkmode = xtal1 + pll16x         'Establish speed
      _xinfreq = 5_000_000              '80Mhz
    
    OBJ
    
      led: "E555_LEDEngine.spin"        'Include LED methods object
    
    VAR
                                        
      long stack[210]                    'Establish working space
    
    PUB Main
    
      cognew(Twinkle(16,clkfreq/25), @stack[0])    'start Twinkle cog 1
      cognew(Twinkle(17,clkfreq/150), @stack[30])  'start Twinkle cog 2
      cognew(Twinkle(18,clkfreq/225), @stack[60])  'start Twinkle cog 3
      cognew(Twinkle(19,clkfreq/50), @stack[90])    'start Twinkle cog 4
      cognew(Twinkle(20,clkfreq/200), @stack[120])  'start Twinkle cog 5
      cognew(Twinkle(21,clkfreq/100), @stack[150])  'start Twinkle cog 6
      cognew(Twinkle(22,clkfreq/10), @stack[180])  'start Twinkle cog 7
      Twinkle(23,clkfreq/75)
      
    
    PUB Twinkle(PIN,RATE)| counter                  'Method declaration 
    
      repeat                               'Initiate a master loop
      
        repeat Counter from 0 to 100       'Repeat loop Counter
          led.LEDBrightness(Counter, PIN)  'Adjust LED brightness 
          waitcnt(RATE + cnt)              'Wait a moment
          
        repeat Counter from 100 to 0       'Repeat loop Counter
          led.LEDBrightness(Counter,PIN)   'Adjust LED brightness 
          waitcnt(RATE + cnt)              'Wait a moment
          
    

    Stack is way to big - the object says 11 longs, but for a demo, we have plenty of memory.
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-02-09 14:16
    D Rat, try it with all 8 cogs....it's hypnotic.
  • ratronicratronic Posts: 1,451
    edited 2012-02-09 14:39
    I bet it is Rick I will give it a shot.
  • kuronekokuroneko Posts: 3,623
    edited 2012-02-09 16:17
    Duane Degn wrote: »
    As it is now, the variable Counter is being changed by three different cogs. Not a good way to do this (IMO).
    So they still haven't fixed it ([post=1045233]this came up before[/post])? What does it take (I told someone by PM) to get stuff fixed around here?
  • g3cwig3cwi Posts: 262
    edited 2012-02-10 00:26
    Demos that don't work when they are supposed to show off the USP of the product are not what you want!
  • ratronicratronic Posts: 1,451
    edited 2012-02-10 08:16
    g3cwi you should leave a message in the "Suggestions to Parallax" forum of this and refer them to this thread.
  • g3cwig3cwi Posts: 262
    edited 2012-02-10 09:11
    Dave

    Just done that.

    Cheers
Sign In or Register to comment.