Shop OBEX P1 Docs P2 Docs Learn Events
A question about cogs — Parallax Forums

A question about cogs

SailerManSailerMan Posts: 337
edited 2006-10-07 02:56 in Propeller 1
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.

·
Con
        _clkmode        = xtal1 + pll16x
        _xinfreq        = 5_000_000
Var     
        Long Stack[noparse][[/noparse]25]
        Long Cog    
         
Pub Start(Pin)
  Cog:=Cognew(Read(Pin),@Stack)
         
Pub Read(Pin):R
  Ctra := 0  
  Ctra := (%11010 << 26 ) | (%001 << 23) | (0 << 9) | (PIN)
  Repeat
    DirA[noparse][[/noparse]PIN]~
    frqa := 1 
    waitpne(1 << pin, |< Pin, 0)                         
    phsa:=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.
Eric


·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-07 02:56
    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.
Sign In or Register to comment.