Shop OBEX P1 Docs P2 Docs Learn Events
Trouble with a PING Ultra Sonic Distance sensor — Parallax Forums

Trouble with a PING Ultra Sonic Distance sensor

CopperCopper Posts: 48
edited 2012-09-20 13:00 in Propeller 1
Preface: I have no Idea what I'm doing.

Now that we got that out of the way...
I picked up one of parallax's PING sensors just to fool around with while my other projects stew a little, but immediatly ran into trouble (inter-cog communication failure is my guess).

First I wrote a simple bit of code to use the driver provided here
which worked, so I know my PING sensor is functional and properly connected

Then I decided to try and integrate the PING sensor into a silly little synth object I had been fussing with earlier. Originally it used two Potentiometers to control the frequency, now I'm trying to use the PING sensor (for absolutely no reason other than to go through the exercise).

Unfortunately I've broken the PING sensor method such that it only returns one value (00000000000000000000010110010000), and I cannot for the life of me figure out what's wrong. I would very much appreciate whatever theories you may have, so that I can avoid creating the same problem in the future.
CON

  _CLKMODE = XTAL1 + PLL16X
  _XINFREQ = 5_000_000


  Ping_Pin = 15
  
OBJ


  pst : "Parallax Serial Terminal"
  
VAR


  long ping_stack[30]
  long debug_stack[30]
  
DAT


  time        long 0
  distance    long 0
  frequency   long 0
  x           long 0
  ping_cog    byte 0
  debug_cog   byte 0


PUB Main


  debug_cog:=cognew(debug, @debug_stack[0])
  ping_cog:= cognew(ticks(Ping_Pin), @ping_stack[0])
   
  repeat
    x|=(distance)
    


    frequency:=x
    'Synth("A", 4, frequency)               'Synth({Counter"A" or Counter"B"},Pin, Freq)
      


        
PUB Synth(CTR_AB, Pin, Freq) | s, d, ctr, frq


  Freq := Freq #> 0 <# 128_000_000     'limit frequency range
  
  if Freq < 500_000                    'if 0 to 499_999 Hz,
    ctr := constant(100 << 26)      'set NCO mode ( desired mode 100 shifted 26bits left into ctr-mode register [30..26] )
    s := 1                             '..shift = 1
  else                                 'if 500_000 to 128_000_000 Hz,
    ctr := constant(010 << 26)      '..set PLL mode
    d := >|((Freq - 1) / 1_000_000)    'determine PLLDIV
    s := 4 - d                         'determine shift
    ctr |= d << 23                     'set PLLDIV
    
  frq := frqfraction(Freq, CLKFREQ, s)    'Compute FRQA/FRQB value
  ctr |= Pin                           'set PINA to complete CTRA/CTRB value


  if CTR_AB == "A"
     CTRA := ctr                        'set CTRA
     FRQA := frq                        'set FRQA                   
     DIRA[Pin]~~                        'make pin output
     
  if CTR_AB == "B"
     CTRB := ctr                        'set CTRB
     FRQB := frq                        'set FRQB                   
     DIRA[Pin]~~                        'make pin output


PUB frqfraction(a, b, shift) : f


  if shift > 0                         'if shift, pre-shift a or b left
    a <<= shift                        'to maintain significant bits while 
  if shift < 0                         'insuring proper result
    b <<= -shift
 
  repeat 32                            'perform long division of a/b
    f <<= 1
    if a => b
      a -= b
      f++           
    a <<= 1


PUB Ticks(Pin) | cnt1, cnt2 
''Return Ping)))'s one-way ultrasonic travel time in microseconds 


  repeat
    outa[Pin]~                                                  ' clear I/O Pin 
    dira[Pin]~~                                                 ' make Pin Output 
    outa[Pin]~~                                                 ' set I/O Pin 
    outa[Pin]~                                                  ' clear I/O Pin (> 2 microsecond pulse) 
    dira[Pin]~                                                  ' make I/O Pin Input 
    waitpne(0, |< Pin, 0)                                       ' wait For Pin To Go HIGH 
    cnt1 := cnt                                                 ' store Current Counter Value 
    waitpeq(0, |< Pin, 0)                                       ' wait For Pin To Go LOW  
    cnt2 := cnt                                                 ' store New Counter Value 
    time := ((||(cnt1 - cnt2) / (clkfreq / 1_000_000)) >> 1)    ' store Time in microseconds
    distance := (time * 10_000) / 29_034                        ' store distance in (millimeters)
    
PUB Debug


  pst.Start(57600)
  waitcnt(clkfreq+cnt)


  repeat
    pst.str(string(13))
    pst.bin(distance, 32)
    waitcnt(clkfreq/4+cnt)

as always, thank you very much for your time.
Sign In or Register to comment.