View Full Version : Quick coginit question

01-07-2009, 04:45 AM
I was using one of the sample code examples in methods and cogs it goes somehting like this
ˇˇˇ long stack[30]
PUB LaunchBlinkCogs
ˇˇˇ coginit( 1, Blink(4, clkfreq/3, 9), @stack[0])ˇˇˇ ' Ijust initialized a cog number1
ˇˇˇ coginit(22, Blink(5, clkfreq/7, 21), @stack[10])ˇ ' I turned on cog 22 how can that be!!!!
ˇˇˇ coginit(3, Blink(6, clkfreq/11, 39), @stack[20])

PUB Blink( pin, rate, reps)
ˇˇˇ dira[pin]~~
ˇˇˇ outa[pin]~
ˇˇˇ repeat reps * 2
ˇˇˇˇˇˇ waitcnt(rate/2 + cnt)
ˇˇˇˇˇˇ !outa[pin]
it blinks some leds I am sure you guys can figure that out by reading code. My question is
when typing I accidently put this in coginit(22, Blink(5, clkfreq/7, 21), @stack[10])ˇ 'ˇI compiled it
and the program ran just fine I then on purpose when I saw this program seemed to work added 3 more lines with
coginit(12, Blink(5, clkfreq/7, 21), @stack[10])ˇ 'ˇ downloaded it and ran the program the Leds all blinked and the program appears to run just fine
but if there are only 8 cogs how can this be?? is spin just defaulting to a know cog state,, and since my program appears to work
is there anyway to actualy see the cogs running in real time? how can I tell which cogs were actualy started if any?

Phil Pilgrim (PhiPi)
01-07-2009, 04:50 AM
First of all, you should get out of the habit of using COGINIT. There's almost never a good reason to have it in your programs. Use COGNEW instead. That said, the cogid argument to COGINIT is ANDed with %0111, so 22 actually becomes 6.


01-07-2009, 04:54 AM
Phil why not use coginit ??? Then I know exactly what Cog I have turned on right? Correct me if I am wrong but cognew just turns on a cog randomly? so say I was running an program and needed to shut down a cog that I had a known section of code running in with coginit, I just shutdown the cog number how could I do that with cognew is there a way to know which cogs are doing what?
Oh thanks that makes sense about the 22 being ANDed

Phil Pilgrim (PhiPi)
01-07-2009, 05:30 AM
COGNEW returns the cog number it has assigned, so you can shut it down when necessary. All cogs are identical. There is absolutely no reason to favor one over another when starting them up. In fact, you could easily run afoul of an already running cog started by another object if you use COGINIT. The best rule of thumb is to make your programs cog-number-agnostic. In other words, just say "No" to COGINIT!


01-07-2009, 06:01 AM
thanks Phil I better understand this now I had an article I was using from an online electronics forum I belong to
and the moderator insisted the proper way was coginit and at the time it kind of made sense to me, but I just got the manual "didnt realize the whole manual was online I thought Parallax charged for the book its very nice of them to make it available I guess thats why I keep ordering stuff from them everyday LOL this is a worse addiction than crack. wife is going to freak when she sees my credit card bill. my office looks like a mad scientist I have wire wrapped circuit boards everwhere I now own at least one of everything Parallax sells ,really! and Boe-Bot keeps pissing off her dog ... Anyway Phil if you see this and get around to it. another problem I could use some help with

I am trying to add my own objects to code I am writing while I can seem to use others from the object files my sample code is not working here is somehting very simple I tried to do can you see wher eI am going wrong.

Thank you in advance ..

Top File: CogObjectExample.spin
Blinks an LED circuit for 20 repetitions.ˇ The LED
blink period is determined by how long the P23 pushbutton
is pressed and held.
ˇˇˇ Blinker : "Blinker"
ˇˇˇ Buttonˇ : "Button"
ˇˇˇ mike : "myobject"

PUB ButtonBlinkTime | time

ˇˇˇ repeat
ˇˇˇˇˇˇ time := Button.Time(23)
ˇˇˇˇˇˇ Blinker.Start(4, time, 20)
ˇˇˇˇˇˇ Mike.start
The object myobject is just another blinker program here """"""""""""""""""""""""""""""""""""""""""""

PUB Blink | pin, rate, reps
ˇˇˇ pin := 6
ˇˇˇ rate := clkfreq/3
ˇˇˇ reps := 9
ˇˇˇ dira[pin]~~
ˇˇˇ outa[pin]~
ˇˇˇ repeat reps * 2
ˇˇˇˇˇˇ waitcnt(rate/2 + cnt)
ˇˇˇˇˇˇ !outa[pin]
it just blinks pin 6 no big deal but I wanted to call it into the main program just trying to learn to bring in objects/methods but every time I try to compile it I get the error expected a subroutine name? I also tried
mike.start or object.start., nothing seems to work right I just want to be able to properly incorporate drivers and other program bits into my main program .