Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
tfmini.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Freek van Tienen <freek.v.tienen@gmail.com>
3 * Copyright (C) 2025 Alejandro Rochas <alrochas@ucm.es>
4 *
5 * This file is part of paparazzi.
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 */
23
28#include "tfmini.h"
29#include "mcu_periph/uart.h"
30#include "modules/core/abi.h"
31
32// State interface for rotation compensation
33#include "state.h"
34
35// Messages
36#include "pprzlink/messages.h"
38
39
40// Horizontal distance from the IMU to the LiDAR (in meters)
41#ifndef LIDAR_OFFSET
42#define LIDAR_OFFSET 0.0f
43#endif
44
45// Height of the LiDAR above the ground (in meters)
46#ifndef LIDAR_HEIGHT
47#define LIDAR_HEIGHT 0.0f
48#endif
49
50static float lidar_offset;
51static float lidar_height;
52
56
57#if PERIODIC_TELEMETRY
59
63static void tfmini_send_lidar(struct transport_tx *trans, struct link_device *dev)
64{
68 &tfmini.mode,
69 &status);
70}
71
72#endif
73
96
97
102void tfmini_event(void)
103{
104 while (tfmini.parse_status != TFMINI_INITIALIZE && tfmini.device->char_available(tfmini.device->periph)) {
105 tfmini_parse(tfmini.device->get_byte(tfmini.device->periph));
106 }
107}
108
109
114{
115 switch (tfmini.parse_status) {
117 break;
119 if (byte == 0x59) {
122 }
123 break;
125 if (byte == 0x59) {
128 } else {
130 }
131 break;
132
137 break;
139 tfmini.raw_dist |= (byte << 8);
142 break;
143
148 break;
150 tfmini.raw_strength |= (byte << 8);
153 break;
154
159 break;
163 break;
164
166 // When the CRC matches
167 if (tfmini.parse_crc == byte) {
168 tfmini.distance = tfmini.raw_dist / 100.f;
171
172 // When the distance is valid
173 if (tfmini.distance != 0xFFFF) {
174 // compensate AGL measurement for body rotation
176 // If it is a rover, we need to compensate the distance
177 if (tfmini.is_rover) {
178 float theta = stateGetNedToBodyEulers_f()->theta;
179 float ground_distance;
180 if (fabs(theta) < 0.01) {
181 ground_distance = 100; // If it is 0 it is straight
182 } else {
183 ground_distance = lidar_height / sinf(-theta) - lidar_offset;
184 }
185
186 if ((tfmini.distance >= ground_distance) && (ground_distance > 0)) {
187 tfmini.distance = 0;
188 }
189 }
190 // If it is not a rover (like a drone), we need to compensate the distance differently
191 else {
192 float phi = stateGetNedToBodyEulers_f()->phi;
193 float theta = stateGetNedToBodyEulers_f()->theta;
194 float gain = (float)fabs((double)(cosf(phi) * cosf(theta)));
196 }
197 }
198
199 // Send the AGL message
201 }
202 }
203
204 // Start reading again
206 break;
207
208 default:
209 // Error, return to start
211 break;
212 }
213}
214
215
216// Send the lidar message (AGL, and, if requested, OBSTACLE_DETECTION)
218{
220 if (tfmini.update_agl) {
222 }
223#ifndef USE_SERVO_LIDAR
224 //send message (if there is not servo module)
226#endif
227}
228
229
Main include for ABI (AirBorneInterface).
#define AGL_LIDAR_TFMINI_ID
static uint8_t status
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
float phi
in radians
float theta
in radians
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition state.h:1306
uint16_t foo
Definition main_demo5.c:58
#define byte
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
int16_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint16_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition telemetry.h:66
void tfmini_event(void)
Lidar event function Receive bytes from the UART port and parse them.
Definition tfmini.c:102
void tfmini_parse(uint8_t byte)
Parse the lidar bytes 1 by 1.
Definition tfmini.c:113
static float lidar_height
Definition tfmini.c:51
#define LIDAR_HEIGHT
Definition tfmini.c:47
static void tfmini_send_lidar(struct transport_tx *trans, struct link_device *dev)
Downlink message lidar.
Definition tfmini.c:63
struct TFMini tfmini
Definition tfmini.c:53
#define LIDAR_OFFSET
Definition tfmini.c:42
void tfmini_send_abi(void)
Definition tfmini.c:217
void tfmini_init(void)
Initialization function.
Definition tfmini.c:77
static float lidar_offset
Definition tfmini.c:50
driver for the TFMini lidar
bool is_rover
Definition tfmini.h:61
uint8_t mode
Definition tfmini.h:58
float distance
Definition tfmini.h:57
bool compensate_rotation
Definition tfmini.h:60
bool update_agl
Definition tfmini.h:59
uint8_t raw_mode
Definition tfmini.h:54
uint16_t raw_dist
Definition tfmini.h:52
uint8_t parse_crc
Definition tfmini.h:51
struct link_device * device
Definition tfmini.h:49
uint16_t raw_strength
Definition tfmini.h:53
enum TFMiniParseStatus parse_status
Definition tfmini.h:50
uint16_t strength
Definition tfmini.h:56
@ TFMINI_INITIALIZE
Definition tfmini.h:36
@ TFMINI_PARSE_STRENGTH_L
Definition tfmini.h:41
@ TFMINI_PARSE_HEAD
Definition tfmini.h:37
@ TFMINI_PARSE_DIST_L
Definition tfmini.h:39
@ TFMINI_PARSE_BYTE7
Definition tfmini.h:44
@ TFMINI_PARSE_HEAD2
Definition tfmini.h:38
@ TFMINI_PARSE_STRENGTH_H
Definition tfmini.h:42
@ TFMINI_PARSE_MODE
Definition tfmini.h:43
@ TFMINI_PARSE_CHECKSUM
Definition tfmini.h:45
@ TFMINI_PARSE_DIST_H
Definition tfmini.h:40
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.