Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
guidance_v_n.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 ENAC
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 #include "state.h"
32 #include "generated/airframe.h"
34 
35 /* mode */
37 
38 /* outer loop */
43 
44 /* inner loop */
48 
49 #ifndef V_CTL_AUTO_THROTTLE_DGAIN
50 #define V_CTL_AUTO_THROTTLE_DGAIN 0.
51 #endif
52 
53 /* "auto throttle" inner loop parameters */
63 #define V_CTL_AUTO_THROTTLE_MAX_SUM_ERR 0.4
66 
67 #ifndef V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_DGAIN
68 #define V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_DGAIN 0.
69 #endif
70 
71 /* "auto pitch" inner loop parameters */
76 #define V_CTL_AUTO_PITCH_MAX_SUM_ERR (RadOfDeg(10.))
77 
78 #ifndef V_CTL_AUTO_PITCH_DGAIN
79 #define V_CTL_AUTO_PITCH_DGAIN 0.
80 #endif
81 #ifndef V_CTL_AUTO_PITCH_IGAIN
82 #define V_CTL_AUTO_PITCH_IGAIN 0.
83 #endif
84 
89 #ifndef V_CTL_PITCH_TRIM
90 #define V_CTL_PITCH_TRIM 0.
91 #endif
93 
94 // Set higher than 2*V_CTL_ALTITUDE_MAX_CLIMB to disable
95 #ifndef V_CTL_AUTO_CLIMB_LIMIT
96 #define V_CTL_AUTO_CLIMB_LIMIT (0.5/4.0) // m/s/s
97 #endif
98 
100 
101 #if USE_AIRSPEED
116 #define V_CTL_AUTO_AIRSPEED_PITCH_MAX_SUM_ERR (RadOfDeg(15.))
117 #define V_CTL_AUTO_AIRSPEED_THROTTLE_MAX_SUM_ERR 0.2
118 #define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR 100
119 #endif
120 
121 void v_ctl_init(void)
122 {
123  /* mode */
126 
127  /* outer loop */
130  v_ctl_altitude_pgain = V_CTL_ALTITUDE_PGAIN;
132 
133  /* inner loops */
137 
140 
141  /* "auto throttle" inner loop parameters */
142  v_ctl_auto_throttle_nominal_cruise_throttle = V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE;
144  v_ctl_auto_throttle_min_cruise_throttle = V_CTL_AUTO_THROTTLE_MIN_CRUISE_THROTTLE;
145  v_ctl_auto_throttle_max_cruise_throttle = V_CTL_AUTO_THROTTLE_MAX_CRUISE_THROTTLE;
146  v_ctl_auto_throttle_climb_throttle_increment = V_CTL_AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT;
147  v_ctl_auto_throttle_pgain = V_CTL_AUTO_THROTTLE_PGAIN;
148  v_ctl_auto_throttle_igain = V_CTL_AUTO_THROTTLE_IGAIN;
151  v_ctl_auto_throttle_pitch_of_vz_pgain = V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_PGAIN;
153 
154  /* "auto pitch" inner loop parameters */
155  v_ctl_auto_pitch_pgain = V_CTL_AUTO_PITCH_PGAIN;
159 
160 #if USE_AIRSPEED
161  v_ctl_auto_airspeed_setpoint = V_CTL_AUTO_AIRSPEED_SETPOINT;
162  v_ctl_auto_airspeed_controlled = V_CTL_AUTO_AIRSPEED_SETPOINT;
163  v_ctl_auto_airspeed_throttle_pgain = V_CTL_AUTO_AIRSPEED_THROTTLE_PGAIN;
164  v_ctl_auto_airspeed_throttle_dgain = V_CTL_AUTO_AIRSPEED_THROTTLE_DGAIN;
165  v_ctl_auto_airspeed_throttle_igain = V_CTL_AUTO_AIRSPEED_THROTTLE_IGAIN;
167  v_ctl_auto_airspeed_pitch_pgain = V_CTL_AUTO_AIRSPEED_PITCH_PGAIN;
168  v_ctl_auto_airspeed_pitch_dgain = V_CTL_AUTO_AIRSPEED_PITCH_DGAIN;
169  v_ctl_auto_airspeed_pitch_igain = V_CTL_AUTO_AIRSPEED_PITCH_IGAIN;
171 
172  v_ctl_auto_groundspeed_setpoint = V_CTL_AUTO_GROUNDSPEED_SETPOINT;
173  v_ctl_auto_groundspeed_pgain = V_CTL_AUTO_GROUNDSPEED_PGAIN;
174  v_ctl_auto_groundspeed_igain = V_CTL_AUTO_GROUNDSPEED_IGAIN;
176 #endif
177 
178  controlled_throttle = 0.;
180 }
181 
187 // Don't use lead controller unless you know what you're doing
188 #define LEAD_CTRL_TAU 0.8
189 #define LEAD_CTRL_A 1.
190 #define LEAD_CTRL_Te (1./60.)
191 
193 {
194  static float v_ctl_climb_setpoint_last = 0.;
195  //static float last_lead_input = 0.;
196 
197  // Altitude error
200 
201  // Lead controller
202  //v_ctl_climb_setpoint = ((LEAD_CTRL_TAU*LEAD_CTRL_A + LEAD_CTRL_Te)*climb_sp + LEAD_CTRL_TAU*(v_ctl_climb_setpoint - LEAD_CTRL_A*last_lead_input)) / (LEAD_CTRL_TAU + LEAD_CTRL_Te);
203  //last_lead_input = pitch_sp;
204 
205  // Limit rate of change of climb setpoint
206  float diff_climb = v_ctl_climb_setpoint - v_ctl_climb_setpoint_last;
207  BoundAbs(diff_climb, V_CTL_AUTO_CLIMB_LIMIT);
208  v_ctl_climb_setpoint = v_ctl_climb_setpoint_last + diff_climb;
209 
210  // Limit climb setpoint
212  v_ctl_climb_setpoint_last = v_ctl_climb_setpoint;
213 }
214 
215 static inline void v_ctl_set_pitch(void)
216 {
217  static float last_err = 0.;
218 
219  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0) {
221  }
222 
223  // Compute errors
224  float err = v_ctl_climb_setpoint - stateGetSpeedEnu_f()->z;
225  float d_err = err - last_err;
226  last_err = err;
227 
228  if (v_ctl_auto_pitch_igain > 0.) {
229  v_ctl_auto_pitch_sum_err += err * (1. / 60.);
231  }
232 
233  // PI loop + feedforward ctl
237  + v_ctl_auto_pitch_pgain * err
238  + v_ctl_auto_pitch_dgain * d_err
240 
241 }
242 
243 static inline void v_ctl_set_throttle(void)
244 {
245  static float last_err = 0.;
246 
247  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0) {
249  }
250 
251  // Compute errors
252  float err = v_ctl_climb_setpoint - stateGetSpeedEnu_f()->z;
253  float d_err = err - last_err;
254  last_err = err;
255 
256  if (v_ctl_auto_throttle_igain > 0.) {
257  v_ctl_auto_throttle_sum_err += err * (1. / 60.);
259  }
260 
261  // PID loop + feedforward ctl
265  + v_ctl_auto_throttle_dgain * d_err
267 
268 }
269 
270 #if USE_AIRSPEED
271 #define AIRSPEED_LOOP_PERIOD (1./60.)
272 
273 // Airspeed control loop (input: [airspeed controlled, climb_setpoint], output: [throttle controlled, pitch setpoint])
274 static inline void v_ctl_set_airspeed(void)
275 {
276  static float last_err_vz = 0.;
277  static float last_err_as = 0.;
278 
279  // Bound airspeed setpoint
280  Bound(v_ctl_auto_airspeed_setpoint, V_CTL_AIRSPEED_MIN, V_CTL_AIRSPEED_MAX);
281 
282  // Compute errors
283  float err_vz = v_ctl_climb_setpoint - stateGetSpeedEnu_f()->z;
284  float d_err_vz = (err_vz - last_err_vz) * AIRSPEED_LOOP_PERIOD;
285  last_err_vz = err_vz;
286  if (v_ctl_auto_throttle_igain > 0.) {
287  v_ctl_auto_throttle_sum_err += err_vz * AIRSPEED_LOOP_PERIOD;
289  }
290  if (v_ctl_auto_pitch_igain > 0.) {
291  v_ctl_auto_pitch_sum_err += err_vz * AIRSPEED_LOOP_PERIOD;
293  }
294 
295  float err_airspeed = v_ctl_auto_airspeed_setpoint - stateGetAirspeed_f();
296  float d_err_airspeed = (err_airspeed - last_err_as) * AIRSPEED_LOOP_PERIOD;
297  last_err_as = err_airspeed;
299  v_ctl_auto_airspeed_throttle_sum_err += err_airspeed * AIRSPEED_LOOP_PERIOD;
301  V_CTL_AUTO_AIRSPEED_THROTTLE_MAX_SUM_ERR / v_ctl_auto_airspeed_throttle_igain);
302  }
304  v_ctl_auto_airspeed_pitch_sum_err += err_airspeed * AIRSPEED_LOOP_PERIOD;
305  BoundAbs(v_ctl_auto_airspeed_pitch_sum_err, V_CTL_AUTO_AIRSPEED_PITCH_MAX_SUM_ERR / v_ctl_auto_airspeed_pitch_igain);
306  }
307 
308 
309  // Reset integrators in manual or before flight
310  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0) {
315  }
316 
317  // Pitch loop
321  + v_ctl_auto_pitch_pgain * err_vz
322  + v_ctl_auto_pitch_dgain * d_err_vz
324  - v_ctl_auto_airspeed_pitch_pgain * err_airspeed
325  - v_ctl_auto_airspeed_pitch_dgain * d_err_airspeed
327 
328  // Throttle loop
331  + v_ctl_auto_throttle_pgain * err_vz
332  + v_ctl_auto_throttle_dgain * d_err_vz
334  + v_ctl_auto_airspeed_throttle_pgain * err_airspeed
335  + v_ctl_auto_airspeed_throttle_dgain * d_err_airspeed
337 
338 }
339 
340 static inline void v_ctl_set_groundspeed(void)
341 {
342  // Ground speed control loop (input: groundspeed error, output: airspeed controlled)
344  v_ctl_auto_groundspeed_sum_err += err_groundspeed;
348 
349 }
350 #endif
351 
353 {
354 
355  switch (v_ctl_speed_mode) {
357  // Set pitch
358  v_ctl_set_pitch();
359  // Set throttle
361  break;
362 #if USE_AIRSPEED
364  v_ctl_set_airspeed();
365  break;
367  v_ctl_set_groundspeed();
368  v_ctl_set_airspeed();
369  break;
370 #endif
371  default:
373  break;
374  }
375 
376  // Set Pitch output
377  Bound(v_ctl_pitch_setpoint, V_CTL_AUTO_PITCH_MIN_PITCH, V_CTL_AUTO_PITCH_MAX_PITCH);
378  // Set Throttle output
380 
381 }
382 
383 #ifdef V_CTL_THROTTLE_SLEW_LIMITER
384 #define V_CTL_THROTTLE_SLEW (1./CONTROL_FREQUENCY/(V_CTL_THROTTLE_SLEW_LIMITER))
385 #endif
386 
387 #ifndef V_CTL_THROTTLE_SLEW
388 #define V_CTL_THROTTLE_SLEW (1.)
389 #endif
390 
394 {
396  BoundAbs(diff_throttle, TRIM_PPRZ(V_CTL_THROTTLE_SLEW * MAX_PPRZ));
397  v_ctl_throttle_slewed += diff_throttle;
398 }
float v_ctl_auto_pitch_dgain
Definition: guidance_v_n.c:73
void v_ctl_altitude_loop(void)
outer loop
Definition: guidance_v_n.c:192
float v_ctl_auto_throttle_nominal_cruise_throttle
Definition: guidance_v_n.c:55
float v_ctl_auto_airspeed_pitch_igain
static void v_ctl_set_pitch(void)
Definition: guidance_v_n.c:215
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition: state.h:923
float v_ctl_auto_pitch_pgain
Definition: guidance_v_n.c:72
float alt
in meters (above WGS84 reference ellipsoid or above MSL)
float v_ctl_auto_airspeed_pitch_sum_err
float v_ctl_pitch_trim
Definition: guidance_v_n.c:92
float v_ctl_auto_groundspeed_setpoint
in meters per second
Definition: energy_ctrl.c:125
float v_ctl_auto_groundspeed_igain
Definition: energy_ctrl.c:127
void v_ctl_climb_loop(void)
Auto-throttle inner loop.
Definition: guidance_v_n.c:352
uint8_t pprz_mode
Definition: autopilot.c:41
int16_t pprz_t
Definition: paparazzi.h:6
#define V_CTL_ALTITUDE_MAX_CLIMB
Definition: energy_ctrl.c:145
"New" vertical control for fixed wing vehicles.
bool launch
Definition: sim_ap.c:38
float controlled_throttle
Definition: guidance_v_n.c:85
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition: state.h:1389
float v_ctl_auto_pitch_sum_err
Definition: guidance_v_n.c:75
#define V_CTL_PITCH_TRIM
Definition: guidance_v_n.c:90
float v_ctl_auto_throttle_max_cruise_throttle
Definition: guidance_v_n.c:57
uint8_t v_ctl_climb_mode
Definition: guidance_v_n.c:46
#define V_CTL_AUTO_PITCH_MAX_SUM_ERR
Definition: guidance_v_n.c:76
#define V_CTL_THROTTLE_SLEW
Definition: guidance_v_n.c:388
float v_ctl_altitude_setpoint
in meters above MSL
Definition: guidance_v_n.c:39
float v_ctl_altitude_error
in meters, (setpoint - alt) -> positive = too low
Definition: guidance_v_n.c:42
float v_ctl_auto_airspeed_setpoint
in meters per second
Definition: energy_ctrl.c:121
#define V_CTL_SPEED_AIRSPEED
Definition: guidance_v_n.h:32
float v_ctl_altitude_pre_climb
Path Angle.
Definition: guidance_v_n.c:40
float v_ctl_climb_setpoint
Definition: guidance_v_n.c:45
float v_ctl_auto_airspeed_throttle_sum_err
#define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR
Definition: energy_ctrl.c:129
#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:128
#define V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_DGAIN
Definition: guidance_v_n.c:68
uint8_t v_ctl_speed_mode
Definition: guidance_v_n.c:99
#define V_CTL_CLIMB_MODE_AUTO_THROTTLE
Vertical control for fixed wing vehicles.
float v_ctl_auto_throttle_sum_err
Definition: guidance_v_n.c:62
float v_ctl_altitude_pgain
Definition: guidance_v_n.c:41
#define V_CTL_AUTO_THROTTLE_DGAIN
Definition: guidance_v_n.c:50
float v_ctl_auto_throttle_igain
Definition: guidance_v_n.c:60
float v_ctl_auto_airspeed_throttle_pgain
float v_ctl_auto_airspeed_throttle_dgain
#define V_CTL_AUTO_PITCH_IGAIN
Definition: guidance_v_n.c:82
uint8_t v_ctl_mode
Definition: guidance_v_n.c:36
static void v_ctl_set_throttle(void)
Definition: guidance_v_n.c:243
#define V_CTL_SPEED_GROUNDSPEED
Definition: guidance_v_n.h:33
float v_ctl_pitch_setpoint
Definition: guidance_v_n.c:88
#define V_CTL_AUTO_CLIMB_LIMIT
Definition: guidance_v_n.c:96
float v_ctl_auto_airspeed_throttle_igain
float v_ctl_auto_throttle_climb_throttle_increment
Definition: guidance_v_n.c:58
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:686
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
float v_ctl_auto_throttle_pitch_of_vz_dgain
Definition: guidance_v_n.c:65
static struct EnuCoor_f * stateGetSpeedEnu_f(void)
Get ground speed in local ENU coordinates (float).
Definition: state.h:905
float v_ctl_auto_throttle_pitch_of_vz_pgain
Definition: guidance_v_n.c:64
float z
in meters
#define V_CTL_SPEED_THROTTLE
Definition: guidance_v_n.h:31
#define V_CTL_AUTO_PITCH_DGAIN
Definition: guidance_v_n.c:79
uint8_t v_ctl_auto_throttle_submode
Definition: guidance_v_n.c:47
float v_ctl_auto_throttle_cruise_throttle
Definition: guidance_v_n.c:54
void v_ctl_throttle_slew(void)
Computes slewed throttle from throttle setpoint called at 20Hz.
Definition: guidance_v_n.c:393
void v_ctl_init(void)
Definition: guidance_v_n.c:121
float v_ctl_auto_airspeed_pitch_pgain
#define V_CTL_AUTO_THROTTLE_MAX_SUM_ERR
Definition: guidance_v_n.c:63
#define MAX_PPRZ
Definition: paparazzi.h:8
#define V_CTL_MODE_MANUAL
#define PPRZ_MODE_MANUAL
AP modes.
Definition: autopilot.h:50
float v_ctl_auto_airspeed_pitch_dgain
float v_ctl_auto_throttle_pgain
Definition: guidance_v_n.c:59
float v_ctl_auto_groundspeed_pgain
Definition: energy_ctrl.c:126
pprz_t v_ctl_throttle_slewed
Definition: guidance_v_n.c:87
float v_ctl_auto_pitch_igain
Definition: guidance_v_n.c:74
#define TRIM_PPRZ(pprz)
Definition: paparazzi.h:11
float v_ctl_auto_airspeed_controlled
Definition: energy_ctrl.c:123
pprz_t v_ctl_throttle_setpoint
Definition: guidance_v_n.c:86
float v_ctl_auto_throttle_dgain
Definition: guidance_v_n.c:61
Fixedwing autopilot modes.
float v_ctl_auto_throttle_min_cruise_throttle
Definition: guidance_v_n.c:56