Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
ground_detect_sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Dennis van Wijngaarden <D.C.vanWijngaarden@tudelft.nl>
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 "state.h"
28 
29 #if USE_GROUND_DETECT_INDI_THRUST
31 #endif
32 
33 #if USE_GROUND_DETECT_AGL_DIST
34 #include "modules/sonar/agl_dist.h"
35 #define GROUND_DETECT_SENSOR_AGL_MIN_VALUE 0.1
36 #endif
37 
38 #include "pprzlink/messages.h"
40 
41 bool ground_detected = false;
42 
43 
44 #ifndef GROUND_DETECT_SENSOR_COUNTER_TRIGGER
45 #define GROUND_DETECT_SENSOR_COUNTER_TRIGGER 10
46 #endif
47 
48 #ifndef GROUND_DETECT_SENSOR_SPECIFIC_THRUST_THRESHOLD
49 #define GROUND_DETECT_SENSOR_SPECIFIC_THRUST_THRESHOLD -5.0
50 #endif
51 
52 
54 {
55  ground_detected = false;
56 }
57 
58 bool ground_detect(void) {
59  return ground_detected;
60 }
61 
63 {
64  bool ground_detect_method = false;
65 #if USE_GROUND_DETECT_INDI_THRUST
66  ground_detect_method = true;
67  static int32_t counter_thrust = 0;
68  // Evaluate thrust given (less than hover thrust)
69  // Use the control effectiveness in thrust in order to estimate the thrust delivered (only works for multicopters)
70  float specific_thrust = 0.0; // m/s2
71  uint8_t i;
72  for (i = 0; i < INDI_NUM_ACT; i++) {
73  specific_thrust += actuator_state_filt_vect[i] * g1g2[3][i] * -((int32_t) act_is_servo[i] - 1);
74  }
75 
76  ground_detected = false;
77  if (specific_thrust > GROUND_DETECT_SENSOR_SPECIFIC_THRUST_THRESHOLD ) {
78  counter_thrust += 1;
79  if (counter_thrust > GROUND_DETECT_SENSOR_COUNTER_TRIGGER) {
80  ground_detected = true;
81  }
82  } else {
83  counter_thrust = 0;
84  }
85 
86  #ifdef DEBUG_GROUND_DETECT
87  uint8_t test_gd = ground_detected;
88  float payload[2];
89  payload[0] = specific_thrust;
90  payload[1] = stateGetAccelNed_f()->z;
91 
92  RunOnceEvery(10, {DOWNLINK_SEND_PAYLOAD(DefaultChannel, DefaultDevice, 1, &test_gd); DOWNLINK_SEND_PAYLOAD_FLOAT(DefaultChannel, DefaultDevice, 4, payload);} );
93  #endif
94 #endif
95 
96 #if USE_GROUND_DETECT_AGL_DIST
97  ground_detect_method = true;
98  static int32_t counter_agl_dist = 0;
99  if (agl_dist_valid && (agl_dist_value_filtered < GROUND_DETECT_SENSOR_AGL_MIN_VALUE)) {
100  counter_agl_dist += 1;
101  } else {
102  counter_agl_dist = 0;
103  }
104  if (counter_agl_dist > GROUND_DETECT_SENSOR_COUNTER_TRIGGER) {
105  ground_detected = true;
106  } else {
107  ground_detected = false;
108  }
109 #endif
110  if (!ground_detect_method) {
111  ground_detected = false;
112  }
113 }
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.
#define GROUND_DETECT_SENSOR_COUNTER_TRIGGER
void ground_detect_sensor_init(void)
bool ground_detected
void ground_detect_sensor_periodic(void)
bool ground_detect(void)
#define GROUND_DETECT_SENSOR_SPECIFIC_THRUST_THRESHOLD
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1038
float z
in meters
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