Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
stabilization_attitude_euler_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 
28 #include "generated/airframe.h"
29 
32 
33 #include "std.h"
34 #include "paparazzi.h"
36 #include "state.h"
37 
38 #ifndef USE_ATT_REF
39 #define USE_ATT_REF 1
40 #endif
41 
44 
47 
48 float stabilization_att_fb_cmd[COMMANDS_NB];
49 float stabilization_att_ff_cmd[COMMANDS_NB];
50 
51 #if PERIODIC_TELEMETRY
53 
54 static void send_att(struct transport_tx *trans, struct link_device *dev)
55 {
56  struct FloatRates *body_rate = stateGetBodyRates_f();
57  struct FloatEulers *att = stateGetNedToBodyEulers_f();
58  float foo = 0.0;
59  pprz_msg_send_STAB_ATTITUDE_FLOAT(trans, dev, AC_ID,
60  &(body_rate->p), &(body_rate->q), &(body_rate->r),
61  &(att->phi), &(att->theta), &(att->psi),
68  &stabilization_att_fb_cmd[COMMAND_ROLL],
69  &stabilization_att_fb_cmd[COMMAND_PITCH],
70  &stabilization_att_fb_cmd[COMMAND_YAW],
71  &stabilization_att_ff_cmd[COMMAND_ROLL],
72  &stabilization_att_ff_cmd[COMMAND_PITCH],
73  &stabilization_att_ff_cmd[COMMAND_YAW],
74  &stabilization_cmd[COMMAND_ROLL],
75  &stabilization_cmd[COMMAND_PITCH],
76  &stabilization_cmd[COMMAND_YAW],
77  &foo, &foo, &foo);
78 }
79 
80 static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
81 {
82  pprz_msg_send_STAB_ATTITUDE_REF_FLOAT(trans, dev, AC_ID,
95 }
96 #endif
97 
99 {
100 
102 
104  STABILIZATION_ATTITUDE_PHI_PGAIN,
105  STABILIZATION_ATTITUDE_THETA_PGAIN,
106  STABILIZATION_ATTITUDE_PSI_PGAIN);
107 
109  STABILIZATION_ATTITUDE_PHI_DGAIN,
110  STABILIZATION_ATTITUDE_THETA_DGAIN,
111  STABILIZATION_ATTITUDE_PSI_DGAIN);
112 
114  STABILIZATION_ATTITUDE_PHI_IGAIN,
115  STABILIZATION_ATTITUDE_THETA_IGAIN,
116  STABILIZATION_ATTITUDE_PSI_IGAIN);
117 
119  STABILIZATION_ATTITUDE_PHI_DDGAIN,
120  STABILIZATION_ATTITUDE_THETA_DDGAIN,
121  STABILIZATION_ATTITUDE_PSI_DDGAIN);
122 
124 
125 #if PERIODIC_TELEMETRY
126  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_FLOAT, send_att);
127  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_REF_FLOAT, send_att_ref);
128 #endif
129 }
130 
131 void stabilization_attitude_read_rc(bool in_flight, bool in_carefree, bool coordinated_turn)
132 {
133  stabilization_attitude_read_rc_setpoint_eulers_f(&stab_att_sp_euler, in_flight, in_carefree, coordinated_turn);
134 }
135 
137 {
138 
139  /* reset psi setpoint to current psi angle */
141 
143 
145 }
146 
148 {
149  stab_att_sp_euler.phi = 0.0;
150  stab_att_sp_euler.theta = 0.0;
152 }
153 
155 {
157 }
158 
160 {
161  struct FloatVect2 cmd_f;
162  cmd_f.x = ANGLE_FLOAT_OF_BFP(cmd->x);
163  cmd_f.y = ANGLE_FLOAT_OF_BFP(cmd->y);
164 
165  /* Rotate horizontal commands to body frame by psi */
166  float psi = stateGetNedToBodyEulers_f()->psi;
167  float s_psi = sinf(psi);
168  float c_psi = cosf(psi);
169  stab_att_sp_euler.phi = -s_psi * cmd_f.x + c_psi * cmd_f.y;
170  stab_att_sp_euler.theta = -c_psi * cmd_f.x - s_psi * cmd_f.y;
172 }
173 
174 #define MAX_SUM_ERR 200
175 
176 void stabilization_attitude_run(bool in_flight)
177 {
178 
179 #if USE_ATT_REF
180  static const float dt = (1./PERIODIC_FREQUENCY);
182 #else
186 #endif
187 
188  /* Compute feedforward */
189  stabilization_att_ff_cmd[COMMAND_ROLL] =
191  stabilization_att_ff_cmd[COMMAND_PITCH] =
193  stabilization_att_ff_cmd[COMMAND_YAW] =
195 
196  /* Compute feedback */
197  /* attitude error */
198  struct FloatEulers *att_float = stateGetNedToBodyEulers_f();
199  struct FloatEulers att_err;
200  EULERS_DIFF(att_err, att_ref_euler_f.euler, *att_float);
201  FLOAT_ANGLE_NORMALIZE(att_err.psi);
202 
203  if (in_flight) {
204  /* update integrator */
207  } else {
209  }
210 
211  /* rate error */
212  struct FloatRates *rate_float = stateGetBodyRates_f();
213  struct FloatRates rate_err;
214  RATES_DIFF(rate_err, att_ref_euler_f.rate, *rate_float);
215 
216  /* PID */
217 
218  stabilization_att_fb_cmd[COMMAND_ROLL] =
219  stabilization_gains.p.x * att_err.phi +
220  stabilization_gains.d.x * rate_err.p +
222 
223  stabilization_att_fb_cmd[COMMAND_PITCH] =
224  stabilization_gains.p.y * att_err.theta +
225  stabilization_gains.d.y * rate_err.q +
227 
228  stabilization_att_fb_cmd[COMMAND_YAW] =
229  stabilization_gains.p.z * att_err.psi +
230  stabilization_gains.d.z * rate_err.r +
232 
233 
234  stabilization_cmd[COMMAND_ROLL] =
235  (stabilization_att_fb_cmd[COMMAND_ROLL] + stabilization_att_ff_cmd[COMMAND_ROLL]);
236  stabilization_cmd[COMMAND_PITCH] =
237  (stabilization_att_fb_cmd[COMMAND_PITCH] + stabilization_att_ff_cmd[COMMAND_PITCH]);
238  stabilization_cmd[COMMAND_YAW] =
239  (stabilization_att_fb_cmd[COMMAND_YAW] + stabilization_att_ff_cmd[COMMAND_YAW]);
240 
241  /* bound the result */
242  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
243  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
244  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
245 }
#define FLOAT_ANGLE_NORMALIZE(_a)
void attitude_ref_euler_float_enter(struct AttRefEulerFloat *ref, float psi)
static void send_att(struct transport_tx *trans, struct link_device *dev)
float phi
in radians
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
Periodic telemetry system header (includes downlink utility and generated code).
General attitude stabilization interface for rotorcrafts.
#define FLOAT_EULERS_ZERO(_e)
void stabilization_attitude_set_failsafe_setpoint(void)
float r
in rad/s
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:124
Read an attitude setpoint from the RC.
#define FLOAT_RATES_ZERO(_r)
float psi
in radians
float q
in rad/s
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:371
float p
in rad/s
#define EULERS_BOUND_CUBE(_v, _min, _max)
Definition: pprz_algebra.h:316
#define ANGLE_FLOAT_OF_BFP(_ai)
void stabilization_attitude_set_earth_cmd_i(struct Int32Vect2 *cmd, int32_t heading)
euler angles
struct FloatEulers stab_att_sp_euler
with INT32_ANGLE_FRAC
float theta
in radians
void stabilization_attitude_enter(void)
static float heading
Definition: ahrs_infrared.c:45
Paparazzi floating point algebra.
Attitude reference state/output (euler float)
euler angles
struct AttRefEulerFloat att_ref_euler_f
struct FloatAttitudeGains stabilization_gains
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
float stabilization_att_fb_cmd[COMMANDS_NB]
void stabilization_attitude_init(void)
uint16_t foo
Definition: main_demo5.c:59
void attitude_ref_euler_float_update(struct AttRefEulerFloat *ref, struct FloatEulers *sp_eulers, float dt)
void stabilization_attitude_run(bool in_flight)
void attitude_ref_euler_float_init(struct AttRefEulerFloat *ref)
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition: state.h:1200
signed long int32_t
Definition: types.h:19
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
API to get/set the generic vehicle states.
#define EULERS_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:294
struct FloatEulers stabilization_att_sum_err
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:28
#define EULERS_COPY(_a, _b)
Definition: pprz_algebra.h:267
void stabilization_attitude_read_rc_setpoint_eulers_f(struct FloatEulers *sp, bool in_flight, bool in_carefree, bool coordinated_turn)
static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
#define EULERS_ADD(_a, _b)
Definition: pprz_algebra.h:280
void stabilization_attitude_set_rpy_setpoint_i(struct Int32Eulers *rpy)
float stabilization_att_ff_cmd[COMMANDS_NB]
#define MAX_PPRZ
Definition: paparazzi.h:8
float stabilization_attitude_get_heading_f(void)
void stabilization_attitude_read_rc(bool in_flight, bool in_carefree, bool coordinated_turn)
angular rates
#define EULERS_FLOAT_OF_BFP(_ef, _ei)
Definition: pprz_algebra.h:653
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46