Home        Store        Docs        Blog

Ping-Protocol C library

Hello .I have a problem . I need more information about Ping-Protocol .You can send a simple example to connect PING-SONAR-R2-RP .but I need a C Library .You have a C++ Library in your web site .But I need a C library .because I used AVR microcontroller with AVR studio .

For a example a need to request a distance_simple …How to ma ke a format message to send a sonar sensor :slight_smile:
Byte 0 :“B” 0x42
Byte 1 : “R” 0x52
Byte 2-3 0x0005
Byte 4-5 0x1211
Byte 6 0x00
Byte 7 0x00
Byte 8-12 ?
Byte 13 check sum

And the sensor return ? . I need to know format .

Hi,

You should use the general_request message, check the documentation and description here.
To create this message, you just need to follow the format description.

  • The message ID of general_request is 6, so this value should be used in message_id field.
  • Usually the master [parent], your AVR, will use the ID 0 and the slave [worker], in that case Ping, will boot with ID 1, you can also use 0 as dst_device_id to do a broadcast. This values should be used in src_device_id and dst_device_id
    respectively.
  • Since the payload of general_request is two bytes, you should use this value in payload_length, in your case 0x04BB for distance_simple (ID 1211).
Byte Value Type Name Description
0 ‘B’ u8 start1 ‘B’
1 ‘R’ u8 start2 ‘R’
2-3 0x0002 u16 payload_length Number of bytes in payload.
4-5 0x0006 u16 message_id The message id.
6 0x00 u8 src_device_id The device ID of the device sending the message.
7 0x00 u8 dst_device_id The device ID of the intended recipient of the message.
8-9 0x04BB u8[2] payload The message payload.
10-11 0x015B u16 checksum The message checksum. The checksum is calculated as the sum of all the non-checksum bytes in the message.

With the created package, you should send the bytes in little-endian format:
0x42, 0x52, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00, 0xBB, 0x04, 0x5B, 0x01

The return from the sensor will be:
0x42, 0x52, 0x05, 0x00, 0xBB, 0x04, 0x00, 0x00, 0x5B, 0x1D, 0x00, 0x00, 0x64, 0x34, 0x02
And you can unpack the information using the same logic behind the pack method.

  • The message distance_simple returns a message with a payload of 5 bytes, where the first 4 is the distance and the last one the confidence, you can check that in the documentation here.
Byte Value Type Name Description
0 ‘B’ u8 start1 ‘B’
1 ‘R’ u8 start2 ‘R’
2-3 0x0005 u16 payload_length Number of bytes in payload.
4-5 0x04BB u16 message_id The message id.
6 0x00 u8 src_device_id The device ID of the device sending the message.
7 0x00 u8 dst_device_id The device ID of the intended recipient of the message.
8-11 0x00001D5B u32 distance Distance to the target.
12 0x64 u8 confidence Confidence in the distance measurement.
13-14 0x0234 u16 checksum The message checksum. The checksum is calculated as the sum of all the non-checksum bytes in the message.

The conversion of distance and confidence from hexadecimal to decimal is 7515mm (7.515M) and 100% respectively.
You can also check the checksum to be sure that the message is not corrupted, and the message_id to check if the message is the one that you are waiting for.

Note: You should use C++ and not C, my avr-gcc version is 9.1.0 and supports C++17.

Thanks Very Much .Right Now I am working about sonar to detect fishes .but I need some help you .I will contact you again.

Helllo .I am working in a echo sonar to detect fishes.I need to known the distance from sensor to fishes .I going to use The message profile (Message id:1300) . This Message return array profile_data . What is the profile data ? Can I detect fishes or another objects from the profile_data message ?

Hi,

The answer is in the documentation of this specific message, check the profile_data description:

An array of return strength measurements taken at regular intervals across the scan region.

About the fish detection, probably it’s possible, but it may be necessary to tune the gain and disable the auto operation mode. Some tests need to be done to be sure.

1 Like

Hello .I have a question . 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"

profile_data is int (u8) ?

Hi,

u8 stands for uint8_t or unsigned int in 8 bits, the values are inside the range of 0 to 255, where 255 is the max power or 100% in profile data.

Hello.I have a question about message profile (1300) . The python library code do not return profile_data_length (The length of the proceeding vector field) . I need to know that minimal distance(mm) between profile_data [0] ,profile_data [1],profile_data[2] …,profile_data[n]. because .I am goint to graph profile_data vs time to detect objects .

Hi Edgar,

Can you provide a minimum example ?
You should be able to use profile_data_length to get the size of the array, that is fixed in 200 points.

Ok . I understand it .but I want to know the distance in mm between each point .for example .I have request a profile and return :

dictionary data looks like this: {‘distance’: 843, ‘confidence’: 50, ‘pulse_duration’: 113, ‘ping_number’: 1310, ‘scan_start’: 0, ‘scan_length’: 1000, ‘gain_index’: 3, ‘profile_data’: b’\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xb8\x1c8\x81\xbe\xe4\xbeh&\x10\x0c\x13\x15\n\x05\x06\n\x0c\x12\x17\x10\x0c\x07\x04\x05\x06\x03\x02\x00\x00\x01\x00\x00\x03\x05\x04\x04\x01\x01\x02\x04\x04\x03\x00\x00\x00\x00\x00\x00\x00\x01\x02\x02\x02\x03\x03\x01\x00\x00\x05\x06\x05\x01\x02\x00\x00\x01\x00\x01\x01\x00\x01\x0b\t\x03\x02\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x02\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xbd\x7f0;l\x91\xfd\xc4c\xaa\x89\xf6\xa5\x00\x00\x00\x00*\xa2\x0e\x94\xc5_\x00\x00\x00\x00’}

distance is 843 mm , the first profile_data[0] have the same distance and next profile_data[1] ? .I need to know distance between each point .

Hi Edgar,

Check the profile message documentation.
You can use scan_start and scan_length to calculate the “position” of each point in the profile_data array.

Type Name Description Units
u32 scan_start The beginning of the scan region in mm from the transducer. mm
u32 scan_length The length of the scan region. mm

From your previous result, with the values: ‘scan_start’: 0, ‘scan_length’: 1000, this 200 points are mostly equally spaced between this distance.
So profile_data[0] represents 0mm, profile_data[1] represents 5mm and etc.