Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
sonar_adc.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 Gautier Hattenberger, 2013 Tobias Münch, 2025 OpenUAS
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, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 */
22
27#include "generated/airframe.h"
29#include "modules/core/abi.h"
30#if defined(SITL) || (defined(SONAR_ADC_COMPENSATE_ROTATION) && (SONAR_ADC_COMPENSATE_ROTATION == 1))
31#include "state.h"
32#endif
33#if PERIODIC_TELEMETRY
35#include "pprzlink/messages.h"
36#endif
37
38#if defined(SENSOR_SYNC_SEND_SONAR) && (!defined(SONAR_ADC_SYNC_SEND))
39#define SONAR_ADC_SYNC_SEND 1
40#endif
41
42#ifdef SONAR_ADC_SYNC_SEND
44#endif
45
46// Check if an ADC device port is in the airframe configuration
47#ifndef SONAR_ADC_PORT
48#error sonar_adc module error: please add and configure SONAR_ADC_PORT, the port your sensor is connected to e.g. ADC_3 and save in your airframe file.
49#endif
51
52#include "mcu_periph/adc.h"
53
54// The minimum and maximum range what we want in our output e.g. sensor can do 7.2m but we only want to get dat less than 4m set MAX_RANGE to 4.0
55
57#ifndef SONAR_ADC_MIN_RANGE
58#define SONAR_ADC_MIN_RANGE 0.24f //Default for Sonar is 0.24m since many older analog sonar sensors cannot measure a range closer than that reliably
59#endif
60
62#ifndef SONAR_ADC_MAX_RANGE
63#define SONAR_ADC_MAX_RANGE 4.0f //Reasonable default value in meters with still usable readings for it is maximum value for common sonar sensors
64#endif
65
66// Just in the rare case the raw ADC value has a deviation on neutral readings. Do not use this for AGL to ground offsets, this is only for the raw ADC value to be correct
67#ifndef SONAR_ADC_RAW_OFFSET
68#define SONAR_ADC_RAW_OFFSET 0
69#endif
70
73#ifndef SONAR_ADC_OFFSET
74#define SONAR_ADC_OFFSET 0.0f // in meters, default is 0.0f
75#endif
76
78#ifndef SONAR_ADC_USE_FILTER
79#define SONAR_ADC_USE_FILTER 1 // Enable filtering for sonar per default is best, sonars tend to give noisy spiking readings
80#endif
81
83#ifndef USE_SONAR_ADC_AGL
84#define USE_SONAR_ADC_AGL 0
85#endif
86
87#ifdef SONAR_ADC_USE_FILTER
89#ifndef SONAR_ADC_LOWPASS_TAU
90#define SONAR_ADC_LOWPASS_TAU 0.16 // Time constant for second order Butterworth low pass filter. Default of 0.16 should give cut-off freq of 1/(2*pi*tau) ~= 1Hz
91#endif
93#endif
94
95// Distance compensation if the aircraft is in large rolled or pitched state to get the real distance to the ground
96#ifndef SONAR_ADC_COMPENSATE_ROTATION
97#define SONAR_ADC_COMPENSATE_ROTATION 0
98#endif
99
103#ifndef SONAR_ADC_SCALE
104#define SONAR_ADC_SCALE 0.001855f // Default value for sonar scale, e.g. Maxbotix LV-EZ1 or compatible sensors
105#endif
106
108
109#ifndef SITL
110static struct adc_buf sonar_adc_buf;
111#endif
112
116static void sonar_adc_send_sonar(struct transport_tx *trans, struct link_device *dev)
117{
119}
120
122{
123 sonar_adc.raw = 0;//SONAR_ADC_RAW_OFFSET;
125 SONAR_ADC_SCALE; // Added to struct as to be able to change the scale live via setting GUI for easier tuning
126 sonar_adc.distance = 0.0;
127 sonar_adc.update_agl = USE_SONAR_ADC_AGL; // Can be switched live if AGL should be updated or not
128
129#ifdef SONAR_ADC_USE_FILTER
131#endif
132
133#if PERIODIC_TELEMETRY
135#endif
136
137#ifndef SITL
139#endif
140}
141
146{
147#ifndef SITL
148
149 // Here the spot where the sonar ADC value is read
151 SONAR_ADC_RAW_OFFSET; // Get the average value from the ADC buffer and apply the offset to it
152
153 // Time of when measurement was taken, not when ABI message was send, to be as close as possible to real measurement time data
155
156#ifdef SONAR_ADC_USE_FILTER
158#else
159 sonar_adc.distance = (float)sonar_adc.raw * sonar_adc.scale; // Convert the raw ADC value to a distance in meters
160#endif
161
162 if (sonar_adc.distance <= (float)SONAR_ADC_MAX_RANGE) { // Discard non reliable readings that are out of range
164
165 // Compensate range measurement for body rotation
166#if SONAR_ADC_COMPENSATE_ROTATION
167 float phi = stateGetNedToBodyEulers_f()->phi;
168 float theta = stateGetNedToBodyEulers_f()->theta;
169 float gain = cosf(phi) * cosf(theta);
171 // To much attitude difference from neutral, e.g. 50deg roll and 40deg pitch, or even negative upside-down for the sensor to be really useful in AGL perspective, set distance to NAN
172 if (gain < 0.4f) { sonar_adc.distance = NAN; } // The magic 0.4f is for regular ultrasonic sensors about maximum range
173#endif
174
175 if (!isnan(sonar_adc.distance)) {
176 sonar_adc.distance = sonar_adc.distance - (float)SONAR_ADC_OFFSET; // Must be applied after rotation compensation
177 // Note that one could get negative values here if offset is larger than measured distance. This is valid and means the sensor is closer to ground than what is considered zero
178 // Mostly negative number behaviour is not wanted, so now bound to zero. Note that one looses the information that the sensor is actually closer than what is considered zero
180 // Send AGL message
181 // Only send valid AGL distance values and positive distances, negative distances do not make sense in AGL perspective as it would mean the aircraft is underground
182 if (sonar_adc.update_agl) {
184 }
185 }
186 } else {
187 // Out of range, set to NAN
189 }
190
191#else // SITL
194 (float)SONAR_ADC_MAX_RANGE); // Sim should also use airframe defined limits since can interact with AGL in simmed flightplan
195#endif // SITL
196
197// Fast data output for debugging purposes, one can it have temporary enabled to see what the sensor give as output in detail
198#if SONAR_ADC_SYNC_SEND
200#endif
201
202}
203
208{
209#if SONAR_ADC_SYNC_SEND
211#endif
212}
213
Main include for ABI (AirBorneInterface).
#define AGL_SONAR_ADC_ID
uint32_t sum
Definition adc.h:54
#define DEFAULT_AV_NB_SAMPLE
Definition adc.h:41
uint8_t av_nb_sample
Definition adc.h:57
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition adc.h:53
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Link between ChibiOS ADC drivers and Paparazzi adc_buffers.
Definition adc_arch.c:312
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
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition state.h:848
Simple first order low pass filter with bilinear transform.
static void init_butterworth_2_low_pass(Butterworth2LowPass *filter, float tau, float sample_time, float value)
Init a second order Butterworth filter.
static float update_butterworth_2_low_pass(Butterworth2LowPass *filter, float value)
Update second order Butterworth low pass filter state with a new value.
Second order low pass filter structure.
uint16_t foo
Definition main_demo5.c:58
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
float z
in meters
static struct adc_buf sonar_adc_buf
Definition sonar_adc.c:110
#define SONAR_ADC_SCALE
Scale Sensor sensitivity in m/adc (float)
Definition sonar_adc.c:104
static Butterworth2LowPass sonar_filt
Definition sonar_adc.c:92
#define SONAR_ADC_RAW_OFFSET
Sonar offset in ADC units.
Definition sonar_adc.c:68
void sonar_adc_init(void)
Definition sonar_adc.c:121
void sonar_adc_report(void)
Debug report Option to send debug informative values over telemetry.
Definition sonar_adc.c:207
#define SONAR_ADC_OFFSET
Rangefinder distance offset value for what should be considered zero distance, e.g.
Definition sonar_adc.c:74
struct SonarADC sonar_adc
Definition sonar_adc.c:107
static void sonar_adc_send_sonar(struct transport_tx *trans, struct link_device *dev)
Sending Send measured value and status information so it can be read back in e.g.
Definition sonar_adc.c:116
#define SONAR_ADC_LOWPASS_TAU
Definition sonar_adc.c:90
#define SONAR_ADC_MIN_RANGE
The minimum chosen distance for the device to be give readings.
Definition sonar_adc.c:58
void sonar_adc_periodic(void)
Reading Read ADC value to update sonar measurement.
Definition sonar_adc.c:145
#define USE_SONAR_ADC_AGL
Option to send AGL data over ABI.
Definition sonar_adc.c:84
#define SONAR_ADC_MAX_RANGE
The maximum chosen distance for the device to be give readings.
Definition sonar_adc.c:63
Driver for an sonar rangfinder sensor when used via an ADC channel.
float scale
Scaling factor to convert raw value to a distance in SI unit (meters)
Definition sonar_adc.h:43
uint16_t raw
raw measuread non scaled range value from sensor
Definition sonar_adc.h:42
float distance
Distance measured in meters.
Definition sonar_adc.h:44
bool update_agl
Do or don't update AGL ABI message.
Definition sonar_adc.h:45
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
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.