Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
ground_detect.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Ewoud Smeur <e.j.j.smeur@tudelft.nl>
3  * Copyright (C) 2023 Dennis van Wijngaarden <D.C.vanWijngaarden@tudelft.nl>
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, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
26 #include "ground_detect.h"
29 
30 #include "state.h"
31 
32 #if USE_GROUND_DETECT_INDI_THRUST
34 #endif
35 
36 #if USE_GROUND_DETECT_AGL_DIST
37 #include "modules/sonar/agl_dist.h"
38 #ifndef GROUND_DETECT_AGL_MIN_VALUE
39 #define GROUND_DETECT_AGL_MIN_VALUE 0.1
40 #endif
41 #endif
42 
43 #include "pprzlink/messages.h"
45 
46 #define GROUND_DETECT_COUNTER_TRIGGER 5
47 
48 // Cutoff frequency of accelerometer filter in Hz
49 #define GROUND_DETECT_FILT_FREQ 5.0
50 
52 
54 
56 bool ground_detected = false;
57 
58 #define DEBUG_GROUND_DETECT TRUE
59 
61 {
62  float tau = 1.0 / (2.0 * M_PI * GROUND_DETECT_FILT_FREQ);
63  float sample_time = 1.0 / PERIODIC_FREQUENCY;
64  init_butterworth_2_low_pass(&accel_filter, tau, sample_time, 0.0);
65 }
66 
67 bool ground_detect(void)
68 {
69  return ground_detected;
70 }
71 
72 // TODO: use standard pprz ground detection interface
73 // bool autopilot_ground_detection(void) {
74 // RunOnceEvery(10, printf("Running!\n"););
75 // return ground_detected;
76 // }
77 
79 {
80 
81  // Evaluate thrust given (less than hover thrust)
82  // Use the control effectiveness in thrust in order to estimate the thrust delivered (only works for multicopters)
83  float specific_thrust = 0.0; // m/s2
84 
85 #if USE_GROUND_DETECT_INDI_THRUST
86  uint8_t i;
87  for (i = 0; i < INDI_NUM_ACT; i++) {
88  specific_thrust += actuator_state_filt_vect[i] * g1g2[3][i] * -((int32_t) act_is_servo[i] - 1);
89  }
90 #endif
91 
92  // vertical component
93  float spec_thrust_down;
94  struct FloatRMat *ned_to_body_rmat = stateGetNedToBodyRMat_f();
95  spec_thrust_down = ned_to_body_rmat->m[8] * specific_thrust;
96 
97  // Evaluate vertical speed (close to zero, not at terminal velocity)
98  float vspeed_ned = stateGetSpeedNed_f()->z;
99 
100  // Detect free fall (to be done, rearm?)
101 
102  // Detect noise level (to be done)
103 
104  // Detect ground based on AND of all triggers
105  if ((fabsf(vspeed_ned) < 5.0)
106  && (spec_thrust_down > -5.0)
107  && (fabsf(accel_filter.o[0]) < 2.0)
108 #if USE_GROUND_DETECT_AGL_DIST
109  && (agl_dist_valid && (agl_dist_value_filtered < GROUND_DETECT_AGL_MIN_VALUE))
110 #endif
111  ) {
112 
113  counter += 1;
115  ground_detected = true;
116 
119  disarm_on_not_in_flight = false;
120  }
121  }
122  } else {
123  ground_detected = false;
124  counter = 0;
125  }
126 
127 #ifdef DEBUG_GROUND_DETECT
128  float payload[7];
129  payload[0] = vspeed_ned;
130  payload[1] = spec_thrust_down;
131  payload[2] = accel_filter.o[0];
132  payload[3] = stateGetAccelNed_f()->z;
133 #if USE_GROUND_DETECT_AGL_DIST
134  payload[4] = agl_dist_valid;
135  payload[5] = agl_dist_value_filtered;
136 #else
137  payload[4] = 0;
138  payload[5] = 0;
139 #endif
140  payload[6] = 1.f * ground_detected;
141 
142  RunOnceEvery(10, {DOWNLINK_SEND_PAYLOAD_FLOAT(DefaultChannel, DefaultDevice, 7, payload);});
143 #endif
144 }
145 
151 {
152  struct NedCoor_f *accel = stateGetAccelNed_f();
154 }
float agl_dist_valid
Definition: agl_dist.c:33
float agl_dist_value_filtered
Definition: agl_dist.c:35
Bind to agl ABI message and provide a filtered value to be used in flight plans.
bool autopilot_set_motors_on(bool motors_on)
turn motors on/off, eventually depending of the current mode set kill_throttle accordingly FIXME is i...
Definition: autopilot.c:250
Butterworth2LowPass accel_filter
Definition: ground_detect.c:51
void ground_detect_filter_accel(void)
Filter the vertical acceleration with a low cutoff frequency.
bool disarm_on_not_in_flight
Definition: ground_detect.c:53
#define GROUND_DETECT_FILT_FREQ
Definition: ground_detect.c:49
#define GROUND_DETECT_COUNTER_TRIGGER
Definition: ground_detect.c:46
void ground_detect_init()
Definition: ground_detect.c:60
bool ground_detected
Definition: ground_detect.c:56
bool ground_detect(void)
Definition: ground_detect.c:67
int32_t counter
Definition: ground_detect.c:55
void ground_detect_periodic()
Definition: ground_detect.c:78
float m[3 *3]
rotation matrix
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1195
static struct FloatRMat * stateGetNedToBodyRMat_f(void)
Get vehicle body attitude rotation matrix (float).
Definition: state.h:1300
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition: state.h:1049
Simple first order low pass filter with bilinear transform.
float o[2]
output history
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.
float z
in meters
vector in North East Down coordinates Units: meters
Rotorcraft specific autopilot interface and initialization.
bool act_is_servo[INDI_NUM_ACT]
float g1g2[INDI_OUTPUTS][INDI_NUM_ACT]
float actuator_state_filt_vect[INDI_NUM_ACT]
API to get/set the generic vehicle states.
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98