Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
stabilization_adaptive.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009-2010 The Paparazzi Team
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
68#include "std.h"
69#include "led.h"
72#include "state.h"
74#include "generated/airframe.h"
75#include CTRL_TYPE_H
76#include "autopilot.h"
77
78
79/* outer loop parameters */
80float h_ctl_course_setpoint; /* rad, CW/north */
86
87/* roll and pitch disabling */
89
90/* AUTO1 rate mode */
92
95 float roll_rate;
101 float yaw_rate;
103
104 float max_p;
106 float max_q;
108 float max_r;
110};
111
113
114/* Reference generator */
115#ifndef H_CTL_REF_W_P
116#define H_CTL_REF_W_P 5.
117#endif
118#ifndef H_CTL_REF_XI_P
119#define H_CTL_REF_XI_P 0.85
120#endif
121#ifndef H_CTL_REF_W_Q
122#define H_CTL_REF_W_Q 5.
123#endif
124#ifndef H_CTL_REF_XI_Q
125#define H_CTL_REF_XI_Q 0.85
126#endif
127#ifndef H_CTL_REF_W_R
128#define H_CTL_REF_W_R 5.
129#endif
130#ifndef H_CTL_REF_XI_R
131#define H_CTL_REF_XI_R 0.85
132#endif
133#ifndef H_CTL_REF_MAX_P
134#define H_CTL_REF_MAX_P RadOfDeg(150.)
135#endif
136#ifndef H_CTL_REF_MAX_P_DOT
137#define H_CTL_REF_MAX_P_DOT RadOfDeg(500.)
138#endif
139#ifndef H_CTL_REF_MAX_Q
140#define H_CTL_REF_MAX_Q RadOfDeg(150.)
141#endif
142#ifndef H_CTL_REF_MAX_Q_DOT
143#define H_CTL_REF_MAX_Q_DOT RadOfDeg(500.)
144#endif
145
146#if USE_ANGLE_REF
147PRINT_CONFIG_MSG("USING ATTITUDE REFERENCE GENERATOR")
154#endif
155
156/* inner roll loop parameters */
166
168
169/* inner pitch loop parameters */
179
180/* inner yaw loop parameters */
181#if H_CTL_YAW_LOOP
183float h_ctl_yaw_dgain;
187#endif
188
189/* inner CL loop parameters */
190#if H_CTL_CL_LOOP
192#endif
193
194/* inner loop pre-command */
197float h_ctl_pitch_of_roll; // Should be used instead of elevator_of_roll
198
201#define AIRSPEED_RATIO_MIN 0.5
202#define AIRSPEED_RATIO_MAX 2.
203
206
207// Pitch trim rate limiter
208#ifndef PITCH_TRIM_RATE_LIMITER
209#define PITCH_TRIM_RATE_LIMITER 3.
210#endif
211inline static void h_ctl_roll_loop(void);
212inline static void h_ctl_pitch_loop(void);
213#if H_CTL_YAW_LOOP
214inline static void h_ctl_yaw_loop(void);
215#endif
216#if H_CTL_CL_LOOP
217inline static void h_ctl_cl_loop(void);
218#endif
219
220// Some default course gains
221// H_CTL_COURSE_PGAIN needs to be define in airframe
222#ifndef H_CTL_COURSE_PRE_BANK_CORRECTION
223#define H_CTL_COURSE_PRE_BANK_CORRECTION 1.
224#endif
225#ifndef H_CTL_COURSE_DGAIN
226#define H_CTL_COURSE_DGAIN 0.
227#endif
228
229// Some default roll gains
230// H_CTL_ROLL_ATTITUDE_GAIN needs to be define in airframe
231#ifndef H_CTL_ROLL_RATE_GAIN
232#define H_CTL_ROLL_RATE_GAIN 0.
233#endif
234#ifndef H_CTL_ROLL_IGAIN
235#define H_CTL_ROLL_IGAIN 0.
236#endif
237#ifndef H_CTL_ROLL_KFFA
238#define H_CTL_ROLL_KFFA 0.
239#endif
240#ifndef H_CTL_ROLL_KFFD
241#define H_CTL_ROLL_KFFD 0.
242#endif
243
244// Some default pitch gains
245// H_CTL_PITCH_PGAIN needs to be define in airframe
246#ifndef H_CTL_PITCH_DGAIN
247#define H_CTL_PITCH_DGAIN 0.
248#endif
249#ifndef H_CTL_PITCH_IGAIN
250#define H_CTL_PITCH_IGAIN 0.
251#endif
252#ifndef H_CTL_PITCH_KFFA
253#define H_CTL_PITCH_KFFA 0.
254#endif
255#ifndef H_CTL_PITCH_KFFD
256#define H_CTL_PITCH_KFFD 0.
257#endif
258
259// H_CTL_YAW_GAINS to be defined in airframe
260#ifndef H_CTL_YAW_KFFD
261#define H_CTL_YAW_KFFD 0.
262#endif
263
264#ifndef USE_GYRO_PITCH_RATE
265#define USE_GYRO_PITCH_RATE TRUE
266#endif
267
268#if PERIODIC_TELEMETRY
270
275
276static void send_tune_roll(struct transport_tx *trans, struct link_device *dev)
277{
280}
281
296#endif
297
298void h_ctl_init(void)
299{
304
311
312 h_ctl_disabled = false;
313
322#ifdef H_CTL_AILERON_OF_THROTTLE
324#endif
325
335
336#if H_CTL_YAW_LOOP
341#endif
342
343#if H_CTL_CL_LOOP
345#endif
346
347 h_ctl_elevator_of_roll = 0; //H_CTL_ELEVATOR_OF_ROLL;
348#ifdef H_CTL_PITCH_OF_ROLL
350#endif
351
352 use_airspeed_ratio = false;
353 airspeed_ratio2 = 1.;
354
355#if USE_PITCH_TRIM
358#else
361#endif
362
363#if PERIODIC_TELEMETRY
367#endif
368}
369
375{
376 static float last_err;
377
378 // Ground path error
380 NormRadAngle(err);
381
382 float d_err = err - last_err;
383 last_err = err;
384 NormRadAngle(d_err);
385
387 Bound(speed_depend_nav, 0.66, 1.5);
388
391 + h_ctl_course_dgain * d_err;
392
394}
395
396#if USE_AIRSPEED
397static inline void compute_airspeed_ratio(void)
398{
399 if (use_airspeed_ratio) {
400 // low pass airspeed
401 static float airspeed = 0.;
402 airspeed = (15 * airspeed + stateGetAirspeed_f()) / 16;
403 // compute ratio
404 float airspeed_ratio = airspeed / NOMINAL_AIRSPEED;
407 } else {
408 airspeed_ratio2 = 1.;
409 }
410}
411#endif
412
414{
415 if (!h_ctl_disabled) {
416#if USE_AIRSPEED
418#endif
421#if H_CTL_YAW_LOOP
423#endif
424#if H_CTL_CL_LOOP
426#endif
427 }
428}
429
434#ifdef AHRS_TRIGGERED_ATTITUDE_LOOP
435#define H_CTL_REF_DT (1./AHRS_PROPAGATE_FREQUENCY)
436#else
437#define H_CTL_REF_DT (1./CONTROL_FREQUENCY)
438#endif
439
445#define KFFA_UPDATE 0.1
446#define KFFD_UPDATE 0.05
447
448inline static void h_ctl_roll_loop(void)
449{
450
451 static float cmd_fb = 0.;
452
453#if USE_ANGLE_REF
454 // Update reference setpoints for roll
459 // Saturation on references
463 if (h_ctl_ref.roll_accel > 0.) { h_ctl_ref.roll_accel = 0.; }
464 } else if (h_ctl_ref.roll_rate < - h_ctl_ref.max_p) {
466 if (h_ctl_ref.roll_accel < 0.) { h_ctl_ref.roll_accel = 0.; }
467 }
468#else
470 h_ctl_ref.roll_rate = 0.;
472#endif
473
474#ifdef USE_KFF_UPDATE_ROLL
475 // update Kff gains
478#ifdef SITL
480#endif
483#endif
484
485 // Compute errors
487 struct FloatRates *body_rate = stateGetBodyRates_f();
488 float d_err = h_ctl_ref.roll_rate - body_rate->p;
489
490 if (autopilot_get_mode() == AP_MODE_MANUAL || autopilot.launch == false) {
492 } else {
493 if (h_ctl_roll_igain > 0.) {
496 } else {
498 }
499 }
500
502 float cmd = - h_ctl_roll_Kffa * h_ctl_ref.roll_accel
504 - cmd_fb
505 - h_ctl_roll_rate_gain * d_err
508
509 cmd /= airspeed_ratio2;
510
511 // Set aileron commands
513}
514
515
516#if USE_PITCH_TRIM
517inline static void loiter(void)
518{
519 float pitch_trim;
520 static float last_pitch_trim;
521
522#if USE_AIRSPEED
525 } else {
527 }
528#else
530 if (throttle_diff > 0) {
533 } else {
536 }
537#endif
538
539 // Pitch trim rate limiter
542
543 last_pitch_trim = pitch_trim;
544 h_ctl_pitch_loop_setpoint += pitch_trim;
545}
546#endif
547
548
549inline static void h_ctl_pitch_loop(void)
550{
551 static float cmd_fb = 0.;
552#if !USE_GYRO_PITCH_RATE
553 static float last_err;
554#endif
555
556 /* sanity check */
557 if (h_ctl_pitch_of_roll < 0.) {
559 }
560
562#if USE_PITCH_TRIM
563 loiter();
564#endif
565
566#if USE_ANGLE_REF
567 // Update reference setpoints for pitch
572 // Saturation on references
576 if (h_ctl_ref.pitch_accel > 0.) { h_ctl_ref.pitch_accel = 0.; }
577 } else if (h_ctl_ref.pitch_rate < - h_ctl_ref.max_q) {
579 if (h_ctl_ref.pitch_accel < 0.) { h_ctl_ref.pitch_accel = 0.; }
580 }
581#else
585#endif
586
587#ifdef USE_KFF_UPDATE_PITCH
588 // update Kff gains
591#ifdef SITL
593#endif
596#endif
597
598 // Compute errors
600#if USE_GYRO_PITCH_RATE
601 float d_err = h_ctl_ref.pitch_rate - stateGetBodyRates_f()->q;
602#else // soft derivation
603 float d_err = (err - last_err) / H_CTL_REF_DT - h_ctl_ref.pitch_rate;
604 last_err = err;
605#endif
606
607 if (autopilot_get_mode() == AP_MODE_MANUAL || autopilot.launch == false) {
609 } else {
610 if (h_ctl_pitch_igain > 0.) {
613 } else {
615 }
616 }
617
621 + cmd_fb
622 + h_ctl_pitch_dgain * d_err
624
625 cmd /= airspeed_ratio2;
626
628}
629
630/* yaw damper */
631#if H_CTL_YAW_LOOP
632inline static void h_ctl_yaw_loop(void)
633{
634
635#if H_CTL_YAW_TRIM_NY
636 // Actual Acceleration from IMU:
637#if (!defined SITL || defined USE_NPS)
642 accel_ned.z -= ACCEL_BFP_OF_REAL(9.81f);
644 float ny = -ACCEL_FLOAT_OF_BFP(accel_meas_body.y) / 9.81f; // Lateral load factor (in g)
645#else
646 float ny = 0.f;
647#endif
648
649 if (autopilot_get_mode() == AP_MODE_MANUAL || autopilot.launch == false) {
651 } else {
652 if (h_ctl_yaw_ny_igain > 0.) {
653 // only update when: phi<60degrees and ny<2g
654 if (fabsf(stateGetNedToBodyEulers_f()->phi) < 1.05 && fabsf(ny) < 2.) {
656 // max half rudder deflection for trim
658 }
659 } else {
661 }
662 }
663#endif
664
665#ifdef USE_AIRSPEED
666 float Vo = stateGetAirspeed_f();
668#else
669 float Vo = NOMINAL_AIRSPEED;
670#endif
671
673 + 9.81f / Vo * sinf(h_ctl_roll_setpoint); // for turns
674 float d_err = h_ctl_ref.yaw_rate - stateGetBodyRates_f()->r;
675
676 float cmd = + h_ctl_yaw_dgain * d_err
677#if H_CTL_YAW_TRIM_NY
679#endif
680 ;
681 cmd /= airspeed_ratio2;
683}
684#endif
685
686/* CL with Flaps - direct lift control */
687#if H_CTL_CL_LOOP
688inline static void h_ctl_cl_loop(void)
689{
690
691#if H_CTL_CL_LOOP_INCREASE_FLAPS_WITH_LOADFACTOR
692#if (!defined SITL || defined USE_NPS)
697 accel_ned.z -= ACCEL_BFP_OF_REAL(9.81f);
699 float nz = -ACCEL_FLOAT_OF_BFP(accel_meas_body.z) / 9.81f;
700 // max load factor to be taken into acount
701 // to prevent negative flap movement du to negative acceleration
702 Bound(nz, 1.f, 2.f);
703#else
704 float nz = 1.f;
705#endif
706#endif
707
708 // Compute a corrected airspeed corresponding to the current load factor nz
709 // with Cz the lift coef at 1g, Czn the lift coef at n g, both at the same speed V,
710 // the corrected airspeed Vn is so that nz = Czn/Cz = V^2 / Vn^2,
711 // thus Vn = V / sqrt(nz)
712#if H_CTL_CL_LOOP_USE_AIRSPEED_SETPOINT
714#else
716#endif
717#if H_CTL_CL_LOOP_INCREASE_FLAPS_WITH_LOADFACTOR
719#endif
721
722 float cmd = 0.f;
723 // deadband around NOMINAL_AIRSPEED, rest linear
726 }
729 }
730
731 // no control in manual mode
733 cmd = 0.f;
734 }
735 // bound max flap angle
737 // from percent to pprz
738 cmd = cmd * MAX_PPRZ;
740}
741#endif
742
uint8_t autopilot_get_mode(void)
get autopilot mode
Definition autopilot.c:222
struct pprz_autopilot autopilot
Global autopilot structure.
Definition autopilot.c:49
Core autopilot interface common to all firmwares.
bool launch
request launch
Definition autopilot.h:71
pprz_t v_ctl_throttle_setpoint
uint8_t v_ctl_auto_throttle_submode
Definition energy_ctrl.c:76
float v_ctl_auto_throttle_sum_err
Definition energy_ctrl.c:77
float v_ctl_auto_throttle_nominal_cruise_throttle
#define STALL_AIRSPEED
float v_ctl_auto_throttle_cruise_throttle
float v_ctl_auto_airspeed_setpoint
in meters per second
#define AP_MODE_MANUAL
AP modes.
float v_ctl_auto_throttle_min_cruise_throttle
Definition guidance_v.c:58
float v_ctl_auto_throttle_max_cruise_throttle
Definition guidance_v.c:59
Fixed wing horizontal control.
float q
in rad/s
float phi
in radians
float p
in rad/s
float r
in rad/s
float theta
in radians
angular rates
#define VECT3_COPY(_a, _b)
#define ACCEL_BFP_OF_REAL(_af)
void int32_rmat_vmult(struct Int32Vect3 *vb, struct Int32RMat *m_a2b, struct Int32Vect3 *va)
rotate 3D vector by rotation matrix.
#define ACCEL_FLOAT_OF_BFP(_ai)
rotation matrix
vector in North East Down coordinates
static struct NedCoor_i * stateGetAccelNed_i(void)
Get acceleration in NED coordinates (int).
Definition state.h:1177
static struct Int32RMat * stateGetNedToBodyRMat_i(void)
Get vehicle body attitude rotation matrix (int).
Definition state.h:1282
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition state.h:1306
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition state.h:1367
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition state.h:1076
static float stateGetHorizontalSpeedDir_f(void)
Get dir of horizontal ground speed (float).
Definition state.h:1085
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition state.h:1590
static float p[2][2]
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
arch independent LED (Light Emitting Diodes) API
uint16_t foo
Definition main_demo5.c:58
Fixedwing Navigation library.
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
int16_t pprz_t
Definition paparazzi.h:6
#define MAX_PPRZ
Definition paparazzi.h:8
#define TRIM_PPRZ(pprz)
Definition paparazzi.h:11
#define AIRSPEED_RATIO_MIN
void h_ctl_course_loop(void)
#define H_CTL_PITCH_IGAIN
static void send_tune_roll(struct transport_tx *trans, struct link_device *dev)
static void h_ctl_pitch_loop(void)
float h_ctl_pitch_setpoint
#define KFFD_UPDATE
static void send_calibration(struct transport_tx *trans, struct link_device *dev)
pprz_t h_ctl_elevator_setpoint
bool use_airspeed_ratio
float h_ctl_pitch_sum_err
#define AIRSPEED_RATIO_MAX
float h_ctl_roll_sum_err
#define H_CTL_COURSE_PRE_BANK_CORRECTION
float h_ctl_course_dgain
float h_ctl_roll_igain
float h_ctl_pitch_Kffd
float h_ctl_roll_pgain
#define H_CTL_REF_MAX_Q
float h_ctl_roll_max_setpoint
bool h_ctl_disabled
float h_ctl_elevator_of_roll
float h_ctl_roll_setpoint
static void send_ctl_a(struct transport_tx *trans, struct link_device *dev)
float airspeed_ratio2
float h_ctl_roll_Kffa
#define H_CTL_REF_XI_P
#define H_CTL_PITCH_DGAIN
#define PITCH_TRIM_RATE_LIMITER
#define H_CTL_PITCH_KFFD
struct HCtlAdaptRef h_ctl_ref
static void h_ctl_roll_loop(void)
float v_ctl_pitch_loiter_trim
float h_ctl_pitch_dgain
pprz_t h_ctl_aileron_setpoint
float h_ctl_pitch_pgain
#define H_CTL_ROLL_KFFD
bool h_ctl_auto1_rate
float v_ctl_pitch_dash_trim
float h_ctl_roll_rate_gain
#define H_CTL_PITCH_KFFA
#define H_CTL_REF_MAX_P_DOT
void h_ctl_init(void)
void h_ctl_attitude_loop(void)
float h_ctl_roll_attitude_gain
#define H_CTL_REF_MAX_Q_DOT
#define KFFA_UPDATE
Adaptive control tuning parameters activate with USE_KFF_UPDATE.
float h_ctl_aileron_of_throttle
#define H_CTL_REF_XI_Q
#define H_CTL_ROLL_IGAIN
#define H_CTL_REF_MAX_P
float h_ctl_course_pgain
#define H_CTL_ROLL_KFFA
float h_ctl_roll_Kffd
#define H_CTL_REF_W_Q
float h_ctl_pitch_loop_setpoint
#define H_CTL_REF_W_P
#define H_CTL_COURSE_DGAIN
float h_ctl_pitch_of_roll
float h_ctl_roll_slew
float h_ctl_course_pre_bank_correction
float h_ctl_pitch_igain
#define H_CTL_REF_DT
Define reference generator time step default to control frequency and ahrs propagation freq if contro...
float h_ctl_course_pre_bank
float h_ctl_course_setpoint
float h_ctl_pitch_Kffa
#define H_CTL_ROLL_RATE_GAIN
Fixed wing horizontal adaptive control.
#define H_CTL_ROLL_SUM_ERR_MAX
#define H_CTL_PITCH_SUM_ERR_MAX
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition usb_ser_hw.c:74
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition telemetry.h:66