View Full Version : Pass values from cogs to cogs
01-26-2008, 04:42 AM
I am running a Ping object in a new cog, that I want to write the resulting value which is updated every loop interation to a shared
ram location where another object executing in a loop in a different cog would access it.
How would I do this. The manual doesn't elude to technique.
I basically want to perform a similiar operation of writing a value to a shared value as done in the bs2p using the scratchpad ram.
01-26-2008, 04:54 AM
As long as your PING object and your other object are written in Spin, you can just store the PING values in any accessible variable and read them from any other cog just by referencing the variable.
If you've got the PING code and the other code in separate objects, the variables themselves are not known between objects. The easiest way to handle this is to put the shared variable in your main program's variable area and to pass the address of this variable (like @variable) to the PING object and your other object, then reference it using LONG[ addressOfVariable ].
Look at the description of the address operator (@) and the LONG / WORD / BYTE keywords in the manual.
01-26-2008, 05:01 AM
This article is also 'good' reading on the subject:
Code for the article can be found here:
01-26-2008, 07:43 AM
I tried what you said and so far..my cog starts but is not returning a confirmation that it did successfully.
Here is an excerpt
In main program I have:
Clock.Init(_XINFREQ) ' set up clock for accurate 10/second
Clock.SetClock(_CLKMODE) ' video update rate
BS2.Start(31,30) 'Initialize BS2 Object
ok := cog := cognew(ping.Millimeters(PING_Pin),@stackPing) + 1
waitcnt(clkfreq + cnt)
In the object referenced in under OBJ I have:
PUB Millimeters(Pin) : Distance
''Measure object distance in millimeters
stackPing := Distance := Ticks(Pin) * 10_000 / TO_CM ' Distance In Millimeters
waitcnt(clkfreq / 100 + cnt)
What am I doing wrong? I have a LONG stackping declared in each VAR section of both object files.
01-26-2008, 08:09 AM
When you declare LONG stackping in two different objects, you have two different variables even though they have the same name. This is not like C where you can declare global variables with the same name and have them be the same. You have to pass the address of the variable to the objects involved as I described.
There are lots of other problems with your program. For example, it's not well documented, but you can't have a COGNEW or COGINIT that starts a method in another object. It won't work. You can start a method in the same object as the COGNEW or COGINIT which only calls a method in another object. That works, but not what you've given (ie: cognew(ping.Millimeters ...)
You might have a look at BoeBotBasic which you can download from the Propeller Object Exchange. It's a large and complicated program, but there is a PING routine that runs in its own cog and there's not a lot of code that's involved. The main cog tells the PING cog to start a measurement by setting a variable to 0 or -1 (I don't remember). The PING cog finishes by setting the variable to the actual distance, then waits for the variable to be set to 0 or -1 again.
All of the code involved is in the main object (BoeBotBasic.spin).
01-26-2008, 08:28 AM
Thank you for your assistance and direction. I moved the methods from the PING object into the main object and everything is now working as expected.