TOFSense Protocol
This tutorial is aimed at developers who develop based on TOFSense raw data, such as robots, drones, terminal navigation devices, etc. TOF ranging, distance status indication, signal strength and other information can be obtained through the UART interface, and the communication format is hexadecimal.
1 UART Overview
Users can obtain the distance measurement results through the UART interface. By default, the module will continue to send protocol data to the serial port at 921600 baud rate in little-endian mode at a fixed frequency. The specific data of different TOFSense models will be slightly different. Please refer to the TOFSense product manual of each model. Data is output in little-endian mode.
1.1 Active Output
TOFSense Series:
TOFSense-F Series:
TOFSense-M Series:
1.2 Parsing Example
The data obtained through UART is stored in hexadecimal. In order to observe the positioning information more intuitively, it needs to be converted into decimal. Please refer to the following analysis example.
1.2.1 TOFSense Series\TOFSense-F Series
A complete frame of hexadecimal data: 57 00 ff 00 9e 8f 00 00 ad 08 00 00 03 00 06 41
id: Hexadecimal 0x00 is converted to decimal 0.
system_time : The hexadecimal value 0x00008f9e is converted to decimal 36766. The local time is 36766ms from the time the power is turned on.
dis : The hexadecimal value 0x0008ad is converted to decimal 2221 and divided by the scaling factor 1000, the distance measurement value is 2.221m.
PS: How to convert?
uint8_t byte[] = {0x00,0x08,0xad};//Represents a decimal value: 2.221m
//uint8_t byte[] = {0xec,0xfb,0xff};//Represents a decimal value: -1.044
int32_t temp = (int32_t)(byte[0] << 8 | byte[1] << 16 | byte[2] << 24) / 256;
float result = temp/1000.0f;
dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication. This parameter is given by the module to the user to refer to whether the current distance measurement value is valid. Different models of TOFSense have different distance status indications. Please refer to the corresponding Datasheet for details.
signal_strength : Hexadecimal 0x0003 converted to decimal 3, signal strength, value range 0-65535, generally reflects the current TOF.
1.2.2 TOFSense-M/MS
For the TOFSense-M series, since it has 64 pixels, the amount of raw data is large, and a complete frame of data is 400 bytes.
A complete frame of hexadecimal data: 57 01 ff 00 03 a0 00 00 40 e0 81 07 00 9f 00 f0 43 03 00 58 00 c0 c8 03 00 55 00 90 e2 00 00 44 00 d0 84 00 00 57 00 18 79 00 00 61 00 e8 80 00 00 7a 00 90 65 00 00 8e 00 d8 d0 01 00 27 00 e8 74 02 00 28 00 00 f4 01 00 2e 00 f8 a7 00 00 39 00 50 c3 00 00 41 00 30 75 00 00 5b 00 70 94 00 00 61 00 00 7d 00 00 9b 00 30 e0 03 00 19 00 c8 79 09 00 1a 00 28 cf 0d 00 3a 00 b0 b3 00 00 20 00 30 75 00 00 31 00 60 6d 00 00 40 00 e8 80 00 00 4b 00 d0 84 00 00 71 00 40 3c 10 00 1e 00 88 b3 0f 00 24 00 20 b9 03 00 12 00 e8 26 0f 00 34 00 f0 d2 00 00 2c 00 c8 af 00 00 30 00 58 98 00 00 3a 00 f8 a7 00 00 47 00 d8 ed 11 09 1c 00 60 84 11 00 1c 00 e0 c8 10 00 21 00 d0 a1 10 00 25 00 88 90 00 00 1c 00 e0 ab 00 00 24 00 18 79 00 00 41 00 08 cf 00 00 41 00 68 47 14 ff 0b 00 c8 b4 14 00 0e 00 20 d6 13 00 11 00 d8 e1 13 00 14 00 d0 84 00 00 1d 00 f0 6c 11 00 19 00 a0 8c 00 00 47 00 90 65 00 00 50 00 88 41 22 ff 12 00 e8 f6 16 00 07 00 80 31 17 ff 0b 00 70 10 16 00 0c 00 40 9c 00 00 20 00 f8 a7 00 00 32 00 80 bb 00 00 33 00 a0 8c 00 00 50 00 90 d6 02 ff 2c 00 b0 e1 22 ff 0b 00 40 19 01 ff 10 00 d8 d6 00 ff 11 00 28 a0 00 00 25 00 e8 80 00 00 2b 00 c8 af 00 00 25 00 90 65 00 00 3c 00 ff ff ff ff ff ff 7d
id : Hexadecimal 0x00 converts to decimal 0 .
system_time : The hexadecimal value 0x0000a003 is converted to decimal 40963. The local time is 40963ms from the time the power is turned on.
zone_map : Hexadecimal 0x40 is converted to decimal 64, the number of pixels in the current mode.
- pixel0 :
- dis : Hexadecimal 0x0781e0 is converted to decimal 492000 and divided by the scaling factor 1000, the distance measurement value is 492mm.
- dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication.
- signal_strength : Hexadecimal 0x009f is converted to decimal 159, signal strength.
- pixel1 :
- dis : The hexadecimal value 0x0343f0 is converted to decimal 214000. Dividing it by the scaling factor 1000, the distance measurement value is 214mm.
- dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication.
- signal_strength : Hexadecimal 0x0058 converted to decimal 88, signal strength. ... ...
- pixel63 :
- dis : The hexadecimal value 0x006590 is converted to decimal 26000 and divided by the scaling factor 1000, the distance measurement value is 26mm.
- dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication.
- signal_strength : Hexadecimal 0x003c converted to decimal 60, signal strength.
1.3 Query Output
In query mode, the module will not actively output data. The user needs to send a specific query frame before the module returns a data frame. The query frame data format is as shown in the figure below.
Take the module with ID set to 3 and UART query mode as an example. If you want to obtain the data of this module, the query command you need to send to the module should be 57 10 ff ff 03 ff ff 66.
For all models of TOFSense, the query frame in UART query mode is in the above format. You only need to replace the ID of different modules and update the checksum.
1.4 Code Sample
2 CAN Overview
Users can obtain the ranging results through the CAN interface. By default, the module will continue to send protocol data to the CAN at 1M baud rate in little-endian mode at a fixed frequency. The specific data will vary slightly among different TOFSense models. Please refer to the TOFSense product manual for each model. Data is output in little-endian mode.
Only TOFSense series and TOFSense-M series products support CAN communication.
2.1 Active Output
For the TOFSense/S model, one data frame is transmitted in the above format. However, due to the particularity of the pixel points and the CAN bus transmission characteristics, the TOFSense-M/MS series will continuously transmit 64/16 (8*8/4*4 mode) data in one communication process.
2.2 Parsing Example
The data obtained through CAN is stored in hexadecimal. In order to observe the positioning information more intuitively, it needs to be converted into decimal. Please refer to the following analysis example.
2.2.1 TOFSense Series
A complete frame of hexadecimal data:
StdID:0x201+Data:ad 08 00 00 03 00 ff ff
id : Hexadecimal 0x201, minus the fixed 0x200 offset, 0x201-0x200 = 0x01 converts to decimal 1.
dis : The hexadecimal value 0x0008ad is converted to decimal 2221 and divided by the scaling factor 1000, the distance measurement value is 2.221m.
PS: How to convert distance?
uint8_t byte[] = {0x00,0x08,0xad};//Represents a decimal value: 2.221m
//uint8_t byte[] = {0xec,0xfb,0xff};//Represents a decimal value: -1.044
int32_t temp = (int32_t)(byte[0] << 8 | byte[1] << 16 | byte[2] << 24) / 256;
float result = temp/1000.0f;
dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication. This parameter is given by the module to the user to refer to whether the current distance measurement value is valid. Different models of TOFSense have different distance status indications. Please refer to the corresponding Datasheet for details.
signal_strength : Hexadecimal 0x0003 converted to decimal 3, signal strength, value range 0-65535, generally reflects the current TOF.
2.2.2 TOFSense-M Series
For the TOFSense-M series, since it has 64 pixels, the amount of raw data is large, and CAN can only transmit 8 bytes of data at a time, so 64 frames of data will be sent out continuously, and each frame of data is distinguished by the index at the end of the frame. Here is the raw hex data for a complete transfer:
StdID:0x201+Data:9D 08 00 00 19 00 00 00
StdID:0x201+Data:CC 08 00 00 19 00 01 00
StdID:0x201+Data:C8 08 00 00 1F 00 02 00
StdID:0x201+Data:C2 08 00 00 1A 00 03 00
StdID:0x201+Data:C0 08 00 00 1B 00 04 00
StdID:0x201+Data:BD 08 00 00 1A 00 05 00
StdID:0x201+Data:D5 08 00 00 11 00 06 00
StdID:0x201+Data:D4 08 00 00 1F 00 07 00
StdID:0x201+Data:A6 08 00 00 17 00 08 00
StdID:0x201+Data:D4 08 00 00 14 00 09 00
StdID:0x201+Data:AF 08 00 00 1C 00 0A 00
StdID:0x201+Data:A4 08 00 00 15 00 0B 00
StdID:0x201+Data:C4 08 00 00 17 00 0C 00
StdID:0x201+Data:EA 08 00 00 17 00 0D 00
StdID:0x201+Data:D1 08 00 00 1C 00 0E 00
StdID:0x201+Data:BD 08 00 00 14 00 0F 00
StdID:0x201+Data:B5 08 00 00 1B 00 10 00
StdID:0x201+Data:BF 08 00 00 1B 00 11 00
StdID:0x201+Data:CB 08 00 00 1E 00 12 00
StdID:0x201+Data:D9 08 00 00 1E 00 13 00
StdID:0x201+Data:E1 08 00 00 1B 00 14 00
StdID:0x201+Data:DB 08 00 00 1B 00 15 00
StdID:0x201+Data:C7 08 00 00 1E 00 16 00
StdID:0x201+Data:DA 08 00 00 16 00 17 00
StdID:0x201+Data:B4 08 00 00 1A 00 18 00
StdID:0x201+Data:CA 08 00 00 18 00 19 00
StdID:0x201+Data:DF 08 00 00 1E 00 1A 00
StdID:0x201+Data:DB 08 00 00 23 00 1B 00
StdID:0x201+Data:E4 08 00 00 1D 00 1C 00
StdID:0x201+Data:E4 08 00 00 1D 00 1D 00
StdID:0x201+Data:D8 08 00 00 16 00 1E 00
StdID:0x201+Data:F6 08 00 00 19 00 1F 00
StdID:0x201+Data:C2 08 00 00 18 00 20 00
StdID:0x201+Data:B7 08 00 00 1D 00 21 00
StdID:0x201+Data:E6 08 00 00 1A 00 22 00
StdID:0x201+Data:D9 08 00 00 24 00 23 00
StdID:0x201+Data:F0 08 00 00 21 00 24 00
StdID:0x201+Data:00 09 00 00 17 00 25 00
StdID:0x201+Data:E9 08 00 00 13 00 26 00
StdID:0x201+Data:DA 08 00 00 15 00 27 00
StdID:0x201+Data:B2 08 00 00 1D 00 28 00
StdID:0x201+Data:CA 08 00 00 1C 00 29 00
StdID:0x201+Data:C5 08 00 00 17 00 2A 00
StdID:0x201+Data:D6 08 00 00 1E 00 2B 00
StdID:0x201+Data:E7 08 00 00 1E 00 2C 00
StdID:0x201+Data:F6 08 00 00 15 00 2D 00
StdID:0x201+Data:17 09 00 00 15 00 2E 00
StdID:0x201+Data:FF 08 00 00 14 00 2F 00
StdID:0x201+Data:B4 08 00 00 19 00 30 00
StdID:0x201+Data:C0 08 00 00 1C 00 31 00
StdID:0x201+Data:C0 08 00 00 1C 00 32 00
StdID:0x201+Data:D1 08 00 00 1C 00 33 00
StdID:0x201+Data:DE 08 00 00 18 00 34 00
StdID:0x201+Data:DD 08 00 00 18 00 35 00
StdID:0x201+Data:EB 08 00 00 16 00 36 00
StdID:0x201+Data:CB 08 00 00 13 00 37 00
StdID:0x201+Data:B4 08 00 00 17 00 38 00
StdID:0x201+Data:B9 08 00 00 1C 00 39 00
StdID:0x201+Data:D3 08 00 00 21 00 3A 00
StdID:0x201+Data:E5 08 00 00 19 00 3B 00
StdID:0x201+Data:D9 08 00 00 18 00 3C 00
StdID:0x201+Data:D5 08 00 00 1C 00 3D 00
StdID:0x201+Data:E7 08 00 00 1A 00 3E 00
StdID:0x201+Data:09 09 00 00 19 00 3F 00
- pixel0 :
- dis : Hexadecimal 0x00089D is converted to decimal 2205 and divided by the scaling factor 1000, the distance value is 2.205m.
- dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication.
- signal_strength : Hexadecimal 0x0019 is converted to decimal 25, signal strength.
- index : Hexadecimal 0x00 is converted to decimal 0, which means that the data of this frame should be the data of Pixel 0.
- pixel1 :
- dis : The hexadecimal value 0x0008cc is converted to decimal 2252 and divided by the scaling factor 1000, the distance measurement value is 2.252m.
- dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication.
- signal_strength : Hexadecimal 0x0019 is converted to decimal 25, signal strength.
- index : Hexadecimal 0x01 is converted to decimal 1, which means that the data of this frame should be the data of Pixel 1. ... ...
- pixel63 :
- dis : The hexadecimal value 0x000909 is converted to decimal 2313 and divided by the scaling factor 1000, the distance measurement value is 2.313m.
- dis_status : Hexadecimal 0x00 is converted to decimal 0, distance status indication.
- signal_strength : Hexadecimal 0x0019 is converted to decimal 25, signal strength.
- index : Hexadecimal 0x3F is converted to decimal 63, which means that the data of this frame should be the data of Pixel 63.
2.3 Query Output
In query mode, the module will not actively output data. The user needs to send a specific query frame before the module returns a data frame. The query frame data format is as shown in the figure below.
Take the module with ID set to 3 and UART query mode as an example. If you want to obtain the data of this module, the query command you need to send to the module should be
StdID:0x402+Data:ff ff ff 01 ff ff ff ff
The StdID is fixed to 0x402, and users only need to modify the id in Data during normal use.
TOFSense/-UART/S:
TOFSense-M/MS:
For all models of TOFSense in UART query mode, the query frame is in the above format. You only need to replace the ID of different modules and update the checksum. C language sending function example (HAL library):
2.4 Code Sample
3 IIC Overview
For the TOFSense-F series modules, users can modify the mode using the NAssistant software and then obtain ranging results via the I2C interface. Data is output in little-endian mode.
In IIC mode, the user needs to actively access the corresponding register to obtain data.
3.1 Register Table
Data acquisition process
Read the 4-byte data of register 0x24 and convert it into distance. It is recommended to read the distance status indication and signal strength in register 0x28 at the same time.