Shop OBEX P1 Docs P2 Docs Learn Events
Transfer variables between COGS — Parallax Forums

Transfer variables between COGS

svenssonsvensson Posts: 11
edited 2009-10-08 15:11 in Propeller 1
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 )?????????????????confused.gif
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!!!!!!!

Thankssmilewinkgrin.gif

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

  • Cluso99Cluso99 Posts: 18,069
    edited 2009-10-07 09:32
    Without looking at your code....

    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
  • Clock LoopClock Loop Posts: 2,069
    edited 2009-10-07 09:49
    Put your variables in the DAT block at the end of your code.

    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.

    DAT
    variable      byte     254
    
    
    



    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)
  • DavidGregDavidGreg Posts: 38
    edited 2009-10-07 12:40
    Another example of what clock loop is talking about:

    http://obex.parallax.com/objects/331/
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-07 16:03
    Hello Svenson,

    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

    PUB GetVAR : MyValue 
    ...
    ...
      MyValue := MyVar
    
    



    best regards

    Stefan
  • svenssonsvensson Posts: 11
    edited 2009-10-08 07:55
    Hello guys,

    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
  • TimmooreTimmoore Posts: 1,031
    edited 2009-10-08 08:16
    2 things to start
    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
  • svenssonsvensson Posts: 11
    edited 2009-10-08 08:49
    It seems that only the blowswitch procedure is working. But when I call BlowSwitch and PIR using an object, the PIR procedure works.

    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
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-08 09:38
    Hello Svenson,

    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

    [b]VAR[/b]
      'long Stack[noparse][[/noparse]*20]  'Stack space for new cog
      'long Stack1[noparse][[/noparse]*20] 'Stack space for new cog
      [b]long[/b] Stack[noparse][[/noparse]* [b]100[/b]]   'Stack space for new cog
      [b]long[/b] Stack1[noparse][[/noparse]* [b]100[/b]]  'Stack space for new cog
      [b]byte[/b] Cog 'Hold ID of cog in use, if any
      [b]long[/b] w
      [b]long[/b] v
      [b]long[/b] variable
    
    
    [b]PUB[/b] StartBlowswitch : Success
    {{Start new blinking process in new cog; [b]return[/b] [b]TRUE[/b] [b]if[/b] successful}}
    
    ' Stop
      'Success := (Cog := cognew(Blowswitch,stack) + 1)
      Success := (Cog := [b]cognew[/b](Blowswitch,[b]@[/b]stack) + 1)
    
    [b]PUB[/b] StartPIR : Success1
    {{Start new blinking process in new cog; [b]return[/b] [b]TRUE[/b] [b]if[/b] successful}}
    
    ' Stop
      'Success1 := (Cog := cognew(PIR,stack1) + 1)
      Success1 := (Cog := [b]cognew[/b](PIR,[b]@[/b]stack1) + 1)
    
    [b]PUB[/b] Stop
    {{Stop toggling process, [b]if[/b] any.}}
    
      [b]if[/b] Cog
        [b]cogstop[/b](Cog~ - 1)
    
    
    [b]PUB[/b] Active: YesNo
    {{[b]Return[/b] [b]TRUE[/b] [b]if[/b] process is active, [b]FALSE[/b] otherwise.}}
    
      YesNo := Cog > 0
    
    
    [b]PUB[/b] Blowswitch
    {{Toggle Pin, Count times with Delay clock cycles in between.}}
    
      [b]DIRA[/b][noparse][[/noparse]* 3..23 ] := %000000000000011111111 'Set I/O pin to output direction
      variable := 1
      [b]repeat[/b]
    
        w := [b]INA[/b]
    
        [b]if[/b] w == 1
          [b]OUTA[/b][noparse][[/noparse]* 16..19 ]~~ ' set I/O Pin (leds)
    
        [b]else[/b]
          [b]OUTA[/b][noparse][[/noparse]* 16..19 ]~ ' reset I/O Pin
    
    
    [b]PUB[/b] PIR
    
      [b]DIRA[/b][noparse][[/noparse]* 3..23 ] := %000000000000011111111 'Set I/O pin to output direction
    
      [b]repeat[/b]
      ' v := INA
    
        [b]if[/b] variable == 1
          [b]OUTA[/b][noparse][[/noparse]* 20..23 ]~~ ' Toggle I/O Pin
    
        [b]else[/b]
          [b]OUTA[/b][noparse][[/noparse]* 20..23 ]~ ' Toggle I/O Pin
    
    




  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-08 09:55
    Hello Svenson,

    if your posting from today 1:49 AM (GMT -7)
    Svenson said...

    It seems that only the blowswitch procedure is working. But when I call BlowSwitch and PIR using an object, the PIR procedure works.

    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

    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

      if (v == 1) [b]and[/b] (PIRVAR == 0) 
    
    



    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
  • svenssonsvensson Posts: 11
    edited 2009-10-08 11:17
    Hello,

    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)
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-08 11:28
    I modified your code to the basic things

    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

    [b]VAR[/b]
      [b]long[/b] PIR_Is_On
    
      [b]long[/b] StateOfBlowSwitch
      [b]long[/b] StateOfPIR
    
    
    [b]CON[/b]
      BlowSwitch_Input = 3
      PIR_Input        = 4
      On               = 1
      Off              = 0
    
      BSwFirst = 16
      BSwLast  = 19
    
      PIRFirst = 20
      PIRLast  = 23
    
    
    [b]PUB[/b] Main
      [b]DIRA[/b][noparse][[/noparse]*3..23] := %000000000000011111111                'Set I/O pin to output direction
    
      [b]repeat[/b]
        StateOfBlowSwitch := [b]INA[/b][noparse][[/noparse]*BlowSwitch_Input]
    
        [b]if[/b] StateOfBlowSwitch == On
            [b]OUTA[/b][noparse][[/noparse]*BSwFirst..BSwLast]~~            'post set switch LEDs ON
            PIR_Is_On := Off
        [b]else[/b]
            [b]OUTA[/b][noparse][[/noparse]*BSwFirst..BSwLast]~             'post clear switch LEDs OFF
            PIR_Is_On := On
    
    
        StateOfPIR := [b]INA[/b][noparse][[/noparse]*PIR_Input]
    
        [b]if[/b]   (StateOfPIR == On) [b]AND[/b] (PIR_Is_On == Off)
    
            [b]OUTA[/b][noparse][[/noparse]*PIRFirst..PIRLast]~~             'post set switch LEDs ON
        [b]else[/b]
            [b]OUTA[/b][noparse][[/noparse]*PIRFirst..PIRLast]~              'post clear switch LEDs OFF
    
    
    



    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
  • svenssonsvensson Posts: 11
    edited 2009-10-08 11:47
    Ok Stefan, I will explain.

    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
  • Clock LoopClock Loop Posts: 2,069
    edited 2009-10-08 12:34
    svensson said...
    Hello,

    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



    top object file

    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
    :
    togpir.StartBlowswitch(@vars)
    togpir.StartPIR(@vars)
    



    (@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)

     (BYTE [noparse][[/noparse]variable][noparse][[/noparse]0] == 1)
    
    
    if BYTE [noparse][[/noparse]variable][noparse][[/noparse]0] == 1
    
    



    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.


    
    VAR
    
    long Stack[noparse][[/noparse]100] 'Stack space for new cog
    long Stack1[noparse][[/noparse]100] 'Stack space for new cog
    byte Cog 'Hold ID of cog in use, if any
    long w
    long v
    
    
    
    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
    
    
    DAT 
    
    variable       long   0
    
    
    



    This should work now.
  • svenssonsvensson Posts: 11
    edited 2009-10-08 14:01
    OK it works !!!!!!!!

    Thanks all for the quick replies, this is a great forum!!!!!!!!!!!!!!

    Now I am able to pass a variable between cogs
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-08 15:11
    Yes this way you can communicate 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
Sign In or Register to comment.