Cognew and object methods
hkimball
Posts: 15
I have a question about cognew and object methods.· Say you have an object for a temperature sensor and the object has several pub methods for returning data/values.· If the object is started using a cognew does the calling/main spin code have access to those methods.· That is can my main app make calls to the object's methods to find out say temp, or id of the device?· Is there any special calling sequence?
Thanks in advance.
Thanks in advance.
Comments
You really have to separate the idea of objects from the idea of parallel execution using more than one cog. They do go together like a pair of gloves, but are very different. An object groups together a set of data with a set of methods to manipulate the data. The data itself is only accessible within the object unless you explicitly make the data available using get/put methods. This notion works on any kind of processor, single, double, or 8-way like the Propeller. COGNEW (and COGINIT) start an "execution thread" where a method is called using a different processor than the one initiating the call. That's all. All of the methods involved and the COGNEW can all be within a single object. There's no reason why you can't have 6 or 8 cogs running off a single Spin method in a single program (main object).
Anyway, if you have an object for a temperature sensor with several pub methods and you declare this object in a "higher level" object, you can call the pub methods in that object using any thread started by a COGNEW/COGINIT. You can get into trouble if two threads try to change the same variables using the get/put calls. That's what LOCKxxx is for (to prevent that).
· just want to make sure i have this right so here is some pseudo code to see if i understand properly
pseudocode below
pub main
· cognew(tempsensor....··· // launch temperature sensor
· cognew(rangesensor....·· // launch range sensor
· repeat
··· gettemp···· // where this is a method in tempsensor that returns the current temperature
··· getrange···· // where this is a method in rangesensor that returns the current range
··· process data
so i would have three cogs running and the main app would be accessing the data through the pub methods of the other objects?
BOE-BOT Basic (here: http://forums.parallax.com/showthread.php?p=635533) uses a similar structure to handle servos and a PING sensor. The routines are near the end. There's no gettemp or getrange since the PING data is made available in a ready to use form in a global variable and the servo driver just reads the positioning information from a global array variable.