Shop OBEX P1 Docs P2 Docs Learn Events
Ping module question — Parallax Forums

Ping module question

James ArmstrongJames Armstrong Posts: 3
edited 2006-12-12 04:55 in Propeller 1
Is there a way to modify the ping code to be able to start multiple ping sensor units at the same time and wait for all the responses and get the measured time before returning? I need to be able to monitor 4 or 5 sensors but don't want to use one cog per sensor or call one function per sensor in series because of the delays that would be added up. If the max wait time is ~50ms (max distance) and I am reading 5 of these in succession that would be about 250ms per ping cycle not allowing for more than 4 reads per second. I thought I saw a function somewhere that would start a timer that would measure a pulse on an input or multiple inputs, and wait for all of them to change before returning. I can't remember where I say that code.

- James

Comments

  • Chad GeorgeChad George Posts: 138
    edited 2006-12-11 18:02
    James,

    I've been working on a robot project that uses the SRF05 sonar unit which is very similar to the PING unit. I had the same problem when I wanted to drive multiple sonars from the same cog. I wrote a new assembly driver that will monitor multiple sonar units simultaneously. It is currently written to drive 3 sonars although the code should scale up to 5 easily. I've also included·SPIN code that demonstrates using the driver.

    One word of·caution, I'm using the DLP-PROP units which run at 96MHz instead of the 80Mhz that the demo board runs at. It should be obvious where to make the appropriate changes.

    Hope it helps,

    Chad

    Post Edited (Chad George) : 12/11/2006 6:06:11 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2006-12-11 18:15
    ' Assuming you have 4 pings starting at pin 4 with pulldowns (say 10K) to ground:
    VAR
      long startTime, elapsed, started, pingCount
    
    PUB timeIt | x
    ' Start 4 pings going and record the pulse width for each in elapsed[noparse][[/noparse]x].
    ' Units for pulse width are system clocks (normally 12.5ns).
      repeat x from 0 to 3
        started[noparse][[/noparse]x] := false
      pingCount := 4
      outa[noparse][[/noparse]4..7] := %0000
      dira[noparse][[/noparse]4..7] := %1111 ' make outputs
      outa[noparse][[/noparse]4..7] := %1111 ' start pulse
      waitcnt(clkfreq / 100_000 + cnt) ' 10us pulse
      dira[noparse][[/noparse]4..7] := %0000 ' change to inputs
      waitcnt(clkfreq / 2_000 + cnt) ' 500us hold off
      repeat while pingCount    ' wait until all pings respond
        repeat x from 0 to 3
          if started[noparse][[/noparse]x]    ' this ping's response pulse has started
            if ina[noparse][[/noparse]4+x] == 0    ' the pulse is now back to zero
              elapsed[noparse][[/noparse]x] := cnt - startTime[noparse][[/noparse]x]    ' record the elapsed time
              pingCount--    ' one less ping to wait for
          else
            if ina[noparse][[/noparse]4+x] == 1   ' not yet started, pulse now high
              startTime[noparse][[/noparse]x] := cnt   ' save the start time
              started[noparse][[/noparse]x] := true   ' mark pulse as started
    
    
  • acantostegaacantostega Posts: 105
    edited 2006-12-12 04:04
    You probably already know this, but just in case, make sure the pings are positioned so the echoes of the respective pings don't interfere with each other.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2006-12-12 04:21
    Mike, won't all the elapsed times equal cnt for last sonor - their individual start times? I don't see anything to prevent multiple assignments to elapsed[noparse][[/noparse]x].
  • Mike GreenMike Green Posts: 23,101
    edited 2006-12-12 04:55
    John,

    You're absolutely right.· Easiest way to fix this is to initialize the elapsed array to zero in the loop where started is initialized, then test for·"ina[noparse][[/noparse]4+x] == 0 and elapsed[noparse][[/noparse]x] == 0".· Sorry

    Mike
Sign In or Register to comment.