View Full Version : A question about cogs

10-07-2006, 09:31 AM
I want a cog to do nothing but take Sonar Readings.

I read the sonar with a Pulsin command from the BS2 Library.

Anyhow... In my Object I have a "Start" method that just starts the cog with a user defined pin number. Then I have a public "Read" method that is a loop that does nothing but take readings.

The "Read" method doesn't return anything because it is in a loop constantly taking readings.

What simple thing am I missing here

Here is the Code for the Sonar Object.


_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
Long Stack[25]
Long Cog

Pub Start(Pin)

Pub Read(Pin):R
Ctra := 0
Ctra := (%11010 << 26 ) | (%001 << 23) | (0 << 9) | (PIN)
frqa := 1
waitpne(1 << pin, |< Pin, 0)
waitpeq(1 << pin, |< Pin, 0)
waitpne(1 << pin, |< Pin, 0)
R:=(PHSA/ (clkfreq / 1_000_000) + 1)/147 ' Calculate Inches from the Pulse Width

My question is how to get·the "Result" from The Pub Read back to the·main program?

Hope this makes sense.


Mike Green
10-07-2006, 09:56 AM
All communication from a cog back to the main program has to be done in HUB memory using VARs. There have been several threads here over the last week or two on this subject. Basically, you have to store the result into a variable declared in the VAR section. Some other cog can read the most recent value from the same variable. If you need for your main program to know when there's been a new value posted, the easiest thing to do is to have your main program set the shared variable to some illegal or impossible value. When the Read cog sets it to a valid value, the main program can tell that a valid value has been posted. When the main program is done with the value, it sets it to something illegal or invalid again. There are other ways, but these two are the simplest.