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_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 #ifndef V_CTL_AUTO_CLIMB_LIMIT
91 #define V_CTL_AUTO_CLIMB_LIMIT (0.5/4.0) // m/s/s
92 #endif
93 
95 
96 #if USE_AIRSPEED
111 #define V_CTL_AUTO_AIRSPEED_PITCH_MAX_SUM_ERR (RadOfDeg(15.))
112 #define V_CTL_AUTO_AIRSPEED_THROTTLE_MAX_SUM_ERR 0.2
113 #define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR 100
114 #endif
115 
116 #pragma message "CAUTION! ALL control gains have to be positive now!"
117 
118 void v_ctl_init( void ) {
119  /* mode */
122 
123  /* outer loop */
126  v_ctl_altitude_pgain = V_CTL_ALTITUDE_PGAIN;
128 
129  /* inner loops */
133 
134  /* "auto throttle" inner loop parameters */
135  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;
139  v_ctl_auto_throttle_climb_throttle_increment = V_CTL_AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT;
140  v_ctl_auto_throttle_pgain = V_CTL_AUTO_THROTTLE_PGAIN;
141  v_ctl_auto_throttle_igain = V_CTL_AUTO_THROTTLE_IGAIN;
144  v_ctl_auto_throttle_pitch_of_vz_pgain = V_CTL_AUTO_THROTTLE_PITCH_OF_VZ_PGAIN;
146 
147  /* "auto pitch" inner loop parameters */
148  v_ctl_auto_pitch_pgain = V_CTL_AUTO_PITCH_PGAIN;
152 
153 #if USE_AIRSPEED
154  v_ctl_auto_airspeed_setpoint = V_CTL_AUTO_AIRSPEED_SETPOINT;
155  v_ctl_auto_airspeed_controlled = V_CTL_AUTO_AIRSPEED_SETPOINT;
156  v_ctl_auto_airspeed_throttle_pgain = V_CTL_AUTO_AIRSPEED_THROTTLE_PGAIN;
157  v_ctl_auto_airspeed_throttle_dgain = V_CTL_AUTO_AIRSPEED_THROTTLE_DGAIN;
158  v_ctl_auto_airspeed_throttle_igain = V_CTL_AUTO_AIRSPEED_THROTTLE_IGAIN;
160  v_ctl_auto_airspeed_pitch_pgain = V_CTL_AUTO_AIRSPEED_PITCH_PGAIN;
161  v_ctl_auto_airspeed_pitch_dgain = V_CTL_AUTO_AIRSPEED_PITCH_DGAIN;
162  v_ctl_auto_airspeed_pitch_igain = V_CTL_AUTO_AIRSPEED_PITCH_IGAIN;
164 
165  v_ctl_auto_groundspeed_setpoint = V_CTL_AUTO_GROUNDSPEED_SETPOINT;
166  v_ctl_auto_groundspeed_pgain = V_CTL_AUTO_GROUNDSPEED_PGAIN;
167  v_ctl_auto_groundspeed_igain = V_CTL_AUTO_GROUNDSPEED_IGAIN;
169 #endif
170 
171  controlled_throttle = 0.;
173 }
174 
180 // Don't use lead controller unless you know what you're doing
181 #define LEAD_CTRL_TAU 0.8
182 #define LEAD_CTRL_A 1.
183 #define LEAD_CTRL_Te (1./60.)
184 
185 void v_ctl_altitude_loop( void ) {
186  static float v_ctl_climb_setpoint_last = 0.;
187  //static float last_lead_input = 0.;
188 
189  // Altitude error
192 
193  // Lead controller
194  //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);
195  //last_lead_input = pitch_sp;
196 
197  // Limit rate of change of climb setpoint
198  float diff_climb = v_ctl_climb_setpoint - v_ctl_climb_setpoint_last;
199  BoundAbs(diff_climb, V_CTL_AUTO_CLIMB_LIMIT);
200  v_ctl_climb_setpoint = v_ctl_climb_setpoint_last + diff_climb;
201 
202  // Limit climb setpoint
204  v_ctl_climb_setpoint_last = v_ctl_climb_setpoint;
205 }
206 
207 static inline void v_ctl_set_pitch ( void ) {
208  static float last_err = 0.;
209 
210  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0)
212 
213  // Compute errors
215  float d_err = err - last_err;
216  last_err = err;
217 
218  if (v_ctl_auto_pitch_igain > 0.) {
219  v_ctl_auto_pitch_sum_err += err*(1./60.);
221  }
222 
223  // PI loop + feedforward ctl
224  nav_pitch = 0. //nav_pitch FIXME it really sucks !
226  + v_ctl_auto_pitch_pgain * err
227  + v_ctl_auto_pitch_dgain * d_err
229 
230 }
231 
232 static inline void v_ctl_set_throttle( void ) {
233  static float last_err = 0.;
234 
235  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0)
237 
238  // Compute errors
240  float d_err = err - last_err;
241  last_err = err;
242 
243  if (v_ctl_auto_throttle_igain > 0.) {
244  v_ctl_auto_throttle_sum_err += err*(1./60.);
246  }
247 
248  // PID loop + feedforward ctl
252  + v_ctl_auto_throttle_dgain * d_err
254 
255 }
256 
257 #if USE_AIRSPEED
258 #define AIRSPEED_LOOP_PERIOD (1./60.)
259 
260 // Airspeed control loop (input: [airspeed controlled, climb_setpoint], output: [throttle controlled, pitch setpoint])
261 static inline void v_ctl_set_airspeed( void ) {
262  static float last_err_vz = 0.;
263  static float last_err_as = 0.;
264 
265  // Bound airspeed setpoint
266  Bound(v_ctl_auto_airspeed_setpoint, V_CTL_AIRSPEED_MIN, V_CTL_AIRSPEED_MAX);
267 
268  // Compute errors
269  float err_vz = v_ctl_climb_setpoint - estimator_z_dot;
270  float d_err_vz = (err_vz - last_err_vz)*AIRSPEED_LOOP_PERIOD;
271  last_err_vz = err_vz;
272  if (v_ctl_auto_throttle_igain > 0.) {
273  v_ctl_auto_throttle_sum_err += err_vz*AIRSPEED_LOOP_PERIOD;
275  }
276  if (v_ctl_auto_pitch_igain > 0.) {
277  v_ctl_auto_pitch_sum_err += err_vz*AIRSPEED_LOOP_PERIOD;
279  }
280 
281  float err_airspeed = v_ctl_auto_airspeed_setpoint - estimator_airspeed;
282  float d_err_airspeed = (err_airspeed - last_err_as)*AIRSPEED_LOOP_PERIOD;
283  last_err_as = err_airspeed;
285  v_ctl_auto_airspeed_throttle_sum_err += err_airspeed*AIRSPEED_LOOP_PERIOD;
286  BoundAbs(v_ctl_auto_airspeed_throttle_sum_err, V_CTL_AUTO_AIRSPEED_THROTTLE_MAX_SUM_ERR / v_ctl_auto_airspeed_throttle_igain);
287  }
289  v_ctl_auto_airspeed_pitch_sum_err += err_airspeed*AIRSPEED_LOOP_PERIOD;
290  BoundAbs(v_ctl_auto_airspeed_pitch_sum_err, V_CTL_AUTO_AIRSPEED_PITCH_MAX_SUM_ERR / v_ctl_auto_airspeed_pitch_igain);
291  }
292 
293 
294  // Reset integrators in manual or before flight
295  if (pprz_mode == PPRZ_MODE_MANUAL || launch == 0) {
300  }
301 
302  // Pitch loop
303  nav_pitch = 0. //nav_pitch FIXME it really sucks !
305  + v_ctl_auto_pitch_pgain * err_vz
306  + v_ctl_auto_pitch_dgain * d_err_vz
308  - v_ctl_auto_airspeed_pitch_pgain * err_airspeed
309  - v_ctl_auto_airspeed_pitch_dgain * d_err_airspeed
311 
312  // Throttle loop
315  + v_ctl_auto_throttle_pgain * err_vz
316  + v_ctl_auto_throttle_dgain * d_err_vz
318  + v_ctl_auto_airspeed_throttle_pgain * err_airspeed
319  + v_ctl_auto_airspeed_throttle_dgain * d_err_airspeed
321 
322 }
323 
324 static inline void v_ctl_set_groundspeed( void ) {
325  // Ground speed control loop (input: groundspeed error, output: airspeed controlled)
326  float err_groundspeed = v_ctl_auto_groundspeed_setpoint - estimator_hspeed_mod;
327  v_ctl_auto_groundspeed_sum_err += err_groundspeed;
330 
331 }
332 #endif
333 
334 void v_ctl_climb_loop ( void ) {
335 
336  switch (v_ctl_speed_mode) {
338  // Set pitch
339  v_ctl_set_pitch();
340  // Set throttle
342  break;
343 #if USE_AIRSPEED
345  v_ctl_set_airspeed();
346  break;
348  v_ctl_set_groundspeed();
349  v_ctl_set_airspeed();
350  break;
351 #endif
352  default:
354  break;
355  }
356 
357  // Set Pitch output
358  Bound(nav_pitch, V_CTL_AUTO_PITCH_MIN_PITCH, V_CTL_AUTO_PITCH_MAX_PITCH);
359  // Set Throttle output
361 
362 }
363 
364 #ifdef V_CTL_THROTTLE_SLEW_LIMITER
365 #define V_CTL_THROTTLE_SLEW (1./CONTROL_RATE/(V_CTL_THROTTLE_SLEW_LIMITER))
366 #endif
367 
368 #ifndef V_CTL_THROTTLE_SLEW
369 #define V_CTL_THROTTLE_SLEW (1.)
370 #endif
371 
374 void v_ctl_throttle_slew( void ) {
376  BoundAbs(diff_throttle, TRIM_PPRZ(V_CTL_THROTTLE_SLEW*MAX_PPRZ));
377  v_ctl_throttle_slewed += diff_throttle;
378 }
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:185
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:207
bool_t launch
Definition: sim_ap.c:41
float estimator_hspeed_mod
module of horizontal ground speed in m/s
Definition: estimator.c:64
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
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
void v_ctl_climb_loop(void)
Definition: guidance_v_n.c:334
int16_t pprz_t
Definition: paparazzi.h:6
#define V_CTL_ALTITUDE_MAX_CLIMB
Definition: energy_ctrl.c:140
"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:369
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:124
#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: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
#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:94
#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 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:232
#define V_CTL_SPEED_GROUNDSPEED
Definition: guidance_v_n.h:33
#define V_CTL_AUTO_CLIMB_LIMIT
Definition: guidance_v_n.c:91
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_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:374
void v_ctl_init(void)
Definition: guidance_v_n.c:118
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
Definition: autopilot.h:44
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:129
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:126
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