Help Please. Variables with multiple cogs and objects
archerbrad700
Posts: 18
I read that a variable defined in the VAR block can be seen by all the methods of that object, but what about in other Cogs running other objects?
What I have is a method in one object that polls the user for input and saves the input to a variable (say desiredX) in another object I have a method that looks for that variable (desiredX) to change and when it does it executes a subroutine. The problem is that the subroutine never gets executed. I imagine it is because one object/cog can't see what is going on in another.
So the question is how do I get the second cog to see that the variable has changed in the first cog?
Thanks for your help,
-Brad
What I have is a method in one object that polls the user for input and saves the input to a variable (say desiredX) in another object I have a method that looks for that variable (desiredX) to change and when it does it executes a subroutine. The problem is that the subroutine never gets executed. I imagine it is because one object/cog can't see what is going on in another.
So the question is how do I get the second cog to see that the variable has changed in the first cog?
Thanks for your help,
-Brad
Comments
If we're talking about PASM there is an extra step of getting the COG data to HUB memory.
It helps if you post your code.
I am coding in Spin and here is my code:
Other object:
In a nutshell... Ask user for X and Y points then run the Resolve subroutine until there is no more disparity between desiredX and Xcount and desiredY and YCount
I don't see how I can store a variable in the DAT block, maybe someone could show me?
Thanks
Your going to need have the parent object pass a pointer, to the variable, to the child object.
Look for information in the manual about the "@" operator.
The child method would then read the variable with word[addressOfWord].
I don't see an advantage to having the variable in the DAT section unless muliple instances of an object are used.
Locks are sometimes needed. It depends on the situation.
I'll look around for an example of passing a pointer and post one if someone else doesn't bet me to it.
Duane
I haven't found a clear example yet.
When the parent starts the child use:
Modify the child object to:
Hopefully that's enough to get you started.
Look up "@" and word to see what the code above is doing.
Duane
Thank you, an example would be great!
If the Read and Write processes were encapsulated in an object, then you would pass the address (pass by reference) of the command variable, @command, to the object's Start method. In this example, you would also pass the ID of the memory lock. Pass by value should be fine. Hope that helps.
I still have two questions though:
1) I followed Duane's example and it is working except something is happening to the values. The code works perfectly in the single object, but when I use another cog to store the values are getting changed somehow.
Resolve Object:
2) I'm still not sure how Mike's code applies to this, but I'm all for an explanation... It perhaps is what's causing my problem?
Thanks again
Sounds like you need a memory lock.
The code simply spins up two process in separate COGs; 1 and 2. Each process has access to the variable created in the top level object. You could, move the Read and Write process to separate files, reference the objects, and start the objects in the top level object. Pass the command variable by reference; @command, to the referenced objects (as you did) along with the semaphore. In the child objects, make sure memory access is not checked out by another process. If memory is available check out the memory lock, Read/Write, release the lock.
The Propeller manual has an excellent explanation of memory locks.