Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
agl_dist.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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  */
21 
29 #include "modules/sonar/agl_dist.h"
30 #include "modules/core/abi.h"
31 #include "generated/airframe.h"
32 
37 
39 #ifndef AGL_DIST_ID
40 #define AGL_DIST_ID ABI_BROADCAST
41 #endif
42 #ifndef AGL_DIST_MAX_RANGE
43 #define AGL_DIST_MAX_RANGE 5.0f
44 #endif
45 #ifndef AGL_DIST_MIN_RANGE
46 #define AGL_DIST_MIN_RANGE 0.001f
47 #endif
48 #ifndef AGL_DIST_FILTER
49 #define AGL_DIST_FILTER 0.1f
50 #endif
51 
53 static void agl_cb(uint8_t sender_id, uint32_t stamp, float distance);
54 
55 
56 #if PREFLIGHT_CHECKS && defined(AGL_DIST_MIN_DISTANCE_CHECK) && defined(AGL_DIST_MAX_DISTANCE_CHECK)
57 /* Preflight checks */
59 static struct preflight_check_t agl_dist_pfc;
60 
61 static void agl_dist_preflight(struct preflight_result_t *result) {
62  if(agl_dist_valid && agl_dist_value > AGL_DIST_MIN_DISTANCE_CHECK && agl_dist_value < AGL_DIST_MAX_DISTANCE_CHECK) {
63  preflight_success(result, "AGL within limits %.2f < %.2f < %.2f", AGL_DIST_MIN_DISTANCE_CHECK, agl_dist_value, AGL_DIST_MAX_DISTANCE_CHECK);
64  } else {
65  preflight_error(result, "AGL outside limits %.2f < %.2f < %.2f", AGL_DIST_MIN_DISTANCE_CHECK, agl_dist_value, AGL_DIST_MAX_DISTANCE_CHECK);
66  }
67 }
68 #endif // PREFLIGHT_CHECKS && defined(AGL_DIST_MIN_DISTANCE_CHECK) && defined(AGL_DIST_MAX_DISTANCE_CHECK)
69 
70 void agl_dist_init(void)
71 {
72  agl_dist_valid = false;
73  agl_dist_value = 0.f;
76 
77  // Bind to AGL message
78  AbiBindMsgAGL(AGL_DIST_ID, &agl_ev, agl_cb);
79 
80  /* Register preflight checks */
81 #if PREFLIGHT_CHECKS && defined(AGL_DIST_MIN_DISTANCE_CHECK) && defined(AGL_DIST_MAX_DISTANCE_CHECK)
82  preflight_check_register(&agl_dist_pfc, agl_dist_preflight);
83 #endif // PREFLIGHT_CHECKS && defined(AGL_DIST_MIN_DISTANCE_CHECK) && defined(AGL_DIST_MAX_DISTANCE_CHECK)
84 }
85 
86 static void agl_cb(uint8_t __attribute__((unused)) sender_id, uint32_t __attribute__((unused)) stamp, float distance)
87 {
88  if (distance < AGL_DIST_MAX_RANGE && distance > AGL_DIST_MIN_RANGE) {
89  agl_dist_value = distance;
90  agl_dist_valid = true;
91  float now = get_sys_time_float();
92  float dt = now - agl_measurement_time;
94 
95  // update multirate exponentially weighted moving average filter
97  } else {
98  agl_dist_valid = false;
99  }
100 }
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
float agl_measurement_time
Definition: agl_dist.c:36
float agl_dist_valid
Definition: agl_dist.c:33
float agl_dist_value
Definition: agl_dist.c:34
float agl_dist_value_filtered
Definition: agl_dist.c:35
#define AGL_DIST_FILTER
Definition: agl_dist.c:49
static void agl_cb(uint8_t sender_id, uint32_t stamp, float distance)
Definition: agl_dist.c:86
void agl_dist_init(void)
Definition: agl_dist.c:70
#define AGL_DIST_ID
default sonar
Definition: agl_dist.c:40
#define AGL_DIST_MIN_RANGE
Definition: agl_dist.c:46
abi_event agl_ev
Definition: agl_dist.c:52
Bind to agl ABI message and provide a filtered value to be used in flight plans.
void preflight_error(struct preflight_result_t *result, const char *fmt,...)
Register a preflight error used inside the preflight checking functions.
void preflight_success(struct preflight_result_t *result, const char *fmt,...)
Register a preflight success used inside the preflight checking functions.
void preflight_check_register(struct preflight_check_t *check, preflight_check_f func)
Register a preflight check and add it to the linked list.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
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