Accessing variables from multiple cogs
T Chap
Posts: 4,223
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:
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
Then in the Motor cog:
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.
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
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.
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
with this democode you should get an output to your terminalsoftware similar to this
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
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.
Is this wise or cowardly?
Nick
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 ...