Shop OBEX P1 Docs P2 Docs Learn Events
Timer Obj crashes my program — Parallax Forums

Timer Obj crashes my program

SiCCSiCC Posts: 21
edited 2011-03-06 20:14 in Propeller 1
Trying to figure out why the TImer Obj would cause wierd issues in my program. My program uses getStr to receive a message from my GPS every 100ms, then continues to execute the Main loop below. The PUB LapTimer below captures IR and manipulates the TIMER. I have narrowed it down to:

When I call getStr from FullDuplexSerial and any ref to the Timer Obj. I can use either successfully, but when I try to have them both working at the same time my program goes whacky.

Example, I can comment out the getStr line (first line of main loop) OR I can comment out the references to TIM in the LapTimer function. But if both are included the program dies.

Any help is appreciated!

CON

  _clkmode = xtal1 + pll16x
 '_xinfreq = 5_000_000
  _clkfreq = 80_000_000

  cmd_length        = 256 

  GPS_BAUD          = 57600

  GPS_RX            = 1
  GPS_TX            = 0

  clockDataPin      = 29
  clockClockPin     = 28
  
  cardDataOutPin    = 12
  cardClockPin      = 13
  cardDataInPin     = 14
  cardChipSelectPin = 15             

  BRAKE_POT         = 26
  THROTTLE_POT      = 27

  angleCS           = 16
  angleDIO          = 17
  angleCLK          = 18

  _irpin            = 24 
  _ics              = 6300       
  _icd              = 1300


    
VAR  

'-----Program Direction

  long funct        'what are we doing?

'-----GPS Variables

  long x            'Counter
  long y            'GPS Bytes
  byte z[10]

'-----New Session Number     

  long fileNum      'Current log file

'-----Bike Control Measurements

  long potPos1      'POT Position
  long potPos2
  long potPin       'Current POT

  long pot1low
  long pot1high
  long pot2low
  long pot2high

'-----Lean Angle Sensor

  long Pitch
  long Roll

'-----Blinky Status LED

  Byte statLED

'-----User Shutdown

  Byte shutDown     'Detect user off

'-----Lap Timer

  long   ibuff      'index buff
  long   buff[60]   'store sample times
        
  long   stack[60]  'for sample running in cog

  long   lap        'check timing
        
  byte   irmode
  byte   beacon
  byte   valid
  byte   first
  long   clock
   
  
OBJ

  PST    : "Parallax Serial Terminal"
  GPS    : "FullDuplexSerialPlus"
  POT    : "BlakePOT3"
  FAT    : "SD2.0_FATEngine.spin"
  NUM    : "Numbers"
  TIM    : "Timer" 




PUB Main

  if PST.Start(115200)
     pst.str(string("PST Started!"))
     pst.char($0D)

  if GPS.start(GPS_RX, GPS_TX, 0, GPS_BAUD) 'startup GPS serial
     pst.str(string("GPS Started!"))
     pst.char($0D)
  else
      pst.str(string("GPS Failed!"))
     pst.char($0D)
      
  if FAT.FATEngineStart(cardDataOutPin, cardClockPin, cardDataInPin, cardChipSelectPin, clockDataPin, clockClockPin)
     pst.str(string("FAT Started!"))
     pst.char($0D)
  else
     pst.str(string("FAT Failed!"))
     pst.char($0D)
  
  NUM.init
  pst.str(string("NUM Started!"))
  pst.char($0D)
  

  cognew(LapTimer,@stack[1000])   
  pst.str(string("LAP Started!"))
  pst.char($0D)

  if TIM.start
     pst.str(string("TIM Started!"))
     pst.char($0D)
  else
     pst.str(string("TIM Failed!"))
     pst.char($0D)

  pst.char($0D)

  

  funct       := 5

  statLED     := 0

  shutDown    := 0

  

  repeat

    if funct == 5

        pst.str(string("Begin Data Logging!"))
        pst.char($0D)
        pst.char($0D)

        waitcnt(clkfreq + cnt)        

        repeat while shutDown <> 1

              statLED++
        
              GPS.getstr(Str12)

              PST.str(Str12)
              FAT.writestring(Str12) 
          
              FAT.writestring(string(","))
              pst.char(",")

              POT.Main(BRAKE_POT,@potPos1) 
              FAT.writestring(NUM.tostr(potPos1, num#dec))
              pst.dec(potPos1)

              FAT.writestring(string(","))
              pst.char(",")
 
              FAT.writestring(NUM.tostr(pot1low, num#dec))
              pst.dec(pot1low)

              FAT.writestring(string(","))
              pst.char(",")

              FAT.writestring(NUM.tostr(pot2high, num#dec))
              pst.dec(pot1high)

              FAT.writestring(string(","))
              pst.char(",")
             
              POT.Main(THROTTLE_POT,@potPos1) 
              FAT.writestring(NUM.tostr(potPos1, num#dec))
              pst.dec(potPos1)

              FAT.writestring(string(","))
              pst.char(",")

              FAT.writestring(NUM.tostr(pot2low, num#dec))
              pst.dec(pot2low)                        

              FAT.writestring(string(","))
              pst.char(",")

              FAT.writestring(NUM.tostr(pot2high, num#dec))
              pst.dec(pot2high)  

              FAT.writestring(string(","))
              pst.char(",")


              if lap == 0
                  pst.str(clock)

              if lap == 1
                  FAT.writestring(clock)          
                  pst.str(clock)
                  lap := 0
                  clock := string("---")                  


              FAT.writebyte($0D)
              PST.char($0D)

              if ina[23] == 1
                 shutDown := 1

              if statLED == 5
                 if dira[25] == 1
                    dira[25] := outa[25] := 0
                 else
                    dira[25] := outa[25] := 1

                 statLED := 0
                 

       pst.char($0D)
       pst.char($0D)
       pst.char($0D)       
       pst.str(string("Stop Indicated!"))
       pst.char($0D)

       FAT.closefile

       waitcnt(clkfreq / 2 + cnt)

       FAT.unmountPartition

       waitcnt(clkfreq / 2 + cnt)

       pst.str(string("Ready To Power Off!"))

       dira[25] := outa[25] := 0

       funct := 99


PUB LapTimer | kbd, n, i, mark, space

  waitcnt(clkfreq * 5 + cnt)

  clock  := string("---")
  irmode := 0
  first  := 0
  lap    := 0


 repeat
  
  if irmode == 0

    waitcnt(clkfreq * 5 + cnt)
  
    repeat 30
      waitpeq(0,|<_irpin,0)          'wait for any ir on 
      buff[ibuff++] := cnt
      waitpeq(|<_irpin, |<_irpin, 0) 'wait for space
      buff[ibuff++] := cnt

    irmode := 1


  if irmode == 1

    beacon := 1
    valid := 0
    i := 2

    repeat i from 2 to ibuff step 2
    
        space:=(buff[i  ]-buff[i-1])/80    'space used

        if beacon == 3
          if space < (_icd + 100)
            if space > (_icd - 100)
               beacon := 1
               valid++

        if beacon == 2
          if space < (_icd + 100)
            if space > (_icd - 100)
               beacon := 3
            else
               beacon := 1
             
        if beacon == 1
          if space < (_ics + 100)
            if space > (_ics - 100)
               beacon := 2
            else
               beacon := 1
             
        if valid == 3
          if first == 1

             clock := TIM.ShowTimer 'string("lap")
             TIM.reset 
             TIM.run 
             lap := 1

          if first == 0
             TIM.reset
             TIM.run
             first++

             
          irmode := 0
          ibuff~
          n~
          i~
          
          quit

    irmode := 0
    ibuff~
    n~
    i~


DAT

Str12 word "1", 0

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-03-06 20:14
    VAR  
      long   stack[[COLOR="red"]60[/COLOR]]  'for sample running in cog
    
    PUB Main
      ...
      cognew(LapTimer,@stack[[COLOR="red"]1000[/COLOR]])   
      ...
    
    Does that look odd to you? I suspect memory corruption, then again you may have (additionally) run out of cogs.

    Also, your usage of Str12 is questionable. You want to refer to it as @Str12. With that out of the way what is the expected length of the string returned by the getStr method call (you only have two words reserved, i.e. 4 bytes)?
Sign In or Register to comment.