Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros 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 #if USE_SETPOINTS_WITH_TRANSITIONS
31 #endif
32 
33 #include <stdio.h>
35 #include "math/pprz_algebra_int.h"
36 #include "subsystems/ahrs.h"
37 #include "generated/airframe.h"
38 
40  {STABILIZATION_ATTITUDE_PHI_PGAIN, STABILIZATION_ATTITUDE_THETA_PGAIN, STABILIZATION_ATTITUDE_PSI_PGAIN },
41  {STABILIZATION_ATTITUDE_PHI_DGAIN, STABILIZATION_ATTITUDE_THETA_DGAIN, STABILIZATION_ATTITUDE_PSI_DGAIN },
42  {STABILIZATION_ATTITUDE_PHI_DDGAIN, STABILIZATION_ATTITUDE_THETA_DDGAIN, STABILIZATION_ATTITUDE_PSI_DDGAIN },
43  {STABILIZATION_ATTITUDE_PHI_IGAIN, STABILIZATION_ATTITUDE_THETA_IGAIN, STABILIZATION_ATTITUDE_PSI_IGAIN }
44 };
45 
46 /* warn if some gains are still negative */
47 #if (STABILIZATION_ATTITUDE_PHI_PGAIN < 0) || \
48  (STABILIZATION_ATTITUDE_THETA_PGAIN < 0) || \
49  (STABILIZATION_ATTITUDE_PSI_PGAIN < 0) || \
50  (STABILIZATION_ATTITUDE_PHI_DGAIN < 0) || \
51  (STABILIZATION_ATTITUDE_THETA_DGAIN < 0) || \
52  (STABILIZATION_ATTITUDE_PSI_DGAIN < 0) || \
53  (STABILIZATION_ATTITUDE_PHI_IGAIN < 0) || \
54  (STABILIZATION_ATTITUDE_THETA_IGAIN < 0) || \
55  (STABILIZATION_ATTITUDE_PSI_IGAIN < 0)
56 #warning "ALL control gains are now positive!!!"
57 #endif
58 
61 
64 
65 #define IERROR_SCALE 1024
66 #define GAIN_PRESCALER_FF 48
67 #define GAIN_PRESCALER_P 48
68 #define GAIN_PRESCALER_D 48
69 #define GAIN_PRESCALER_I 48
70 
72 
74 
77 }
78 
80 
81 #if !USE_SETPOINTS_WITH_TRANSITIONS
82  /* reset psi setpoint to current psi angle */
84 #endif
85 
87 
90 }
91 
92 #define OFFSET_AND_ROUND(_a, _b) (((_a)+(1<<((_b)-1)))>>(_b))
93 #define OFFSET_AND_ROUND2(_a, _b) (((_a)+(1<<((_b)-1))-((_a)<0?1:0))>>(_b))
94 
95 static void attitude_run_ff(int32_t ff_commands[], struct Int32AttitudeGains *gains, struct Int32Rates *ref_accel)
96 {
97  /* Compute feedforward based on reference acceleration */
98 
99  ff_commands[COMMAND_ROLL] = GAIN_PRESCALER_FF * gains->dd.x * RATE_FLOAT_OF_BFP(ref_accel->p) / (1 << 7);
100  ff_commands[COMMAND_PITCH] = GAIN_PRESCALER_FF * gains->dd.y * RATE_FLOAT_OF_BFP(ref_accel->q) / (1 << 7);
101  ff_commands[COMMAND_YAW] = GAIN_PRESCALER_FF * gains->dd.z * RATE_FLOAT_OF_BFP(ref_accel->r) / (1 << 7);
102 }
103 
104 static void attitude_run_fb(int32_t fb_commands[], struct Int32AttitudeGains *gains, struct Int32Quat *att_err,
105  struct Int32Rates *rate_err, struct Int32Quat *sum_err)
106 {
107  /* PID feedback */
108  fb_commands[COMMAND_ROLL] =
109  GAIN_PRESCALER_P * gains->p.x * QUAT1_FLOAT_OF_BFP(att_err->qx) / 4 +
110  GAIN_PRESCALER_D * gains->d.x * RATE_FLOAT_OF_BFP(rate_err->p) / 16 +
111  GAIN_PRESCALER_I * gains->i.x * QUAT1_FLOAT_OF_BFP(sum_err->qx) / 2;
112 
113  fb_commands[COMMAND_PITCH] =
114  GAIN_PRESCALER_P * gains->p.y * QUAT1_FLOAT_OF_BFP(att_err->qy) / 4 +
115  GAIN_PRESCALER_D * gains->d.y * RATE_FLOAT_OF_BFP(rate_err->q) / 16 +
116  GAIN_PRESCALER_I * gains->i.y * QUAT1_FLOAT_OF_BFP(sum_err->qy) / 2;
117 
118  fb_commands[COMMAND_YAW] =
119  GAIN_PRESCALER_P * gains->p.z * QUAT1_FLOAT_OF_BFP(att_err->qz) / 4 +
120  GAIN_PRESCALER_D * gains->d.z * RATE_FLOAT_OF_BFP(rate_err->r) / 16 +
121  GAIN_PRESCALER_I * gains->i.z * QUAT1_FLOAT_OF_BFP(sum_err->qz) / 2;
122 
123 }
124 
125 void stabilization_attitude_run(bool_t enable_integrator) {
126 
127  /*
128  * Update reference
129  */
131 
132  /*
133  * Compute errors for feedback
134  */
135 
136  /* attitude error */
137  struct Int32Quat att_err;
139  /* wrap it in the shortest direction */
140  INT32_QUAT_WRAP_SHORTEST(att_err);
141  INT32_QUAT_NORMALIZE(att_err);
142 
143  /* rate error */
144  struct Int32Rates rate_err;
146 
147  /* integrated error */
148  if (enable_integrator) {
149  struct Int32Quat new_sum_err, scaled_att_err;
150  /* update accumulator */
151  scaled_att_err.qi = att_err.qi;
152  scaled_att_err.qx = att_err.qx / IERROR_SCALE;
153  scaled_att_err.qy = att_err.qy / IERROR_SCALE;
154  scaled_att_err.qz = att_err.qz / IERROR_SCALE;
155  INT32_QUAT_COMP(new_sum_err, stabilization_att_sum_err_quat, scaled_att_err);
156  INT32_QUAT_NORMALIZE(new_sum_err);
159  } else {
160  /* reset accumulator */
163  }
164 
165  /* compute the feed forward command */
167 
168  /* compute the feed back command */
169  attitude_run_fb(stabilization_att_fb_cmd, &stabilization_gains, &att_err, &rate_err, &stabilization_att_sum_err_quat);
170 
171  /* sum feedforward and feedback */
172  stabilization_cmd[COMMAND_ROLL] = stabilization_att_fb_cmd[COMMAND_ROLL] + stabilization_att_ff_cmd[COMMAND_ROLL];
173  stabilization_cmd[COMMAND_PITCH] = stabilization_att_fb_cmd[COMMAND_PITCH] + stabilization_att_ff_cmd[COMMAND_PITCH];
174  stabilization_cmd[COMMAND_YAW] = stabilization_att_fb_cmd[COMMAND_YAW] + stabilization_att_ff_cmd[COMMAND_YAW];
175 
176  /* bound the result */
177  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
178  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
179  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
180 }
181 
182 void stabilization_attitude_read_rc(bool_t in_flight) {
183 
184 #if USE_SETPOINTS_WITH_TRANSITIONS
186 #else
188  /* update quaternion setpoint from euler setpoint */
191 #endif
192 
193 }
void stabilization_attitude_read_rc(bool_t in_flight)
struct Int32Rates body_rate
Rotational velocity in body frame.
Definition: ahrs.h:52
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:338
int32_t stabilization_att_fb_cmd[COMMANDS_NB]
void stabilization_attitude_ref_enter()
Attitude and Heading Reference System interface.
int32_t p
in rad/s^2 with INT32_RATE_FRAC
#define INT32_QUAT_ZERO(_q)
Rotation quaternion.
float psi
in radians
#define RATE_FLOAT_OF_BFP(_ai)
#define INT32_QUAT_NORMALIZE(q)
Read an attitude setpoint from the RC.
struct Ahrs ahrs
global AHRS state (fixed point version)
Definition: ahrs.c:24
void stabilization_attitude_init(void)
#define GAIN_PRESCALER_I
void stabilization_attitude_ref_init(void)
#define INT_EULERS_ZERO(_e)
void stabilization_attitude_run(bool_t enable_integrator)
struct FloatQuat stab_att_ref_quat
#define GAIN_PRESCALER_D
struct FloatEulers stab_att_sp_euler
with INT32_ANGLE_FRAC
#define INT32_QUAT_OF_EULERS(_q, _e)
Paparazzi floating point algebra.
static void stabilization_attitude_read_rc_setpoint_eulers(struct Int32Eulers *sp, bool_t in_flight)
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 ltp_to_body_quat
Rotation from LocalTangentPlane to body frame as unit quaternion.
Definition: ahrs.h:49
struct Int32AttitudeGains stabilization_gains
#define IERROR_SCALE
angular rates
signed long int32_t
Definition: types.h:19
int32_t stabilization_att_ff_cmd[COMMANDS_NB]
#define GAIN_PRESCALER_FF
struct Int32Eulers ltp_to_body_euler
Rotation from LocalTangentPlane to body frame as Euler angles.
Definition: ahrs.h:50
struct FloatQuat stab_att_sp_quat
#define QUAT1_FLOAT_OF_BFP(_qi)
#define QUAT_COPY(_qo, _qi)
Definition: pprz_algebra.h:478
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
void stabilization_attitude_ref_update(void)
#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
void stabilization_attitude_read_rc_absolute(bool_t in_flight)
#define INT32_QUAT_WRAP_SHORTEST(q)
struct FloatRates stab_att_ref_accel
with REF_ACCEL_FRAC
Paparazzi fixed point algebra.
euler angles