Variables between objects
Danico
Posts: 10
Have searched the manual, forum and wiki, but just cannot see how to pass data between objects and cogs.· Here is an example of what I would like to do;
·
One object running continually·in one cog·evaluating the ambient light level, then once per second storing this level as a variable say "LightLevel" as a number between 1-20.
Another object running in another cog, will once every 10 seconds grab the current value, and send it out serially.· The issue is with this object being able to access 'LightLevel'.
·
Thought that there must be a way to have a global variable that each object could access.··In my searching, I found reference in the forum·to storing this variable·in·'hub ram', but could not find in the manual how to do this (or specific reference to hub ram).·
·
The aim is that both objects run independently, no passing of data between.
·
I guess this is a basic premise of multiple processors, so want to be sure I can get it right.
Any guidance would be greatly appreciated.
·
D
·
One object running continually·in one cog·evaluating the ambient light level, then once per second storing this level as a variable say "LightLevel" as a number between 1-20.
Another object running in another cog, will once every 10 seconds grab the current value, and send it out serially.· The issue is with this object being able to access 'LightLevel'.
·
Thought that there must be a way to have a global variable that each object could access.··In my searching, I found reference in the forum·to storing this variable·in·'hub ram', but could not find in the manual how to do this (or specific reference to hub ram).·
·
The aim is that both objects run independently, no passing of data between.
·
I guess this is a basic premise of multiple processors, so want to be sure I can get it right.
Any guidance would be greatly appreciated.
·
D
Comments
Variables are local for an instance of the object. So, if you use one object twice, each will have it's own copy of the variables.
Data stored in dat section is global for all instances of the object. So, if you use one object twice they work on the same data.
But neither of these myValues will be visible in other code. So, the SPIN2 has no direct access to these variables. If you need access to one variable in more than one objects, you need a piece of central code which creates the variable, pass the adress of this variable to the object and then run the code in a COG.
In dirty programming this is of course much easier ;o)
http://forums.parallax.com/forums/default.aspx?f=25&m=318611&g=318699#m318699
Good luck.
This code (although pointless) works fine;
So the variable 'GlobalVariable' is available at all times.
What I would like to do is have another object with the 'Meter' routines in it, kind of like this;
·The the meter object;
But of course the variable GlobalVariable is not accessable by the Meter object file.
This may all look like a strange idea, but the application I am thinking will involve a couple of large free running objects each·in their own cog, with one overarching object and cog picking relevant data from each·object as it needs it.· Sure enough this could be done in one long·object file, but it would be difficult to read and manage.· To have them seperate should make it maintainable.
I hope I haven't confused the matter further!!
Any ideas?
D
as you already realized there is no direct access to variables ACROSS objects.
You can make a big file still maintainable by self defined naming conventions and how you place
the methods in a well defined order
first all methods dealing with "task A"
then all methods dealing with "task b"
etc. where task means things like serial connection, read in sensor values, drive a display etc.
all in alphabetical order
and every method does ONE thing wich is explained by a selfexplaining name of the method itself
(instead of adding comments all the time) once written copy and paste the longer PUB-Name is faster than copy and paste a short PUB-Name AND a comment
Now there are different possabilities:
As long as ONE object starts 1,2...6 cogs from the SAME object (=same *.SPIN-file)
all variables of THAT object are accessible from EVERY cog.
If you want to divide your code into multiple *.SPIN-files and still want to have access to the variables
ACROSS the SPIN-files(=objects) one easy way is to define "SET" and "GET"-methods in the object that holds the variable
this piece of code shows the basic principle.
If methods running in different cogs and if two methods call one and the same of the SET-Methods
it can happend that the value gets overwritten before important things are done
To avoid this you would have to program a lock-mechanism by using the lockxxx-commands
Here is no general advice possible.
Some times it is OK to just wait for the variable to become status "unlocked"
in other cases it is better to have a timeout while waiting
sometimes just to NOT change the value
This depends on your application
best regards
Stefan
Post Edited (StefanL38) : 5/5/2009 10:49:44 AM GMT
b, w and l can give you a hint what type the variables are. That's what I meant with dirty programming in my first post. But this way all Objects which load MemMap have access to all variables there. Hopefully the compiler is clever enough not to create much object overhead for the constant only object.
Need to try the suggestions, and see what works·best -·I suspect one long file, well formatted, would be the easiest solution.
D.