Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
guidance_v.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Pascal Brisset, Antoine Drouin, Michel Gorraz
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 
29 #include "state.h"
30 #include "subsystems/nav.h"
31 #include "generated/airframe.h"
33 
34 /* mode */
36 
37 /* outer loop */
44 
45 /* inner loop */
49 
50 #ifndef V_CTL_AUTO_THROTTLE_DGAIN
51 #define V_CTL_AUTO_THROTTLE_DGAIN 0.
52 #endif
53 
54 /* "auto throttle" inner loop parameters */
64 #define V_CTL_AUTO_THROTTLE_MAX_SUM_ERR 150
67 
68 #ifndef V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_DGAIN
69 #define V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_DGAIN 0.
70 #endif
71 
72 /* agressive tuning */
73 #ifdef TUNE_AGRESSIVE_CLIMB
74 float agr_climb_throttle;
75 float agr_climb_pitch;
76 float agr_climb_nav_ratio;
77 float agr_descent_throttle;
78 float agr_descent_pitch;
79 float agr_descent_nav_ratio;
80 #endif
81 
82 /* "auto pitch" inner loop parameters */
86 #define V_CTL_AUTO_PITCH_MAX_SUM_ERR 100
87 
91 
92 inline static void v_ctl_climb_auto_throttle_loop( void );
93 #ifdef V_CTL_AUTO_PITCH_PGAIN
94 inline static void v_ctl_climb_auto_pitch_loop( void );
95 #endif
96 
97 #if USE_AIRSPEED
100 float v_ctl_auto_airspeed_pgain;
101 float v_ctl_auto_airspeed_igain;
102 float v_ctl_auto_airspeed_sum_err;
107 #define V_CTL_AUTO_AIRSPEED_MAX_SUM_ERR 200
108 #define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR 100
109 #define V_CTL_AUTO_CLIMB_LIMIT 0.5/4.0 // m/s/s
110 #define V_CTL_AUTO_AGR_CLIMB_GAIN 2.0 // altitude gain multiplier while in aggressive climb mode
111 #endif
112 
113 #ifndef V_CTL_ALTITUDE_PRE_CLIMB_CORRECTION
114 #define V_CTL_ALTITUDE_PRE_CLIMB_CORRECTION 1.0f
115 #endif
116 
117 
118 void v_ctl_init( void ) {
119  /* mode */
121 
122  /* outer loop */
125  v_ctl_altitude_pgain = V_CTL_ALTITUDE_PGAIN;
129 
130  /* inner loops */
134 
135  /* "auto throttle" inner loop parameters */
136  v_ctl_auto_throttle_nominal_cruise_throttle = V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE;
137  v_ctl_auto_throttle_min_cruise_throttle = V_CTL_AUTO_THROTTLE_MIN_CRUISE_THROTTLE;
138  v_ctl_auto_throttle_max_cruise_throttle = V_CTL_AUTO_THROTTLE_MAX_CRUISE_THROTTLE;
140  v_ctl_auto_throttle_climb_throttle_increment = V_CTL_AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT;
141  v_ctl_auto_throttle_pgain = V_CTL_AUTO_THROTTLE_PGAIN;
142  v_ctl_auto_throttle_igain = V_CTL_AUTO_THROTTLE_IGAIN;
145  v_ctl_auto_throttle_pitch_of_vz_pgain = V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_PGAIN;
147 
148 #ifdef V_CTL_AUTO_PITCH_PGAIN
149  /* "auto pitch" inner loop parameters */
150  v_ctl_auto_pitch_pgain = V_CTL_AUTO_PITCH_PGAIN;
153 #endif
154 
155 #if USE_AIRSPEED
156  v_ctl_auto_airspeed_setpoint = V_CTL_AUTO_AIRSPEED_SETPOINT;
157  v_ctl_auto_airspeed_controlled = V_CTL_AUTO_AIRSPEED_SETPOINT;
158  v_ctl_auto_airspeed_pgain = V_CTL_AUTO_AIRSPEED_PGAIN;
159  v_ctl_auto_airspeed_igain = V_CTL_AUTO_AIRSPEED_IGAIN;
160  v_ctl_auto_airspeed_sum_err = 0.;
161 
162  v_ctl_auto_groundspeed_setpoint = V_CTL_AUTO_GROUNDSPEED_SETPOINT;
163  v_ctl_auto_groundspeed_pgain = V_CTL_AUTO_GROUNDSPEED_PGAIN;
164  v_ctl_auto_groundspeed_igain = V_CTL_AUTO_GROUNDSPEED_IGAIN;
166 #endif
167 
169 
170 /*agressive tuning*/
171 #ifdef TUNE_AGRESSIVE_CLIMB
172  agr_climb_throttle = AGR_CLIMB_THROTTLE;
173  #undef AGR_CLIMB_THROTTLE
174  #define AGR_CLIMB_THROTTLE agr_climb_throttle
175  agr_climb_pitch = AGR_CLIMB_PITCH;
176  #undef AGR_CLIMB_PITCH
177  #define AGR_CLIMB_PITCH agr_climb_pitch
178  agr_climb_nav_ratio = AGR_CLIMB_NAV_RATIO;
179  #undef AGR_CLIMB_NAV_RATIO
180  #define AGR_CLIMB_NAV_RATIO agr_climb_nav_ratio
181  agr_descent_throttle = AGR_DESCENT_THROTTLE;
182  #undef AGR_DESCENT_THROTTLE
183  #define AGR_DESCENT_THROTTLE agr_descent_throttle
184  agr_descent_pitch = AGR_DESCENT_PITCH;
185  #undef AGR_DESCENT_PITCH
186  #define AGR_DESCENT_PITCH agr_descent_pitch
187  agr_descent_nav_ratio = AGR_DESCENT_NAV_RATIO;
188  #undef AGR_DESCENT_NAV_RATIO
189  #define AGR_DESCENT_NAV_RATIO agr_descent_nav_ratio
190 #endif
191 }
192 
197 void v_ctl_altitude_loop( void ) {
198  float altitude_pgain_boost = 1.0;
199 
200 #if USE_AIRSPEED && defined(AGR_CLIMB)
201  // Aggressive climb mode (boost gain of altitude loop)
203  float dist = fabs(v_ctl_altitude_error);
204  altitude_pgain_boost = 1.0 + (V_CTL_AUTO_AGR_CLIMB_GAIN-1.0)*(dist-AGR_BLEND_END)/(AGR_BLEND_START-AGR_BLEND_END);
205  Bound(altitude_pgain_boost, 1.0, V_CTL_AUTO_AGR_CLIMB_GAIN);
206  }
207 #endif
208 
213 
214 #ifdef AGR_CLIMB
216  float dist = fabs(v_ctl_altitude_error);
217  if (dist < AGR_BLEND_END) {
219  }
220  else if (dist > AGR_BLEND_START) {
222  }
223  else {
225  }
226  }
227 #endif
228 }
229 
230 void v_ctl_climb_loop ( void ) {
231  switch (v_ctl_climb_mode) {
233  default:
235  break;
236 #ifdef V_CTL_AUTO_PITCH_PGAIN
237 #pragma message "AUTO PITCH Enabled!"
239  v_ctl_climb_auto_pitch_loop();
240  break;
241 #endif
242  }
243 }
244 
250 #if !USE_AIRSPEED
251 
252 inline static void v_ctl_climb_auto_throttle_loop(void) {
253  static float last_err;
254 
255  float f_throttle = 0;
256  float err = stateGetSpeedEnu_f()->z - v_ctl_climb_setpoint;
257  float d_err = err - last_err;
258  last_err = err;
263  + v_ctl_auto_throttle_dgain * d_err);
264 
265  /* pitch pre-command */
267 
268 #if defined AGR_CLIMB
269  switch (v_ctl_auto_throttle_submode) {
271  if (v_ctl_climb_setpoint > 0) { /* Climbing */
272  f_throttle = AGR_CLIMB_THROTTLE;
273  v_ctl_pitch_setpoint = AGR_CLIMB_PITCH;
274  }
275  else { /* Going down */
276  f_throttle = AGR_DESCENT_THROTTLE;
277  v_ctl_pitch_setpoint = AGR_DESCENT_PITCH;
278  }
279  break;
280 
282  float ratio = (fabs(v_ctl_altitude_error) - AGR_BLEND_END)
283  / (AGR_BLEND_START - AGR_BLEND_END);
284  f_throttle = (1-ratio) * controlled_throttle;
285  v_ctl_pitch_setpoint = (1-ratio) * v_ctl_pitch_of_vz;
286  v_ctl_auto_throttle_sum_err += (1-ratio) * err;
288  /* positive error -> too low */
289  if (v_ctl_altitude_error > 0) {
290  f_throttle += ratio * AGR_CLIMB_THROTTLE;
291  v_ctl_pitch_setpoint += ratio * AGR_CLIMB_PITCH;
292  } else {
293  f_throttle += ratio * AGR_DESCENT_THROTTLE;
294  v_ctl_pitch_setpoint += ratio * AGR_DESCENT_PITCH;
295  }
296  break;
297  }
298 
300  default:
301 #endif
302  f_throttle = controlled_throttle;
305  v_ctl_pitch_setpoint = v_ctl_pitch_of_vz + nav_pitch;
306 #if defined AGR_CLIMB
307  break;
308  } /* switch submode */
309 #endif
310 
312 }
313 
314 #else // USE_AIRSPEED
315 
316 inline static void v_ctl_climb_auto_throttle_loop(void) {
317  float f_throttle = 0;
318  float controlled_throttle;
319  float v_ctl_pitch_of_vz;
320 
321  // Limit rate of change of climb setpoint (to ensure that airspeed loop can catch-up)
322  static float v_ctl_climb_setpoint_last = 0;
323  float diff_climb = v_ctl_climb_setpoint - v_ctl_climb_setpoint_last;
324  Bound(diff_climb, -V_CTL_AUTO_CLIMB_LIMIT, V_CTL_AUTO_CLIMB_LIMIT);
325  v_ctl_climb_setpoint = v_ctl_climb_setpoint_last + diff_climb;
326  v_ctl_climb_setpoint_last = v_ctl_climb_setpoint;
327 
328  // Pitch control (input: rate of climb error, output: pitch setpoint)
329  float err = stateGetSpeedEnu_f()->z - v_ctl_climb_setpoint;
332  v_ctl_pitch_of_vz = -v_ctl_auto_pitch_pgain *
334 
335  // Ground speed control loop (input: groundspeed error, output: airspeed controlled)
336  float err_groundspeed = (v_ctl_auto_groundspeed_setpoint - *stateGetHorizontalSpeedNorm_f());
337  v_ctl_auto_groundspeed_sum_err += err_groundspeed;
340 
341  // Do not allow controlled airspeed below the setpoint
345  }
346 
347  // Airspeed control loop (input: airspeed controlled, output: throttle controlled)
348  float err_airspeed = (v_ctl_auto_airspeed_controlled - *stateGetAirspeed_f());
349  v_ctl_auto_airspeed_sum_err += err_airspeed;
350  BoundAbs(v_ctl_auto_airspeed_sum_err, V_CTL_AUTO_AIRSPEED_MAX_SUM_ERR);
351  controlled_throttle = (err_airspeed + v_ctl_auto_airspeed_sum_err * v_ctl_auto_airspeed_igain) * v_ctl_auto_airspeed_pgain;
352 
353  // Done, set outputs
354  Bound(controlled_throttle, 0, v_ctl_auto_throttle_max_cruise_throttle);
355  f_throttle = controlled_throttle;
356  v_ctl_pitch_setpoint = v_ctl_pitch_of_vz;
358  Bound(v_ctl_pitch_setpoint, V_CTL_AUTO_PITCH_MIN_PITCH, V_CTL_AUTO_PITCH_MAX_PITCH);
359 }
360 
361 #endif // USE_AIRSPEED
362 
363 
368 #ifdef V_CTL_AUTO_PITCH_PGAIN
369 inline static void v_ctl_climb_auto_pitch_loop(void) {
370  float err = stateGetSpeedEnu_f()->z - v_ctl_climb_setpoint;
376  Bound(v_ctl_pitch_setpoint, V_CTL_AUTO_PITCH_MIN_PITCH, V_CTL_AUTO_PITCH_MAX_PITCH);
377 }
378 #endif
379 
380 #ifdef V_CTL_THROTTLE_SLEW_LIMITER
381 #define V_CTL_THROTTLE_SLEW (1./CONTROL_FREQUENCY/(V_CTL_THROTTLE_SLEW_LIMITER))
382 #endif
383 
384 #ifndef V_CTL_THROTTLE_SLEW
385 #define V_CTL_THROTTLE_SLEW 1.
386 #endif
387 
390 void v_ctl_throttle_slew( void ) {
392  BoundAbs(diff_throttle, TRIM_PPRZ(V_CTL_THROTTLE_SLEW*MAX_PPRZ));
393  v_ctl_throttle_slewed += diff_throttle;
394 }
#define V_CTL_AUTO_THROTTLE_DGAIN
Definition: guidance_v.c:51
#define V_CTL_AUTO_PITCH_MAX_SUM_ERR
Definition: guidance_v.c:86
float v_ctl_auto_throttle_pgain
Definition: guidance_v.c:60
pprz_t v_ctl_throttle_slewed
Definition: guidance_v.c:89
float v_ctl_altitude_max_climb
Definition: guidance_v.c:43
float v_ctl_auto_groundspeed_setpoint
in meters per second
Definition: energy_ctrl.c:128
float v_ctl_auto_groundspeed_igain
Definition: energy_ctrl.c:130
float v_ctl_auto_throttle_sum_err
Definition: guidance_v.c:63
uint8_t v_ctl_climb_mode
Definition: guidance_v.c:47
float v_ctl_auto_throttle_igain
Definition: guidance_v.c:61
int16_t pprz_t
Definition: paparazzi.h:6
static float * stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition: state.h:854
#define V_CTL_ALTITUDE_MAX_CLIMB
Definition: energy_ctrl.c:141
float z
in meters
float controlled_throttle
Definition: guidance_v_n.c:85
void v_ctl_altitude_loop(void)
outer loop
Definition: guidance_v.c:197
float v_ctl_altitude_pre_climb
Path Angle.
Definition: guidance_v.c:39
float v_ctl_auto_throttle_climb_throttle_increment
Definition: guidance_v.c:59
float v_ctl_auto_throttle_pitch_of_vz_dgain
Definition: guidance_v.c:66
float v_ctl_auto_throttle_nominal_cruise_throttle
Definition: guidance_v.c:56
float v_ctl_auto_throttle_max_cruise_throttle
Definition: guidance_v.c:58
float v_ctl_auto_airspeed_setpoint
in meters per second
Definition: energy_ctrl.c:124
uint8_t v_ctl_auto_throttle_submode
Definition: guidance_v.c:48
static float * stateGetAirspeed_f(void)
Get airspeed (float).
Definition: state.h:1199
static void v_ctl_climb_auto_throttle_loop(void)
auto throttle inner loop
Definition: guidance_v.c:252
#define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR
Definition: energy_ctrl.c:132
#define V_CTL_AUTO_THROTTLE_STANDARD
#define TRIM_UPPRZ(pprz)
Definition: paparazzi.h:14
float v_ctl_auto_groundspeed_sum_err
Definition: energy_ctrl.c:131
float v_ctl_auto_pitch_pgain
Definition: guidance_v.c:83
#define V_CTL_CLIMB_MODE_AUTO_THROTTLE
Vertical control for fixed wing vehicles.
void v_ctl_throttle_slew(void)
Computes slewed throttle from throttle setpoint called at 20Hz.
Definition: guidance_v.c:390
float v_ctl_altitude_error
in meters, (setpoint - alt) -> positive = too low
Definition: guidance_v.c:41
#define V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_DGAIN
Definition: guidance_v.c:69
#define V_CTL_AUTO_PITCH_IGAIN
Definition: guidance_v_n.c:82
float v_ctl_auto_pitch_sum_err
Definition: guidance_v.c:85
float v_ctl_auto_throttle_min_cruise_throttle
Definition: guidance_v.c:57
float v_ctl_auto_pitch_igain
Definition: guidance_v.c:84
float v_ctl_pitch_setpoint
Definition: guidance_v.c:90
pprz_t v_ctl_throttle_setpoint
Definition: guidance_v.c:88
#define V_CTL_AUTO_CLIMB_LIMIT
Definition: guidance_v_n.c:92
float v_ctl_altitude_pgain
Definition: guidance_v.c:40
float v_ctl_altitude_setpoint
in meters above MSL
Definition: guidance_v.c:38
float v_ctl_auto_throttle_cruise_throttle
Definition: guidance_v.c:55
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:651
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
void v_ctl_init(void)
Definition: guidance_v.c:118
static struct EnuCoor_f * stateGetSpeedEnu_f(void)
Get ground speed in local ENU coordinates (float).
Definition: state.h:840
#define V_CTL_AUTO_THROTTLE_MAX_SUM_ERR
Definition: guidance_v.c:64
void v_ctl_climb_loop(void)
auto throttle inner loop
Definition: guidance_v.c:230
float v_ctl_climb_setpoint
Definition: guidance_v.c:46
#define V_CTL_AUTO_THROTTLE_BLENDED
#define V_CTL_AUTO_THROTTLE_AGRESSIVE
float v_ctl_auto_throttle_pitch_of_vz_pgain
Definition: guidance_v.c:65
#define V_CTL_ALTITUDE_PRE_CLIMB_CORRECTION
Definition: guidance_v.c:114
uint8_t v_ctl_mode
Definition: guidance_v.c:35
#define MAX_PPRZ
Definition: paparazzi.h:8
#define V_CTL_CLIMB_MODE_AUTO_PITCH
#define V_CTL_MODE_MANUAL
float v_ctl_auto_groundspeed_pgain
Definition: energy_ctrl.c:129
#define V_CTL_THROTTLE_SLEW
auto pitch inner loop
Definition: guidance_v.c:385
float alt
in meters above WGS84 reference ellipsoid
#define TRIM_PPRZ(pprz)
Definition: paparazzi.h:11
float v_ctl_auto_airspeed_controlled
Definition: energy_ctrl.c:126
float v_ctl_auto_throttle_dgain
Definition: guidance_v.c:62
float v_ctl_altitude_pre_climb_correction
Definition: guidance_v.c:42
Fixedwing autopilot modes.