Shop OBEX P1 Docs P2 Docs Learn Events
passing variable between 2 objects, 2 cogs — Parallax Forums

passing variable between 2 objects, 2 cogs

I'm trying to learn how to pass variables from an object in a second cog back to an object in the original cog. The parent object (z_temp) displays a measurement from the child object(z_scratch). There is an example in the manual i have tried to copy, but the example seems to be 2 methods from the same object. I've tried declaring the variable in the DAT section as x long 0 I found in a thread that solved someones else's similar problem. I've tried other things as well and not sure how to proceed. Also the code gets stuck in the child object after the cognew call (from the parent object) unless there is a RETURN dis at the end of the repeat loop(in the child object), nothing after the cognew will print in the serial terminal without this, and with the RETURN dis nothing correct prints either.
  '/////////////////parent object////////////////
CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000
VAR
  long stack[200]
  long x
obj
  pst : "Parallax Serial Terminal"
  cheap_ping  : "z_scratch"                         
PUB display 
  pst.start(115200)
 
  pst.str(string("here"))                              'here ect. for debug...
  pst.str(string("after here"))
  
  cognew(cheap_ping.ticks(@x),@stack)    'start ticks method of z_scratch object into new cog
                                                                'point to distance measurement storage, global variable x
  pst.str(string("now here")) 
 
 repeat  

  pst.str(string("DISTANCE  "))
  pst.dec(x)                                               'print distance measurement made in other method/cog
  pst.str(string(13))
   waitcnt(clkfreq/2+cnt)
'//////////////////child object/////////////////
CON

  TO_IN = 73_746                                     ' Inches
  TO_CM = 29_034                                   ' Centimeters
  trig = 12
  echo = 13                                                                               
                                                                        
PUB ticks(dis) | Microseconds , cnt1, cnt2, distance
  
repeat                                                                                 
  outa[trig]~                                                                    ' Clear I/O Pin
  dira[trig]~~                                                                   ' Make Pin Output  
  outa[trig]~~                                                                  ' Set I/O Pin
  outa[trig]~
  outa[echo]~                                                                  ' Clear I/O Pin (> 2 µs pulse)
  dira[echo]~
                                                                                      ' Make I/O Pin Input
  waitpne(0, |< echo, 0)                                                  ' Wait For Pin To Go HIGH
  cnt1 := cnt                                                                   ' Store Current Counter Value
  waitpeq(0, |< echo, 0)                                                  ' Wait For Pin To Go LOW 
  cnt2 := cnt                                                                   ' Store New Counter Value
  Microseconds := (||(cnt1 - cnt2) / (clkfreq / 1_000_000)) >> 1                ' Return Time in µs
  distance :=Microseconds  * 1_000 / TO_IN                    
  dis:=distance                           '/////////return distance to parent object variable x through parameter dis//////////////  

Comments

  • JonnyMacJonnyMac Posts: 8,912
    edited 2019-04-24 04:08
    If you've passed the address of a long you can use it in the child cog like this:
      long[dis] := distance
    
    I tend to preface my pointer variables with "p_" so I would have called that variable (in the child cog) p_distance.

    On another note, the post bit set and clear operators are not as fast as direct assignments:
      outa[trig] := 0
      dira[trig] := 1
      outa[trig] := 1
      outa[trig] := 0
      outa[echo] := 0
      dira[echo] := 0
    

    Another possible improvement is using a counter in positive detect mode for your pulse measurement. Clear the phsx register, wait for the pin to go high, then back low, then capture the pulse width in the phsx register -- this eliminates some of the math and instruction overhead.
  • mikea wrote: »
    I There is an example in the manual i have tried to copy, but the example seems to be 2 methods from the same object.

    For launching Spin methods the COGNEW must be in the same object as the method. That is, doing
       cognew(otherobj.method, @stackspace)
    
    will not work as expected, and worse will actually try to do something completely different. There's a discussion in the Propeller Manual under COGNEW, titled "Spin Code Can Only be Launched by its Containing Object".

  • Does a child object somehow inherit the clock setting from a parent object? From the library it looks like a lot of drivers don't declare clockmode. I changed dis:=distance to long[dis]:=distance...i still have a problem getting any numbers to display. I'll try again tonight. Thanks Jon and for the extra info, good to know.
  • Cluso99Cluso99 Posts: 18,066
    The clock setting is global and only the top object actually sets the clock even tho a number of sub-objects incorrectly declare clock values, but they shouldn’t.

    When the binary runs, the clock mode is set and the clock frequency is written into hub address $0 which is called clockfreq. When a routine references clockfreq it uses the value in hub $0.

  • I think your indention is wrong.

    your repeat below pst.str(string("now here")) needs to be in the same column as pst.str(string("now here"))

    everything below that repeat has to be indented farther right .

    Enjoy

    Mike
Sign In or Register to comment.