Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
airspeed_consistency.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Noah Wechtler <noahwechtler@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 <stdio.h>
29#include "navigation.h"
30#include "state.h"
32#include "generated/modules.h"
33#include "modules/core/abi.h"
36
37#ifndef AIRSPEED_CONSISTENCY_MIN_SAMPLES
38#define AIRSPEED_CONSISTENCY_MIN_SAMPLES 20 // Minimum number of samples before circle fitting starts
39#endif
40
41#ifndef AIRSPEED_CONSISTENCY_INTERVAL
42#define AIRSPEED_CONSISTENCY_INTERVAL 10
43#endif
44
45#if AIRSPEED_CONSISTENCY_BUFFER_SIZE < AIRSPEED_CONSISTENCY_MIN_SAMPLES
46#error "AIRSPEED_CONSISTENCY_BUFFER_SIZE must be larger than or equal to AIRSPEED_CONSISTENCY_MIN_SAMPLES"
47#endif
48
49#if AIRSPEED_CONSISTENCY_BUFFER_SIZE > UINT16_MAX
50#error "AIRSPEED_CONSISTENCY_BUFFER_SIZE must be less than or equal to UINT16_MAX"
51#endif
52
53bool asc_reset = false; // Flag to reset the airspeed consistency check
54
55static void save_speeds(void);
56static void reset_speeds(void);
57static enum CircFitStatus_t get_circle_from_speeds(struct circle_t *c, float *N, float *E);
58
59static struct asc_t asc = {0};
61
64 float tau = 1.0 / (2.0 * M_PI);
66}
67
69 struct FloatVect2 diff;
72
75 // Wipe old data if not flying circles
76 if (asc.i != 0) {
78 }
79 return;
80 }
81
83
86 asc.as_circ_status = get_circle_from_speeds(&asc.as, asc.as_N, asc.as_E);
87 asc.gs_circ_status = get_circle_from_speeds(&asc.gs, asc.gs_N, asc.gs_E);
88 }
89
91 asc.ratio = asc.gs.r / asc.as.r; // Calculate the ratio of the radii of the circles
92 asc.ratio *= asc.ratio; // Square the ratio so it can be multiplied with the current airspeed sensor scale
93 char error_msg[100];
94 int rc = snprintf(error_msg, sizeof(error_msg), "ASC: ratio squared = %.4f", asc.ratio);
95 DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, rc, error_msg);
96#if FLIGHTRECORDER_SDLOG
97 pprz_msg_send_INFO_MSG(&pprzlog_tp.trans_tx, &flightrecorder_sdlog.device, AC_ID, rc, error_msg);
98#endif
99 }
100 });
101}
102
103static void save_speeds(void) {
106
108 float psi = stateGetNedToBodyEulers_f()->psi;
109 asc.as_N[asc.i] = tas_filt.o[0] * cosf(psi);
110 asc.as_E[asc.i] = tas_filt.o[0] * sinf(psi);
111
112 asc.i++;
114 asc.filled = true; // Set to true if the buffer has been filled once
115 asc.i = 0;
116 }
117}
118
119static void reset_speeds(void) {
120 asc.filled = false;
121 asc.i = 0;
124
125 for (int j = 0; j < AIRSPEED_CONSISTENCY_BUFFER_SIZE; j++) {
126 asc.gs_N[j] = NAN;
127 asc.gs_E[j] = NAN;
128 asc.as_N[j] = NAN;
129 asc.as_E[j] = NAN;
130 }
131}
132
133static enum CircFitStatus_t get_circle_from_speeds(struct circle_t *c, float *X, float *Y) {
135 enum CircFitStatus_t status = pprz_circfit_wei_float(c, X, Y, N, NULL); // Feeding the last estimated circle as initial guess leads to NaNs here
136 return status;
137}
138
Main include for ABI (AirBorneInterface).
struct AirData air_data
global AirData state
Definition air_data.c:41
float tas
True Air Speed (TAS) in m/s, -1 if unknown.
Definition air_data.h:45
#define AIRSPEED_CONSISTENCY_INTERVAL
static void save_speeds(void)
static enum CircFitStatus_t get_circle_from_speeds(struct circle_t *c, float *N, float *E)
#define AIRSPEED_CONSISTENCY_MIN_SAMPLES
static struct asc_t asc
bool asc_reset
void airspeed_consistency_init(void)
static Butterworth2LowPass tas_filt
void airspeed_consistency_reset(bool reset)
void airspeed_consistency_periodic(void)
static void reset_speeds(void)
enum CircFitStatus_t gs_circ_status
float gs_N[AIRSPEED_CONSISTENCY_BUFFER_SIZE]
float gs_E[AIRSPEED_CONSISTENCY_BUFFER_SIZE]
float as_E[AIRSPEED_CONSISTENCY_BUFFER_SIZE]
enum CircFitStatus_t as_circ_status
float as_N[AIRSPEED_CONSISTENCY_BUFFER_SIZE]
#define AIRSPEED_CONSISTENCY_BUFFER_SIZE
struct circle_t as
static uint8_t status
static uint8_t reset[3]
if(GpsFixValid() &&e_identification_started)
float psi
in radians
static float float_vect2_norm(struct FloatVect2 *v)
#define VECT2_DIFF(_c, _a, _b)
#define N
#define E
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
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.
uint16_t foo
Definition main_demo5.c:58
#define NAVIGATION_FREQUENCY
Default fixedwing navigation frequency.
Definition nav.h:49
struct EnuCoor_f center
center WP position
Definition nav_base.h:46
struct NavCircle_t circle
Definition nav_base.h:64
float radius
radius in meters
Definition nav_base.h:47
struct NavBase_t nav_rotorcraft_base
Basic Nav struct.
enum CircFitStatus_t pprz_circfit_wei_float(struct circle_t *c, const float *x, const float *y, uint16_t n, struct circle_t *g)
CircFitStatus_t
@ CIRC_FIT_OK
@ CIRC_FIT_ERROR
float x
in meters
float y
in meters
struct RotorcraftNavigation nav
Definition navigation.c:51
#define NAV_HORIZONTAL_MODE_CIRCLE
Definition navigation.h:87
#define ARRIVED_AT_WAYPOINT
minimum horizontal distance to waypoint to mark as arrived
Definition navigation.h:55
API to get/set the generic vehicle states.
Periodic telemetry system header (includes downlink utility and generated code).
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.