Speed measurement with ultrasonic sensor Parallax PING))) (Python)

This tutorial guides you through calculating a relative velocity by using the distance measurement of the Parallax PING))) sensor over the time.

In order to be able to measure a speed at all, we must first be able to measure a distance to an object. Ultimately, the ultrasonic sensor can do no more. Therefore, first read Distance measurement with ultrasonic sensor Parallax PING))) (Python).

Calculating the speed

Let's take another closer look at the triangle from the previous chapter:

https://lastminuteengineers.b-cdn.net/wp-content/uploads/arduino/Distance-Speed-Time-Formula-Triangle.png

So we need

Speed = Distance / Time

However, we do not want to go to the speed of sound now and check whether there is a deviation here.

I will show you how to measure the speed of movement of an object using this sensor. For that purpose we need to take two distance measurements in a short time apart and we have:

distance2 - distance1 = distance speed at a given time

If we make the measurements in a time period of 1 second, then we get the speed of movement of the object in cm/s.

When the object is moving in the opposite direction, the speed represented on the display has a negative sign.

Programming the speed measurement

We change our code from the previous example as follows:

# import libraries
import RPi.GPIO as GPIO
import time
 
# GPIO Modus (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
# assign GPIO Pins
GPIO_SIG = 17
 
def distance():
    GPIO.setup(GPIO_SIG, GPIO_OUT)
    GPIO.output(GPIO_SIG, 0)

    time.sleep(0.000002)

    #send trigger signal
    GPIO.output(GPIO_SIG, 1)

    time.sleep(0.000005)

    GPIO.output(GPIO_SIG, 0)

    GPIO.setup(GPIO_SIG, GPIO.IN)

    while GPIO.input(GPIO_SIG) == 0:
        starttime = time.time()

    while GPIO.input(GPIO_SIG) == 1:
        endtime = time.time()
      
    duration = endtime - starttime
    # Distance is defined as time/2 (there and back) * speed of sound 34000 cm/s 
    distance = (duration*34000)/2
 
    return distance

def speed():
    # calls the distance() function above
    distance1 = distance()

    # giving a time gap of 1 sec
    time.sleep(1)

    # calls the distance() function above a second time
    distance2 = distance()

    # formula change in distance divided by change in time
    # as the time gap is 1 sec we divide it by 1.
    speed = (distance2 - distance1)/1.0

    return speed

if __name__ == '__main__':
    try:
        while True:
            speed = speed()
            print ("Measured speed = %.1f cm" % speed)
            time.sleep(1)
 
        # When canceling with CTRL+C, resetting
    except KeyboardInterrupt:
        print("Measurement stopped by user")
        GPIO.cleanup()

Now that we know what we can measure with the Parallax PING))) and how, we can write our driver and wrap it in ROS in the next chapter. Please read for this Writing your Parallax PING))) driver (Python).

Wiki: Drivers/Tutorials/SpeedMeasurementWithUltrasonicSensorParallaxPINGPython (last edited 2022-11-14 12:37:05 by Michdo93)