Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
energy_ctrl.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 TUDelft, Tobias Muench
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
65#include "state.h"
67#include "generated/airframe.h"
68#include "autopilot.h"
69#include "modules/core/abi.h"
70
72
73/* mode */
78
79#ifdef LOITER_TRIM
80#error "Energy Controller can not accept Loiter Trim"
81#endif
82//#ifdef V_CTL_AUTO_THROTTLE_MIN_CRUISE_THROTTLE
83//#error
84
86
87/* outer loop */
93
96
97/* inner loop */
99
100/* "auto throttle" inner loop parameters */
102
108
114
117
120
124
129#define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR 100
130
134
135uint8_t v_ctl_speed_mode; //To be compatible with universal flightplan, not used for etecs
136
138
140
142#ifndef V_CTL_ALTITUDE_MAX_CLIMB
143#define V_CTL_ALTITUDE_MAX_CLIMB 2;
144INFO("V_CTL_ALTITUDE_MAX_CLIMB not defined - default is 2 , indicating 2 m/s")
145#endif
146#ifndef STALL_AIRSPEED
147INFO("No STALL_AIRSPEED defined. Using NOMINAL_AIRSPEED")
148#define STALL_AIRSPEED NOMINAL_AIRSPEED
149#endif
150#ifndef V_CTL_GLIDE_RATIO
151#define V_CTL_GLIDE_RATIO 8.
152INFO("V_CTL_GLIDE_RATIO not defined - default is 8.")
153#endif
154#ifndef AIRSPEED_SETPOINT_SLEW
155#define AIRSPEED_SETPOINT_SLEW 1
156#endif
157#ifndef V_CTL_MAX_ACCELERATION
158#define V_CTL_MAX_ACCELERATION 0.5 //G
159#endif
160
161#ifndef V_CTL_ENERGY_IMU_ID
162#define V_CTL_ENERGY_IMU_ID ABI_BROADCAST
163#endif
165// Automatically found airplane characteristics
166
168float ac_char_climb_max = 0.0f;
176
177static void ac_char_average(float *last_v, float new_v, int count)
178{
179 *last_v = (((*last_v) * (((float)count) - 1.0f)) + new_v) / ((float) count);
180}
181
182static void ac_char_update(float throttle, float pitch, float climb, float accelerate)
183{
184 if ((accelerate > -0.02) && (accelerate < 0.02)) {
185 if (throttle >= 1.0f) {
189 } else if (throttle <= 0.0f) {
193 } else if ((climb > -0.125) && (climb < 0.125)) {
197 }
198 }
199}
200
202 uint32_t stamp __attribute__((unused)),
203 struct Int32Vect3 *accel)
204{
205 accel_imu_meas = *accel;
206}
207
208void v_ctl_init(void)
209{
210 /* mode */
212 v_ctl_speed_mode = V_CTL_SPEED_THROTTLE; //There is only one, added here to be universal in flightplan for different control modes
213
214 /* outer loop */
218
219#ifdef V_CTL_AUTO_THROTTLE_NOMINAL_CRUISE_PITCH
221#else
223#endif
224
228
230
231 /* inner loops */
233
234 /* "auto throttle" inner loop parameters */
240
244
245
246#ifdef V_CTL_ENERGY_TOT_PGAIN
251#else
256#warning "V_CTL_ENERGY_TOT GAINS are not defined and set to 0"
257#endif
258
259#ifdef V_CTL_ALTITUDE_MAX_CLIMB
261#else
262 v_ctl_max_climb = 2;
263#warning "V_CTL_ALTITUDE_MAX_CLIMB not defined - default is 2m/s"
264#endif
265
266#ifdef V_CTL_AUTO_GROUNDSPEED_SETPOINT
271#endif
272
274
276}
277
278static const float dt_attidude = 1.0 / ((float)CONTROL_FREQUENCY);
279static const float dt_navigation = 1.0 / ((float)NAVIGATION_FREQUENCY);
280
287{
288 // Airspeed Command Saturation
290
291 // Altitude Controller
294
295 // Vertical Speed Limiter
297
298 // Vertical Acceleration Limiter
299 float incr = sp - v_ctl_climb_setpoint;
300 BoundAbs(incr, 2 * dt_navigation);
301 v_ctl_climb_setpoint += incr;
302}
303
304
305// Running Average Filter
306float lp_vdot[5];
307
308static float low_pass_vdot(float v);
309static float low_pass_vdot(float v)
310{
311 lp_vdot[4] += (v - lp_vdot[4]) / 3;
312 lp_vdot[3] += (lp_vdot[4] - lp_vdot[3]) / 3;
313 lp_vdot[2] += (lp_vdot[3] - lp_vdot[2]) / 3;
314 lp_vdot[1] += (lp_vdot[2] - lp_vdot[1]) / 3;
315 lp_vdot[0] += (lp_vdot[1] - lp_vdot[0]) / 3;
316
317 return lp_vdot[0];
318}
319
325{
326 // Airspeed setpoint rate limiter:
327 // AIRSPEED_SETPOINT_SLEW in m/s/s - a change from 15m/s to 18m/s takes 3s with the default value of 1
331
332#ifdef V_CTL_AUTO_GROUNDSPEED_SETPOINT
333// Ground speed control loop (input: groundspeed error, output: airspeed controlled)
339
340 // Do not allow controlled airspeed below the setpoint
343 // reset integrator of ground speed loop
346 }
347#else
349#endif
350
351 // Airspeed outerloop: positive means we need to accelerate
353
354 // Speed Controller to PseudoControl: gain 1 -> 5m/s error = 0.5g acceleration
355 v_ctl_desired_acceleration = speed_error * v_ctl_airspeed_pgain / 9.81f;
357
358 // Actual Acceleration from IMU: attempt to reconstruct the actual kinematic acceleration
359#ifndef SITL
360 /* convert last imu accel measurement to float */
363 float vdot = accel_meas_body.x / 9.81f - sinf(stateGetNedToBodyEulers_f()->theta);
364#else
365 float vdot = 0;
366#endif
367
368 // Acceleration Error: positive means UAV needs to accelerate: needs extra energy
370
371 // Flight Path Outerloop: positive means needs to climb more: needs extra energy
373
374 // Total Energy Error: positive means energy should be added
375 float en_tot_err = gamma_err + vdot_err;
376
377 // Energy Distribution Error: positive means energy should go from overspeed to altitude = pitch up
378 float en_dis_err = gamma_err - vdot_err;
379
380 // Auto Cruise Throttle
386 }
387
388 // Total Controller
393
394 if ((controlled_throttle >= 1.0f) || (controlled_throttle <= 0.0f) || (autopilot_throttle_killed() == 1)) {
395 // If your energy supply is not sufficient, then neglect the climb requirement
397
398 // adjust climb_setpoint to maintain airspeed in case of an engine failure or an unrestriced climb
401 }
402
403
404 /* pitch pre-command */
409 }
410 float v_ctl_pitch_of_vz =
411 + (v_ctl_climb_setpoint /*+ d_err * v_ctl_auto_throttle_pitch_of_vz_dgain*/) * v_ctl_auto_throttle_pitch_of_vz_pgain
417
420
422
424}
425
426
427#ifdef V_CTL_THROTTLE_SLEW_LIMITER
428#define V_CTL_THROTTLE_SLEW (1./CONTROL_FREQUENCY/(V_CTL_THROTTLE_SLEW_LIMITER))
429#endif
430
431#ifndef V_CTL_THROTTLE_SLEW
432#define V_CTL_THROTTLE_SLEW 1.
433#endif
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition abi_common.h:67
struct pprz_autopilot autopilot
Global autopilot structure.
Definition autopilot.c:49
bool autopilot_throttle_killed(void)
get kill status
Definition autopilot.c:313
Core autopilot interface common to all firmwares.
bool launch
request launch
Definition autopilot.h:71
float ac_char_climb_max
float v_ctl_energy_total_pgain
float v_ctl_auto_pitch_of_airspeed_igain
pprz_t v_ctl_throttle_setpoint
int ac_char_cruise_count
static void ac_char_update(float throttle, float pitch, float climb, float accelerate)
float v_ctl_desired_acceleration
float v_ctl_auto_groundspeed_sum_err
float v_ctl_auto_throttle_climb_throttle_increment
int ac_char_descend_count
void v_ctl_climb_loop(void)
Auto-throttle inner loop.
float ac_char_climb_pitch
float v_ctl_auto_airspeed_controlled
float v_ctl_auto_throttle_nominal_cruise_pitch
static abi_event accel_ev
float v_ctl_max_climb
Definition energy_ctrl.c:94
uint8_t v_ctl_climb_mode
Definition energy_ctrl.c:75
float v_ctl_energy_total_igain
static struct Int32Vect3 accel_imu_meas
uint8_t v_ctl_auto_throttle_submode
Definition energy_ctrl.c:76
float v_ctl_auto_groundspeed_igain
float v_ctl_auto_pitch_of_airspeed_dgain
float v_ctl_altitude_pgain
Definition energy_ctrl.c:90
float ac_char_cruise_throttle
float v_ctl_pitch_setpoint
float ac_char_descend_max
void v_ctl_throttle_slew(void)
Computes slewed throttle from throttle setpoint called at 20Hz.
float v_ctl_energy_diff_pgain
float v_ctl_auto_throttle_sum_err
Definition energy_ctrl.c:77
float v_ctl_max_acceleration
Definition energy_ctrl.c:95
float v_ctl_altitude_setpoint
in meters above MSL
Definition energy_ctrl.c:88
float ac_char_cruise_pitch
pprz_t v_ctl_throttle_slewed
static const float dt_navigation
static const float dt_attidude
#define V_CTL_AUTO_GROUNDSPEED_MAX_SUM_ERR
float v_ctl_auto_throttle_nominal_cruise_throttle
float v_ctl_auto_throttle_of_airspeed_igain
static void ac_char_average(float *last_v, float new_v, int count)
#define V_CTL_ALTITUDE_MAX_CLIMB
uint8_t v_ctl_speed_mode
#define STALL_AIRSPEED
#define V_CTL_ENERGY_IMU_ID
static void accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel)
float lp_vdot[5]
float v_ctl_auto_airspeed_setpoint_slew
float v_ctl_airspeed_pgain
Definition energy_ctrl.c:91
float v_ctl_auto_throttle_pitch_of_vz_pgain
#define AIRSPEED_SETPOINT_SLEW
void v_ctl_altitude_loop(void)
outer loop
#define V_CTL_THROTTLE_SLEW
float v_ctl_auto_throttle_cruise_throttle
void v_ctl_init(void)
float v_ctl_altitude_pre_climb
Path Angle.
Definition energy_ctrl.c:89
float v_ctl_auto_airspeed_setpoint
in meters per second
static float low_pass_vdot(float v)
float v_ctl_altitude_error
in meters, (setpoint - alt) -> positive = too low
Definition energy_ctrl.c:92
float v_ctl_auto_pitch_of_airspeed_pgain
float v_ctl_auto_groundspeed_pgain
float ac_char_descend_pitch
int ac_char_climb_count
float v_ctl_auto_groundspeed_setpoint
in meters per second
#define V_CTL_GLIDE_RATIO
float v_ctl_climb_setpoint
Definition energy_ctrl.c:98
float v_ctl_auto_throttle_of_airspeed_pgain
#define V_CTL_MAX_ACCELERATION
uint8_t v_ctl_mode
Definition energy_ctrl.c:74
float v_ctl_energy_diff_igain
Vertical control using total energy control for fixed wing vehicles.
#define V_CTL_SPEED_THROTTLE
Definition energy_ctrl.h:36
#define CONTROL_FREQUENCY
#define ACCELS_FLOAT_OF_BFP(_ef, _ei)
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition state.h:1306
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition state.h:821
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition state.h:1076
static struct EnuCoor_f * stateGetSpeedEnu_f(void)
Get ground speed in local ENU coordinates (float).
Definition state.h:1058
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition state.h:1590
#define V_CTL_MODE_MANUAL
#define V_CTL_CLIMB_MODE_AUTO_THROTTLE
#define V_CTL_MODE_AUTO_CLIMB
float controlled_throttle
uint16_t foo
Definition main_demo5.c:58
float nav_pitch
Definition nav.c:310
Fixedwing Navigation library.
#define NAVIGATION_FREQUENCY
Default fixedwing navigation frequency.
Definition nav.h:49
#define TRIM_UPPRZ(pprz)
Definition paparazzi.h:14
int16_t pprz_t
Definition paparazzi.h:6
#define MAX_PPRZ
Definition paparazzi.h:8
#define TRIM_PPRZ(pprz)
Definition paparazzi.h:11
float alt
in meters (above WGS84 reference ellipsoid or above MSL)
float z
in meters
API to get/set the generic vehicle states.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.