Shop OBEX P1 Docs P2 Docs Learn Events
Accessing variables from multiple cogs — Parallax Forums

Accessing variables from multiple cogs

T ChapT Chap Posts: 4,223
edited 2008-10-07 21:36 in Propeller 1
I have been trying to figure out a method to update variables from one cog so another can use the data.

There is a method that has worked before, so that a motor driver can access the position from the rotary encoder obj, here is an example of that process:



PUB EncoderStart    'start a new cog with encoder 
     encoder.start(21, 1, 0, @pos)




So that Long[noparse][[/noparse]pos] would access the value from another cog.

I having a lot of trouble understanding how to use this between several cogs thoug in similar fashion where I need to update the variable within one cog and read it in another.

For example, I want to start a PWM method in anew cog, that PWM method will need to access a continuously changing variable from a motor controller cog.

I tried something like this in the PWMSpin cog



PUB PWMspin
      'code for pwm that uses a Var called OnTime, using all combinations, ie, long[noparse][[/noparse]ontime], long[noparse][[/noparse]@ontime], ontime, etc to read.




Then in the Motor cog:

PUB Motor
       Long[noparse][[/noparse]OnTime] := X_value   'also  Long[noparse][[/noparse]@OnTime]  etc






I am doing these new tests without using objects, all cogs are launching off the same page.

There are cases where things seem to work, and others where it doesn't. The problem is lack of a real understanding of how to do this.

Any suggestions to solve this would be appreciated.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-03 00:45
    If OnTime is a global variable (in the VAR section of the same object), you don't need the LONG[noparse][[/noparse] ]. You can just write OnTime := X_value or reference it as OnTime.

    The important thing is that only one cog can change the variable (there are some exceptions, but this is an easy rule this way). If you need something more complicated, you may be able to communicate both ways (from one cog to another with one variable and from the 2nd cog to the 1st with a second variable). If that's not enough, you may need to use a shared group of variables and the LOCKxxx statements to ensure that only one cog accesses the variables at a time.
  • T ChapT Chap Posts: 4,223
    edited 2008-10-03 03:34
    Thanks Mike. After a lot of experimenting, I can see that just because I may try to affect a var in one cog, the other cog has it's own set of var's running, and even though the names may be the same, they are unique. Much simpler just to create a object, and put an updater method within it to call from anywhere else, send it parameters that you want changed.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-10-03 05:27
    hello originator,

    in your previous post you wrote
    code for pwm that uses a Var called OnTime, using all combinations, ie, long[noparse][[/noparse]ontime], long[noparse][[/noparse]@ontime], ontime, etc to read.

    this would be really strange to use OnTime in all these combinations

    let's assume variable OnTime is stored at RAM-adress 123
    let's assume the CONTENT of variable OnTime is 567

    long[noparse][[/noparse]OnTime]=long[noparse][[/noparse]567] will give back the value of RAM-Adress 567 (which is NOT the ADRESS of variable OnTime

    long[noparse][[/noparse]@OnTime]=long[noparse][[/noparse]123] will give back the value of variable OnTime the same way as using "OnTime" itself

    For further support I have some questions:
    - did you understand the DIFFERENCE of long[noparse][[/noparse]OnTime] to long[noparse][[/noparse]@OnTime] ?
    - did you wrote this code yourself or did you pick it up from somebody else ?
    - could you attach this code to a posting ?

    here is a code-example showing acess to a variable from different cogs within the same *.SPIN-File
    you simple use the NAME of the variable itself

    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
      
    VAR
      long heart_stack[noparse][[/noparse] 20]
      long CogStack1[noparse][[/noparse] 20]
      long CogStack2[noparse][[/noparse] 20]
      
      long MyTestVar
    
    OBJ
      'heart : "heartbeat" little object from the obex making an LED blink  
      debug : "FullDuplexSerial"
    
    
    PUB Main
    'the FIRST PUB-Method inside a spinfile is ALWAYS the startpoint where the program starts to run
      'heart.Start(27, 200)
      debug.start(31, 30, 0, 9600)
      
      MyTestVar := 100
      debug.str(string("Start MyTestVar="))
      debug.dec(MyTestVar)
      debug.Tx(13)
    
      cognew(M1,@CogStack1)
      cognew(M2,@CogStack2)
      
      repeat
        waitcnt(clkfreq + cnt)
        debug.str(string("MyTestVar="))
        debug.dec(MyTestVar)
        debug.Tx(13)
    
    
    
    PUB M1
      repeat
        waitcnt(ClkFreq * 3 + cnt)
        MyTestVar := 1
    
    
    PUB M2
      repeat
        waitcnt(ClkFreq * 5 + cnt)
        MyTestVar := 2
    
    
    



    with this democode you should get an output to your terminalsoftware similar to this

    Start MyTestVar=100
    MyTestVar=100
    MyTestVar=100
    MyTestVar=1
    MyTestVar=1
    MyTestVar=2
    MyTestVar=1
    MyTestVar=1
    MyTestVar=1
    MyTestVar=1
    MyTestVar=2
    MyTestVar=2
    MyTestVar=1
    MyTestVar=1
    MyTestVar=1
    MyTestVar=2
    MyTestVar=2
    MyTestVar=2
    MyTestVar=1
    MyTestVar=1
    MyTestVar=2
    MyTestVar=1
    MyTestVar=1
    MyTestVar=1
    MyTestVar=1
    MyTestVar=2
    MyTestVar=2
    
    



    As you can see from this demo
    as long as you want to use a variable from the SAME *.SPIN-file
    you just use the NAME of the variable

    It is the same as long as you want to access the variable in the SAME object
    (which is defined in another *.SPIN-File)
    therefore the variable has to be defined INSIDE that object (that *.SPIN-file)

    to get access to variables ACROSS OBJECTS the variable is defined in one object
    and the OTHER objects can access this variable through the RAM-Adress of this variable
    you get the RAM-Adress of a variable with the "@"-operator



    best regards

    Stefan

    Post Edited (StefanL38) : 10/3/2008 6:18:31 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-03 05:34
    Originator,
    It all depends on what you're trying to do with the extra cogs and how complex the code is. In BoeBotBasic, there are separate cogs started in the main program to handle the PING, the 3 servos, and an assembly routine to do the arctangent via Cordic transformation. All of these are simple and integrated well with the main BoeBotBasic code.
  • nohabnohab Posts: 96
    edited 2008-10-03 09:48
    I keep one cog as a Main cog, from this I start the other needed cogs and there I also declare the variables needed to be shared between cogs.
    Is this wise or cowardly?


    Nick
  • T ChapT Chap Posts: 4,223
    edited 2008-10-03 17:33
    Thanks for that additional info Stephan and Mike Very helpful indeed. Sometimes it is not easy to master everything when there are too many hats to wear, so the forum is a God send.
  • KeezinatorKeezinator Posts: 21
    edited 2008-10-07 21:36
    I have been wondering: Why would a DAT entry not be possible?

    My theory: A DAT block is only loaded once, so all cogs and objects would share it.


    I have not tested this, but if it is not dismissed, I will ...
Sign In or Register to comment.