Home        Store        Docs        Blog

Ping Sonar Python code

Hello all,

I’m fairly new to coding in Python and ran into an issue. I’m using this code: https://github.com/bluerobotics/ping-python/blob/master/examples/simplePingExample.py except instead of myPing.get_distance() I changed it to myPing.get_distance_simple(). The issue I am running into is that the sonar is giving the distance values every 4 seconds instead of instantaneously or every second. Do you know what I can do to resolve this issue?

Thank you for your help!

#!/usr/bin/env python

#simplePingExample.py
from brping import Ping1D
import time
import argparse

from builtins import input

##Parse Command line options
############################

parser = argparse.ArgumentParser(description="Ping python library example.")
parser.add_argument('--device', action="store", required=True, type=str, help="Ping device port.")
parser.add_argument('--baudrate', action="store", type=int, default=115200, help="Ping device baudrate.")
args = parser.parse_args()

#Make a new Ping
myPing = Ping1D(args.device, args.baudrate)
if myPing.initialize() is False:
    print("Failed to initialize Ping!")
    exit(1)

print("------------------------------------")
print("Starting Ping..")
print("Press CTRL+C to exit")
print("------------------------------------")

input("Press Enter to continue...")

# Read and print distance measurements with confidence
while True:
    data = myPing.get_distance_simple()
    if data:
        print("Distance: %s\tConfidence: %s%%" % (data["distance"], data["confidence"]))
    else:
        print("Failed to get distance data")

Hello,

What version of the python package are you using? We recommend to get the 0.0.7.

How are you testing it? What is the environment like?

Hello,

Thank you for your response. I double checked and it is 0.0.7 package. It is being tested by having it connected to the raspberry pi. We are currently testing in air, but the same results were received when tested in water. Any other suggestions?

Are you using a Bluart or the Raspberry PI hardware serial?

Are you using the default firmware communicating at 115200 baud?

You code runs fine here, but I don’t have a Pi Zero I could test it on. Can you check the cpu usage while it is running?

Hello again. I have done some tests here. I believe the source of the issue is the cpu usage of this example. Try adding a time.sleep(0.1) at the last line, like this:

#simplePingExample.py
from brping import Ping1D
import time
import argparse

from builtins import input

##Parse Command line options
############################

parser = argparse.ArgumentParser(description="Ping python library example.")
parser.add_argument('--device', action="store", required=True, type=str, help="Ping device port.")
parser.add_argument('--baudrate', action="store", type=int, default=115200, help="Ping device baudrate.")
args = parser.parse_args()

#Make a new Ping
myPing = Ping1D(args.device, args.baudrate)
if myPing.initialize() is False:
    print("Failed to initialize Ping!")
    exit(1)

print("------------------------------------")
print("Starting Ping..")
print("Press CTRL+C to exit")
print("------------------------------------")

input("Press Enter to continue...")

# Read and print distance measurements with confidence
while True:
    data = myPing.get_distance_simple()
    if data:
        print("Distance: %s\tConfidence: %s%%" % (data["distance"], data["confidence"]))
    else:
        print("Failed to get distance data")
    time.sleep(0.1)

Note that with this, the theoretical maximum frequency you will get will be 10 Hz. One would think that removing the sleep would make things faster, but that will increase cpu usage so much that it can get worse instead. You can decrease that number, but not remove it.