Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 
28 
29 #include <stdio.h>
31 #include "math/pprz_algebra_int.h"
32 #include "state.h"
33 #include "generated/airframe.h"
34 
36  {STABILIZATION_ATTITUDE_PHI_PGAIN, STABILIZATION_ATTITUDE_THETA_PGAIN, STABILIZATION_ATTITUDE_PSI_PGAIN },
37  {STABILIZATION_ATTITUDE_PHI_DGAIN, STABILIZATION_ATTITUDE_THETA_DGAIN, STABILIZATION_ATTITUDE_PSI_DGAIN },
38  {STABILIZATION_ATTITUDE_PHI_DDGAIN, STABILIZATION_ATTITUDE_THETA_DDGAIN, STABILIZATION_ATTITUDE_PSI_DDGAIN },
39  {STABILIZATION_ATTITUDE_PHI_IGAIN, STABILIZATION_ATTITUDE_THETA_IGAIN, STABILIZATION_ATTITUDE_PSI_IGAIN }
40 };
41 
42 /* warn if some gains are still negative */
43 #if (STABILIZATION_ATTITUDE_PHI_PGAIN < 0) || \
44  (STABILIZATION_ATTITUDE_THETA_PGAIN < 0) || \
45  (STABILIZATION_ATTITUDE_PSI_PGAIN < 0) || \
46  (STABILIZATION_ATTITUDE_PHI_DGAIN < 0) || \
47  (STABILIZATION_ATTITUDE_THETA_DGAIN < 0) || \
48  (STABILIZATION_ATTITUDE_PSI_DGAIN < 0) || \
49  (STABILIZATION_ATTITUDE_PHI_IGAIN < 0) || \
50  (STABILIZATION_ATTITUDE_THETA_IGAIN < 0) || \
51  (STABILIZATION_ATTITUDE_PSI_IGAIN < 0)
52 #warning "ALL control gains are now positive!!!"
53 #endif
54 
57 
60 
61 #define IERROR_SCALE 1024
62 #define GAIN_PRESCALER_FF 48
63 #define GAIN_PRESCALER_P 48
64 #define GAIN_PRESCALER_D 48
65 #define GAIN_PRESCALER_I 48
66 
68 
70 
73 }
74 
76 
77  /* reset psi setpoint to current psi angle */
79 
81 
84 }
85 
87  /* set failsafe to zero roll/pitch and current heading */
90  stab_att_sp_quat.qx = 0;
91  stab_att_sp_quat.qy = 0;
93 }
94 
96  // copy euler setpoint for debugging
97  memcpy(&stab_att_sp_euler, sp_euler, sizeof(struct Int32Eulers));
100 }
101 
102 #define OFFSET_AND_ROUND(_a, _b) (((_a)+(1<<((_b)-1)))>>(_b))
103 #define OFFSET_AND_ROUND2(_a, _b) (((_a)+(1<<((_b)-1))-((_a)<0?1:0))>>(_b))
104 
105 static void attitude_run_ff(int32_t ff_commands[], struct Int32AttitudeGains *gains, struct Int32Rates *ref_accel)
106 {
107  /* Compute feedforward based on reference acceleration */
108 
109  ff_commands[COMMAND_ROLL] = GAIN_PRESCALER_FF * gains->dd.x * RATE_FLOAT_OF_BFP(ref_accel->p) / (1 << 7);
110  ff_commands[COMMAND_PITCH] = GAIN_PRESCALER_FF * gains->dd.y * RATE_FLOAT_OF_BFP(ref_accel->q) / (1 << 7);
111  ff_commands[COMMAND_YAW] = GAIN_PRESCALER_FF * gains->dd.z * RATE_FLOAT_OF_BFP(ref_accel->r) / (1 << 7);
112 }
113 
114 static void attitude_run_fb(int32_t fb_commands[], struct Int32AttitudeGains *gains, struct Int32Quat *att_err,
115  struct Int32Rates *rate_err, struct Int32Quat *sum_err)
116 {
117  /* PID feedback */
118  fb_commands[COMMAND_ROLL] =
119  GAIN_PRESCALER_P * gains->p.x * QUAT1_FLOAT_OF_BFP(att_err->qx) / 4 +
120  GAIN_PRESCALER_D * gains->d.x * RATE_FLOAT_OF_BFP(rate_err->p) / 16 +
121  GAIN_PRESCALER_I * gains->i.x * QUAT1_FLOAT_OF_BFP(sum_err->qx) / 2;
122 
123  fb_commands[COMMAND_PITCH] =
124  GAIN_PRESCALER_P * gains->p.y * QUAT1_FLOAT_OF_BFP(att_err->qy) / 4 +
125  GAIN_PRESCALER_D * gains->d.y * RATE_FLOAT_OF_BFP(rate_err->q) / 16 +
126  GAIN_PRESCALER_I * gains->i.y * QUAT1_FLOAT_OF_BFP(sum_err->qy) / 2;
127 
128  fb_commands[COMMAND_YAW] =
129  GAIN_PRESCALER_P * gains->p.z * QUAT1_FLOAT_OF_BFP(att_err->qz) / 4 +
130  GAIN_PRESCALER_D * gains->d.z * RATE_FLOAT_OF_BFP(rate_err->r) / 16 +
131  GAIN_PRESCALER_I * gains->i.z * QUAT1_FLOAT_OF_BFP(sum_err->qz) / 2;
132 
133 }
134 
135 void stabilization_attitude_run(bool_t enable_integrator) {
136 
137  /*
138  * Update reference
139  */
141 
142  /*
143  * Compute errors for feedback
144  */
145 
146  /* attitude error */
147  struct Int32Quat att_err;
148  struct Int32Quat* att_quat = stateGetNedToBodyQuat_i();
149  INT32_QUAT_INV_COMP(att_err, *att_quat, stab_att_ref_quat);
150  /* wrap it in the shortest direction */
151  INT32_QUAT_WRAP_SHORTEST(att_err);
152  INT32_QUAT_NORMALIZE(att_err);
153 
154  /* rate error */
155  struct Int32Rates rate_err;
156  struct Int32Rates* body_rate = stateGetBodyRates_i();
157  RATES_DIFF(rate_err, stab_att_ref_rate, *body_rate);
158 
159  /* integrated error */
160  if (enable_integrator) {
161  struct Int32Quat new_sum_err, scaled_att_err;
162  /* update accumulator */
163  scaled_att_err.qi = att_err.qi;
164  scaled_att_err.qx = att_err.qx / IERROR_SCALE;
165  scaled_att_err.qy = att_err.qy / IERROR_SCALE;
166  scaled_att_err.qz = att_err.qz / IERROR_SCALE;
167  INT32_QUAT_COMP(new_sum_err, stabilization_att_sum_err_quat, scaled_att_err);
168  INT32_QUAT_NORMALIZE(new_sum_err);
171  } else {
172  /* reset accumulator */
175  }
176 
177  /* compute the feed forward command */
179 
180  /* compute the feed back command */
181  attitude_run_fb(stabilization_att_fb_cmd, &stabilization_gains, &att_err, &rate_err, &stabilization_att_sum_err_quat);
182 
183  /* sum feedforward and feedback */
184  stabilization_cmd[COMMAND_ROLL] = stabilization_att_fb_cmd[COMMAND_ROLL] + stabilization_att_ff_cmd[COMMAND_ROLL];
185  stabilization_cmd[COMMAND_PITCH] = stabilization_att_fb_cmd[COMMAND_PITCH] + stabilization_att_ff_cmd[COMMAND_PITCH];
186  stabilization_cmd[COMMAND_YAW] = stabilization_att_fb_cmd[COMMAND_YAW] + stabilization_att_ff_cmd[COMMAND_YAW];
187 
188  /* bound the result */
189  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
190  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
191  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
192 }
193 
194 void stabilization_attitude_read_rc(bool_t in_flight) {
195  struct FloatQuat q_sp;
196 #if USE_EARTH_BOUND_RC_SETPOINT
198 #else
200 #endif
202 }
void stabilization_attitude_read_rc(bool_t in_flight)
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:336
int32_t stabilization_att_fb_cmd[COMMANDS_NB]
#define QUAT_BFP_OF_REAL(_qi, _qf)
Definition: pprz_algebra.h:610
int32_t p
in rad/s^2 with INT32_RATE_FRAC
#define INT32_QUAT_ZERO(_q)
Rotation quaternion.
void stabilization_attitude_set_from_eulers_i(struct Int32Eulers *sp_euler)
void stabilization_attitude_set_failsafe_setpoint(void)
#define RATE_FLOAT_OF_BFP(_ai)
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
void stabilization_attitude_read_rc_setpoint_quat_earth_bound_f(struct FloatQuat *q_sp, bool_t in_flight)
#define INT_EULERS_ZERO(_e)
void stabilization_attitude_read_rc_setpoint_quat_f(struct FloatQuat *q_sp, bool_t in_flight)
void stabilization_attitude_run(bool_t enable_integrator)
struct FloatQuat stab_att_ref_quat
static struct Int32Quat * stateGetNedToBodyQuat_i(void)
Get vehicle body attitude quaternion (int).
Definition: state.h:1002
#define GAIN_PRESCALER_D
Roation quaternion.
#define PPRZ_ITRIG_SIN(_s, _a)
Definition: pprz_trig_int.h:40
#define INT32_QUAT_OF_EULERS(_q, _e)
static struct Int32Rates * stateGetBodyRates_i(void)
Get vehicle body angular rate (int).
Definition: state.h:1071
Paparazzi floating point algebra.
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 Int32Quat stab_att_sp_quat
with INT32_QUAT_FRAC
struct Int32AttitudeGains stabilization_gains
struct Int32Eulers stab_att_sp_euler
with INT32_ANGLE_FRAC
void stabilization_attitude_ref_init(void)
#define IERROR_SCALE
void stabilization_attitude_ref_update()
angular rates
signed long int32_t
Definition: types.h:19
int32_t stabilization_att_ff_cmd[COMMANDS_NB]
#define GAIN_PRESCALER_FF
#define QUAT1_FLOAT_OF_BFP(_qi)
API to get/set the generic vehicle states.
#define QUAT_COPY(_qo, _qi)
Definition: pprz_algebra.h:476
static void attitude_run_ff(int32_t ff_commands[], struct Int32AttitudeGains *gains, struct Int32Rates *ref_accel)
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^2 with INT32_RATE_FRAC
#define MAX_PPRZ
Definition: paparazzi.h:8
int32_t r
in rad/s^2 with INT32_RATE_FRAC
#define INT32_QUAT_COMP(_a2c, _a2b, _b2c)
#define GAIN_PRESCALER_P
#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
#define PPRZ_ITRIG_COS(_c, _a)
Definition: pprz_trig_int.h:50
Paparazzi fixed point algebra.
euler angles