Shop OBEX P1 Docs P2 Docs Learn Events
Cognew and object methods — Parallax Forums

Cognew and object methods

hkimballhkimball Posts: 15
edited 2007-03-30 04:05 in Propeller 1
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.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-30 03:24
    The short answer is YES.

    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).
  • hkimballhkimball Posts: 15
    edited 2007-03-30 03:39
    Thanks Mike,
    · 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?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-30 03:50
    Your pseudocode looks fine. If you want to move the temperature sensor stuff or the range sensor stuff to a separate cog, the COGNEW needs to be moved too. Usually an object that starts its own cog has a start and stop method for this purpose. Look at some of the objects in the Propeller Tool's library or in the Propeller Object Exchange for examples of this.

    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.
  • hkimballhkimball Posts: 15
    edited 2007-03-30 04:05
    Thanks Mike, I understand exactly what you are saying and I will check out that code.
Sign In or Register to comment.