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_float.c
Go to the documentation of this file.
1 /*
2  * $Id: stabilization_attitude_quat_float.c 3787 2009-07-24 15:33:54Z poine $
3  *
4  * Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 
30 #include <stdio.h>
32 #include "math/pprz_algebra_int.h"
33 #include "subsystems/ahrs.h"
34 #include "generated/airframe.h"
37 
39 
42 
43 float stabilization_att_fb_cmd[COMMANDS_NB];
44 float stabilization_att_ff_cmd[COMMANDS_NB];
45 
47 
48 static const float phi_pgain[] = STABILIZATION_ATTITUDE_FLOAT_PHI_PGAIN;
49 static const float theta_pgain[] = STABILIZATION_ATTITUDE_FLOAT_THETA_PGAIN;
50 static const float psi_pgain[] = STABILIZATION_ATTITUDE_FLOAT_PSI_PGAIN;
51 
52 static const float phi_dgain[] = STABILIZATION_ATTITUDE_FLOAT_PHI_DGAIN;
53 static const float theta_dgain[] = STABILIZATION_ATTITUDE_FLOAT_THETA_DGAIN;
54 static const float psi_dgain[] = STABILIZATION_ATTITUDE_FLOAT_PSI_DGAIN;
55 
56 static const float phi_igain[] = STABILIZATION_ATTITUDE_FLOAT_PHI_IGAIN;
57 static const float theta_igain[] = STABILIZATION_ATTITUDE_FLOAT_THETA_IGAIN;
58 static const float psi_igain[] = STABILIZATION_ATTITUDE_FLOAT_PSI_IGAIN;
59 
60 static const float phi_ddgain[] = STABILIZATION_ATTITUDE_FLOAT_PHI_DDGAIN;
61 static const float theta_ddgain[] = STABILIZATION_ATTITUDE_FLOAT_THETA_DDGAIN;
62 static const float psi_ddgain[] = STABILIZATION_ATTITUDE_FLOAT_PSI_DDGAIN;
63 
64 static const float phi_dgain_d[] = STABILIZATION_ATTITUDE_FLOAT_PHI_DGAIN_D;
65 static const float theta_dgain_d[] = STABILIZATION_ATTITUDE_FLOAT_THETA_DGAIN_D;
66 static const float psi_dgain_d[] = STABILIZATION_ATTITUDE_FLOAT_PSI_DGAIN_D;
67 
68 static const float phi_pgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PHI_PGAIN_SURFACE;
69 static const float theta_pgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_THETA_PGAIN_SURFACE;
70 static const float psi_pgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PSI_PGAIN_SURFACE;
71 
72 static const float phi_dgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PHI_DGAIN_SURFACE;
73 static const float theta_dgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_THETA_DGAIN_SURFACE;
74 static const float psi_dgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PSI_DGAIN_SURFACE;
75 
76 static const float phi_igain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PHI_IGAIN_SURFACE;
77 static const float theta_igain_surface[] = STABILIZATION_ATTITUDE_FLOAT_THETA_IGAIN_SURFACE;
78 static const float psi_igain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PSI_IGAIN_SURFACE;
79 
80 static const float phi_ddgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PHI_DDGAIN_SURFACE;
81 static const float theta_ddgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_THETA_DDGAIN_SURFACE;
82 static const float psi_ddgain_surface[] = STABILIZATION_ATTITUDE_FLOAT_PSI_DDGAIN_SURFACE;
83 
84 #define IERROR_SCALE 1024
85 
87 
89 
90  for (int i = 0; i < STABILIZATION_ATTITUDE_FLOAT_GAIN_NB; i++) {
100  }
101 
104 }
105 
107 {
109  // This could be bad -- Just say no.
110  return;
111  }
112  gain_idx = idx;
114 }
115 
117 
119 
122 }
123 
124 #ifndef GAIN_PRESCALER_FF
125 #define GAIN_PRESCALER_FF 1
126 #endif
127 static void attitude_run_ff(float ff_commands[], struct FloatAttitudeGains *gains, struct FloatRates *ref_accel)
128 {
129  /* Compute feedforward based on reference acceleration */
130 
131  ff_commands[COMMAND_ROLL] = GAIN_PRESCALER_FF * gains->dd.x * ref_accel->p;
132  ff_commands[COMMAND_PITCH] = GAIN_PRESCALER_FF * gains->dd.y * ref_accel->q;
133  ff_commands[COMMAND_YAW] = GAIN_PRESCALER_FF * gains->dd.z * ref_accel->r;
134  ff_commands[COMMAND_ROLL_SURFACE] = GAIN_PRESCALER_FF * gains->surface_dd.x * ref_accel->p;
135  ff_commands[COMMAND_PITCH_SURFACE] = GAIN_PRESCALER_FF * gains->surface_dd.y * ref_accel->q;
136  ff_commands[COMMAND_YAW_SURFACE] = GAIN_PRESCALER_FF * gains->surface_dd.z * ref_accel->r;
137 }
138 
139 #ifndef GAIN_PRESCALER_P
140 #define GAIN_PRESCALER_P 1
141 #endif
142 #ifndef GAIN_PRESCALER_D
143 #define GAIN_PRESCALER_D 1
144 #endif
145 #ifndef GAIN_PRESCALER_I
146 #define GAIN_PRESCALER_I 1
147 #endif
148 static void attitude_run_fb(float fb_commands[], struct FloatAttitudeGains *gains, struct FloatQuat *att_err,
149  struct FloatRates *rate_err, struct FloatRates *rate_err_d, struct FloatQuat *sum_err)
150 {
151  /* PID feedback */
152  fb_commands[COMMAND_ROLL] =
153  GAIN_PRESCALER_P * gains->p.x * att_err->qx +
154  GAIN_PRESCALER_D * gains->d.x * rate_err->p +
155  GAIN_PRESCALER_D * gains->rates_d.x * rate_err_d->p +
156  GAIN_PRESCALER_I * gains->i.x * sum_err->qx;
157 
158  fb_commands[COMMAND_PITCH] =
159  GAIN_PRESCALER_P * gains->p.y * att_err->qy +
160  GAIN_PRESCALER_D * gains->d.y * rate_err->q +
161  GAIN_PRESCALER_D * gains->rates_d.y * rate_err_d->q +
162  GAIN_PRESCALER_I * gains->i.y * sum_err->qy;
163 
164  fb_commands[COMMAND_YAW] =
165  GAIN_PRESCALER_P * gains->p.z * att_err->qz +
166  GAIN_PRESCALER_D * gains->d.z * rate_err->r +
167  GAIN_PRESCALER_D * gains->rates_d.z * rate_err_d->r +
168  GAIN_PRESCALER_I * gains->i.z * sum_err->qz;
169 
170  fb_commands[COMMAND_ROLL_SURFACE] =
171  GAIN_PRESCALER_P * gains->surface_p.x * att_err->qx +
172  GAIN_PRESCALER_D * gains->surface_d.x * rate_err->p +
173  GAIN_PRESCALER_I * gains->surface_i.x * sum_err->qx;
174 
175  fb_commands[COMMAND_PITCH_SURFACE] =
176  GAIN_PRESCALER_P * gains->surface_p.y * att_err->qy +
177  GAIN_PRESCALER_D * gains->surface_d.y * rate_err->q +
178  GAIN_PRESCALER_I * gains->surface_i.y * sum_err->qy;
179 
180  fb_commands[COMMAND_YAW_SURFACE] =
181  GAIN_PRESCALER_P * gains->surface_p.z * att_err->qz +
182  GAIN_PRESCALER_D * gains->surface_d.z * rate_err->r +
183  GAIN_PRESCALER_I * gains->surface_i.z * sum_err->qz;
184 
185 }
186 
187 void stabilization_attitude_run(bool_t enable_integrator) {
188 
189  /*
190  * Update reference
191  */
193 
194  /*
195  * Compute errors for feedback
196  */
197 
198  /* attitude error */
199  struct FloatQuat att_err;
201  /* wrap it in the shortest direction */
202  FLOAT_QUAT_WRAP_SHORTEST(att_err);
203 
204  /* rate error */
205  struct FloatRates rate_err;
207 
208  /* integrated error */
209  if (enable_integrator) {
210  struct FloatQuat new_sum_err, scaled_att_err;
211  /* update accumulator */
212  scaled_att_err.qi = att_err.qi;
213  scaled_att_err.qx = att_err.qx / IERROR_SCALE;
214  scaled_att_err.qy = att_err.qy / IERROR_SCALE;
215  scaled_att_err.qz = att_err.qz / IERROR_SCALE;
216  FLOAT_QUAT_COMP(new_sum_err, stabilization_att_sum_err_quat, scaled_att_err);
217  FLOAT_QUAT_NORMALIZE(new_sum_err);
220  } else {
221  /* reset accumulator */
224  }
225 
227 
229 
230  // FIXME: this is very dangerous! only works if this really includes all commands
231  for (int i = COMMAND_ROLL; i <= COMMAND_YAW_SURFACE; i++) {
233  }
234 
235  /* bound the result */
236  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
237  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
238  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
239 }
240 
241 void stabilization_attitude_read_rc(bool_t in_flight) {
242 
243  stabilization_attitude_read_rc_setpoint_quat_float(&stab_att_sp_quat, in_flight);
244  //FLOAT_QUAT_WRAP_SHORTEST(stab_att_sp_quat);
245 }
#define FLOAT_QUAT_COPY(_qo, _qi)
void stabilization_attitude_ref_schedule(uint8_t idx)
struct FloatRates body_rate_d
Definition: ahrs.h:70
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:338
static const float theta_igain_surface[]
void stabilization_attitude_ref_enter()
Attitude and Heading Reference System interface.
static const float theta_dgain[]
static const float theta_ddgain[]
static const float theta_ddgain_surface[]
static int gain_idx
static const float phi_igain[]
angular rates
#define STABILIZATION_ATTITUDE_FLOAT_GAIN_NB
static const float psi_dgain_d[]
static const float theta_pgain_surface[]
float stabilization_att_fb_cmd[COMMANDS_NB]
Read an attitude setpoint from the RC.
struct FloatEulers stabilization_att_sum_err_eulers
static const float phi_dgain_surface[]
void stabilization_attitude_ref_init(void)
static const float phi_pgain[]
#define STABILIZATION_ATTITUDE_FLOAT_GAIN_IDX_DEFAULT
static const float phi_pgain_surface[]
struct FloatQuat stab_att_ref_quat
euler angles
static const float psi_ddgain_surface[]
static void attitude_run_ff(float ff_commands[], struct FloatAttitudeGains *gains, struct FloatRates *ref_accel)
float p
in rad/s^2
static const float phi_dgain_d[]
Roation quaternion.
void stabilization_attitude_gain_schedule(uint8_t idx)
void stabilization_attitude_read_rc(bool_t in_flight)
void stabilization_attitude_enter(void)
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)
struct AhrsFloat ahrs_float
global AHRS state (floating point version)
Definition: ahrs.c:25
static const float psi_ddgain[]
static const float psi_pgain_surface[]
struct FloatQuat ltp_to_body_quat
Rotation from LocalTangentPlane to body frame as unit quaternion.
Definition: ahrs.h:66
struct FloatAttitudeGains stabilization_gains[STABILIZATION_ATTITUDE_FLOAT_GAIN_NB]
static const float theta_dgain_d[]
static const float psi_pgain[]
void stabilization_attitude_run(bool_t enable_integrator)
void stabilization_attitude_init(void)
#define FLOAT_QUAT_WRAP_SHORTEST(_q)
static const float psi_igain[]
static const float theta_igain[]
float r
in rad/s^2
#define FLOAT_QUAT_INV_COMP(_b2c, _a2b, _a2c)
static const float phi_dgain[]
static const float theta_dgain_surface[]
#define FLOAT_QUAT_NORMALIZE(_q)
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:99
static const float phi_ddgain_surface[]
#define FLOAT_EULERS_ZERO(_e)
#define FLOAT_QUAT_COMP(_a2c, _a2b, _b2c)
struct FloatQuat stab_att_sp_quat
unsigned char uint8_t
Definition: types.h:14
#define FLOAT_EULERS_OF_QUAT(_e, _q)
General stabilization interface for rotorcrafts.
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:28
static const float phi_igain_surface[]
void stabilization_attitude_ref_update(void)
float q
in rad/s^2
struct FloatRates body_rate
Rotational velocity in body frame.
Definition: ahrs.h:69
float stabilization_att_ff_cmd[COMMANDS_NB]
#define MAX_PPRZ
Definition: paparazzi.h:8
static const float psi_igain_surface[]
static const float theta_pgain[]
static const float phi_ddgain[]
#define FLOAT_QUAT_ZERO(_q)
struct FloatQuat stabilization_att_sum_err_quat
struct FloatRates stab_att_ref_rate
with REF_RATE_FRAC
struct FloatRates stab_att_ref_accel
with REF_ACCEL_FRAC
Paparazzi fixed point algebra.
static const float psi_dgain_surface[]