Shop OBEX P1 Docs P2 Docs Learn Events
Determining Oncoming Object Has Stopped — Parallax Forums

Determining Oncoming Object Has Stopped

Rizz1Rizz1 Posts: 3
edited 2009-06-24 15:42 in Accessories
I have a Basic Stamp II and have·a Ping Ultrasonic range finder on order.· I have a project where I want to monitor the decreasing distance to an oncoming object and fire a solenoid valve when the object "stops" (at least when the distance change is minimal).· I am new to programming but assume I can have the Stamp continue comparing·distance values from the Ping until the difference is virtually zero.· Once this condition occurs, an output signal can be sent to fire the solenoid.·

Is this a workable application for my Stamp and Ping?· If so, is there a reference code I can access and adapt?

Thanks in advance for any help.

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-06-23 20:48
    I don't think there is any readily available code to do what you want, but it shouldn't be too difficult. You'll need a loop and two variables (old and new measurements) and you can break out of the loop when the two measurements are within x units of each other and less than the maximum. The within x units part is because the ping won't give a single reading for a stationary object: the reading will bounce around some within a more or less certain amount. The less than the maximum part is becuase if there's no object then it will get two "identical" readings, which based on the previous condition would indicate the need to fire a solenoid.

    If you haven't done so already, take a look at the ping sample code on it's product page, and the books "What's a Microcontroller" and "Basic Stamp Syntax and Reference Manual" available as PDFs from Parallax.
  • Rizz1Rizz1 Posts: 3
    edited 2009-06-23 21:04
    Thanks SRLM. I have the book "What's a Microcontroller". I have done some digging but haven't found (or understood" how to do the comparison of successive distance readings. I assume I would continue redefining variavle values with the latest distance reading. I just checked the anticiated delivery of my Ping and it is supposed to be here Saturday. I will continue trying to develop a code as I anxiously await the delivery so I can try it out. I didn't find anything in completed projects that I thought was applicable. If anyone wants to contribute a sample code string to try, I would appreciate it. Thanks!
  • SRLMSRLM Posts: 5,045
    edited 2009-06-23 23:06
    You'd use something like this (pseudo-code):
    //new, old are word sized variables
    //OFFSET is a constant that determines the minimum amount of motion acceptable when "stopped"
    //MAXIMUM is a threshold beyond which an object cannot be stopped
    new = old = MAXIMUM
    while(true) 
       new = ping.get_distance()
       if(new > old-OFFSET && new < old+OFFSET && new < MAXIMUM) then break
       old = new
    
    solenoid.fire()
    
    
  • edited 2009-06-24 00:40
    Velocity is the change in distance divided by the change in time.· If the change in time is constant between each measurement, (for example, loop checks the distance every 50 ms)·the program can just check to find out if the change in distance is above some threshold.· Here is an example.· Note that the current measurement gets copied to a variable that stores the previous measurement before each repetition of the loop.

    ' {$STAMP BS2}                      ' BASIC Stamp 2
    ' {$PBASIC 2.5}
     
    ping      PIN 15                    ' Ping))) I/O pinping
     
    threshold CON 45                    ' threshold value
     
    ticks     VAR Word                  ' current distance measurement
    ticksOld  VAR Word                  ' previous distance measurement
    
     
    PULSOUT ping, 5                     ' Initialize previous distance
    PULSIN  ping, 1, ticksOld
     
    DO                                  ' Main loop
     
      PULSOUT ping, 5                   ' Get current distance
      PULSIN  ping, 1, ticks
     
      ' If difference in distances is below threshold then...
      IF ABS(ticks - ticksOld) <= threshold THEN
        DEBUG HOME, "slow enough"
      ELSE                              ' Otherwise...
        DEBUG HOME, "too fast   "
      ENDIF
     
      ticksOld = ticks                  ' Save current as previouis
      PAUSE 50                          ' Wait fixed amount of time
     
    LOOP                                ' repeat main loop
    

    For more fun with the Ping))) sensor, download Smart Sensors and Applications from www.parallax.com -> Downloads -> Stamps in Class·Downloads.· See Chapter 2.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • Rizz1Rizz1 Posts: 3
    edited 2009-06-24 12:03
    Thanks fellow Parallaxers! I am anxiously awaiting the delivery of my Ping))) to try it out.
  • SRLMSRLM Posts: 5,045
    edited 2009-06-24 15:42
    Rizz1 said...
    Thanks fellow Parallaxers!

    According to their newsletter, I believe it's "Parallaxians"... [noparse]:)[/noparse]
Sign In or Register to comment.