Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
guidance_indi.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Ewoud Smeur <ewoud.smeur@gmail.com>
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, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
33#include "generated/airframe.h"
40#include "mcu_periph/sys_time.h"
41#include "state.h"
42#include "autopilot.h"
44#include "modules/core/abi.h"
45
46// The acceleration reference is calculated with these gains. If you use GPS,
47// they are probably limited by the update rate of your GPS. The default
48// values are tuned for 4 Hz GPS updates. If you have high speed position updates, the
49// gains can be higher, depending on the speed of the inner loop.
50#ifdef GUIDANCE_INDI_POS_GAIN
52#else
54#endif
55
56#ifdef GUIDANCE_INDI_SPEED_GAIN
58#else
60#endif
61
62#ifndef GUIDANCE_INDI_ACCEL_SP_ID
63#define GUIDANCE_INDI_ACCEL_SP_ID ABI_BROADCAST
64#endif
67struct FloatVect3 indi_accel_sp = {0.0f, 0.0f, 0.0f};
70
71// FIXME make a proper structure for these variables
72struct FloatVect3 speed_sp = {0.0f, 0.0f, 0.0f};
73struct FloatVect3 sp_accel = {0.0f, 0.0f, 0.0f};
74#ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
76static void guidance_indi_filter_thrust(void);
77
78#ifdef GUIDANCE_INDI_THRUST_DYNAMICS
79#warning GUIDANCE_INDI_THRUST_DYNAMICS is deprecated, use GUIDANCE_INDI_THRUST_DYNAMICS_FREQ instead.
80#warning "The thrust dynamics are now specified in continuous time with the corner frequency of the first order model!"
81#warning "define GUIDANCE_INDI_THRUST_DYNAMICS_FREQ in rad/s"
82#warning "Use -ln(1 - old_number) * PERIODIC_FREQUENCY to compute it from the old value."
83#endif
84
85#ifndef GUIDANCE_INDI_THRUST_DYNAMICS_FREQ
86#ifndef STABILIZATION_INDI_ACT_FREQ_P
87#error "You need to define GUIDANCE_INDI_THRUST_DYNAMICS_FREQ to be able to use indi vertical control"
88#else // assume that the same actuators are used for thrust as for roll (e.g. quadrotor)
89#define GUIDANCE_INDI_THRUST_DYNAMICS_FREQ STABILIZATION_INDI_ACT_FREQ_P
90#endif
91#endif //GUIDANCE_INDI_THRUST_DYNAMICS_FREQ
92
93
94#endif //GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
95
96#ifndef GUIDANCE_INDI_FILTER_CUTOFF
97#ifdef STABILIZATION_INDI_FILT_CUTOFF
98#define GUIDANCE_INDI_FILTER_CUTOFF STABILIZATION_INDI_FILT_CUTOFF
99#else
100#define GUIDANCE_INDI_FILTER_CUTOFF 3.0
101#endif
102#endif
103
104float thrust_dyn = 0.f;
105float thrust_act = 0.f;
111
113
114#if GUIDANCE_INDI_USE_WLS
115#include "math/wls/wls_alloc.h"
116
117// priorities on control inputs (ax, ay, az)
118#ifndef GUIDANCE_INDI_WLS_PRIORITIES
119#define GUIDANCE_INDI_WLS_PRIORITIES {100.f, 100.f, 100.f}
120#endif
121
122// weighting of prefered control outputs (phi, theta, psi, Tx, Ty, Tz)
123#ifndef GUIDANCE_INDI_WLS_WU
124#define GUIDANCE_INDI_WLS_WU {1000.f,1000.f,10.f,1.f,1.f,1.f}
125#endif
126
127#define NU_MAX 6 // Example constant value // [dtheta, dphi, dthrust, dtx , dty]
128#define NV_MAX 3 // Example constant value (ax,ay,az)
129static float du_guidance[GUIDANCE_INDI_NU];
130static float *Bwls_gi[GUIDANCE_INDI_NV];
131
132static struct WLS_t wls_guid_p = {
134 .nv = GUIDANCE_INDI_NV,
135 .gamma_sq = 100.0,
136 .v = {0.0},
139 .u_pref = {0.0},
140 .u_min = {0.0},
141 .u_max = {0.0},
142 .PC = 0.0,
143 .SC = 0.0,
144 .iter = 0
145};
146
147#else
148
149// inverse matrix directly if not using WLS
152struct FloatVect3 control_increment; // [dtheta, dphi, dthrust]
153
154#endif
155
158
161
165float thrust_vect[3];
166
167static void guidance_indi_propagate_filters(struct FloatEulers *eulers);
168
169//------------------------------------------------------------//
170
171#if PERIODIC_TELEMETRY
173static void send_indi_guidance(struct transport_tx *trans, struct link_device *dev)
174{
176 &sp_accel.x,
177 &sp_accel.y,
178 &sp_accel.z,
180 &du_guidance[0],
181 &du_guidance[1],
182 &du_guidance[2],
183#else
187#endif
188 &filt_accel_ned[0].o[0],
189 &filt_accel_ned[1].o[0],
190 &filt_accel_ned[2].o[0],
191 &speed_sp.x,
192 &speed_sp.y,
193 &speed_sp.z);
194}
195#endif
196
210
216{
217 /* set nav_heading to current heading */
219
222
223#ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
224#ifdef GUIDANCE_INDI_THRUST_DYNAMICS
226#else
228#endif //GUIDANCE_INDI_THRUST_DYNAMICS
229#endif //GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
230
231 float tau = 1.0 / (2.0 * M_PI * filter_cutoff);
232 float sample_time = 1.0 / PERIODIC_FREQUENCY;
233 for (int8_t i = 0; i < 3; i++) {
235 }
240}
241
250{
251 struct FloatEulers euler_yxz;
254
255 // set global accel sp variable FIXME clean this
257
258 //filter accel to get rid of noise and filter attitude to synchronize with accel
260
261 struct FloatVect3 a_diff = {
262 sp_accel.x - filt_accel_ned[0].o[0],
263 sp_accel.y - filt_accel_ned[1].o[0],
264 sp_accel.z - filt_accel_ned[2].o[0]
265 };
266 Bound(a_diff.x, -6.0, 6.0);
267 Bound(a_diff.y, -6.0, 6.0);
268 Bound(a_diff.z, -9.0, 9.0);
269
270 //If the thrust to specific force ratio has been defined, include vertical control
271 //else ignore the vertical acceleration error
272#ifndef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
273#ifndef STABILIZATION_ATTITUDE_INDI_FULL
274 a_diff.z = 0.0;
275#endif
276#endif
277
278#if GUIDANCE_INDI_USE_WLS
279 const float m = GUIDANCE_INDI_MASS;
280 wls_guid_p.v[0] = m*a_diff.x;
281 wls_guid_p.v[1] = m*a_diff.y;
282 wls_guid_p.v[2] = m*a_diff.z;
283
286 for (int i = 0; i < GUIDANCE_INDI_NV; i++) {
287 Bwls_gi[i] = Gmat[i];
288 }
289 wls_alloc(&wls_guid_p,Bwls_gi, 0, 0, 10);
290 for (int i = 0; i < GUIDANCE_INDI_NU; i++) {
291 du_guidance [i] = wls_guid_p.u[i];
292 }
293
297 thrust_vect[0] = du_guidance[3]; // (TX)
298 thrust_vect[1] = du_guidance[4]; // (TY)
299 thrust_vect[2] = du_guidance[5]; // (TZ)
301
302#else // !USE_WLS
303
304 // Calculate matrix of partial derivatives
306
307 RMAT_ELMT(Ga, 0, 0) = Gmat[0][0];
308 RMAT_ELMT(Ga, 1, 0) = Gmat[1][0];
309 RMAT_ELMT(Ga, 2, 0) = Gmat[2][0];
310 RMAT_ELMT(Ga, 0, 1) = Gmat[0][1];
311 RMAT_ELMT(Ga, 1, 1) = Gmat[1][1];
312 RMAT_ELMT(Ga, 2, 1) = Gmat[2][1];
313 RMAT_ELMT(Ga, 0, 2) = Gmat[0][2];
314 RMAT_ELMT(Ga, 1, 2) = Gmat[1][2];
315 RMAT_ELMT(Ga, 2, 2) = Gmat[2][2];
316 // Invert this matrix // FIXME input format
318 // Calculate roll,pitch and thrust command
320
323 guidance_euler_cmd.psi = heading_sp;
324
325 // Compute and store thust setpoint
326#ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
328 //Add the increment in specific force * specific_force_to_thrust_gain to the filtered thrust
332
333#else
334 // basic quad, no side force
335 thrust_vect[0] = 0.0f;
336 thrust_vect[1] = 0.0f;
338
339 // specific force not defined, return required increment
341#endif
342
343#endif // USE_WLS
344
345 // Bound euler angles to prevent flipping
348
349 // set the quat setpoint with the calculated roll and pitch
350 struct FloatQuat q_sp;
352
353 return stab_sp_from_quat_f(&q_sp);
354}
355
357{
358 struct FloatVect3 pos_err = { 0 };
359 struct FloatVect3 accel_sp = { 0 };
360
361 struct FloatVect3 speed_fb;
362
364 // Speed feedback is included in the guidance when running in ACCEL mode
365 speed_fb.x = 0.;
366 speed_fb.y = 0.;
367 }
368 else {
369 // Generate speed feedback for acceleration, as it is estimated
371 speed_sp.x = SPEED_FLOAT_OF_BFP(gh->ref.speed.x);
372 speed_sp.y = SPEED_FLOAT_OF_BFP(gh->ref.speed.y);
373 }
374 else { // H_POS
379 }
382 }
383
385 // Speed feedback is included in the guidance when running in ACCEL mode
386 speed_fb.z = 0;
387 }
388 else {
389 // Generate speed feedback for acceleration, as it is estimated
391 speed_sp.z = SPEED_FLOAT_OF_BFP(gv->zd_ref);
392 }
393 else { // V_POS
396 }
398 }
399
400 accel_sp.x = speed_fb.x + ACCEL_FLOAT_OF_BFP(gh->ref.accel.x);
401 accel_sp.y = speed_fb.y + ACCEL_FLOAT_OF_BFP(gh->ref.accel.y);
402 accel_sp.z = speed_fb.z + ACCEL_FLOAT_OF_BFP(gv->zdd_ref);
403
404 return accel_sp;
405}
406
408{
409 struct FloatVect3 accel_sp;
410
411 // If set and valid, the ABI message overwrite the accel setpoint
412 // TODO This is not ideal, guided mode with accel setpoint would be better
416 // In 2D the vertical motion is derived from the flight plan
419 // If the input command is not updated after a timeout, switch back to flight plan control
420 if (dt > 0.5) {
421 indi_accel_sp_set_2d = false;
422 }
423 } else if (indi_accel_sp_set_3d) {
428 // If the input command is not updated after a timeout, switch back to flight plan control
429 if (dt > 0.5) {
430 indi_accel_sp_set_3d = false;
431 }
432 }
433 else {
435 }
436
437 return guidance_indi_run(&accel_sp, gh->sp.heading);
438}
439
440
441#ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
446{
447 // Actuator dynamics
449
450 // same filter as for the acceleration
452}
453#endif
454
471
477{
478 if (flag == 0) {
483 } else if (flag == 1) {
489 }
490}
491
492#if GUIDANCE_INDI_USE_AS_DEFAULT
493// guidance indi control function is implementing the default functions of guidance
494
495void guidance_h_run_enter(void)
496{
498}
499
500void guidance_v_run_enter(void)
501{
502 // nothing to do
503}
504
505static struct VerticalGuidance *_gv = &guidance_v;
507
509{
511}
512
514{
516}
517
519{
521}
522
524{
525 _gv = gv;
527 return thrust_sp;
528}
529
531{
532 _gv = gv;
534 return thrust_sp;
535}
536
538{
539 _gv = gv;
541 return thrust_sp;
542}
543
544#endif
545
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition abi_common.h:68
Core autopilot interface common to all firmwares.
#define UNUSED(x)
static struct uart_periph * dev
float phi
in radians
float theta
in radians
float psi
in radians
#define FLOAT_EULERS_ZERO(_e)
void float_eulers_of_quat_yxz(struct FloatEulers *e, const struct FloatQuat *q)
euler rotation 'YXZ' This function calculates from a quaternion the Euler angles with the order YXZ,...
void float_quat_of_eulers_yxz(struct FloatQuat *q, const struct FloatEulers *e)
quat from euler rotation 'YXZ' This function calculates a quaternion from Euler angles with the order...
euler angles
Roation quaternion.
#define MAT33_VECT3_MUL(_vout, _mat, _vin)
#define MAT33_INV(_minv, _m)
#define RMAT_ELMT(_rm, _row, _col)
#define POS_FLOAT_OF_BFP(_ai)
#define SPEED_FLOAT_OF_BFP(_ai)
#define ACCEL_FLOAT_OF_BFP(_ai)
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition state.h:1203
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition state.h:1314
static struct FloatQuat * stateGetNedToBodyQuat_f(void)
Get vehicle body attitude quaternion (float).
Definition state.h:1302
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition state.h:839
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition state.h:1049
#define GUIDANCE_INDI_ACCEL_SP_ID
Butterworth2LowPass roll_filt
float thrust_in
struct StabilizationSetpoint guidance_indi_run_mode(bool in_flight, struct HorizontalGuidance *gh, struct VerticalGuidance *gv, enum GuidanceIndi_HMode h_mode, enum GuidanceIndi_VMode v_mode)
float guidance_indi_max_bank
bool indi_accel_sp_set_3d
float guidance_indi_pos_gain
struct FloatVect3 sp_accel
struct FloatEulers guidance_euler_cmd
struct FloatVect3 speed_sp
float thrust_act
struct FloatMat33 Ga_inv
void guidance_indi_enter(void)
Call upon entering indi guidance.
static void accel_sp_cb(uint8_t sender_id, uint8_t flag, struct FloatVect3 *accel_sp)
ABI callback that obtains the acceleration setpoint from telemetry flag: 0 -> 2D, 1 -> 3D.
Butterworth2LowPass pitch_filt
abi_event accel_sp_ev
struct ThrustSetpoint thrust_sp
void guidance_indi_init(void)
Init function.
float time_of_accel_sp_3d
float filter_cutoff
Butterworth2LowPass yaw_filt
#define GUIDANCE_INDI_FILTER_CUTOFF
struct FloatVect3 WEAK guidance_indi_controller(bool in_flight UNUSED, struct HorizontalGuidance *gh, struct VerticalGuidance *gv, enum GuidanceIndi_HMode h_mode, enum GuidanceIndi_VMode v_mode)
bool indi_accel_sp_set_2d
struct FloatVect3 indi_accel_sp
float thrust_vect[3]
struct FloatVect3 control_increment
struct FloatMat33 Ga
Butterworth2LowPass filt_accel_ned[3]
float guidance_indi_speed_gain
float time_of_accel_sp_2d
float thrust_dyn
static float Gmat[GUIDANCE_INDI_NV][GUIDANCE_INDI_NU]
struct StabilizationSetpoint guidance_indi_run(struct FloatVect3 *accel_sp, float heading_sp)
Butterworth2LowPass thrust_filt
static void send_indi_guidance(struct transport_tx *trans, struct link_device *dev)
A guidance mode based on Incremental Nonlinear Dynamic Inversion.
void guidance_indi_calcG(float Gmat[GUIDANCE_INDI_NV][GUIDANCE_INDI_NU], struct FloatEulers att)
#define GUIDANCE_INDI_NV
float guidance_indi_specific_force_gain
#define GUIDANCE_INDI_NU
GuidanceIndi_VMode
@ GUIDANCE_INDI_V_ACCEL
@ GUIDANCE_INDI_V_SPEED
@ GUIDANCE_INDI_V_POS
GuidanceIndi_HMode
@ GUIDANCE_INDI_H_SPEED
@ GUIDANCE_INDI_H_ACCEL
@ GUIDANCE_INDI_H_POS
void guidance_indi_set_wls_settings(struct WLS_t *wls, struct FloatEulers *euler_yxz, float heading_sp)
#define GUIDANCE_INDI_POS_GAIN
void guidance_indi_propagate_filters(void)
Low pass the accelerometer measurements to remove noise from vibrations.
#define GUIDANCE_INDI_SPEED_GAIN
static enum GuidanceOneloop_VMode _v_mode
void guidance_v_run_enter(void)
static struct VerticalGuidance * _gv
struct ThrustSetpoint guidance_v_run_pos(bool in_flight UNUSED, struct VerticalGuidance *gv)
struct ThrustSetpoint guidance_v_run_speed(bool in_flight UNUSED, struct VerticalGuidance *gv)
struct ThrustSetpoint guidance_v_run_accel(bool in_flight UNUSED, struct VerticalGuidance *gv)
Simple first order low pass filter with bilinear transform.
float o[2]
output history
static void init_butterworth_2_low_pass(Butterworth2LowPass *filter, const float tau, const float sample_time, const float value)
Init a second order Butterworth filter.
static float update_butterworth_2_low_pass(Butterworth2LowPass *filter, const float value)
Update second order Butterworth low pass filter state with a new value.
Second order low pass filter structure.
uint16_t foo
Definition main_demo5.c:58
#define MAX_PPRZ
Definition paparazzi.h:8
float z
in meters
float x
in meters
float y
in meters
vector in North East Down coordinates Units: meters
Generic interface for radio control modules.
Some helper functions to check RC sticks.
Horizontal guidance for rotorcrafts.
void guidance_h_run_enter(void)
struct StabilizationSetpoint guidance_h_run_pos(bool in_flight, struct HorizontalGuidance *gh)
struct StabilizationSetpoint guidance_h_run_speed(bool in_flight, struct HorizontalGuidance *gh)
struct StabilizationSetpoint guidance_h_run_accel(bool in_flight, struct HorizontalGuidance *gh)
#define GUIDANCE_H_MAX_BANK
Max bank controlled by guidance.
Definition guidance_h.h:64
struct VerticalGuidance guidance_v
Definition guidance_v.c:60
Vertical guidance for rotorcrafts.
struct RotorcraftNavigation nav
Definition navigation.c:51
float heading
heading setpoint (in radians)
Definition navigation.h:133
struct Stabilization stabilization
struct ThrustSetpoint th_sp_from_incr_vect_f(float th_increment[3])
struct StabilizationSetpoint stab_sp_from_quat_f(struct FloatQuat *quat)
struct ThrustSetpoint th_sp_from_thrust_i(int32_t thrust, uint8_t axis)
General stabilization interface for rotorcrafts.
#define THRUST_SP_SET_ZERO(_sp)
#define THRUST_AXIS_Z
int32_t cmd[COMMANDS_NB]
output command vector, range from [-MAX_PPRZ:MAX_PPRZ] (store for messages)
API to get/set the generic vehicle states.
Stabilization setpoint.
Thrust setpoint // TODO to a setpoint header Structure to store the desired thrust vector with differ...
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition sys_time.h:168
int16_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint16_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
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
signed char int8_t
Typedef defining 8 bit char type.
void wls_alloc(struct WLS_t *WLS_p, float **B, float *u_guess, float *W_init, int imax)
active set algorithm for control allocation
Definition wls_alloc.c:119
int nu
Definition wls_alloc.h:67