Python to access the Ping raw data on Raspberry PI

I would like to try and access the the raw Ping data in Python on the Raspberry PI.

I am looking for (I assume) an array of { x,y, value }. How is that best achieved? I see there are some likely functions in the device API guide but wanted to confirm.

Thanks in advance.

1 Like

Hi Andrew,

Take a look in our python repository, there is also an example available to get only the distance and confidence from the sensor, but if you want to do more stuff, take a look in the python doxygen documentation.

1 Like

Thanks Patrick

I have the basic distance measurements sorted.

Should I be looking at the get_range and get-profile functions to get the raw data?

Hi Andrew,

What is your idea of raw data ?
I would recommend to read the documentation for both functions and see the best one for you.

1 Like

Hi Patrick

Initially I would like to see if I can replicate Ping Viewer at the source (ROV) rather than the client PC. Beside less versions of the application software to support for me it could provide a more seamless and consistent user experience. You could also have the ability to integrate with all other available sensor data at source. See:

(The mobile phone is the lowest common denominator. Besides being convenient, it ensures suitable performance is built in. If it works there, it will work on larger devices and tablets)

Moving forward as you look towards autonomous operation, automatic obstacle avoidance, and potentially a stitched array of ping devices providing a wider arc of view, I believe most of the smart processing will occur real-time at the ROV end which is where I am thinking I should focus some time?

I am also thinking about clustering some Raspberry PI devices in the 4" enclosure to provide dedicated compute resource if needed
:grinning:

I was hoping to get a slightly longer introduction to the those two functions as I think the doco is a bit brief on background explanation and that appears to be where most of the core data collection occurs?

Thanks in advance.

That sounds cool. You are probably after profile data.

See an explanation of the device operation here. You will use the get_profile function to obtain the data.

Docs are still actively WIP, let us know if you have some specific question or suggestion.

1 Like

Hi @jwalser

I have the following sample code returning an error with the get_profile function:

#!/usr/bin/env python

#echoprofile.py
from brping import Ping1D

#Make a new Ping
myPing = Ping1D("/dev/ttyUSB0" , 115200)
if myPing.initialize() is False:
    print("Failed to initialize Ping!")
    exit(1)

# set speed of sound in water
myPing.set_speed_of_sound(1450000)

# Read some sample Ping data successfully
distancedata = myPing.get_distance()
print (distancedata["distance"])
print (myPing.get_device_id())
print (myPing.get_firmware_version())
print (myPing.get_general_info())
print (myPing.get_pcb_temperature())
print (myPing.get_processor_temperature())

# Read some sample Ping Profile data unsuccessfully
profiledata = myPing.get_profile() 

The above errors with the message: AttributeError: ‘PingMessage’ object has no attribute ‘payload_field_names’

I see that get_profile() is returning an array within an array? Do I need to declare a special data type such as pingmessage.PING1D_PROFILE to remove the error?

Also just an FYI, there are some broken links on this page

@deepsouth We are looking into the issue and tracking it here: error unpacking payload: 'PingMessage' object has no attribute 'payload_field_names' · Issue #55 · bluerobotics/ping-python · GitHub

@deepsouth which links are not working?

A couple of places I found:

The three links here:
http://docs.bluerobotics.com/ping-viewer/hotkeys-and-shortcuts/#mouse-shortcuts

The link to device configuration here:
http://docs.bluerobotics.com/ping-viewer/#application-and-device-settings

The issue has been resolved in v0.0.7, try pip install to upgrade.

I’ve also fixed the broken links in the documentation.

Thanks Jacob

I saw you had made the updates and I refreshed my code last night. The bug is gone!!!

It would be really helpful if you could illustrate the extraction of data from ‘profile-data’ variable say to JSON?

It would be really helpful if you could illustrate the extraction of data from ‘profile-data’ variable say to JSON?

It’s as simple as print(data)

Here is an example program:

#!/usr/bin/env python

from brping import Ping1D
import argparse

##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)

data = myPing.get_profile()
print("dictionary data looks like this:", data)
print("access distance:", data["distance"])

And the output:

$ python ex.py --device=/dev/ttyUSB0 --baudrate=9600
Opening /dev/ttyUSB0 at 9600 bps
('dictionary data looks like this:', {'distance': 13917, 'confidence': 0, 'scan_start': 0, 'profile_data': "\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd'0\x08\x00\x01\x01\x08\x01\x00\x01\x00\x02\x04\x03\x06\x08\x10\x03\x00\x01\x00\x00\x00\x00\x03\x01\x01\x01\x03\n\x03\x03\x01\x00\x03\x08\x03\x03\x00\x00\x00\x02\x02\x05\x00\x00\x00\x00\x01\x05\x01\x04\x04\x06\x06\x00\x00\x00\x00\x01\x00\x02\x02\x02\x02\x00\x00\x02\x03\x00\x00\x00\x00\x00\x03\x04\x03\x00\x00\x00\x00\x03\x02\x05\x04\x04\x04\x00\x01\x00\x00\x00\x00\x00\x00\x03\x03\x03\x01\x00\x00\x00\x00\x00\x00\x00\x03\x05\x04\x02\x00\x00\x01\x02\x0b\x0c\x04\x07\r+?5.\x16\t\x02\x07\x01\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x01\x00\x00\x00\x00\x04\x02\x03\x05\x00\x00\x01\x01\x00\x00\x03\x02\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00.\xfd8\x00", 'scan_length': 20750, 'gain_index': 6, 'ping_number': 570, 'pulse_duration': 556})
('access distance:', 13917)

Thanks Jacob

I think data is a given and easy to access. I am looking to access the array the raw measurements?

You can see it in the output. It is in data["profile_data"].
'profile_data': "\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd'0\x08\x00\x01\x01\x08\x01\x00\x01\x00\x02\x04\x03\x06\x08\x10\x03\x00\x01\x00\x00\x00\x00\x03\x01\x01\x01\x03\n\x03\x03\x01\x00\x03\x08\x03\x03\x00\x00\x00\x02\x02\x05\x00\x00\x00\x00\x01\x05\x01\x04\x04\x06\x06\x00\x00\x00\x00\x01\x00\x02\x02\x02\x02\x00\x00\x02\x03\x00\x00\x00\x00\x00\x03\x04\x03\x00\x00\x00\x00\x03\x02\x05\x04\x04\x04\x00\x01\x00\x00\x00\x00\x00\x00\x03\x03\x03\x01\x00\x00\x00\x00\x00\x00\x00\x03\x05\x04\x02\x00\x00\x01\x02\x0b\x0c\x04\x07\r+?5.\x16\t\x02\x07\x01\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x01\x00\x00\x00\x00\x04\x02\x03\x05\x00\x00\x01\x01\x00\x00\x03\x02\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00.\xfd8\x00"


and how did you get that specific output?

By running the program in this post.

Hi Everyone

I am new using the ping sonar, I follow the instructions from the link: GitHub - bluerobotics/ping-python: Python scripts and examples for the Ping sonar.. However I was not able to run simplePingExample.py. Actually, I included in the bash profile the path that is suggested in the link above. However I am getting the following error

Traceback (most recent call last):
File “simplePingExample.py”, line 5, in
from brping import Ping1D
File “build/bdist.linux-x86_64/egg/brping/init.py”, line 2, in
ImportError: No module named definitions

I am also new in python, checking online I figure out that probably python is not recognizing import Ping1D

Any suggestion, I would appreciate your help. Thanks in advance

Hi Adrian,

did you follow our instructions ?
You should install it via pip and from that use the quick start guide.
I just tested it in a fresh OS (ubuntu 18.04) and it’s working.
image

1 Like

Hi Patrick

I could manage to run the example, however to use the USB in Linux 16.04, I had to install the version 0.0.8 or lower. So Definitely, the latest version can not installed in Linux 16.04. And I had to include the following line to use the USB port

sudo chmod 666 /dev/ttyUSB*

Thank you so much for your help. It really worked!

Adrian