Shop OBEX P1 Docs P2 Docs Learn Events
Code for using Ping as 0 1 High Low: Ultrasonic BS2 — Parallax Forums

Code for using Ping as 0 1 High Low: Ultrasonic BS2

SpazmSiSpazmSi Posts: 2
edited 2009-11-24 02:59 in Accessories
We are firing a ping pong ball through a series of vertical targets. I want to use the PING sensor as a simple 0 or 1, high or low edge finder. The idea being to rotate our bot until the edge of our target is found, then continue until the single is lost, thus finding the beginning of the hole cut in out target. I am really just hoping to find a basic code sample that I can begin to modify. We are not using any type of roaming function and the sensor if not mounted on the servo turret.

Thanks

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-11-22 21:38
    I'm not sure the ping))) has the resolution to find the edge of an object. It would depend on the distance and the size of the hole. If you can you might consider the cmucam or a webcam and roborealm.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • SRLMSRLM Posts: 5,045
    edited 2009-11-22 22:29
    The code below is in spin, but it can be ported easily enough. Basically, it records the values seen during a calibration period, and calculates the average between the two extremes seen. Each side is then either a 0 or a 1, with a small dead band in the middle where the value could be either.

    Code to calibrate:
    
      'Get the values for the line
      repeat until ina == 1
        repeat i from 0 to rcount
          line[noparse][[/noparse] i] := adc.in(i)
          if(linemin[noparse][[/noparse] i] > line[noparse][[/noparse] i] or linemin[noparse][[/noparse] i] == 0)
            linemin[noparse][[/noparse] i] := line[noparse][[/noparse] i]
          if(linemax[noparse][[/noparse] i] < line[noparse][[/noparse] i])
            linemax[noparse][[/noparse] i] := line[noparse][[/noparse] i]
            
      'com.str(string("Calculating Line Thresholds"))
      'com.tx(13) 
      repeat i from 0 to rcount
        t1 := (linemin[noparse][[/noparse] i] + linemax[noparse][[/noparse] i])/2
        t2 := 0'||(linemin[noparse][[/noparse] i] - linemax[noparse][[/noparse] i])/64
        linemin[noparse][[/noparse] i] := t1-t2
        linemax[noparse][[/noparse] i] := t1+t2
    
    



    Code to execute when you get a reading:
    repeat i from rcount to 0
          line[noparse][[/noparse] i] := adc.in(i)
          if(line[noparse][[/noparse] i] > linemax[noparse][[/noparse] i])
            binout[noparse][[/noparse] i] := 1
          if(line[noparse][[/noparse] i] < linemin[noparse][[/noparse] i])
            binout[noparse][[/noparse] i] := 0
          image := image + (binout[noparse][[/noparse] i] << i)
    
    



    I used arrays since I have eight sensors that I want to get the input of (from an ADC). You would only need single variables. BTW, the code is for a line following robot.

    Post Edited (SRLM) : 11/22/2009 10:37:24 PM GMT
  • tronsnavytronsnavy Posts: 70
    edited 2009-11-22 23:48
    Here is a project that you might be able to manipulate.· It is written in BS2.· Good luck.
    http://forums.parallax.com/showthread.php?p=590119
  • SpazmSiSpazmSi Posts: 2
    edited 2009-11-24 01:57
    The holes are fairly large, 8in diameter, but they are a good distance from the boebot. I am not too worried about the resolution or accuracy, I just need to get with an inch of the edge and our ping pong cannon should fire through the targets. Thanks for the info folks, i will start trying to adapt it to what I need.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-11-24 02:59
    How far is "a good distance"?. It does matter.

    -Phil
Sign In or Register to comment.