stopping/starting multiple cogs
mikea
Posts: 283
in Propeller 1
I'm having a problem with identifying cogs to be able to start and stop them. I've seen the examples in the pe kit and prop manual. I tried the example in the prop manual , but the compiler said I need a unique variable when I used cogid instead of cog1 (or 2). Also with the prop manual example i'm not sure how to control multiple cogs. The code will compile the way I have it below, but does not work.
var long servostack [150] long servo2stack [150] long cog1 long cog2 pub go halt:=1 'input changes state of halt and is intended to stop a cog cog1:=cognew(servo2,@servo2stack) cog2:=cognew(servo,@servostack) .....other unrelated code here..... repeat phsa:= -thar tt+= tc waitcnt(tt) if halt==0 cogstop( cog1) (cogstop for cog2 is the same as this group of code)
Comments
Crappy forum software, killed fixing old posts, attachments, and kills old urls, and old youtube video urls.
DAMN the torpedos, full steam ahead into lame forum software!
And then it was UNattached because it had issues, but this post remains, forever! HUZZZAH!
The reason for the + 1 is that cognew returns -1 if the cog didn't start, or 0 to 7 if it did. By adding one we promote the ID to a value that can be used in a true-false expression (i.e., you'll end up with a value between 1..8 if the cog started). Just remember to account for this when stopping said cog.
And, FWIW, you're consuming WAY too much RAM (4K total) for stack space. With simple routines like that you could get away with far fewer longs. For example, here's an actual bit of code from the EFX-TEK HC-8+ controller. This embedded object runs in its own cog providing a 1ms clock, maitaining a Red/Green LED, and reading 16 inputs via two shift registers. Note the size of the stack = 32 longs.
I start that cog from my setup() method with this line of code
I have not spend much time with fine tuning that yet. For the cog stop/start methods I was using example code posted earlier, but I think i'm doing something wrong. It will start the cog and i'm confident it's ending up at the halt method at the appropriate time, but it's not stopping the cogs. I'm all set now, but would like to know what I was doing wrong.
I think thats the same problem I have with it, the stop function dosen't stop the cogs, but i never used it.. I just used the code to start cogs (and comment out ones i didn't want started)
It seems Jon is saying the code is more complicated than it needs to be, perhaps so, I don't understand it myself, i should probably take it and put some debug code into it and see what the cog variables are doing...
Thats interesting that Jon mentions keeping the main cog running... i stated that once in a thread but it was missed.. and my other bad points were focused on. meh...
I also show I did this in the code above.
I'll dig into the code i posted and figure it out soon. That will be my project tonight...
I didn't know you could do that.. Since (from what i understand) there is a minimum limit to the waitcnt command.
I think that needs to be fixed so the cogs get a unique id.
I am looking at the propeller manual and thats where the above code i originally attached, came from.
Propeller manual bottom of page 83.
This "UN-necessarily" complicated code is in the manual and im still looking it over to fully understand it, so i don't know why the manual uses "UN-necessarily" complicated code.
But I see some differences... from what i posted... time to do more testing.
The @ symbol in front of the Function is not correct, it should be in front of the variable stack.
I think its a typo and meant to read
So,.... After fixing a bug in the manual, I also fixed my bug, both fixed, due to understanding the manual's examples.
My code was changed to match the manual, except for its own address symbol bug...
Get putty.
Upload this code.
And connect to your serial port with putty.
The count will stop every 10 seconds and wait 10 seconds and start the count again. It strays a bit due to starting and stopping the cog without consideration for the timing; when its best to stop the cog, in relation to the variable update, and the display painting.
But the start and stop cog code, works now.
Waitcnt(0) is unnecessary.
However, the benefit of using waitcnt(0) is that it will put the cog to go into low power mode for ~54 seconds per loop of the 32 bit counter.
The example is correct, it just doesn't have the correct reference. The cognew function has two syntax, one to start Spin functions and another to start PASM code, what you see in that example is the latter. Look at page 79-81 where both syntax are explained.
Your code needs to start a Spin function so your fix is correct because in that case the second argument is the pointer to the Spin stack.
The way I understood the manual...
The prop assembly has a different section.
Page 83 is for SPIN
Page 286 is for pasm.
But the cogstop for pasm says nothing about that, nor does the cogstop for spin mention how the example code is for pasm. (and why is the spin section using a pasm example) LOL
From what I can tell actually pasm and spin syntax are all mixed up in the manual, and not seperate.
THAT IS EXCELLENT, and not confusing at all.
(and i must page back to an area for cognew, to find info on cogstop???)
THAT IS EVEN MORE EXCELLENT!
Im closing PropellerTool, and gonna watch a movie...
Since chaos seems the theme for me tonight.. HardCore Henry should be an excellent movie right now.
Page 286 is to start cogs from PASM while page 83 is to start cogs from SPIN and from Spin you have two different syntax for cognew!
And My page 286 has CogStop for pasm. (but probably not enough examples)
What version of the manual are you using? 1.2 is the one on the parallax website; the one i have.
https://www.parallax.com/downloads/propeller-manual
Page 286 of PropellerManual version 1.2.
Should read
again.... Im closing PropellerTool, and gonna watch a movie... Since chaos seems the theme for me tonight.. HardCore Henry should be an excellent movie right now.
I suppose i could use my theme of the evening to find all the chaos around my world... but that sucks... im just gonna waste it by watching chaos.
By the way, you don't need to define separate method for each cog; the beauty of Spin cogs is that you can run the same cog in multiple interpreter cogs (this is why each needs its own stack). For example: You could blink the LEDs on a PAB at different rates like this