Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros 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 "estimator.h"
31 #include "subsystems/nav.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 
88 
89 // Set higher than 2*V_CTL_ALTITUDE_MAX_CLIMB to disable
90 #define V_CTL_AUTO_CLIMB_LIMIT (0.5/4.0) // m/s/s
91 
93 
94 #if USE_AIRSPEED
95 float v_ctl_auto_airspeed_setpoint;
96 float v_ctl_auto_airspeed_controlled;
105 float v_ctl_auto_groundspeed_setpoint;
106 float v_ctl_auto_groundspeed_pgain;
107 float v_ctl_auto_groundspeed_igain;
108 float v_ctl_auto_groundspeed_sum_err;
109 #define V_CTL_AUTO_AIRSPEED_PITCH_MAX_SUM_ERR (RadOfDeg(15.))
110 #define V_CTL_AUTO_AIRSPEED_THROTTLE_MAX_SUM_ERR 0.2
111 #define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR 100
112 #endif
113 
114 #pragma message "CAUTION! ALL control gains have to be positive now!"
115 
116 void v_ctl_init( void ) {
117  /* mode */
120 
121  /* outer loop */
124  v_ctl_altitude_pgain = V_CTL_ALTITUDE_PGAIN;
126 
127  /* inner loops */
131 
132  /* "auto throttle" inner loop parameters */
133  v_ctl_auto_throttle_nominal_cruise_throttle = V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE;
135  v_ctl_auto_throttle_min_cruise_throttle = V_CTL_AUTO_THROTTLE_MIN_CRUISE_THROTTLE;
136  v_ctl_auto_throttle_max_cruise_throttle = V_CTL_AUTO_THROTTLE_MAX_CRUISE_THROTTLE;
137  v_ctl_auto_throttle_climb_throttle_increment = V_CTL_AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT;
138  v_ctl_auto_throttle_pgain = V_CTL_AUTO_THROTTLE_PGAIN;
139  v_ctl_auto_throttle_igain = V_CTL_AUTO_THROTTLE_IGAIN;
142  v_ctl_auto_throttle_pitch_of_vz_pgain = V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_PGAIN;
144 
145  /* "auto pitch" inner loop parameters */
146  v_ctl_auto_pitch_pgain = V_CTL_AUTO_PITCH_PGAIN;
150 
151 #if USE_AIRSPEED
152  v_ctl_auto_airspeed_setpoint = V_CTL_AUTO_AIRSPEED_SETPOINT;
153  v_ctl_auto_airspeed_controlled = V_CTL_AUTO_AIRSPEED_SETPOINT;
154  v_ctl_auto_airspeed_throttle_pgain = V_CTL_AUTO_AIRSPEED_THROTTLE_PGAIN;
155  v_ctl_auto_airspeed_throttle_dgain = V_CTL_AUTO_AIRSPEED_THROTTLE_DGAIN;
156  v_ctl_auto_airspeed_throttle_igain = V_CTL_AUTO_AIRSPEED_THROTTLE_IGAIN;
158  v_ctl_auto_airspeed_pitch_pgain = V_CTL_AUTO_AIRSPEED_PITCH_PGAIN;
159  v_ctl_auto_airspeed_pitch_dgain = V_CTL_AUTO_AIRSPEED_PITCH_DGAIN;
160  v_ctl_auto_airspeed_pitch_igain = V_CTL_AUTO_AIRSPEED_PITCH_IGAIN;
162 
163  v_ctl_auto_groundspeed_setpoint = V_CTL_AUTO_GROUNDSPEED_SETPOINT;
164  v_ctl_auto_groundspeed_pgain = V_CTL_AUTO_GROUNDSPEED_PGAIN;
165  v_ctl_auto_groundspeed_igain = V_CTL_AUTO_GROUNDSPEED_IGAIN;
166  v_ctl_auto_groundspeed_sum_err = 0.;
167 #endif
168 
169  controlled_throttle = 0.;
171 }
172 
178 // Don't use lead controller unless you know what you're doing
179 #define LEAD_CTRL_TAU 0.8
180 #define LEAD_CTRL_A 1.
181 #define LEAD_CTRL_Te (1./60.)
182 
183 void v_ctl_altitude_loop( void ) {
184  static float v_ctl_climb_setpoint_last = 0.;
185  //static float last_lead_input = 0.;
186 
187  // Altitude error
190 
191  // Lead controller
192  //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);
193  //last_lead_input = pitch_sp;
194 
195  // Limit rate of change of climb setpoint
196  float diff_climb = v_ctl_climb_setpoint - v_ctl_climb_setpoint_last;
197  BoundAbs(diff_climb, V_CTL_AUTO_CLIMB_LIMIT);
198  v_ctl_climb_setpoint = v_ctl_climb_setpoint_last + diff_climb;
199 
200  // Limit climb setpoint
201  BoundAbs(v_ctl_climb_setpoint, V_CTL_ALTITUDE_MAX_CLIMB);
202  v_ctl_climb_setpoint_last = v_ctl_climb_setpoint;
203 }
204 
205 static inline void v_ctl_set_pitch ( void ) {
206  static float last_err = 0.;
207 
208  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0)
210 
211  // Compute errors
213  float d_err = err - last_err;
214  last_err = err;
215 
216  if (v_ctl_auto_pitch_igain > 0.) {
217  v_ctl_auto_pitch_sum_err += err*(1./60.);
219  }
220 
221  // PI loop + feedforward ctl
222  nav_pitch = 0. //nav_pitch FIXME it really sucks !
224  + v_ctl_auto_pitch_pgain * err
225  + v_ctl_auto_pitch_dgain * d_err
227 
228 }
229 
230 static inline void v_ctl_set_throttle( void ) {
231  static float last_err = 0.;
232 
233  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0)
235 
236  // Compute errors
238  float d_err = err - last_err;
239  last_err = err;
240 
241  if (v_ctl_auto_throttle_igain > 0.) {
242  v_ctl_auto_throttle_sum_err += err*(1./60.);
244  }
245 
246  // PID loop + feedforward ctl
250  + v_ctl_auto_throttle_dgain * d_err
252 
253 }
254 
255 #if USE_AIRSPEED
256 #define AIRSPEED_LOOP_PERIOD (1./60.)
257 
258 // Airspeed control loop (input: [airspeed controlled, climb_setpoint], output: [throttle controlled, pitch setpoint])
259 static inline void v_ctl_set_airspeed( void ) {
260  static float last_err_vz = 0.;
261  static float last_err_as = 0.;
262 
263  // Bound airspeed setpoint
264  Bound(v_ctl_auto_airspeed_setpoint, V_CTL_AIRSPEED_MIN, V_CTL_AIRSPEED_MAX);
265 
266  // Compute errors
267  float err_vz = v_ctl_climb_setpoint - estimator_z_dot;
268  float d_err_vz = (err_vz - last_err_vz)*AIRSPEED_LOOP_PERIOD;
269  last_err_vz = err_vz;
270  if (v_ctl_auto_throttle_igain > 0.) {
271  v_ctl_auto_throttle_sum_err += err_vz*AIRSPEED_LOOP_PERIOD;
273  }
274  if (v_ctl_auto_pitch_igain > 0.) {
275  v_ctl_auto_pitch_sum_err += err_vz*AIRSPEED_LOOP_PERIOD;
277  }
278 
279  float err_airspeed = v_ctl_auto_airspeed_setpoint - estimator_airspeed;
280  float d_err_airspeed = (err_airspeed - last_err_as)*AIRSPEED_LOOP_PERIOD;
281  last_err_as = err_airspeed;
283  v_ctl_auto_airspeed_throttle_sum_err += err_airspeed*AIRSPEED_LOOP_PERIOD;
284  BoundAbs(v_ctl_auto_airspeed_throttle_sum_err, V_CTL_AUTO_AIRSPEED_THROTTLE_MAX_SUM_ERR / v_ctl_auto_airspeed_throttle_igain);
285  }
287  v_ctl_auto_airspeed_pitch_sum_err += err_airspeed*AIRSPEED_LOOP_PERIOD;
288  BoundAbs(v_ctl_auto_airspeed_pitch_sum_err, V_CTL_AUTO_AIRSPEED_PITCH_MAX_SUM_ERR / v_ctl_auto_airspeed_pitch_igain);
289  }
290 
291 
292  // Reset integrators in manual or before flight
293  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0) {
298  }
299 
300  // Pitch loop
301  nav_pitch = 0. //nav_pitch FIXME it really sucks !
303  + v_ctl_auto_pitch_pgain * err_vz
304  + v_ctl_auto_pitch_dgain * d_err_vz
306  - v_ctl_auto_airspeed_pitch_pgain * err_airspeed
307  - v_ctl_auto_airspeed_pitch_dgain * d_err_airspeed
309 
310  // Throttle loop
313  + v_ctl_auto_throttle_pgain * err_vz
314  + v_ctl_auto_throttle_dgain * d_err_vz
316  + v_ctl_auto_airspeed_throttle_pgain * err_airspeed
317  + v_ctl_auto_airspeed_throttle_dgain * d_err_airspeed
319 
320 }
321 
322 static inline void v_ctl_set_groundspeed( void ) {
323  // Ground speed control loop (input: groundspeed error, output: airspeed controlled)
324  float err_groundspeed = v_ctl_auto_groundspeed_setpoint - estimator_hspeed_mod;
325  v_ctl_auto_groundspeed_sum_err += err_groundspeed;
326  BoundAbs(v_ctl_auto_groundspeed_sum_err, V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR);
327  v_ctl_auto_airspeed_setpoint = err_groundspeed * v_ctl_auto_groundspeed_pgain + v_ctl_auto_groundspeed_sum_err * v_ctl_auto_groundspeed_igain;
328 
329 }
330 #endif
331 
332 void v_ctl_climb_loop ( void ) {
333 
334  switch (v_ctl_speed_mode) {
336  // Set pitch
337  v_ctl_set_pitch();
338  // Set throttle
340  break;
341 #if USE_AIRSPEED
343  v_ctl_set_airspeed();
344  break;
346  v_ctl_set_groundspeed();
347  v_ctl_set_airspeed();
348  break;
349 #endif
350  default:
352  break;
353  }
354 
355  // Set Pitch output
356  Bound(nav_pitch, V_CTL_AUTO_PITCH_MIN_PITCH, V_CTL_AUTO_PITCH_MAX_PITCH);
357  // Set Throttle output
359 
360 }
361 
362 #ifdef V_CTL_THROTTLE_SLEW_LIMITER
363 #define V_CTL_THROTTLE_SLEW (1./CONTROL_RATE/(V_CTL_THROTTLE_SLEW_LIMITER))
364 #endif
365 
366 #ifndef V_CTL_THROTTLE_SLEW
367 #define V_CTL_THROTTLE_SLEW (1.)
368 #endif
369 
372 void v_ctl_throttle_slew( void ) {
374  BoundAbs(diff_throttle, TRIM_PPRZ(V_CTL_THROTTLE_SLEW*MAX_PPRZ));
375  v_ctl_throttle_slewed += diff_throttle;
376 }
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:183
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:205
bool_t launch
Definition: sim_ap.c:41
float estimator_hspeed_mod
module of horizontal ground speed in m/s
Definition: estimator.c:64
#define V_CTL_CLIMB_MODE_AUTO_THROTTLE
Definition: guidance_v.h:53
float v_ctl_auto_pitch_pgain
Definition: guidance_v_n.c:72
uint8_t pprz_mode
Definition: main_ap.c:111
float v_ctl_auto_airspeed_pitch_sum_err
float estimator_z_dot
Definition: estimator.c:46
void v_ctl_climb_loop(void)
Definition: guidance_v_n.c:332
int16_t pprz_t
Definition: paparazzi.h:6
"New" vertical control for fixed wing vehicles.
float controlled_throttle
Definition: guidance_v_n.c:85
float v_ctl_auto_pitch_sum_err
Definition: guidance_v_n.c:75
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:367
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
#define V_CTL_SPEED_AIRSPEED
Definition: guidance_v_n.h:32
float v_ctl_altitude_pre_climb
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 TRIM_UPPRZ(pprz)
Definition: paparazzi.h:14
#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:92
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_MODE_MANUAL
Definition: guidance_v.h:35
#define V_CTL_AUTO_THROTTLE_DGAIN
Definition: guidance_v_n.c:50
float estimator_airspeed
m/s
Definition: estimator.c:69
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:230
#define V_CTL_SPEED_GROUNDSPEED
Definition: guidance_v_n.h:33
#define V_CTL_AUTO_CLIMB_LIMIT
Definition: guidance_v_n.c:90
float v_ctl_auto_airspeed_throttle_igain
float v_ctl_auto_throttle_climb_throttle_increment
Definition: guidance_v_n.c:58
unsigned char uint8_t
Definition: types.h:14
float estimator_z
altitude above MSL in meters
Definition: estimator.c:44
float v_ctl_auto_throttle_pitch_of_vz_dgain
Definition: guidance_v_n.c:65
float v_ctl_auto_throttle_pitch_of_vz_pgain
Definition: guidance_v_n.c:64
#define V_CTL_SPEED_THROTTLE
Definition: guidance_v_n.h:31
#define V_CTL_AUTO_THROTTLE_STANDARD
Definition: guidance_v.h:57
#define V_CTL_AUTO_PITCH_DGAIN
Definition: guidance_v_n.c:79
State estimation, fusioning sensors.
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:372
void v_ctl_init(void)
Definition: guidance_v_n.c:116
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 PPRZ_MODE_MANUAL
Definition: autopilot.h:44
float v_ctl_auto_airspeed_pitch_dgain
float v_ctl_auto_throttle_pgain
Definition: guidance_v_n.c:59
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
pprz_t v_ctl_throttle_setpoint
Definition: guidance_v_n.c:86
float v_ctl_auto_throttle_dgain
Definition: guidance_v_n.c:61
float v_ctl_auto_throttle_min_cruise_throttle
Definition: guidance_v_n.c:56