Transfer variables between COGS
svensson
Posts: 11
Hello,
I'm new working with the propeller. So I've questions, maybe stupid, but I don't care.
For this propeller controller, I want to use it to read sensors, like PIR,·blow switch. PIR and Blowswitch are separated cogs, I think. See code
But my question is, what is the simplest way to transfer a variable between blowswitch and PIR ( 2 cogs )?????????????????
This is for checking the blowswich was enabled, set,unset·variable PIRVAR in blowswitch method·and then use PIRVAR in the PIR method. You may only check the PIR if the blowswitch was enabled!!!!!!!
Thanks
VAR
· long· Stack[noparse][[/noparse]20]············ 'Stack space for new cog
· long· Stack1[noparse][[/noparse]20]··········· 'Stack space for new cog
· byte· Cog···························· 'Hold ID of cog in use, if any
· long w
· long v
··· long PIRVAR··
··
PUB StartBlowswitch : Success
{{Start new blinking process in new cog; return TRUE if successful}}
'·· Stop
··· Success := (Cog := cognew(Blowswitch,stack) + 1)
PUB StartPIR : Success1
{{Start new blinking process in new cog; return TRUE if successful}}
'·· Stop
··· Success1 := (Cog := cognew(PIR,stack1) + 1)
PUB Stop
{{Stop toggling process, if any.}}
· if Cog
··· cogstop(Cog~ - 1)
PUB Active: YesNo
{{Return TRUE if process is active, FALSE otherwise.}}
· YesNo := Cog > 0
PUB Blowswitch
{{Toggle Pin, Count times with Delay clock cycles in between.}}
· DIRA[noparse][[/noparse]3..23] := %000000000000011111111··············· 'Set I/O pin to output direction
· repeat
··· w := INA[noparse][[/noparse]3]
·····
··· if w == 1
·
······· OUTA[noparse][[/noparse]16..19]~~············ '· set I/O Pin· (leds)
······· PIRVAR := 0·
··
··· else
······· OUTA[noparse][[/noparse]16..19]~············ '· reset I/O Pin
······· PIRVAR := 1
········
PUB PIR··
· DIRA[noparse][[/noparse]3..23] := %000000000000011111111··············· 'Set I/O pin to output direction
· repeat
··· v := INA[noparse][[/noparse]4]
·····
··· if·· v == 1 & PIRVAR == 0······
·
······· OUTA[noparse][[/noparse]20..23]~~············ '· Toggle I/O Pin··
··
··· else
······· OUTA[noparse][[/noparse]20..23]~············ '· Toggle I/O Pin
I'm new working with the propeller. So I've questions, maybe stupid, but I don't care.
For this propeller controller, I want to use it to read sensors, like PIR,·blow switch. PIR and Blowswitch are separated cogs, I think. See code
But my question is, what is the simplest way to transfer a variable between blowswitch and PIR ( 2 cogs )?????????????????
This is for checking the blowswich was enabled, set,unset·variable PIRVAR in blowswitch method·and then use PIRVAR in the PIR method. You may only check the PIR if the blowswitch was enabled!!!!!!!
Thanks
VAR
· long· Stack[noparse][[/noparse]20]············ 'Stack space for new cog
· long· Stack1[noparse][[/noparse]20]··········· 'Stack space for new cog
· byte· Cog···························· 'Hold ID of cog in use, if any
· long w
· long v
··· long PIRVAR··
··
PUB StartBlowswitch : Success
{{Start new blinking process in new cog; return TRUE if successful}}
'·· Stop
··· Success := (Cog := cognew(Blowswitch,stack) + 1)
PUB StartPIR : Success1
{{Start new blinking process in new cog; return TRUE if successful}}
'·· Stop
··· Success1 := (Cog := cognew(PIR,stack1) + 1)
PUB Stop
{{Stop toggling process, if any.}}
· if Cog
··· cogstop(Cog~ - 1)
PUB Active: YesNo
{{Return TRUE if process is active, FALSE otherwise.}}
· YesNo := Cog > 0
PUB Blowswitch
{{Toggle Pin, Count times with Delay clock cycles in between.}}
· DIRA[noparse][[/noparse]3..23] := %000000000000011111111··············· 'Set I/O pin to output direction
· repeat
··· w := INA[noparse][[/noparse]3]
·····
··· if w == 1
·
······· OUTA[noparse][[/noparse]16..19]~~············ '· set I/O Pin· (leds)
······· PIRVAR := 0·
··
··· else
······· OUTA[noparse][[/noparse]16..19]~············ '· reset I/O Pin
······· PIRVAR := 1
········
PUB PIR··
· DIRA[noparse][[/noparse]3..23] := %000000000000011111111··············· 'Set I/O pin to output direction
· repeat
··· v := INA[noparse][[/noparse]4]
·····
··· if·· v == 1 & PIRVAR == 0······
·
······· OUTA[noparse][[/noparse]20..23]~~············ '· Toggle I/O Pin··
··
··· else
······· OUTA[noparse][[/noparse]20..23]~············ '· Toggle I/O Pin
Comments
You will have to transfer via the hub. Usually, you set a value and then a flag. Alternately, you can set a value in hub, and when the other cog reads it, it clears it (provided 0 is not a legal value). If you have a spare pin, you can use this as an internal flag.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
All cogs that use "variable" will be able to modify it and read it. I do this in my project: Black Box
I have 4 prop chips inter-communicating and sharing data between the cogs of each chip. Most of the chips use all 8 cogs to do the job.
You can look at my code to see how I did it.
http://forums.parallax.com/showthread.php?p=831833
If you open the Audio Sequencer file you will see the dat block has variables in it.
And I am reading and modifying them using more than 1 cog.
Look up DAT in the prop manual to see syntax.
Keep in mind your code will prevent collisions if you write and read in bytes. (no need for code protect bit, i think its bytes)
http://obex.parallax.com/objects/331/
as long as your PIR-code and your blowswitch code belong to the SAME *.SPIN-file you can access them just by using the name
even ACROSS cogs !!
as long as you start a cog by cognew from the SAME *.SPIN.file you can access the variables from both cogs simply by the variable-name
If you want to access variables across *.SPIN-files = objects you could define methods in one of the objects that give back the desired value
best regards
Stefan
I make a global variable and its is still not working. I make a global variable, variable. I make it "1" in PUB BlowSwitch and I want to read it in PIR, but it can't read it.
Can somebody say what's wrong in code? Already thanks for the ideas.
VAR
long Stack[noparse][[/noparse]20] 'Stack space for new cog
long Stack1[noparse][[/noparse]20] 'Stack space for new cog
byte Cog 'Hold ID of cog in use, if any
long w
long v
long variable
PUB StartBlowswitch : Success
{{Start new blinking process in new cog; return TRUE if successful}}
' Stop
Success := (Cog := cognew(Blowswitch,stack) + 1)
PUB StartPIR : Success1
{{Start new blinking process in new cog; return TRUE if successful}}
' Stop
Success1 := (Cog := cognew(PIR,stack1) + 1)
PUB Stop
{{Stop toggling process, if any.}}
if Cog
cogstop(Cog~ - 1)
PUB Active: YesNo
{{Return TRUE if process is active, FALSE otherwise.}}
YesNo := Cog > 0
PUB Blowswitch
{{Toggle Pin, Count times with Delay clock cycles in between.}}
DIRA[noparse][[/noparse]3..23] := %000000000000011111111 'Set I/O pin to output direction
variable := 1
repeat
w := INA
if w == 1
OUTA[noparse][[/noparse]16..19]~~ ' set I/O Pin (leds)
else
OUTA[noparse][[/noparse]16..19]~ ' reset I/O Pin
PUB PIR
DIRA[noparse][[/noparse]3..23] := %000000000000011111111 'Set I/O pin to output direction
repeat
' v := INA
if variable == 1
OUTA[noparse][[/noparse]20..23]~~ ' Toggle I/O Pin
else
OUTA[noparse][[/noparse]20..23]~ ' Toggle I/O Pin
1. Increase the stack and stack1 size, start with 100 until its working.
2. Change the stack and stack1 in cognew to @stack and @stack1
But how can I exchange data ( a variable ) between these 2 procedures?
'' object code
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
OBJ
togpir : "leds, microfoon,..."
DAQ : "MCP4922"
PUB Main
togpir.StartBlowswitch
togpir.StartPIR
I want to give you a big tap on your shoulder meaning WELL DONE. You are maybe thinking hoo ? what ?
Yes indeed you made a VERY GOOD posting because it included your code !
With the code it was very clear here is the bug.
By writing this I want to invite every newbee to include their code to their posting.
As with everything it can be improved. If you use the post-replay button instead of the quick-reply-form
you get a new form with buttons to insert code.
text between
[noparse][[/noparse] code ]
[noparse][[/noparse] / code ] written WITHOUT the spaces appears this way that the indention is still there.
Some problems are indention-problems and you can't find them if the indention is removed.
ADD SMALL pieces of code directly inserted with code /code
ADD BIG pieces of code as zip-archives of the COMPLETE project by using the archive function of the propeller-tool
There is a website SPIN-code-formater that does all the work for
SPIN-code-formatter
with this form you just do a Ctrl-a, Ctrl-c in your Spinfile in the propeller-tool.
Then Ctrl-v in the web-form and you will receive your code ready for copy &paste into the propeller-forum-form
like shown below even with bolding the reserved SPIN-words
if your posting from today 1:49 AM (GMT -7)
still refers to your code from the first posting including the file leds.spin
I think it is the bitwise and operator "&" that is causing the problems
in your case I think you want to have a logical boolean combinig of "(v == 1)" and "(PIRVAR == 0)"
and thats just the way you write it
second thing is the "~" or the "~~" does NOT toggle a pin
it sets or clears a pin
you can do toggling by using the "!"-operator
again you can see how much important it is to provide your COMPLETE code !
AND additional: If you write us what you want to do IN THE END you will get even better support !
I'm now active for 3 years in this forum. From this experience I know: if the WHOLE project is known
in 95% of all cases the solutions that were found are much better than if somebody is only asking for a detail.
best regards
Stefan
Post Edited (StefanL38) : 10/8/2009 10:17:30 AM GMT
I just made some changes in my code. Below you can see the child spin object. Below the child object I gave the TOP object file. Now I try to pass a "1" value through VARS in the top object file and variable in the child object. But still I can't read these "1" value in the child procedures ( PIR and Blowswitch ). Can someone say whats wrong now?
child object
VAR
long Stack[noparse][[/noparse]500] 'Stack space for new cog
long Stack1[noparse][[/noparse]500] 'Stack space for new cog
byte Cog 'Hold ID of cog in use, if any
long w
long v
PUB StartBlowswitch (variable) : Success
{{Start new blinking process in new cog; return TRUE if successful}}
' Stop
Success := (Cog := cognew(Blowswitch(variable) ,@stack) + 1)
PUB StartPIR (variable) : Success1
{{Start new blinking process in new cog; return TRUE if successful}}
' Stop
Success1 := (Cog := cognew(PIR(variable) ,@stack1) + 1)
PUB Stop
{{Stop toggling process, if any.}}
if Cog
cogstop(Cog~ - 1)
PUB Active: YesNo
{{Return TRUE if process is active, FALSE otherwise.}}
YesNo := Cog > 0
PUB Blowswitch (variable)
{{Toggle Pin, Count times with Delay clock cycles in between.}}
DIRA[noparse][[/noparse]3..23] := %000000000000011111111 'Set I/O pin to output direction
repeat
w := INA
if (w == 1) and (variable == 1)
OUTA[noparse][[/noparse]16..19]~~ ' set I/O Pin (leds)
else
OUTA[noparse][[/noparse]16..19]~ ' reset I/O Pin
PUB PIR (variable)
DIRA[noparse][[/noparse]3..23] := %000000000000011111111 'Set I/O pin to output direction
repeat
' v := INA
if variable == 1
OUTA[noparse][[/noparse]20..23]~~ ' Toggle I/O Pin
else
OUTA[noparse][[/noparse]20..23]~ ' Toggle I/O Pin
top object file
DAT
vars byte 1
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
OBJ
togpir : "leds, microfoon,..."
DAQ : "MCP4922"
PUB Main
togpir.StartBlowswitch(@vars)
togpir.StartPIR(@vars)
as your methods blowswitch and PIR do nothing else than looking at INA 3 INA 4
these lines of code can run in ONE cog sequentially after each other without changing the result
It will become different if you add some other code that makes the executiontime of the repeat-loop long
(longer than you can wait until next checking of INA 3 /4)
so from the MODIFIED code I get the following results:
INA3 low
INA4 low
ALL LEDs OFF
INA3 low
INA4 high
ALL LEDs OFF
INA3 high
INA4 low
LEDs 16..19 ON
INA3 high
INA4 high
ALL LEDs ON
INA3 low
INA4 high
ALL LEDs OFF
is that what you want ? I guess not. So it's time to write what you want to do IN THE END.
And as an old programmers advice:
define constants with SELF-EXPLAINING names for your Inputs and Outputs and logic states
This is a little bit more of typing but once you have typed it the code is much more understandable by just reading it
instead of reading one line jumping up to a definition line or research in your memory what was INA[noparse][[/noparse]17] for ????
If you read "INA[noparse][[/noparse] PIR_2] " everything is clear by just reading THIS single line
so please give me a favor and write what you want to do IN THE END
best regards
Stefan
Post Edited (StefanL38) : 10/8/2009 11:39:34 AM GMT
My code has 2 procedures , blowswitch and PIR. With blowswitch I read an external switch and with PIR I read a PIR motion sensor input.
But for learning and using the propeller in the future, I want to have these 2 procedures in parallell in 2 different cogs. But I want also see a PIR input signal, only if the external switch was pressed.
So I want to give a "1" signal to the PIR procedure when the switch was pressed in the blowswitch procedure.
LEDS 16-19 are ON when blowswitch is pressed
LEDS 20-23 must be ON when blowswitch is pressed AND PIR is ON ( toggle is wrong actually , just leds on and off to see the sensors are working )
I hope its more clear now????
Thanks for the help,
Sven
Why are you using a child object?
But to answer your question, your code doesn't work because you have to remember what your giving that child object when you run this
:
(@vars) = the memory ADDRESS is passed. This can be called a pointer. (because its just a value that points at your desired data)
Then in your child object you need to work with that address.
In my code programming experience, objects suck. Only use them if you must.
To work with that address you will need to do this:
When you need to read or write to "variable" you need to use the following format. Because the data at vars is a BYTE use the BYTE command.
BYTE COMMAND SYNTAX FOR ADDRESS PASSING:
BYTE [noparse][[/noparse]BaseAddress] 〈[noparse][[/noparse]Offset]〉
SO your code would read (only two areas work with "variable" in the child object)
Also if you change "variable" to VarAddress then you will know whats really in that variable.
BUT you didn't need to do all this.
Your original code just needed this:
remove variable from the VAR area. and put it at the bottom in DAT area.
This should work now.
Thanks all for the quick replies, this is a great forum!!!!!!!!!!!!!!
Now I am able to pass a variable between cogs
But you don't PASS the variable.
a DAT-variable is stored in Hub-RAM. So any code can SHARE one and the same variable
The variable can be easily accessed through a pointer to the HUB-RAM-adress where the value is stored
best regards
Stefan