Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
guidance_v_adpt.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009-2010 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 
30 #ifndef GUIDANCE_V_ADPT
31 #define GUIDANCE_V_ADPT
32 
33 #include "paparazzi.h"
34 
40 #ifndef GUIDANCE_V_ADAPT_NOISE_FACTOR
41 #define GUIDANCE_V_ADAPT_NOISE_FACTOR 1.0
42 #endif
43 
47 #ifndef GUIDANCE_V_ADAPT_MAX_ACCEL
48 #define GUIDANCE_V_ADAPT_MAX_ACCEL 4.0
49 #endif
50 
54 #ifndef GUIDANCE_V_ADAPT_MAX_CMD
55 #define GUIDANCE_V_ADAPT_MAX_CMD 0.9
56 #endif
57 #ifndef GUIDANCE_V_ADAPT_MIN_CMD
58 #define GUIDANCE_V_ADAPT_MIN_CMD 0.1
59 #endif
60 
65 extern int32_t gv_adapt_X;
66 #define GV_ADAPT_X_FRAC 24
67 
72 extern int32_t gv_adapt_P;
73 #define GV_ADAPT_P_FRAC 18
74 
76 extern int32_t gv_adapt_Xmeas;
77 
78 
79 #ifdef GUIDANCE_V_C
80 
84 
85 
86 /* Initial State and Covariance */
87 #define GV_ADAPT_X0_F 0.0015
88 #define GV_ADAPT_X0 BFP_OF_REAL(GV_ADAPT_X0_F, GV_ADAPT_X_FRAC)
89 #define GV_ADAPT_P0_F 0.1
90 #define GV_ADAPT_P0 BFP_OF_REAL(GV_ADAPT_P0_F, GV_ADAPT_P_FRAC)
91 
92 /* System noises */
93 #define GV_ADAPT_SYS_NOISE_F 0.00005
94 #define GV_ADAPT_SYS_NOISE BFP_OF_REAL(GV_ADAPT_SYS_NOISE_F, GV_ADAPT_P_FRAC)
95 
96 /* Measuremement noises */
97 #define GV_ADAPT_MEAS_NOISE_HOVER_F (50.0*GUIDANCE_V_ADAPT_NOISE_FACTOR)
98 #define GV_ADAPT_MEAS_NOISE_HOVER BFP_OF_REAL(GV_ADAPT_MEAS_NOISE_HOVER_F, GV_ADAPT_P_FRAC)
99 #define GV_ADAPT_MEAS_NOISE_OF_ZD (100.0*GUIDANCE_V_ADAPT_NOISE_FACTOR)
100 
101 /* Max accel */
102 #define GV_ADAPT_MAX_ACCEL ACCEL_BFP_OF_REAL(GUIDANCE_V_ADAPT_MAX_ACCEL)
103 
104 /* Command bounds */
105 #define GV_ADAPT_MAX_CMD ((int32_t)(GUIDANCE_V_ADAPT_MAX_CMD*MAX_PPRZ))
106 #define GV_ADAPT_MIN_CMD ((int32_t)(GUIDANCE_V_ADAPT_MIN_CMD*MAX_PPRZ))
107 
108 /* Output bounds.
109  * Don't let it climb over a value that would
110  * give less than zero throttle and don't let it down to zero.
111  * Worst cases:
112  * MIN_ACCEL / MAX_THROTTLE
113  * MAX_ACCEL / MIN_THROTTLE
114  * ex:
115  * 9.81*2^18/255 = 10084
116  * 9.81*2^18/1 = 2571632
117  */
118 // TODO Check this properly
119 #define GV_ADAPT_MAX_OUT (BFP_OF_REAL(9.81, GV_ADAPT_X_FRAC))
120 #define GV_ADAPT_MIN_OUT (BFP_OF_REAL(9.81, GV_ADAPT_X_FRAC) / MAX_PPRZ)
121 
122 
123 static inline void gv_adapt_init(void) {
126 }
127 
128 #define K_FRAC 12
129 
135 static inline void gv_adapt_run(int32_t zdd_meas, int32_t thrust_applied, int32_t zd_ref) {
136 
137  /* Update only if accel and commands are in a valid range */
138  /* This also ensures we don't divide by zero */
139  if (thrust_applied < GV_ADAPT_MIN_CMD || thrust_applied > GV_ADAPT_MAX_CMD
140  || zdd_meas < -GV_ADAPT_MAX_ACCEL || zdd_meas > GV_ADAPT_MAX_ACCEL) {
141  return;
142  }
143 
144  /* We don't propagate state, it's constant ! */
145  /* We propagate our covariance */
147 
148  /* Compute our measurement. If zdd_meas is in the range +/-5g, meas is less than 30 bits */
149  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);
150  if ( g_m_zdd > 0) {
151  gv_adapt_Xmeas = (g_m_zdd + (thrust_applied>>1)) / thrust_applied;
152  } else {
153  gv_adapt_Xmeas = (g_m_zdd - (thrust_applied>>1)) / thrust_applied;
154  }
155 
156  /* Compute a residual */
157  int32_t residual = gv_adapt_Xmeas - gv_adapt_X;
158 
159  /* Covariance Error E = P + R */
161  if (zd_ref < 0) ref = -ref;
163 
164  /* Kalman gain K = P / (P + R) = P / E */
165  int32_t K = (gv_adapt_P<<K_FRAC) / E;
166 
167  /* Update Covariance Pnew = P - K * P */
168  gv_adapt_P = gv_adapt_P - ((K * gv_adapt_P)>>K_FRAC);
169  /* Don't let covariance climb over initial value */
171 
172  /* Update State */
173  gv_adapt_X = gv_adapt_X + (((int64_t)(K * residual))>>K_FRAC);
174 
175  /* Again don't let it climb over a value that would
176  * give less than zero throttle and don't let it down to zero.
177  */
178  Bound(gv_adapt_X, GV_ADAPT_MIN_OUT, GV_ADAPT_MAX_OUT);
179 }
180 
181 
182 #endif /* GUIDANCE_V_C */
183 
184 #endif /* GUIDANCE_V_ADPT */
#define GV_ADAPT_MEAS_NOISE_HOVER
static void gv_adapt_init(void)
#define GV_ADAPT_X0
int32_t gv_adapt_X
State of the estimator.
signed long long int64_t
Definition: types.h:21
#define GV_ADAPT_P0
#define E
int32_t gv_adapt_Xmeas
Measurement.
#define GV_ADAPT_MIN_OUT
uint16_t ref[TCOUPLE_NB]
#define GV_ADAPT_MAX_ACCEL
#define GV_ADAPT_MAX_OUT
#define GV_ADAPT_P_FRAC
int32_t gv_adapt_P
Covariance.
#define BFP_OF_REAL(_vr, _frac)
static void gv_adapt_run(int32_t zdd_meas, int32_t thrust_applied, int32_t zd_ref)
Adaptation function.
signed long int32_t
Definition: types.h:19
#define GV_ADAPT_MEAS_NOISE_OF_ZD
#define GV_ADAPT_SYS_NOISE
#define INT32_SPEED_FRAC
#define GV_ADAPT_MAX_CMD
#define INT32_ACCEL_FRAC
#define K_FRAC
#define GV_ADAPT_X_FRAC