Home        Store        Learn        Blog

Ping 360 get device data


I have been trying to use python to access the data from the ping 360.
This is the code I am using to access the data but the byte array is always empty

for x in range(400):
d1 = p.get_device_data()

not sure what the issue is. Any help would be appreciated. This is what the output looks like always

{‘mode’: 1, ‘gain_setting’: 0, ‘angle’: 20, ‘transmit_duration’: 32, ‘sample_period’: 80, ‘transmit_frequency’: 800, ‘number_of_samples’: 200, ‘data’: bytearray(b’’)}


Hi @keshavr,

Receiving Data

The transmitAngle method asks the Ping360 to transmit a ping at the specified angle, using the last used transducer settings (transmit frequency, number of samples, etc), and then waits for and returns the collected data. Your code then calls get_device_data, which doesn’t receive any new data because no new ping has been sent since the last data was collected.

Your code isn’t formatted so I’m not sure if you’re intending to get and print the data from each transmit or if you’re just wanting the last one. Assuming you want to the data for each angle you instead want to do

# import Ping360 class
from brping import Ping360

# Create Ping360 instance
p = Ping360()

... # Connect to, initialize, and set up Ping360 settings

# Loop through a full circle, one gradian at a time
for x in range(400):
    response = p.transmitAngle(x)

Processing Data

If you’re wanting to process the data it might be worth using numpy to turn it into an array that you can do vectorised operations on.

import numpy as np
from brping import Ping360

... # create and initialise Ping360 object

response = p.transmitAngle(x)
data = np.frombuffer(response.data, dtype=np.uint8)
print(data.min(), data.max())
# print all locations that are above threshold
threshold = 200
print(np.where(data >= threshold))

It’s also possible to do processing on the bytearray object directly, but if you’re doing anything much more complicated than a min or max you’ll likely benefit from numpy’s convenience and speed.

On a normal desktop or laptop computer you can install numpy through pip (e.g. pip3 install numpy), but if you’re using the companion computer you’ll need to do

sudo apt install libatlas3-base
sudo pip3 install numpy==1.15.4

which installs the libatlas library, which numpy on Raspberry Pi depends upon, and specifies numpy version 1.15.4, which is the last one supported by Python 3.4 (which is what companion currently uses/has access to). Note that both of those installs take quite a while because they need to compile stuff (unfortunately didn’t think to time it when I installed them earlier - it’s at least less than an hour but not sure if it’s less than half an hour).

1 Like

I see how the get data method works. Thanks a bunch for the help.

1 Like

A quick follow up question. When I do this. I keep getting parse error as my output but I can see a clear image when I use the ping viewer. Is there a particular reason this is happening?

Try upgrading the library to the latest version (python3 -m pip install --upgrade bluerobotics-ping - add sudo in front if on RPi) - there was a checksum calculation overflow error that was fixed very recently that was seemingly causing 1-2% of messages to parse incorrectly (at least by the super rough+quick test I just ran).

1 Like