Having Trouble passing parameters
gdrowell
Posts: 7
Hello All,
I am having difficulty passing information from two slave cogs back to the main cog. My code is as follows:
CON
high = 1
low = 0
VAR
long stack[30]
long stack2[30]
long stack3[30]
PUB Main
Coginit (1, Task1(5, 5, 2) , @stack2)
Coginit (2, Task2(3, 2, 6) , @stack3)
dira[16]~
**
if Task_1_finished_flag == Task_2_finish_flag
outa[16] = high
**
PUB Task1(a, b, c) : Answer1 | Task_1_finished_flag
Answer1 := a + b / c
Task_1_finished_flag := 1
PUB Task2(d, e, f) : Answer2 | Task_2_finished_flag
Answer2 := d - e * f
Task_2_finished_flag := 1
I am trying to get a LED light to turn on once all operations are done in the 2 cogs doing the simple arithmetic. Any direction or advice would be helpful. Thanks in advance
p.s. The ** area has syntax errors.
I am having difficulty passing information from two slave cogs back to the main cog. My code is as follows:
CON
high = 1
low = 0
VAR
long stack[30]
long stack2[30]
long stack3[30]
PUB Main
Coginit (1, Task1(5, 5, 2) , @stack2)
Coginit (2, Task2(3, 2, 6) , @stack3)
dira[16]~
**
if Task_1_finished_flag == Task_2_finish_flag
outa[16] = high
**
PUB Task1(a, b, c) : Answer1 | Task_1_finished_flag
Answer1 := a + b / c
Task_1_finished_flag := 1
PUB Task2(d, e, f) : Answer2 | Task_2_finished_flag
Answer2 := d - e * f
Task_2_finished_flag := 1
I am trying to get a LED light to turn on once all operations are done in the 2 cogs doing the simple arithmetic. Any direction or advice would be helpful. Thanks in advance
p.s. The ** area has syntax errors.
Comments
John Abshier
John Abshier
You should make Answer1 and Answer2 global variables as well. Every function in the same file has access to the VARs. To make it more flexible you could also pass the address of an Answer variable to the task function.
This is an overview of my project . . . Thanks.
Also, thanks MagIO2 . . . .I'll try that and get back to you. Thanks a bunch
Why I ask?! Because starting a COG for doing such simple things is meaningless. Start time is simply to long compared to the execution time of the task itself. And if COG0 has nothing better to do than wait for the results it could have done the job by itself faster ;o) So, usually for repetitive tasks COGs are started and wait for a command from the master. In this way you don't have the startup-time.
By the way ... your if statement should be a
repeat until Task_1_finished_flag and Task_2_finish_flag
otherwise the master does not wait for the results.
And last but not least, after you set the output to high you should have an endless repeat loop, otherwise the COG0 will be stopped, which will disable the outputs of that COG.
By changing the if statement, should it be
repeat
if Task1_finshed_flag && Task2_finished_flag
! outa[16]
or do i still keep separate?
This may be helpful:
-- http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp10.pdf
COG 0 starts another piece of code in another COG to do some task and goes on its way. If at some point the code COG 0 is running needs to check on the work the COGs it started are doing, it needs to check a global variable - did a change from 1 to 2? sit on a global variable in a repeat - did this variable get set to true yet? The communication between independent code paths needs to be coordinated by the programmer. You need to set up the communication method (variables, flags, semaphores, interrupts, whatever your hardware and software supports). In propeller-land, it's global variables for a nice clean way to do it.
The repeat that was suggested by MaglO2 is that type of arrangement within your code. COG0 code is waiting for both of the "finished" flags to be set to true by the other COGS that were started. It just sits and waits....it could be waiting on math...a servo to be moved into position....a character to come in from a serial pin.....anything your sensors desire but in the end, the code in the other COG needs to set a global variable to something the COG0 code understands as a signal.
Multiple processor communication is wickedly fun!!
You have an array named stack which is not used. The main COG does not need a stack array. The compiler takes care that COG 0 always has the whole rest of HUB-RAM as stack-space. "whole rest" means all memory which is not used by VAR/DAT/PUB/PRI - the free RAM reported when clicking "View Info" in the Propeller Tool.
Boy do I really relate to your problem! I raised this question in this threadhttp://http://forums.parallax.com/showthread.php?128922-Passing-Memory-Address-I-am-stumped! toward the bottom Jonny-Mac attached version 2 of etimer that finally gave me the insite i needed to understand using global VARS. I used v2 and am passing the timer var to 3 different cogs that are reading the "master clock" I also am passing several other arrays and pointers from the top object to multiple cogs.
Jim