STM32F103 drives VL53L0X laser ranging module

Introduction

TOF is the abbreviation of time-of-flight (Tlme of Flight) technology, that is, the sensor emits modulated near-infrared light, which is reflected after encountering an object. The sensor calculates the distance of the measured object by calculating the time difference or phase difference between light emission and reflection.
The VL53L0X is a new generation time-of-flight (ToF) laser ranging module that provides accurate distance measurement regardless of target reflectivity. It can measure an absolute distance of 2m, setting a new benchmark for ranging performance levels. The VL53L0X integrates a leading SPAD array (Single Photon Avalanche Diode) and embeds ST's second-generation FlightSense™ patented technology.
The 940nm VCSEL emitter (Vertical Cavity Surface Emitting Laser) of VL53L0X is completely invisible to human eyes, and the built-in physical infrared filter makes it have a longer ranging distance and stronger immunity to ambient light. The optical crosstalk has better stability.
The VL53L0X sensor provides 4 measurement modes. They are default measurement mode, high-precision measurement mode, long-distance measurement mode and high-speed measurement mode. According to the characteristics of the 4 different precision modes officially provided by ST, the specific parameters are shown in the table below.

precision mode S measurement time (ms) Ranging performance (m)
default 30 1.2
High precision 200 1.2 Accuracy<±3%
long distance 33 2
high speed 20 1.2 Accuracy <±5%

It can be seen from the table that in different precision working modes, the length of measurement time is different. The fastest measurement time is the high-speed mode, which can be sampled once within 20ms, but the accuracy does have an error range of ±5%. In the long-distance precision mode, the ranging distance can reach 2m, and the measurement time is within 33ms, but it needs to be measured in dark conditions. Users can use different working modes in different application scenarios according to actual needs.
insert image description here

pin definition

insert image description here
insert image description here
SCL: IIC interface clock signal line;
SDA: IIC interface data signal line;
VIN: 3.3-5V power supply positive pole;
GND: power supply negative pole
XSHUT (reset): reset, low effective;
GPIO1: interrupt output
IIC device address: 0x52
IIC communication Frequency: up to 400KHz

Wiring between STM32F103ZET6 development board and VL53L0X module

STM32F103ZET6 VL53L0X
3.3V VIN
GND GND
PB10 SCL
PB11 SDA
XSHUT PA4

insert image description here

test code

  1. Main function and initialization configuration
    The following follows the execution flow of the program, code list 1 main function (main.c file), in the main function, first initialize the configuration of the USART serial port. Then enter the stage of VL53L0X sensor function test.
int main(void)
{
    
    
 /*初始化 USART 配置模式为 115200 8-N-1,中断接收*/
 USART_Config();
 
 printf(" 欢迎使用野火开发板\r\n");
 printf(" 激光测距传感器实验\r\n");
  while (1) {
    
    
 vl53l0x_test();
 }
}

Code Listing 2 VL53L0X sensor test (VL53L0X.c file) In the test function, the VL53L0X sensor is initialized first, and when the initialization is successful, it exits the while loop function and enters the laser ranging process (line 16).

1 /**
2 * @brief VL53L0X 主测试程序
3 * @param 无
4 * @retval 无
5 */
6 void vl53l0x_test(void)
7 {
    
    
8 /*vl53l0x 初始化*/
9 while (vl53l0x_init(&vl53l0x_dev)) {
    
    
10 printf("初始化失败\r\n");
11 printf("请检查连接\r\n");
12 }
13 printf("初始化成功\r\n");
14 
15 while (1) {
    
    
16 vl53l0x_general_test(&vl53l0x_dev);
17 }
18 }

Code list 3 VL53L0X working mode test function (VL53L0X.c file) In the test function, the eighth line uses the scanf function to make the development board obtain the control commands from the serial port debugging assistant. When the command character ch received by the development board is a valid command (integer of 0<=ch<4), the development board controls the sensor to enter the corresponding laser ranging mode through the vl53l0x_general_start(dev,ch) function (the 17th line of code); When the received character is an invalid command, it will print and prompt the user to input a legal command.

1 /**
2 * @brief vl53l0x 工作模式测试
3 * @param dev:设备 I2C 参数结构体
4 * @retval 无
5 */
6 void vl53l0x_general_test(VL53L0X_Dev_t *dev)
7 {
    
    
8 uint32_t ch;
9 
10 Show_GenTask_Message(); //显示普通测量模式 UI
11 
12 while (1) {
    
    
13 scanf("%d",&ch);
14 printf("接收到字符:%d\r\n",ch);
15 
16 if ((ch>=0) && (ch<=3)) {
    
    
17 vl53l0x_general_start(dev,ch);
18 Show_GenTask_Message();
19 } else {
    
    
20 /*如果不是指定指令字符,打印提示信息*/
21 printf("请输入合法指令!\r\n");
22 Show_GenTask_Message();
23 }
24 }
25 }

Code Listing 4 VL53L0X measurement mode configuration function (VL53L0X.c file) In the measurement mode configuration function, due to frequent switching of accuracy modes, the collected distance data is likely to be inaccurate, so reset the VL53L0X sensor before use (the 18th line of code ), and then use the VL53L0X_StaticIni() function to restore the sensor parameters to the default. In addition, the configuration of the precision mode is determined according to the function parameter mode variable, and the mode value corresponds to 0: default, 1: high precision, 2: long distance, 3: high speed, according to the writing of the global array Mode_data[] variable, so as to achieve different precision mode configuration.

1 /**
2 * @brief VL53L0X 测量模式配置
3 * @param dev:设备 I2C 参数结构体
4 * @param mode: 0:默认;1:高精度;2:长距离
5 * @retval 状态信息
6 */
7 VL53L0X_Error vl53l0x_set_mode(VL53L0X_Dev_t *dev,uint8_t mode)
8 {
    
    
9 VL53L0X_Error status = VL53L0X_ERROR_NONE;
10 uint8_t VhvSettings;
11 uint8_t PhaseCal;
12 uint32_t refSpadCount;
13 uint8_t isApertureSpads;
14 
15 /*复位 vl53l0x(频繁切换工作模式容易导致采集距离数据不准,需加上这一代码)*/
18 vl53l0x_reset(dev);
19 status = VL53L0X_StaticInit(dev);
20 
21 /*已校准好了,写入校准值*/
22 if (AjustOK!=0) {
    
    
23 /*设定 Spads 校准值*/
24 status= VL53L0X_SetReferenceSpads(dev,Vl53l0x_data.refSpadCount,
25 Vl53l0x_data.isApertureSpads);
26 if (status!=VL53L0X_ERROR_NONE) goto error;
27 delay_ms(2);
28 
29 /*设定 Ref 校准值*/
30 status= VL53L0X_SetRefCalibration(dev,Vl53l0x_data.VhvSettings,
31 Vl53l0x_data.PhaseCal);
32 if (status!=VL53L0X_ERROR_NONE) goto error;
33 delay_ms(2);
34 
35 /*中间省略代码*/
36 .......
37
38 
39 /*设定 VCSEL 脉冲周期*/
40 status = VL53L0X_SetVcselPulsePeriod(dev,
41 VL53L0X_VCSEL_PERIOD_PRE_RANGE, 
Mode_data[mode].preRangeVcselPeriod);
43 if (status!=VL53L0X_ERROR_NONE) goto error;
44 delay_ms(2);
45 
46 /*设定 VCSEL 脉冲周期范围*/
47 status = VL53L0X_SetVcselPulsePeriod(dev,
 VL53L0X_VCSEL_PERIOD_FINAL_RANGE,
 Mode_data[mode].finalRangeVcselPeriod);
50 
51 error:/*错误信息*/
52 if (status!=VL53L0X_ERROR_NONE) {
    
    
53 print_pal_error(status);
54 return status;
55 }
56 return status;
57}

Code Listing 5 VL53L0X single distance measurement function (VL53L0X.c file) In this function, the single measurement is started by calling the VL53L0X_PerformSingleRangingMeasurement () function (the 15th line of code). This function is a blocking function. The data will be saved to the pdata measurement data structure, and finally the measurement data obtained from the measurement structure will be assigned to the Distance_data global variable (line 24). By reading the value of the Distance_data variable, we can know the measurement distance.

1 /**
2 * @brief VL53L0X 单次距离测量函数
3 * @param dev:设备 I2C 参数结构体
4 * @param pdata:保存测量数据结构体
5 * @retval 状态信息
6 */
7 VL53L0X_Error vl53l0x_start_single_test(VL53L0X_Dev_t *dev, 
VL53L0X_RangingMeasurementData_t *pdata,
9 char *buf)
10 {
    
    
11 VL53L0X_Error status = VL53L0X_ERROR_NONE;
12 uint8_t RangeStatus;
13 
14 /*执行单次测距并获取测距测量数据*/
15 status = VL53L0X_PerformSingleRangingMeasurement(dev, pdata);
16 if (status !=VL53L0X_ERROR_NONE) 
return status;
17 
18 /*获取当前测量状态*/
19 RangeStatus = pdata->RangeStatus;
20 memset(buf,0x00,VL53L0X_MAX_STRING_LENGTH);
21 /*根据测量状态读取状态字符串*/
22 VL53L0X_GetRangeStatusString(RangeStatus,buf);
23 /*保存最近一次测距测量数据*/
24 Distance_data = pdata->RangeMilliMeter;
25 
26 return status;
27 }

Experimental results

After the serial port assistant software enters the number corresponding to the ranging mode, the laser ranging sensor obtains the sensing information, and outputs the result to the serial port debugging assistant software through the development version usart1 serial port for display.
insert image description here

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/131668293