Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
stabilization_attitude_quat_int.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  {STABILIZATION_ATTITUDE_PHI_PGAIN, STABILIZATION_ATTITUDE_THETA_PGAIN, STABILIZATION_ATTITUDE_PSI_PGAIN },
40  {STABILIZATION_ATTITUDE_PHI_DGAIN, STABILIZATION_ATTITUDE_THETA_DGAIN, STABILIZATION_ATTITUDE_PSI_DGAIN },
41  {STABILIZATION_ATTITUDE_PHI_DDGAIN, STABILIZATION_ATTITUDE_THETA_DDGAIN, STABILIZATION_ATTITUDE_PSI_DDGAIN },
42  {STABILIZATION_ATTITUDE_PHI_IGAIN, STABILIZATION_ATTITUDE_THETA_IGAIN, STABILIZATION_ATTITUDE_PSI_IGAIN }
43 };
44 
45 /* warn if some gains are still negative */
46 #if (STABILIZATION_ATTITUDE_PHI_PGAIN < 0) || \
47  (STABILIZATION_ATTITUDE_THETA_PGAIN < 0) || \
48  (STABILIZATION_ATTITUDE_PSI_PGAIN < 0) || \
49  (STABILIZATION_ATTITUDE_PHI_DGAIN < 0) || \
50  (STABILIZATION_ATTITUDE_THETA_DGAIN < 0) || \
51  (STABILIZATION_ATTITUDE_PSI_DGAIN < 0) || \
52  (STABILIZATION_ATTITUDE_PHI_IGAIN < 0) || \
53  (STABILIZATION_ATTITUDE_THETA_IGAIN < 0) || \
54  (STABILIZATION_ATTITUDE_PSI_IGAIN < 0)
55 #error "ALL control gains have to be positive!!!"
56 #endif
57 
60 
63 
64 #define IERROR_SCALE 1024
65 #define GAIN_PRESCALER_FF 48
66 #define GAIN_PRESCALER_P 48
67 #define GAIN_PRESCALER_D 48
68 #define GAIN_PRESCALER_I 48
69 
70 #if PERIODIC_TELEMETRY
72 
73 static void send_att(void) { //FIXME really use this message here ?
74  struct Int32Rates* body_rate = stateGetBodyRates_i();
75  struct Int32Eulers* att = stateGetNedToBodyEulers_i();
76  DOWNLINK_SEND_STAB_ATTITUDE_INT(DefaultChannel, DefaultDevice,
77  &(body_rate->p), &(body_rate->q), &(body_rate->r),
78  &(att->phi), &(att->theta), &(att->psi),
85  &stabilization_att_fb_cmd[COMMAND_ROLL],
86  &stabilization_att_fb_cmd[COMMAND_PITCH],
87  &stabilization_att_fb_cmd[COMMAND_YAW],
88  &stabilization_att_ff_cmd[COMMAND_ROLL],
89  &stabilization_att_ff_cmd[COMMAND_PITCH],
90  &stabilization_att_ff_cmd[COMMAND_YAW],
91  &stabilization_cmd[COMMAND_ROLL],
92  &stabilization_cmd[COMMAND_PITCH],
93  &stabilization_cmd[COMMAND_YAW]);
94 }
95 
96 static void send_att_ref(void) {
97  DOWNLINK_SEND_STAB_ATTITUDE_REF_INT(DefaultChannel, DefaultDevice,
110 }
111 
112 static void send_ahrs_ref_quat(void) {
113  struct Int32Quat* quat = stateGetNedToBodyQuat_i();
114  DOWNLINK_SEND_AHRS_REF_QUAT(DefaultChannel, DefaultDevice,
119  &(quat->qi),
120  &(quat->qx),
121  &(quat->qy),
122  &(quat->qz));
123 }
124 #endif
125 
127 
129 
132 
133 #if PERIODIC_TELEMETRY
134  register_periodic_telemetry(DefaultPeriodic, "STAB_ATTITUDE", send_att);
135  register_periodic_telemetry(DefaultPeriodic, "STAB_ATTITUDE_REF", send_att_ref);
136  register_periodic_telemetry(DefaultPeriodic, "AHRS_REF_QUAT", send_ahrs_ref_quat);
137 #endif
138 }
139 
141 
142  /* reset psi setpoint to current psi angle */
144 
146 
149 
150 }
151 
153  /* set failsafe to zero roll/pitch and current heading */
156  stab_att_sp_quat.qx = 0;
157  stab_att_sp_quat.qy = 0;
159 }
160 
162  // stab_att_sp_euler.psi still used in ref..
163  memcpy(&stab_att_sp_euler, rpy, sizeof(struct Int32Eulers));
164 
166 }
167 
169  // stab_att_sp_euler.psi still used in ref..
171 
172  // compute sp_euler phi/theta for debugging/telemetry
173  /* Rotate horizontal commands to body frame by psi */
175  int32_t s_psi, c_psi;
176  PPRZ_ITRIG_SIN(s_psi, psi);
177  PPRZ_ITRIG_COS(c_psi, psi);
178  stab_att_sp_euler.phi = (-s_psi * cmd->x + c_psi * cmd->y) >> INT32_TRIG_FRAC;
179  stab_att_sp_euler.theta = -(c_psi * cmd->x + s_psi * cmd->y) >> INT32_TRIG_FRAC;
180 
181  quat_from_earth_cmd_i(&stab_att_sp_quat, cmd, heading);
182 }
183 
184 #define OFFSET_AND_ROUND(_a, _b) (((_a)+(1<<((_b)-1)))>>(_b))
185 #define OFFSET_AND_ROUND2(_a, _b) (((_a)+(1<<((_b)-1))-((_a)<0?1:0))>>(_b))
186 
187 static void attitude_run_ff(int32_t ff_commands[], struct Int32AttitudeGains *gains, struct Int32Rates *ref_accel)
188 {
189  /* Compute feedforward based on reference acceleration */
190 
191  ff_commands[COMMAND_ROLL] = GAIN_PRESCALER_FF * gains->dd.x * RATE_FLOAT_OF_BFP(ref_accel->p) / (1 << 7);
192  ff_commands[COMMAND_PITCH] = GAIN_PRESCALER_FF * gains->dd.y * RATE_FLOAT_OF_BFP(ref_accel->q) / (1 << 7);
193  ff_commands[COMMAND_YAW] = GAIN_PRESCALER_FF * gains->dd.z * RATE_FLOAT_OF_BFP(ref_accel->r) / (1 << 7);
194 }
195 
196 static void attitude_run_fb(int32_t fb_commands[], struct Int32AttitudeGains *gains, struct Int32Quat *att_err,
197  struct Int32Rates *rate_err, struct Int32Quat *sum_err)
198 {
199  /* PID feedback */
200  fb_commands[COMMAND_ROLL] =
201  GAIN_PRESCALER_P * gains->p.x * QUAT1_FLOAT_OF_BFP(att_err->qx) / 4 +
202  GAIN_PRESCALER_D * gains->d.x * RATE_FLOAT_OF_BFP(rate_err->p) / 16 +
203  GAIN_PRESCALER_I * gains->i.x * QUAT1_FLOAT_OF_BFP(sum_err->qx) / 2;
204 
205  fb_commands[COMMAND_PITCH] =
206  GAIN_PRESCALER_P * gains->p.y * QUAT1_FLOAT_OF_BFP(att_err->qy) / 4 +
207  GAIN_PRESCALER_D * gains->d.y * RATE_FLOAT_OF_BFP(rate_err->q) / 16 +
208  GAIN_PRESCALER_I * gains->i.y * QUAT1_FLOAT_OF_BFP(sum_err->qy) / 2;
209 
210  fb_commands[COMMAND_YAW] =
211  GAIN_PRESCALER_P * gains->p.z * QUAT1_FLOAT_OF_BFP(att_err->qz) / 4 +
212  GAIN_PRESCALER_D * gains->d.z * RATE_FLOAT_OF_BFP(rate_err->r) / 16 +
213  GAIN_PRESCALER_I * gains->i.z * QUAT1_FLOAT_OF_BFP(sum_err->qz) / 2;
214 
215 }
216 
217 void stabilization_attitude_run(bool_t enable_integrator) {
218 
219  /*
220  * Update reference
221  */
223 
224  /*
225  * Compute errors for feedback
226  */
227 
228  /* attitude error */
229  struct Int32Quat att_err;
230  struct Int32Quat* att_quat = stateGetNedToBodyQuat_i();
231  INT32_QUAT_INV_COMP(att_err, *att_quat, stab_att_ref_quat);
232  /* wrap it in the shortest direction */
233  INT32_QUAT_WRAP_SHORTEST(att_err);
234  INT32_QUAT_NORMALIZE(att_err);
235 
236  /* rate error */
237  const struct Int32Rates rate_ref_scaled = {
241  struct Int32Rates rate_err;
242  struct Int32Rates* body_rate = stateGetBodyRates_i();
243  RATES_DIFF(rate_err, rate_ref_scaled, (*body_rate));
244 
245  /* integrated error */
246  if (enable_integrator) {
247  struct Int32Quat new_sum_err, scaled_att_err;
248  /* update accumulator */
249  scaled_att_err.qi = att_err.qi;
250  scaled_att_err.qx = att_err.qx / IERROR_SCALE;
251  scaled_att_err.qy = att_err.qy / IERROR_SCALE;
252  scaled_att_err.qz = att_err.qz / IERROR_SCALE;
253  INT32_QUAT_COMP(new_sum_err, stabilization_att_sum_err_quat, scaled_att_err);
254  INT32_QUAT_NORMALIZE(new_sum_err);
257  } else {
258  /* reset accumulator */
261  }
262 
263  /* compute the feed forward command */
265 
266  /* compute the feed back command */
267  attitude_run_fb(stabilization_att_fb_cmd, &stabilization_gains, &att_err, &rate_err, &stabilization_att_sum_err_quat);
268 
269  /* sum feedforward and feedback */
270  stabilization_cmd[COMMAND_ROLL] = stabilization_att_fb_cmd[COMMAND_ROLL] + stabilization_att_ff_cmd[COMMAND_ROLL];
271  stabilization_cmd[COMMAND_PITCH] = stabilization_att_fb_cmd[COMMAND_PITCH] + stabilization_att_ff_cmd[COMMAND_PITCH];
272  stabilization_cmd[COMMAND_YAW] = stabilization_att_fb_cmd[COMMAND_YAW] + stabilization_att_ff_cmd[COMMAND_YAW];
273 
274  /* bound the result */
275  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
276  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
277  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
278 }
279 
280 void stabilization_attitude_read_rc(bool_t in_flight, bool_t in_carefree, bool_t coordinated_turn) {
281  struct FloatQuat q_sp;
282 #if USE_EARTH_BOUND_RC_SETPOINT
283  stabilization_attitude_read_rc_setpoint_quat_earth_bound_f(&q_sp, in_flight, in_carefree, coordinated_turn);
284 #else
285  stabilization_attitude_read_rc_setpoint_quat_f(&q_sp, in_flight, in_carefree, coordinated_turn);
286 #endif
288 }
void stabilization_attitude_read_rc(bool_t in_flight, bool_t in_carefree, bool_t coordinated_turn)
int32_t phi
in rad with INT32_ANGLE_FRAC
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:361
int32_t stabilization_att_fb_cmd[COMMANDS_NB]
#define QUAT_BFP_OF_REAL(_qi, _qf)
Definition: pprz_algebra.h:665
Quaternion transformation functions.
int32_t p
in rad/s with INT32_RATE_FRAC
Periodic telemetry system header (includes downlink utility and generated code).
#define INT32_QUAT_ZERO(_q)
Rotation quaternion.
float psi
in radians
void stabilization_attitude_set_failsafe_setpoint(void)
#define RATE_FLOAT_OF_BFP(_ai)
struct Int32Eulers stab_att_sp_euler
with INT32_ANGLE_FRAC
int32_t stabilization_attitude_get_heading_i(void)
#define INT32_QUAT_NORMALIZE(q)
Read an attitude setpoint from the RC.
void stabilization_attitude_ref_enter()
void stabilization_attitude_init(void)
#define GAIN_PRESCALER_I
#define OFFSET_AND_ROUND(_a, _b)
int32_t theta
in rad with INT32_ANGLE_FRAC
#define INT_EULERS_ZERO(_e)
#define INT32_RATE_FRAC
float theta
in radians
#define INT32_TRIG_FRAC
void stabilization_attitude_run(bool_t enable_integrator)
struct FloatQuat stab_att_ref_quat
bool_t register_periodic_telemetry(struct pprz_telemetry *_pt, const char *_msg, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:38
static struct Int32Quat * stateGetNedToBodyQuat_i(void)
Get vehicle body attitude quaternion (int).
Definition: state.h:1002
#define GAIN_PRESCALER_D
float p
in rad/s
Roation quaternion.
#define PPRZ_ITRIG_SIN(_s, _a)
Definition: pprz_trig_int.h:40
static struct Int32Rates * stateGetBodyRates_i(void)
Get vehicle body angular rate (int).
Definition: state.h:1071
float heading
Definition: ahrs_infrared.c:40
struct FloatEulers stab_att_ref_euler
with REF_ANGLE_FRAC
Paparazzi floating point algebra.
float phi
in radians
#define REF_RATE_FRAC
void quat_from_rpy_cmd_i(struct Int32Quat *quat, struct Int32Eulers *rpy)
struct Int32Eulers stabilization_att_sum_err
static void attitude_run_fb(int32_t fb_commands[], struct Int32AttitudeGains *gains, struct Int32Quat *att_err, struct Int32Rates *rate_err, struct Int32Quat *sum_err)
struct Int32AttitudeGains stabilization_gains
void stabilization_attitude_ref_init(void)
#define IERROR_SCALE
void quat_from_earth_cmd_i(struct Int32Quat *quat, struct Int32Vect2 *cmd, int32_t heading)
void stabilization_attitude_ref_update()
angular rates
float r
in rad/s
signed long int32_t
Definition: types.h:19
int32_t stabilization_att_ff_cmd[COMMANDS_NB]
#define GAIN_PRESCALER_FF
struct FloatQuat stab_att_sp_quat
#define QUAT1_FLOAT_OF_BFP(_qi)
API to get/set the generic vehicle states.
#define QUAT_COPY(_qo, _qi)
Definition: pprz_algebra.h:512
static void attitude_run_ff(int32_t ff_commands[], struct Int32AttitudeGains *gains, struct Int32Rates *ref_accel)
void stabilization_attitude_read_rc_setpoint_quat_f(struct FloatQuat *q_sp, bool_t in_flight, bool_t in_carefree, bool_t coordinated_turn)
Read attitude setpoint from RC as quaternion Interprets the stick positions as axes.
int32_t psi
in rad with INT32_ANGLE_FRAC
General stabilization interface for rotorcrafts.
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:28
#define INT32_QUAT_INV_COMP(_b2c, _a2b, _a2c)
void stabilization_attitude_enter(void)
int32_t q
in rad/s with INT32_RATE_FRAC
float q
in rad/s
#define MAX_PPRZ
Definition: paparazzi.h:8
int32_t r
in rad/s with INT32_RATE_FRAC
#define INT32_QUAT_COMP(_a2c, _a2b, _b2c)
#define GAIN_PRESCALER_P
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition: state.h:1012
void stabilization_attitude_read_rc_setpoint_quat_earth_bound_f(struct FloatQuat *q_sp, bool_t in_flight, bool_t in_carefree, bool_t coordinated_turn)
void stabilization_attitude_set_rpy_setpoint_i(struct Int32Eulers *rpy)
#define INT32_EULERS_OF_QUAT(_e, _q)
struct FloatRates stab_att_ref_rate
with REF_RATE_FRAC
struct Int32Quat stabilization_att_sum_err_quat
#define INT32_QUAT_WRAP_SHORTEST(q)
struct FloatRates stab_att_ref_accel
with REF_ACCEL_FRAC
void stabilization_attitude_set_earth_cmd_i(struct Int32Vect2 *cmd, int32_t heading)
#define PPRZ_ITRIG_COS(_c, _a)
Definition: pprz_trig_int.h:50
Paparazzi fixed point algebra.
euler angles