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#define NU_MAX 6 // Example constant value // [dtheta, dphi, dthrust, dtx , dty]
118#define NV_MAX 3 // Example constant value (ax,ay,az)
119static float du_guidance[GUIDANCE_INDI_NU];
120static float *Bwls_gi[GUIDANCE_INDI_NV];
121
122static struct WLS_t wls_guid_p = {
124 .nv = GUIDANCE_INDI_NV,
125 .gamma_sq = 1000.0,
126 .v = {0.0},
127 .Wv = {100.f, 100.f, 100.f}, // x,y,z
128 .Wu = {10.f,10.f,0.f,0.f,0.f,10.f}, // minimize the control input (thetq,phi,Tx,Ty,Tz,psi)
129 .u_pref = {0.0},
130 .u_min = {0.0},
131 .u_max = {0.0},
132 .PC = 0.0,
133 .SC = 0.0,
134 .iter = 0
135};
136
137#else
138
139// inverse matrix directly if not using WLS
142struct FloatVect3 control_increment; // [dtheta, dphi, dthrust]
143
144#endif
145
148
151
155float thrust_vect[3];
156
157static void guidance_indi_propagate_filters(struct FloatEulers *eulers);
158
159//------------------------------------------------------------//
160
161#if PERIODIC_TELEMETRY
163static void send_indi_guidance(struct transport_tx *trans, struct link_device *dev)
164{
166 &sp_accel.x,
167 &sp_accel.y,
168 &sp_accel.z,
170 &du_guidance[0],
171 &du_guidance[1],
172 &du_guidance[2],
173#else
177#endif
178 &filt_accel_ned[0].o[0],
179 &filt_accel_ned[1].o[0],
180 &filt_accel_ned[2].o[0],
181 &speed_sp.x,
182 &speed_sp.y,
183 &speed_sp.z);
184}
185#endif
186
200
206{
207 /* set nav_heading to current heading */
209
212
213#ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
214#ifdef GUIDANCE_INDI_THRUST_DYNAMICS
216#else
218#endif //GUIDANCE_INDI_THRUST_DYNAMICS
219#endif //GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
220
221 float tau = 1.0 / (2.0 * M_PI * filter_cutoff);
222 float sample_time = 1.0 / PERIODIC_FREQUENCY;
223 for (int8_t i = 0; i < 3; i++) {
225 }
230}
231
240{
241 struct FloatEulers euler_yxz;
244
245 // set global accel sp variable FIXME clean this
247
248 //filter accel to get rid of noise and filter attitude to synchronize with accel
250
251 struct FloatVect3 a_diff = {
252 sp_accel.x - filt_accel_ned[0].o[0],
253 sp_accel.y - filt_accel_ned[1].o[0],
254 sp_accel.z - filt_accel_ned[2].o[0]
255 };
256 Bound(a_diff.x, -6.0, 6.0);
257 Bound(a_diff.y, -6.0, 6.0);
258 Bound(a_diff.z, -9.0, 9.0);
259
260 //If the thrust to specific force ratio has been defined, include vertical control
261 //else ignore the vertical acceleration error
262#ifndef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
263#ifndef STABILIZATION_ATTITUDE_INDI_FULL
264 a_diff.z = 0.0;
265#endif
266#endif
267
268#if GUIDANCE_INDI_USE_WLS
269 float m = GUIDANCE_INDI_MASS;
270 wls_guid_p.v[0] = m*a_diff.x;
271 wls_guid_p.v[1] = m*a_diff.y;
272 wls_guid_p.v[2] = m*a_diff.z;
273
276 for (int i = 0; i < GUIDANCE_INDI_NV; i++) {
277 Bwls_gi[i] = Gmat[i];
278 }
279 wls_alloc(&wls_guid_p,Bwls_gi, 0, 0, 10);
280 for (int i = 0; i < GUIDANCE_INDI_NU; i++) {
281 du_guidance [i] = wls_guid_p.u[i];
282 }
283
287 thrust_vect[0] = du_guidance[3]; // (TX)
288 thrust_vect[1] = du_guidance[4]; // (TY)
289 thrust_vect[2] = du_guidance[5]; // (TZ)
291
292#else // !USE_WLS
293
294 // Calculate matrix of partial derivatives
296
297 RMAT_ELMT(Ga, 0, 0) = Gmat[0][0];
298 RMAT_ELMT(Ga, 1, 0) = Gmat[1][0];
299 RMAT_ELMT(Ga, 2, 0) = Gmat[2][0];
300 RMAT_ELMT(Ga, 0, 1) = Gmat[0][1];
301 RMAT_ELMT(Ga, 1, 1) = Gmat[1][1];
302 RMAT_ELMT(Ga, 2, 1) = Gmat[2][1];
303 RMAT_ELMT(Ga, 0, 2) = Gmat[0][2];
304 RMAT_ELMT(Ga, 1, 2) = Gmat[1][2];
305 RMAT_ELMT(Ga, 2, 2) = Gmat[2][2];
306 // Invert this matrix // FIXME input format
308 // Calculate roll,pitch and thrust command
310
313 guidance_euler_cmd.psi = heading_sp;
314
315 // Compute and store thust setpoint
316#ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
318 //Add the increment in specific force * specific_force_to_thrust_gain to the filtered thrust
322
323#else
324 // basic quad, no side force
325 thrust_vect[0] = 0.0f;
326 thrust_vect[1] = 0.0f;
328
329 // specific force not defined, return required increment
331#endif
332
333#endif // USE_WLS
334
335 // Bound euler angles to prevent flipping
338
339 // set the quat setpoint with the calculated roll and pitch
340 struct FloatQuat q_sp;
342
343 return stab_sp_from_quat_f(&q_sp);
344}
345
347{
348 struct FloatVect3 pos_err = { 0 };
349 struct FloatVect3 accel_sp = { 0 };
350
351 struct FloatVect3 speed_fb;
352
354 // Speed feedback is included in the guidance when running in ACCEL mode
355 speed_fb.x = 0.;
356 speed_fb.y = 0.;
357 }
358 else {
359 // Generate speed feedback for acceleration, as it is estimated
361 speed_sp.x = SPEED_FLOAT_OF_BFP(gh->ref.speed.x);
362 speed_sp.y = SPEED_FLOAT_OF_BFP(gh->ref.speed.y);
363 }
364 else { // H_POS
369 }
372 }
373
375 // Speed feedback is included in the guidance when running in ACCEL mode
376 speed_fb.z = 0;
377 }
378 else {
379 // Generate speed feedback for acceleration, as it is estimated
381 speed_sp.z = SPEED_FLOAT_OF_BFP(gv->zd_ref);
382 }
383 else { // V_POS
386 }
388 }
389
390 accel_sp.x = speed_fb.x + ACCEL_FLOAT_OF_BFP(gh->ref.accel.x);
391 accel_sp.y = speed_fb.y + ACCEL_FLOAT_OF_BFP(gh->ref.accel.y);
392 accel_sp.z = speed_fb.z + ACCEL_FLOAT_OF_BFP(gv->zdd_ref);
393
394 return accel_sp;
395}
396
398{
399 struct FloatVect3 accel_sp;
400
401 // If set and valid, the ABI message overwrite the accel setpoint
402 // TODO This is not ideal, guided mode with accel setpoint would be better
406 // In 2D the vertical motion is derived from the flight plan
409 // If the input command is not updated after a timeout, switch back to flight plan control
410 if (dt > 0.5) {
411 indi_accel_sp_set_2d = false;
412 }
413 } else if (indi_accel_sp_set_3d) {
418 // If the input command is not updated after a timeout, switch back to flight plan control
419 if (dt > 0.5) {
420 indi_accel_sp_set_3d = false;
421 }
422 }
423 else {
425 }
426
427 return guidance_indi_run(&accel_sp, gh->sp.heading);
428}
429
430
431#ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
436{
437 // Actuator dynamics
439
440 // same filter as for the acceleration
442}
443#endif
444
461
467{
468 if (flag == 0) {
473 } else if (flag == 1) {
479 }
480}
481
482#if GUIDANCE_INDI_USE_AS_DEFAULT
483// guidance indi control function is implementing the default functions of guidance
484
485void guidance_h_run_enter(void)
486{
488}
489
490void guidance_v_run_enter(void)
491{
492 // nothing to do
493}
494
495static struct VerticalGuidance *_gv = &guidance_v;
497
499{
501}
502
504{
506}
507
509{
511}
512
514{
515 _gv = gv;
517 return thrust_sp;
518}
519
521{
522 _gv = gv;
524 return thrust_sp;
525}
526
528{
529 _gv = gv;
531 return thrust_sp;
532}
533
534#endif
535
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 UNUSED)
#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