Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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  gv_adapt_X = gv_adapt_X0;
117  gv_adapt_P = gv_adapt_P0;
118 }
119 
120 #define K_FRAC 12
121 
127 void gv_adapt_run(int32_t zdd_meas, int32_t thrust_applied, int32_t zd_ref) {
128 
129  static const int32_t gv_adapt_min_cmd = GUIDANCE_V_ADAPT_MIN_CMD * MAX_PPRZ;
130  static const int32_t gv_adapt_max_cmd = GUIDANCE_V_ADAPT_MAX_CMD * MAX_PPRZ;
131  static const int32_t gv_adapt_max_accel = ACCEL_BFP_OF_REAL(GUIDANCE_V_ADAPT_MAX_ACCEL);
132 
133  /* Update only if accel and commands are in a valid range */
134  /* This also ensures we don't divide by zero */
135  if (thrust_applied < gv_adapt_min_cmd || thrust_applied > gv_adapt_max_cmd
136  || zdd_meas < -gv_adapt_max_accel || zdd_meas > gv_adapt_max_accel) {
137  return;
138  }
139 
140  /* We don't propagate state, it's constant ! */
141  /* We propagate our covariance */
142  gv_adapt_P = gv_adapt_P + GV_ADAPT_SYS_NOISE;
143 
144  /* Compute our measurement. If zdd_meas is in the range +/-5g, meas is less than 30 bits */
145  const int32_t g_m_zdd = ((int32_t)BFP_OF_REAL(9.81, INT32_ACCEL_FRAC) - zdd_meas)<<(GV_ADAPT_X_FRAC - INT32_ACCEL_FRAC);
146  if ( g_m_zdd > 0) {
147  gv_adapt_Xmeas = (g_m_zdd + (thrust_applied>>1)) / thrust_applied;
148  } else {
149  gv_adapt_Xmeas = (g_m_zdd - (thrust_applied>>1)) / thrust_applied;
150  }
151 
152  /* Compute a residual */
153  int32_t residual = gv_adapt_Xmeas - gv_adapt_X;
154 
155  /* Covariance Error E = P + R */
157  if (zd_ref < 0) ref = -ref;
159 
160  /* Kalman gain K = P / (P + R) = P / E */
161  int32_t K = (gv_adapt_P<<K_FRAC) / E;
162 
163  /* Update Covariance Pnew = P - K * P */
164  gv_adapt_P = gv_adapt_P - ((K * gv_adapt_P)>>K_FRAC);
165  /* Don't let covariance climb over initial value */
166  if (gv_adapt_P > gv_adapt_P0) {
167  gv_adapt_P = gv_adapt_P0;
168  }
169 
170  /* Update State */
171  gv_adapt_X = gv_adapt_X + (((int64_t)(K * residual))>>K_FRAC);
172 
173  /* Output bounds.
174  * Don't let it climb over a value that would
175  * give less than #GUIDANCE_V_ADAPT_MIN_HOVER_THROTTLE % throttle
176  * or more than #GUIDANCE_V_ADAPT_MAX_HOVER_THROTTLE % throttle.
177  */
178  static const int32_t max_out = BFP_OF_REAL(9.81, GV_ADAPT_X_FRAC) /
180  static const int32_t min_out = BFP_OF_REAL(9.81, GV_ADAPT_X_FRAC) /
182  Bound(gv_adapt_X, min_out, max_out);
183 }
#define GV_ADAPT_P_FRAC
#define K_FRAC
Adaptation block of the vertical guidance.
#define GV_ADAPT_SYS_NOISE
#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 ACCEL_BFP_OF_REAL(_af)
#define GV_ADAPT_X_FRAC
signed long long int64_t
Definition: types.h:21
#define E
#define GUIDANCE_V_ADAPT_MAX_HOVER_THROTTLE
Maximum hover throttle as factor of MAX_PPRZ.
uint16_t ref[TCOUPLE_NB]
#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)
#define BFP_OF_REAL(_vr, _frac)
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 INT32_SPEED_FRAC
#define GUIDANCE_V_ADAPT_MIN_HOVER_THROTTLE
Minimum hover throttle as factor of MAX_PPRZ.
#define MAX_PPRZ
Definition: paparazzi.h:8
#define GUIDANCE_V_ADAPT_INITIAL_HOVER_THROTTLE
Initial hover throttle as factor of MAX_PPRZ.
#define GV_ADAPT_P0_F
#define INT32_ACCEL_FRAC
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.