Home        Store        Docs        Blog

Python to access the Ping raw data on Raspberry PI


(Andrew) #1

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.


(Patrick José Pereira) #2

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.


(Andrew) #3

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?


(Patrick José Pereira) #4

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.


(Andrew) #5

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.


(Jacob) #6

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.


(Andrew) #7

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


(Jacob) #8

@deepsouth We are looking into the issue and tracking it here: https://github.com/bluerobotics/ping-python/issues/55


(Jacob) #9

@deepsouth which links are not working?


(Andrew) #10

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


(Jacob) #11

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

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


(Andrew) #12

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?


(Jacob) #13

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)

(Andrew) #14

Thanks Jacob

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


(Jacob) #15

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"


(Andrew) #16

…and how did you get that specific output?


(Jacob) #17

By running the program in this post.