Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
lidar_vl53l5cx.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Fabien-B <name.surname@gmail.com>
3  *
4  * This file is part of paparazzi
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
27 #include "mcu_periph/i2c.h"
28 #include "ch.h"
31 #include "modules/core/abi.h"
33 #include "ch.h"
34 #include "hal.h"
35 #include "mcu_periph/ram_arch.h"
36 
37 #ifndef LIDAR_VL53L5CX_I2C_ADDR
38 #define LIDAR_VL53L5CX_I2C_ADDR 0x29
39 #endif
40 
41 #define SUBTYPE_DISTANCE 0
42 
44 static IN_DMA_SECTION(VL53L5CX_ResultsData vl53l5cx_results);
45 
46 static THD_WORKING_AREA(wa_thd_lidar_vl53l5cx, 1024);
47 static void thd_lidar_vl53l5cx(void *arg);
48 
49 
51  "VL53L5CX_NO_ERROR",
52  "VL53L5CX_NOT_DETECTED",
53  "VL53L5CX_ULD_LOADING_FAILED",
54  "VL53L5CX_SET_RESOLUTION_FAILED",
55  "VL53L5CX_RUNTIME_ERROR",
56 };
57 
58 
60 {
61  vl53l5cx_dev.platform.i2cdev = &LIDAR_VL53L5CX_I2C_DEV;
62  vl53l5cx_dev.platform.address = LIDAR_VL53L5CX_I2C_ADDR;
63  vl53l5cx_dev.platform.user_data = NULL;
64  vl53l5cx_dev.platform.error_code = VL53L5CX_NO_ERROR;
65 
66  // Create thread
67  vl53l5cx_dev.platform.user_data = chThdCreateStatic(wa_thd_lidar_vl53l5cx, sizeof(wa_thd_lidar_vl53l5cx),
68  NORMALPRIO, thd_lidar_vl53l5cx, (void *)&vl53l5cx_dev);
69 }
70 
72 {
73 
74  if (vl53l5cx_dev.platform.user_data != NULL) {
75  thread_t* thread_handle = (thread_t*) vl53l5cx_dev.platform.user_data;
76  // check thread status
77  if (thread_handle->state == CH_STATE_FINAL) {
78  vl53l5cx_dev.platform.error_code = (enum VL53L5CX_ERRORS) chThdWait(thread_handle);
79  vl53l5cx_dev.platform.user_data = NULL;
80  }
81  } else {
82 
83  // thread exited, send error code periodically
84  size_t len = strlen(VL53L5CX_ERROR_MSGS[vl53l5cx_dev.platform.error_code]);
85  // send exitcode to telemetry
86  RunOnceEvery(10, DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, len,
87  VL53L5CX_ERROR_MSGS[vl53l5cx_dev.platform.error_code]));
88  ;
89  }
90 
91 
92  if (vl53l5cx_dev.platform.data_available) {
93  uint32_t now_ts = get_sys_time_usec();
94  AbiSendMsgLIDAR_DATA(LIDAR_DATA_VL53L5CX_ID, now_ts,
95  8, 8, sizeof(vl53l5cx_dev.platform.distances_mm[0]), SUBTYPE_DISTANCE,
96  (uint8_t *)vl53l5cx_dev.platform.distances_mm);
97 
98  vl53l5cx_dev.platform.data_available = false;
99  }
100 
101 
102 }
103 
104 
105 
106 static void thd_lidar_vl53l5cx(void *arg)
107 {
108  chRegSetThreadName("vl53l5cx");
110 
111  uint8_t status, isAlive, isReady;
112 
113 
114  chThdSleepMilliseconds(2000);
115 
116  status = vl53l5cx_is_alive(dev, &isAlive);
117  if (!isAlive || status) {
118  chThdExit(VL53L5CX_NOT_DETECTED);
119  }
120 
122  if (status) {
123  chThdExit(VL53L5CX_ULD_LOADING_FAILED);
124  }
125 
127  if (status) {
129  }
130 
132 
133  while (true) {
134  status = vl53l5cx_check_data_ready(dev, &isReady);
135  if (isReady) {
136  status = vl53l5cx_get_ranging_data(dev, &vl53l5cx_results);
137  if (status == 0) {
138  memcpy(dev->platform.distances_mm, vl53l5cx_results.distance_mm, 64 * sizeof(vl53l5cx_results.distance_mm[0]));
139  dev->platform.data_available = true;
140  }
141  }
142 
143  chThdSleepMilliseconds(100);
144  }
145 
146 
147 }
148 
Main include for ABI (AirBorneInterface).
#define LIDAR_DATA_VL53L5CX_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
if(GpsFixValid() &&e_identification_started)
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
#define LIDAR_VL53L5CX_I2C_ADDR
static IN_DMA_SECTION(VL53L5CX_Configuration vl53l5cx_dev)
static void thd_lidar_vl53l5cx(void *arg)
#define SUBTYPE_DISTANCE
void lidar_vl53l5cx_init(void)
char * VL53L5CX_ERROR_MSGS[]
void lidar_vl53l5cx_periodic(void)
static THD_WORKING_AREA(wa_thd_lidar_vl53l5cx, 1024)
uint8_t status
Specific RAM section for DMA usage on F7.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
uint8_t vl53l5cx_is_alive(VL53L5CX_Configuration *p_dev, uint8_t *p_is_alive)
Definition: vl53l5cx_api.c:221
uint8_t vl53l5cx_get_ranging_data(VL53L5CX_Configuration *p_dev, VL53L5CX_ResultsData *p_results)
This function gets the ranging data, using the selected output and the resolution.
Definition: vl53l5cx_api.c:712
uint8_t vl53l5cx_init(VL53L5CX_Configuration *p_dev)
Mandatory function used to initialize the sensor.
Definition: vl53l5cx_api.c:245
uint8_t vl53l5cx_start_ranging(VL53L5CX_Configuration *p_dev)
This function starts a ranging session.
Definition: vl53l5cx_api.c:490
uint8_t vl53l5cx_check_data_ready(VL53L5CX_Configuration *p_dev, uint8_t *p_isReady)
This function checks if a new data is ready by polling I2C.
Definition: vl53l5cx_api.c:681
uint8_t vl53l5cx_set_resolution(VL53L5CX_Configuration *p_dev, uint8_t resolution)
This function sets a new resolution (4x4 or 8x8).
Definition: vl53l5cx_api.c:898
#define VL53L5CX_RESOLUTION_4X4
Macro VL53L5CX_RESOLUTION_4X4 or VL53L5CX_RESOLUTION_8X8 allows setting sensor in 4x4 mode or 8x8 mod...
Definition: vl53l5cx_api.h:47
Structure VL53L5CX_Configuration contains the sensor configuration.
Definition: vl53l5cx_api.h:271
Structure VL53L5CX_ResultsData contains the ranging results of VL53L5CX.
Definition: vl53l5cx_api.h:305
VL53L5CX_ERRORS
@ VL53L5CX_SET_RESOLUTION_FAILED
@ VL53L5CX_NOT_DETECTED
@ VL53L5CX_NO_ERROR
@ VL53L5CX_ULD_LOADING_FAILED