Home        Store        Docs        Blog

Ping-Protocol C library

(edgar) #1

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 .

(Patrick José Pereira) #2

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.

(edgar) #3

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