Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
stabilization_attitude_quat_float.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@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, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
26 #include "generated/airframe.h"
27 
31 
32 #include "std.h"
33 #include "paparazzi.h"
35 #include "math/pprz_algebra_int.h"
36 #include "state.h"
37 
39 
42 
44 
46 
49 
50 float stabilization_att_fb_cmd[COMMANDS_NB];
51 float stabilization_att_ff_cmd[COMMANDS_NB];
52 
54 
55 static const float phi_pgain[] = STABILIZATION_ATTITUDE_PHI_PGAIN;
56 static const float theta_pgain[] = STABILIZATION_ATTITUDE_THETA_PGAIN;
57 static const float psi_pgain[] = STABILIZATION_ATTITUDE_PSI_PGAIN;
58 
59 static const float phi_dgain[] = STABILIZATION_ATTITUDE_PHI_DGAIN;
60 static const float theta_dgain[] = STABILIZATION_ATTITUDE_THETA_DGAIN;
61 static const float psi_dgain[] = STABILIZATION_ATTITUDE_PSI_DGAIN;
62 
63 static const float phi_igain[] = STABILIZATION_ATTITUDE_PHI_IGAIN;
64 static const float theta_igain[] = STABILIZATION_ATTITUDE_THETA_IGAIN;
65 static const float psi_igain[] = STABILIZATION_ATTITUDE_PSI_IGAIN;
66 
67 static const float phi_ddgain[] = STABILIZATION_ATTITUDE_PHI_DDGAIN;
68 static const float theta_ddgain[] = STABILIZATION_ATTITUDE_THETA_DDGAIN;
69 static const float psi_ddgain[] = STABILIZATION_ATTITUDE_PSI_DDGAIN;
70 
71 static const float phi_dgain_d[] = STABILIZATION_ATTITUDE_PHI_DGAIN_D;
72 static const float theta_dgain_d[] = STABILIZATION_ATTITUDE_THETA_DGAIN_D;
73 static const float psi_dgain_d[] = STABILIZATION_ATTITUDE_PSI_DGAIN_D;
74 
75 
76 #if defined COMMAND_ROLL_SURFACE && defined COMMAND_PITCH_SURFACE && defined COMMAND_YAW_SURFACE
77 #define HAS_SURFACE_COMMANDS 1
78 #endif
79 
80 #ifdef HAS_SURFACE_COMMANDS
81 static const float phi_pgain_surface[] = STABILIZATION_ATTITUDE_PHI_PGAIN_SURFACE;
82 static const float theta_pgain_surface[] = STABILIZATION_ATTITUDE_THETA_PGAIN_SURFACE;
83 static const float psi_pgain_surface[] = STABILIZATION_ATTITUDE_PSI_PGAIN_SURFACE;
84 
85 static const float phi_dgain_surface[] = STABILIZATION_ATTITUDE_PHI_DGAIN_SURFACE;
86 static const float theta_dgain_surface[] = STABILIZATION_ATTITUDE_THETA_DGAIN_SURFACE;
87 static const float psi_dgain_surface[] = STABILIZATION_ATTITUDE_PSI_DGAIN_SURFACE;
88 
89 static const float phi_igain_surface[] = STABILIZATION_ATTITUDE_PHI_IGAIN_SURFACE;
90 static const float theta_igain_surface[] = STABILIZATION_ATTITUDE_THETA_IGAIN_SURFACE;
91 static const float psi_igain_surface[] = STABILIZATION_ATTITUDE_PSI_IGAIN_SURFACE;
92 
93 static const float phi_ddgain_surface[] = STABILIZATION_ATTITUDE_PHI_DDGAIN_SURFACE;
94 static const float theta_ddgain_surface[] = STABILIZATION_ATTITUDE_THETA_DDGAIN_SURFACE;
95 static const float psi_ddgain_surface[] = STABILIZATION_ATTITUDE_PSI_DDGAIN_SURFACE;
96 #endif
97 
98 #define IERROR_SCALE 1024
99 
100 #if PERIODIC_TELEMETRY
102 
103 static void send_att(struct transport_tx *trans, struct link_device *dev)
104 {
105  struct FloatRates *body_rate = stateGetBodyRates_f();
106  struct FloatEulers *att = stateGetNedToBodyEulers_f();
107  pprz_msg_send_STAB_ATTITUDE_FLOAT(trans, dev, AC_ID,
108  &(body_rate->p), &(body_rate->q), &(body_rate->r),
109  &(att->phi), &(att->theta), &(att->psi),
116  &stabilization_att_fb_cmd[COMMAND_ROLL],
117  &stabilization_att_fb_cmd[COMMAND_PITCH],
118  &stabilization_att_fb_cmd[COMMAND_YAW],
119  &stabilization_att_ff_cmd[COMMAND_ROLL],
120  &stabilization_att_ff_cmd[COMMAND_PITCH],
121  &stabilization_att_ff_cmd[COMMAND_YAW],
122  &stabilization_cmd[COMMAND_ROLL],
123  &stabilization_cmd[COMMAND_PITCH],
124  &stabilization_cmd[COMMAND_YAW],
126 }
127 
128 static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
129 {
130  pprz_msg_send_STAB_ATTITUDE_REF_FLOAT(trans, dev, AC_ID,
143 }
144 #endif
145 
147 {
148  /* setpoints */
151  /* reference */
154 
155  for (int i = 0; i < STABILIZATION_ATTITUDE_GAIN_NB; i++) {
161 #ifdef HAS_SURFACE_COMMANDS
162  VECT3_ASSIGN(stabilization_gains[i].surface_p, phi_pgain_surface[i], theta_pgain_surface[i], psi_pgain_surface[i]);
163  VECT3_ASSIGN(stabilization_gains[i].surface_d, phi_dgain_surface[i], theta_dgain_surface[i], psi_dgain_surface[i]);
164  VECT3_ASSIGN(stabilization_gains[i].surface_i, phi_igain_surface[i], theta_igain_surface[i], psi_igain_surface[i]);
165  VECT3_ASSIGN(stabilization_gains[i].surface_dd, phi_ddgain_surface[i], theta_ddgain_surface[i], psi_ddgain_surface[i]);
166 #endif
167  }
168 
172 
173 #if PERIODIC_TELEMETRY
174  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_FLOAT, send_att);
175  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_REF_FLOAT, send_att_ref);
176 #endif
177 }
178 
180 {
182  // This could be bad -- Just say no.
183  return;
184  }
185  gain_idx = idx;
187 }
188 
190 {
191 
192  /* reset psi setpoint to current psi angle */
194 
196 
198 }
199 
201 {
202  /* set failsafe to zero roll/pitch and current heading */
203  float heading2 = stabilization_attitude_get_heading_f() / 2;
204  stab_att_sp_quat.qi = cosf(heading2);
205  stab_att_sp_quat.qx = 0.0;
206  stab_att_sp_quat.qy = 0.0;
207  stab_att_sp_quat.qz = sinf(heading2);
208 }
209 
211 {
212  // copy euler setpoint for debugging
214 
216 }
217 
219 {
220  struct FloatVect2 cmd_f;
221  cmd_f.x = ANGLE_FLOAT_OF_BFP(cmd->x);
222  cmd_f.y = ANGLE_FLOAT_OF_BFP(cmd->y);
223  float heading_f;
224  heading_f = ANGLE_FLOAT_OF_BFP(heading);
225 
226  quat_from_earth_cmd_f(&stab_att_sp_quat, &cmd_f, heading_f);
227 }
228 
229 #ifndef GAIN_PRESCALER_FF
230 #define GAIN_PRESCALER_FF 1
231 #endif
232 static void attitude_run_ff(float ff_commands[], struct FloatAttitudeGains *gains, struct FloatRates *ref_accel)
233 {
234  /* Compute feedforward based on reference acceleration */
235 
236  ff_commands[COMMAND_ROLL] = GAIN_PRESCALER_FF * gains->dd.x * ref_accel->p;
237  ff_commands[COMMAND_PITCH] = GAIN_PRESCALER_FF * gains->dd.y * ref_accel->q;
238  ff_commands[COMMAND_YAW] = GAIN_PRESCALER_FF * gains->dd.z * ref_accel->r;
239 #ifdef HAS_SURFACE_COMMANDS
240  ff_commands[COMMAND_ROLL_SURFACE] = GAIN_PRESCALER_FF * gains->surface_dd.x * ref_accel->p;
241  ff_commands[COMMAND_PITCH_SURFACE] = GAIN_PRESCALER_FF * gains->surface_dd.y * ref_accel->q;
242  ff_commands[COMMAND_YAW_SURFACE] = GAIN_PRESCALER_FF * gains->surface_dd.z * ref_accel->r;
243 #endif
244 }
245 
246 #ifndef GAIN_PRESCALER_P
247 #define GAIN_PRESCALER_P 1
248 #endif
249 #ifndef GAIN_PRESCALER_D
250 #define GAIN_PRESCALER_D 1
251 #endif
252 #ifndef GAIN_PRESCALER_I
253 #define GAIN_PRESCALER_I 1
254 #endif
255 static void attitude_run_fb(float fb_commands[], struct FloatAttitudeGains *gains, struct FloatQuat *att_err,
256  struct FloatRates *rate_err, struct FloatRates *rate_err_d, struct FloatQuat *sum_err)
257 {
258  /* PID feedback */
259  fb_commands[COMMAND_ROLL] =
260  GAIN_PRESCALER_P * gains->p.x * att_err->qx +
261  GAIN_PRESCALER_D * gains->d.x * rate_err->p +
262  GAIN_PRESCALER_D * gains->rates_d.x * rate_err_d->p +
263  GAIN_PRESCALER_I * gains->i.x * sum_err->qx;
264 
265  fb_commands[COMMAND_PITCH] =
266  GAIN_PRESCALER_P * gains->p.y * att_err->qy +
267  GAIN_PRESCALER_D * gains->d.y * rate_err->q +
268  GAIN_PRESCALER_D * gains->rates_d.y * rate_err_d->q +
269  GAIN_PRESCALER_I * gains->i.y * sum_err->qy;
270 
271  fb_commands[COMMAND_YAW] =
272  GAIN_PRESCALER_P * gains->p.z * att_err->qz +
273  GAIN_PRESCALER_D * gains->d.z * rate_err->r +
274  GAIN_PRESCALER_D * gains->rates_d.z * rate_err_d->r +
275  GAIN_PRESCALER_I * gains->i.z * sum_err->qz;
276 
277 #ifdef HAS_SURFACE_COMMANDS
278  fb_commands[COMMAND_ROLL_SURFACE] =
279  GAIN_PRESCALER_P * gains->surface_p.x * att_err->qx +
280  GAIN_PRESCALER_D * gains->surface_d.x * rate_err->p +
281  GAIN_PRESCALER_I * gains->surface_i.x * sum_err->qx;
282 
283  fb_commands[COMMAND_PITCH_SURFACE] =
284  GAIN_PRESCALER_P * gains->surface_p.y * att_err->qy +
285  GAIN_PRESCALER_D * gains->surface_d.y * rate_err->q +
286  GAIN_PRESCALER_I * gains->surface_i.y * sum_err->qy;
287 
288  fb_commands[COMMAND_YAW_SURFACE] =
289  GAIN_PRESCALER_P * gains->surface_p.z * att_err->qz +
290  GAIN_PRESCALER_D * gains->surface_d.z * rate_err->r +
291  GAIN_PRESCALER_I * gains->surface_i.z * sum_err->qz;
292 #endif
293 }
294 
295 void stabilization_attitude_run(bool enable_integrator)
296 {
297 
298  /*
299  * Update reference
300  */
301  static const float dt = (1./PERIODIC_FREQUENCY);
303 
304  /*
305  * Compute errors for feedback
306  */
307 
308  /* attitude error */
309  struct FloatQuat att_err;
310  struct FloatQuat *att_quat = stateGetNedToBodyQuat_f();
311  float_quat_inv_comp(&att_err, att_quat, &att_ref_quat_f.quat);
312  /* wrap it in the shortest direction */
313  float_quat_wrap_shortest(&att_err);
314 
315  /* rate error */
316  struct FloatRates rate_err;
317  struct FloatRates *body_rate = stateGetBodyRates_f();
318  RATES_DIFF(rate_err, att_ref_quat_f.rate, *body_rate);
319  /* rate_d error */
320  RATES_DIFF(body_rate_d, *body_rate, last_body_rate);
321  RATES_COPY(last_body_rate, *body_rate);
322 
323 #define INTEGRATOR_BOUND 1.0
324  /* integrated error */
325  if (enable_integrator) {
332  } else {
333  /* reset accumulator */
335  }
336 
338 
339  attitude_run_fb(stabilization_att_fb_cmd, &stabilization_gains[gain_idx], &att_err, &rate_err, &body_rate_d,
341 
342  stabilization_cmd[COMMAND_ROLL] = stabilization_att_fb_cmd[COMMAND_ROLL] + stabilization_att_ff_cmd[COMMAND_ROLL];
343  stabilization_cmd[COMMAND_PITCH] = stabilization_att_fb_cmd[COMMAND_PITCH] + stabilization_att_ff_cmd[COMMAND_PITCH];
344  stabilization_cmd[COMMAND_YAW] = stabilization_att_fb_cmd[COMMAND_YAW] + stabilization_att_ff_cmd[COMMAND_YAW];
345 
346 #ifdef HAS_SURFACE_COMMANDS
347  stabilization_cmd[COMMAND_ROLL_SURFACE] = stabilization_att_fb_cmd[COMMAND_ROLL_SURFACE] +
348  stabilization_att_ff_cmd[COMMAND_ROLL_SURFACE];
349  stabilization_cmd[COMMAND_PITCH_SURFACE] = stabilization_att_fb_cmd[COMMAND_PITCH_SURFACE] +
350  stabilization_att_ff_cmd[COMMAND_PITCH_SURFACE];
351  stabilization_cmd[COMMAND_YAW_SURFACE] = stabilization_att_fb_cmd[COMMAND_YAW_SURFACE] +
352  stabilization_att_ff_cmd[COMMAND_YAW_SURFACE];
353 #endif
354 
355  /* bound the result */
356  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
357  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
358  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
359 }
360 
361 void stabilization_attitude_read_rc(bool in_flight, bool in_carefree, bool coordinated_turn)
362 {
363 
364  stabilization_attitude_read_rc_setpoint_quat_f(&stab_att_sp_quat, in_flight, in_carefree, coordinated_turn);
365  //float_quat_wrap_shortest(&stab_att_sp_quat);
366 }
Attitude reference models and state/output (float)
void float_quat_of_eulers(struct FloatQuat *q, struct FloatEulers *e)
Quaternion from Euler angles.
void stabilization_attitude_read_rc_setpoint_quat_f(struct FloatQuat *q_sp, bool in_flight, bool in_carefree, bool coordinated_turn)
Read attitude setpoint from RC as quaternion Interprets the stick positions as axes.
struct FloatRates last_body_rate
static uint32_t idx
static const float theta_dgain[]
static const float theta_ddgain[]
static void send_att(struct transport_tx *trans, struct link_device *dev)
float phi
in radians
void attitude_ref_quat_float_enter(struct AttRefQuatFloat *ref, float psi)
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1125
Quaternion transformation functions.
static int gain_idx
Periodic telemetry system header (includes downlink utility and generated code).
static const float phi_igain[]
General attitude stabilization interface for rotorcrafts.
void stabilization_attitude_set_earth_cmd_i(struct Int32Vect2 *cmd, int32_t heading)
#define FLOAT_EULERS_ZERO(_e)
static void float_quat_identity(struct FloatQuat *q)
initialises a quaternion to identity
float r
in rad/s
static const float psi_dgain_d[]
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:124
float stabilization_att_fb_cmd[COMMANDS_NB]
Read an attitude setpoint from the RC.
#define FLOAT_RATES_ZERO(_r)
float psi
in radians
static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
static const float phi_pgain[]
float dt
struct FloatRates body_rate_d
#define INTEGRATOR_BOUND
float q
in rad/s
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:371
float p
in rad/s
void float_quat_inv_comp(struct FloatQuat *b2c, struct FloatQuat *a2b, struct FloatQuat *a2c)
Composition (multiplication) of two quaternions.
#define ANGLE_FLOAT_OF_BFP(_ai)
struct FloatAttitudeGains stabilization_gains[STABILIZATION_ATTITUDE_GAIN_NB]
static struct FloatQuat * stateGetNedToBodyQuat_f(void)
Get vehicle body attitude quaternion (float).
Definition: state.h:1113
euler angles
Roation quaternion.
static void attitude_run_ff(float ff_commands[], struct FloatAttitudeGains *gains, struct FloatRates *ref_accel)
static const float phi_dgain_d[]
float theta
in radians
struct FloatEulers stab_att_sp_euler
with INT32_ANGLE_FRAC
void stabilization_attitude_gain_schedule(uint8_t idx)
void stabilization_attitude_run(bool enable_integrator)
void stabilization_attitude_enter(void)
static float heading
Definition: ahrs_infrared.c:45
Paparazzi floating point algebra.
static const float psi_dgain[]
static void attitude_run_fb(float fb_commands[], struct FloatAttitudeGains *gains, struct FloatQuat *att_err, struct FloatRates *rate_err, struct FloatRates *rate_err_d, struct FloatQuat *sum_err)
static const float psi_ddgain[]
euler angles
static const float theta_dgain_d[]
static const float psi_pgain[]
void attitude_ref_quat_float_update(struct AttRefQuatFloat *ref, struct FloatQuat *sp_quat, float dt)
void stabilization_attitude_init(void)
stabilization_attitude_init
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
void attitude_ref_quat_float_schedule(struct AttRefQuatFloat *ref, uint8_t idx)
static const float psi_igain[]
struct FloatQuat stab_att_sp_quat
with INT32_QUAT_FRAC
static const float theta_igain[]
void stabilization_attitude_set_rpy_setpoint_i(struct Int32Eulers *rpy)
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition: state.h:1182
signed long int32_t
Definition: types.h:19
static const float phi_dgain[]
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
struct AttRefQuatFloat att_ref_quat_f
void stabilization_attitude_read_rc(bool in_flight, bool in_carefree, bool coordinated_turn)
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
void quat_from_earth_cmd_f(struct FloatQuat *quat, struct FloatVect2 *cmd, float heading)
static void float_quat_wrap_shortest(struct FloatQuat *q)
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:28
static float p[2][2]
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
float stabilization_att_ff_cmd[COMMANDS_NB]
#define MAX_PPRZ
Definition: paparazzi.h:8
float stabilization_attitude_get_heading_f(void)
#define STABILIZATION_ATTITUDE_GAIN_IDX_DEFAULT
static const float theta_pgain[]
static const float phi_ddgain[]
struct FloatQuat stabilization_att_sum_err_quat
angular rates
void attitude_ref_quat_float_init(struct AttRefQuatFloat *ref)
void stabilization_attitude_set_failsafe_setpoint(void)
#define EULERS_FLOAT_OF_BFP(_ef, _ei)
Definition: pprz_algebra.h:642
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
Paparazzi fixed point algebra.
#define STABILIZATION_ATTITUDE_GAIN_NB