Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
guidance_v_adapt.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009-2013 The Paparazzi Team
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 
31 #include "paparazzi.h"
32 #include "math/pprz_algebra_int.h"
33 #include "generated/airframe.h"
34 
35 
42 #ifndef GUIDANCE_V_ADAPT_INITIAL_HOVER_THROTTLE
43 #define GUIDANCE_V_ADAPT_INITIAL_HOVER_THROTTLE 0.3
44 #endif
46 
47 
51 #ifndef GUIDANCE_V_ADAPT_MIN_HOVER_THROTTLE
52 #define GUIDANCE_V_ADAPT_MIN_HOVER_THROTTLE 0.2
53 #endif
55 
56 
60 #ifndef GUIDANCE_V_ADAPT_MAX_HOVER_THROTTLE
61 #define GUIDANCE_V_ADAPT_MAX_HOVER_THROTTLE 0.75
62 #endif
64 
65 
70 #ifndef GUIDANCE_V_ADAPT_NOISE_FACTOR
71 #define GUIDANCE_V_ADAPT_NOISE_FACTOR 1.0
72 #endif
73 
74 
78 #ifndef GUIDANCE_V_ADAPT_MAX_ACCEL
79 #define GUIDANCE_V_ADAPT_MAX_ACCEL 4.0
80 #endif
81 
85 #ifndef GUIDANCE_V_ADAPT_MAX_CMD
86 #define GUIDANCE_V_ADAPT_MAX_CMD 0.9
87 #endif
88 #ifndef GUIDANCE_V_ADAPT_MIN_CMD
89 #define GUIDANCE_V_ADAPT_MIN_CMD 0.1
90 #endif
91 
92 
93 
97 
98 /* System noises */
99 #ifndef GV_ADAPT_SYS_NOISE_F
100 #define GV_ADAPT_SYS_NOISE_F 0.00005
101 #endif
102 #define GV_ADAPT_SYS_NOISE BFP_OF_REAL(GV_ADAPT_SYS_NOISE_F, GV_ADAPT_P_FRAC)
103 
104 /* Measuremement noises */
105 #define GV_ADAPT_MEAS_NOISE_HOVER_F (50.0*GUIDANCE_V_ADAPT_NOISE_FACTOR)
106 #define GV_ADAPT_MEAS_NOISE_HOVER BFP_OF_REAL(GV_ADAPT_MEAS_NOISE_HOVER_F, GV_ADAPT_P_FRAC)
107 #define GV_ADAPT_MEAS_NOISE_OF_ZD (100.0*GUIDANCE_V_ADAPT_NOISE_FACTOR)
108 
109 /* Initial Covariance */
110 #define GV_ADAPT_P0_F 0.1
114 
115 void gv_adapt_init(void)
116 {
117  gv_adapt_X = gv_adapt_X0;
118  gv_adapt_P = gv_adapt_P0;
119 }
120 
121 #define K_FRAC 12
122 
128 void gv_adapt_run(int32_t zdd_meas, int32_t thrust_applied, int32_t zd_ref)
129 {
130 
131  static const int32_t gv_adapt_min_cmd = GUIDANCE_V_ADAPT_MIN_CMD * MAX_PPRZ;
132  static const int32_t gv_adapt_max_cmd = GUIDANCE_V_ADAPT_MAX_CMD * MAX_PPRZ;
133  static const int32_t gv_adapt_max_accel = ACCEL_BFP_OF_REAL(GUIDANCE_V_ADAPT_MAX_ACCEL);
134 
135  /* Update only if accel and commands are in a valid range */
136  /* This also ensures we don't divide by zero */
137  if (thrust_applied < gv_adapt_min_cmd || thrust_applied > gv_adapt_max_cmd
138  || zdd_meas < -gv_adapt_max_accel || zdd_meas > gv_adapt_max_accel) {
139  return;
140  }
141 
142  /* We don't propagate state, it's constant ! */
143  /* We propagate our covariance */
144  gv_adapt_P = gv_adapt_P + GV_ADAPT_SYS_NOISE;
145 
146  /* Compute our measurement. If zdd_meas is in the range +/-5g, meas is less than 30 bits */
147  const int32_t g_m_zdd = ((int32_t)BFP_OF_REAL(9.81,
149  if (g_m_zdd > 0) {
150  gv_adapt_Xmeas = (g_m_zdd + (thrust_applied >> 1)) / thrust_applied;
151  } else {
152  gv_adapt_Xmeas = (g_m_zdd - (thrust_applied >> 1)) / thrust_applied;
153  }
154 
155  /* Compute a residual */
156  int32_t residual = gv_adapt_Xmeas - gv_adapt_X;
157 
158  /* Covariance Error E = P + R */
160  if (zd_ref < 0) { ref = -ref; }
162 
163  /* Kalman gain K = P / (P + R) = P / E */
164  int32_t K = (gv_adapt_P << K_FRAC) / E;
165 
166  /* Update Covariance Pnew = P - K * P */
167  gv_adapt_P = gv_adapt_P - ((K * gv_adapt_P) >> K_FRAC);
168  /* Don't let covariance climb over initial value */
169  if (gv_adapt_P > gv_adapt_P0) {
170  gv_adapt_P = gv_adapt_P0;
171  }
172 
173  /* Update State */
174  gv_adapt_X = gv_adapt_X + (((int64_t)(K * residual)) >> K_FRAC);
175 
176  /* Output bounds.
177  * Don't let it climb over a value that would
178  * give less than #GUIDANCE_V_ADAPT_MIN_HOVER_THROTTLE % throttle
179  * or more than #GUIDANCE_V_ADAPT_MAX_HOVER_THROTTLE % throttle.
180  */
181  static const int32_t max_out = BFP_OF_REAL(9.81, GV_ADAPT_X_FRAC) /
183  static const int32_t min_out = BFP_OF_REAL(9.81, GV_ADAPT_X_FRAC) /
185  Bound(gv_adapt_X, min_out, max_out);
186 }
#define GV_ADAPT_P_FRAC
#define K_FRAC
Adaptation block of the vertical guidance.
#define INT32_ACCEL_FRAC
#define GV_ADAPT_SYS_NOISE
#define INT32_SPEED_FRAC
#define GUIDANCE_V_ADAPT_MAX_CMD
Filter is not fed if command values are out of a % of 0/MAX_PPRZ.
#define GV_ADAPT_MEAS_NOISE_HOVER
#define GV_ADAPT_X_FRAC
signed long long int64_t
Definition: types.h:21
#define GUIDANCE_V_ADAPT_MAX_HOVER_THROTTLE
Maximum hover throttle as factor of MAX_PPRZ.
uint16_t ref[TCOUPLE_NB]
#define BFP_OF_REAL(_vr, _frac)
#define GUIDANCE_V_ADAPT_MIN_CMD
static const int32_t gv_adapt_P0
#define GV_ADAPT_MEAS_NOISE_OF_ZD
void gv_adapt_init(void)
static const int32_t gv_adapt_X0
void gv_adapt_run(int32_t zdd_meas, int32_t thrust_applied, int32_t zd_ref)
Adaptation function.
int32_t gv_adapt_P
Covariance.
signed long int32_t
Definition: types.h:19
int32_t gv_adapt_Xmeas
Measurement.
#define E
#define GUIDANCE_V_ADAPT_MIN_HOVER_THROTTLE
Minimum hover throttle as factor of MAX_PPRZ.
#define MAX_PPRZ
Definition: paparazzi.h:8
#define ACCEL_BFP_OF_REAL(_af)
#define GUIDANCE_V_ADAPT_INITIAL_HOVER_THROTTLE
Initial hover throttle as factor of MAX_PPRZ.
#define GV_ADAPT_P0_F
int32_t gv_adapt_X
State of the estimator.
#define GUIDANCE_V_ADAPT_MAX_ACCEL
Filter is not fed if accel values are more than +/- MAX_ACCEL.
Paparazzi fixed point algebra.