Shop OBEX P1 Docs P2 Docs Learn Events
Passing variables between cogs - Need Help Please — Parallax Forums

Passing variables between cogs - Need Help Please

WookieWookie Posts: 27
edited 2010-01-05 19:24 in Propeller 1
Resident Spin Experts....

I am trying to pass one variable generated in one cog to another and am having trouble compiling and running my code. In short, I have one object that reads in and displays GPS data, while the other reads several "one wire" temperature sensors. I want to pass the temperature variable from the one-wire section of code/cog to the GPS read and display cog. If I run both on the same cog, I get too much of a delay in the output speed (I need 10Hz speed readings from my 10Hz GPS).

I have looked over the forum, the user guide, and the Propeller Spin Code examples book, but I find nothing wrong. If anyone could please look over my code and comment, I would appreciate it. I am sure it is something easy I have missed, but I have spent way too much time on this already and am completely frustrated.

Thanks for your help in advance!!!

Joe



CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

  MAX_DEVICES = 4
  
OBJ
  debug   : "pc_text"
  ow      : "SpinOneWire"
  f       : "FloatMath"
  fp      : "FloatString"
  gps     : "GPS_IO_mini2"
  
VAR
  long addrs[noparse][[/noparse]2 * MAX_DEVICES]
  Long Stack0[noparse][[/noparse]20]
  Long Stack1[noparse][[/noparse]20]   







PUB start 

'cognew(readtemperature(@addr), @wire)

CogNew (GPSread, @Stack0)
CogNew (OnewireData, @Stack1)



 

PUB GPSread | i, numDevices, addr, main, gmt

  gps.start
  debug.start(12) 

  repeat
     
      
      debug.str((string("Latitude")))
      debug.str(gps.latitude)  
      debug.str((string(",Longitude")))
      debug.str(gps.longitude)
      debug.str((string(",GPS_Altitude")))
      debug.str(gps.GPSaltitude)      
      debug.str((string(",GPS_Speed")))
      debug.str(gps.speed)     
      debug.str((string(",Time GMT")))                                                                                                                                                                                                                                                                                                                                                    
      debug.str(gps.time)      
      debug.str((string(",Date")))
      debug.str(gps.date)
      debug.str((string(",Temp ")))
      fp.SetPrecision(4)
      'debug.str(fp.FloatToString(readtemperature.degF))
      debug.str(degF)



PUB OnewireData | i, numDevices, addr


  ow.start(24)

  repeat
    numDevices := ow.search(ow#REQUIRE_CRC, MAX_DEVICES, @addrs)

    'debug.str(string($01, "── SpinOneWire Test ──", 13, 13, "Devices:"))

    repeat i from 0 to MAX_DEVICES-1
      'debug.out(13)
    
      if i => numDevices
        ' No device: Blank line
        repeat 39
          'debug.out(" ")

      else
        addr := @addrs + (i << 3)

        ' Display the 64-bit address        

        'debug.hex(LONG[noparse][[/noparse]addr + 4], 8)
        'debug.hex(LONG[noparse][[/noparse]addr], 8)
        'debug.str(string("  "))

        if BYTE[noparse][[/noparse]addr] == ow#FAMILY_DS18B20
          ' It's a DS18B20 temperature sensor. Read it.
          readTemperature(addr)

        'else
          ' Nothing else to show...
          'debug.str(string(9,9))

PUB readTemperature(addr) | temp, degC, degF
  '' Read the temperature from a DS18B20 sensor, and display it.
  '' Blocks while the conversion is taking place.

  ow.reset
  ow.writeByte(ow#MATCH_ROM)
  ow.writeAddress(addr)

  ow.writeByte(ow#CONVERT_T)

  ' Wait for the conversion
  repeat
    waitcnt(clkfreq/100 + cnt)

    if ow.readBits(1)
      ' Have a reading! Read it from the scratchpad.

      ow.reset
      ow.writeByte(ow#MATCH_ROM)
      ow.writeAddress(addr)

      ow.writeByte(ow#READ_SCRATCHPAD)
      temp := ow.readBits(16)

      ' Convert from fixed point to floating point
      degC := f.FDiv(f.FFloat(temp), 16.0)

      ' Convert celsius to fahrenheit
      degF := f.FAdd(f.FMul(degC, 1.8), 32.0)

      'fp.SetPrecision(4)
      'debug.str(fp.FloatToString(degF))
      'debug.str(string("°F    "))

      return


Comments

  • WookieWookie Posts: 27
    edited 2010-01-05 19:08
    BTW, it might be helpful to explain the error I am getting.

    When I compile, I get "Expected an expresssion term" highlighting the degF variable in the GPSRead section of code. That variable is generated in the readtemperature section of code at the bottom of the page.

    Thanks!
  • Mike GreenMike Green Posts: 23,101
    edited 2010-01-05 19:24
    "degF" is a local variable in "readTemperature". It doesn't exist outside "readTemperature" and you can't reference it anywhere else. That's why you got an error message. If you want to reference it from elsewhere, you need to declare it as a global variable in the VAR section near the beginning of your program. You also need to remove its definition from the "readTemperature" declaration.

    By the way, why does your "start" method just call COGNEW a couple of times, then quit? That's your main program. It can call other methods and normally organizes the whole shebang.

    Post Edited (Mike Green) : 1/5/2010 7:29:59 PM GMT
Sign In or Register to comment.