Shop OBEX P1 Docs P2 Docs Learn Events
How to keep a cog running a background task? — Parallax Forums

How to keep a cog running a background task?

Larry C.Larry C. Posts: 48
edited 2011-06-30 12:49 in Propeller 1
I thought I had a pretty good handle on the cog concept, but I can't fix what seems like it should be a fairly simple problem. Could use some good advice.

I'm trying to construct a program that puts out a continuous 25 Hz square wave while monitoring an external pin. When the pin pulses high, I want to run a separate cog that turns on a LED for one second, without interrupting the 25 Hz. Trouble is that the LED driver cog stops the 25 Hz. I need to trigger the LED and have the cog go on about it's business without hanging up the 25-Hz generator.

Here is the bare-bones .SPIN code that I've come up with, showing the problem.
CON
  _xinfreq = 5_000_000          ' specify 5 MHz xtal 
  _clkmode = xtal1 + PLL16X     ' and set clock to 80 MHz
  
' I/O pin assignments
  dtpin         = 20
  LEDpin        = 19
  testpin       = 23
  
OBJ
  longdim : "LongCog" 

VAR

PUB Main
{{

Generates a continuous 25 Hz square wave. When dtpin pulses high, an
independent cog "LongCog" turns on an LED for one second. The 25 Hz
clock should continue without interruption.

(But it doesn't.)

}}
  dira[testpin]~~
  longdim.Start 
  repeat
    IF ina[dtpin] == %1
      longdim.LCog   ' trigger LED on for 1 second
    outa[testpin]~~
    waitcnt(cnt + clkfreq/50)
    outa[testpin]~
    waitcnt(cnt + clkfreq/50)



And this calls:
{{ LongCog.SPIN
28 June 2011
}}

CON
  _xinfreq = 5_000_000          ' specify 5 MHz xtal 
  _clkmode = xtal1 + PLL16X     ' and set clock to 80 MHz
  
' I/O pin assignments
  LEDpin        = 19

VAR
  long LCogStack[10]
  long LCogID

PUB Start
  dira[LEDpin]~~
  Stop
  LCogID := cognew(LCog, @LCogStack)
  
PUB Stop
  IF LCogID > 0
    cogstop(LCogID)
    
PUB LCog
{
Turns LED on for 1 second
}
  outa[LEDpin]~~
  waitcnt(cnt + clkfreq)
  outa[LEDpin]~
    

Surely, I'm overlooking something obvious, but I can't get a handle on it. Any help is appreciated,

Thanks

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-06-28 18:47
    Shouldn't you be calling the Start method of the LongCog object?

    Update: Did I really overlook that? What I meant was Start instead of LCog.
  • Mark_TMark_T Posts: 1,981
    edited 2011-06-28 20:21
    You are simply calling code in the same cog. You need as mentioned to start a separate cog. This cog then looks at the dtpin, not the cog that's doing the 25Hz.

    Even better configure a timer to do the square wave, then you'll only need one cog.
  • Larry C.Larry C. Posts: 48
    edited 2011-06-29 06:53
    Mark_T:

    OK, that's sorta what I thought -- that everything is running in the same cog. But I don't understand why it's so.

    The first object (running 25 Hz) runs in cog 0. Then I call the start method in "LongCog" ( a totally separate object) which runs cognew and starts its code its own cog, (cog 1, I assume).

    So, how can everything be running in the same cog?

    What am I not seeing here?

    Thanks for any comments,

    Larry C.
  • mindrobotsmindrobots Posts: 6,506
    edited 2011-06-29 07:36
    Larry,

    Assume you start execution on COG0:

    In your main routine, your set the direction of a pin and call start for longdim. At this point, COG0 runs that code which includes a COGNEW - this instruction causes COG0 to start up a new COG (assume COG1) and starts it running the code at LCog.

    COG1 goes off and runs its code - turns LED on for a second and then STOPS

    COG0 returns to the instruction after longdim.start, enters the repeat loop and executes here for the rest of its life. Lighting the light and then doing some frequency stuff, lighting up, then doing frequency stuff.....it's busy, COG1 is no longer in the picture.

    COG 1 needs to be started and then the code path for COG1 needs to check the pin and light the light while COG0 goes back to do its frequency thing.

    When a COG executes a call to code in another object (longdim.LCog) it just goes to execute that code it doesn't start another COG or have any idea if some other COG was supposed to be executing that code....that's your job as programmer and ruler of the world you just created. The independant COGs need to communicate with each other in some way if they need to be aware of each other.

    Hope this helps!
  • Larry C.Larry C. Posts: 48
    edited 2011-06-30 08:36
    Thanks, Rick, et.al.

    I have it running now.

    This is what I needed to hear (from Rick):

    "When a COG executes a call to code in another object (longdim.LCog) it just goes to execute that code it doesn't start another COG or have any idea if some other COG was supposed to be executing that code....that's your job as programmer and ruler of the world you just created. "

    I just never ran into that before now.

    Larry C.
  • BEEPBEEP Posts: 58
    edited 2011-06-30 12:49
    dira and outa must be in same cog.
    PUB LCog   
      dira[LEDpin]~~  '<----------------------------------------------!!!
    {
    Turns LED on for 1 second
    }
      outa[LEDpin]~~
      waitcnt(cnt + clkfreq)
      outa[LEDpin]~
    
Sign In or Register to comment.