Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
stabilization_attitude_euler_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 #include "generated/airframe.h"
29 
32 
33 #include "std.h"
34 #include "paparazzi.h"
35 #include "math/pprz_algebra_int.h"
36 #include "state.h"
37 
39 #ifndef USE_ATTITUDE_REF
40 #define USE_ATTITUDE_REF 1
41 #endif
42 
44 #ifndef STABILIZATION_ATTITUDE_PHI_FFDGAIN
45 #define STABILIZATION_ATTITUDE_PHI_FFDGAIN 0
46 #endif
47 #ifndef STABILIZATION_ATTITUDE_THETA_FFDGAIN
48 #define STABILIZATION_ATTITUDE_THETA_FFDGAIN 0
49 #endif
50 #ifndef STABILIZATION_ATTITUDE_PSI_FFDGAIN
51 #define STABILIZATION_ATTITUDE_PSI_FFDGAIN 0
52 #endif
53 
55 
56 /* warn if some gains are still negative */
57 #if (STABILIZATION_ATTITUDE_PHI_PGAIN < 0) || \
58  (STABILIZATION_ATTITUDE_THETA_PGAIN < 0) || \
59  (STABILIZATION_ATTITUDE_PSI_PGAIN < 0) || \
60  (STABILIZATION_ATTITUDE_PHI_DGAIN < 0) || \
61  (STABILIZATION_ATTITUDE_THETA_DGAIN < 0) || \
62  (STABILIZATION_ATTITUDE_PSI_DGAIN < 0) || \
63  (STABILIZATION_ATTITUDE_PHI_IGAIN < 0) || \
64  (STABILIZATION_ATTITUDE_THETA_IGAIN < 0) || \
65  (STABILIZATION_ATTITUDE_PSI_IGAIN < 0)
66 #error "ALL control gains have to be positive!!!"
67 #endif
68 
70 
73 
76 
77 static inline void reset_psi_ref_from_body(void)
78 {
79  //sp has been set from body using stabilization_attitude_get_yaw_i, use that value
83 }
84 
85 #if PERIODIC_TELEMETRY
87 
88 static void send_att(struct transport_tx *trans, struct link_device *dev)
89 {
90  struct Int32Rates *body_rate = stateGetBodyRates_i();
91  struct Int32Eulers *att = stateGetNedToBodyEulers_i();
92  pprz_msg_send_STAB_ATTITUDE_INT(trans, dev, AC_ID,
93  &(body_rate->p), &(body_rate->q), &(body_rate->r),
94  &(att->phi), &(att->theta), &(att->psi),
101  &stabilization_att_fb_cmd[COMMAND_ROLL],
102  &stabilization_att_fb_cmd[COMMAND_PITCH],
103  &stabilization_att_fb_cmd[COMMAND_YAW],
104  &stabilization_att_ff_cmd[COMMAND_ROLL],
105  &stabilization_att_ff_cmd[COMMAND_PITCH],
106  &stabilization_att_ff_cmd[COMMAND_YAW],
107  &stabilization_cmd[COMMAND_ROLL],
108  &stabilization_cmd[COMMAND_PITCH],
109  &stabilization_cmd[COMMAND_YAW]);
110 }
111 
112 static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
113 {
114  pprz_msg_send_STAB_ATTITUDE_REF_INT(trans, dev, AC_ID,
127 }
128 #endif
129 
131 {
132 
134 
136 
137 
139  STABILIZATION_ATTITUDE_PHI_PGAIN,
140  STABILIZATION_ATTITUDE_THETA_PGAIN,
141  STABILIZATION_ATTITUDE_PSI_PGAIN);
142 
144  STABILIZATION_ATTITUDE_PHI_DGAIN,
145  STABILIZATION_ATTITUDE_THETA_DGAIN,
146  STABILIZATION_ATTITUDE_PSI_DGAIN);
147 
149  STABILIZATION_ATTITUDE_PHI_IGAIN,
150  STABILIZATION_ATTITUDE_THETA_IGAIN,
151  STABILIZATION_ATTITUDE_PSI_IGAIN);
152 
154  STABILIZATION_ATTITUDE_PHI_DDGAIN,
155  STABILIZATION_ATTITUDE_THETA_DDGAIN,
156  STABILIZATION_ATTITUDE_PSI_DDGAIN);
157 
162 
164 
165 #if PERIODIC_TELEMETRY
166  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_INT, send_att);
167  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_REF_INT, send_att_ref);
168 #endif
169 }
170 
171 void stabilization_attitude_read_rc(bool in_flight, bool in_carefree, bool coordinated_turn)
172 {
173  stabilization_attitude_read_rc_setpoint_eulers(&stab_att_sp_euler, in_flight, in_carefree, coordinated_turn);
174 }
175 
177 {
181 }
182 
184 {
188 }
189 
191 {
192  stab_att_sp_euler = *rpy;
193 }
194 
196 {
197  /* Rotate horizontal commands to body frame by psi */
199  int32_t s_psi, c_psi;
200  PPRZ_ITRIG_SIN(s_psi, psi);
201  PPRZ_ITRIG_COS(c_psi, psi);
202  stab_att_sp_euler.phi = (-s_psi * cmd->x + c_psi * cmd->y) >> INT32_TRIG_FRAC;
203  stab_att_sp_euler.theta = -(c_psi * cmd->x + s_psi * cmd->y) >> INT32_TRIG_FRAC;
205 }
206 
207 #define OFFSET_AND_ROUND(_a, _b) (((_a)+(1<<((_b)-1)))>>(_b))
208 #define OFFSET_AND_ROUND2(_a, _b) (((_a)+(1<<((_b)-1))-((_a)<0?1:0))>>(_b))
209 
210 #define MAX_SUM_ERR 4000000
211 
212 void stabilization_attitude_run(bool in_flight)
213 {
214 
215  /* update reference */
216 #if USE_ATTITUDE_REF
218 #else
222 #endif
223 
224  const struct Int32Eulers att_ref_scaled = {
228  };
229  const struct Int32Rates rate_ref_scaled = {
233  };
234 
235  /* compute feedforward command */
236  stabilization_att_ff_cmd[COMMAND_ROLL] =
238  stabilization_gains.ffd.x * rate_ref_scaled.p;
239  stabilization_att_ff_cmd[COMMAND_PITCH] =
241  stabilization_gains.ffd.y * rate_ref_scaled.q;
242  stabilization_att_ff_cmd[COMMAND_YAW] =
244  stabilization_gains.ffd.z * rate_ref_scaled.r;
245 
246  /* with FFD gain of 100, reference rate of 180deg/s (3.14 rad/s)
247  * INT32_RATE_FRAC = 12, CMD_SHIFT = 11
248  * fb cmd: 100 * 3.14 * 2^INT32_RATE_FRAC / 2^CMD_SHIFT = 628
249  * max possible command is 9600
250  */
251 
252  /* compute feedback command */
253  /* attitude error */
254  struct Int32Eulers att_err;
255  struct Int32Eulers *ltp_to_body_euler = stateGetNedToBodyEulers_i();
256  EULERS_DIFF(att_err, att_ref_scaled, (*ltp_to_body_euler));
257  INT32_ANGLE_NORMALIZE(att_err.psi);
258 
259  if (in_flight) {
260  /* update integrator */
263  } else {
265  }
266 
267  /* rate error */
268  struct Int32Rates rate_err;
269  struct Int32Rates *body_rate = stateGetBodyRates_i();
270  RATES_DIFF(rate_err, rate_ref_scaled, (*body_rate));
271 
272  /* PID */
273  stabilization_att_fb_cmd[COMMAND_ROLL] =
274  stabilization_gains.p.x * att_err.phi +
275  stabilization_gains.d.x * rate_err.p +
277 
278  stabilization_att_fb_cmd[COMMAND_PITCH] =
279  stabilization_gains.p.y * att_err.theta +
280  stabilization_gains.d.y * rate_err.q +
282 
283  stabilization_att_fb_cmd[COMMAND_YAW] =
284  stabilization_gains.p.z * att_err.psi +
285  stabilization_gains.d.z * rate_err.r +
287 
288 
289  /* with P gain of 100, att_err of 180deg (3.14 rad)
290  * fb cmd: 100 * 3.14 * 2^12 / 2^CMD_SHIFT = 628
291  * max possible command is 9600
292  */
293 #define CMD_SHIFT 11
294 
295  /* sum feedforward and feedback */
296  stabilization_cmd[COMMAND_ROLL] =
298 
299  stabilization_cmd[COMMAND_PITCH] =
301 
302  stabilization_cmd[COMMAND_YAW] =
304 
305  /* bound the result */
306  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
307  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
308  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
309 
310 }
int32_t psi
in rad with INT32_ANGLE_FRAC
#define INT32_RATE_FRAC
int32_t stabilization_att_fb_cmd[COMMANDS_NB]
angular rates
void stabilization_attitude_enter(void)
struct AttRefEulerInt att_ref_euler_i
Periodic telemetry system header (includes downlink utility and generated code).
General attitude stabilization interface for rotorcrafts.
#define STABILIZATION_ATTITUDE_PSI_FFDGAIN
static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
int32_t theta
in rad with INT32_ANGLE_FRAC
#define INT32_ANGLE_FRAC
#define INT_RATES_ZERO(_e)
void stabilization_attitude_run(bool in_flight)
#define OFFSET_AND_ROUND(_a, _b)
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
Read an attitude setpoint from the RC.
struct Int32Rates rate
with REF_RATE_FRAC
struct Int32AttitudeGains stabilization_gains
void stabilization_attitude_read_rc(bool in_flight, bool in_carefree, bool coordinated_turn)
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:372
#define EULERS_BOUND_CUBE(_v, _min, _max)
Definition: pprz_algebra.h:317
Attitude reference models and state/output (euler int)
void stabilization_attitude_set_failsafe_setpoint(void)
#define INT32_EULERS_LSHIFT(_o, _i, _r)
int32_t r
in rad/s with INT32_RATE_FRAC
#define PPRZ_ITRIG_SIN(_s, _a)
static void reset_psi_ref_from_body(void)
static struct Int32Rates * stateGetBodyRates_i(void)
Get vehicle body angular rate (int).
Definition: state.h:1191
static float heading
Definition: ahrs_infrared.c:45
void stabilization_attitude_init(void)
#define REF_RATE_FRAC
#define STABILIZATION_ATTITUDE_THETA_FFDGAIN
euler angles
#define INT32_ANGLE_NORMALIZE(_a)
#define OFFSET_AND_ROUND2(_a, _b)
struct Int32Eulers stabilization_att_sum_err
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
#define CMD_SHIFT
void attitude_ref_euler_int_init(struct AttRefEulerInt *ref)
void attitude_ref_euler_int_update(struct AttRefEulerInt *ref, struct Int32Eulers *sp_euler)
Propagate reference model.
#define REF_ANGLE_FRAC
int32_t stabilization_att_ff_cmd[COMMANDS_NB]
signed long int32_t
Definition: types.h:19
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
#define INT32_TRIG_FRAC
#define STABILIZATION_ATTITUDE_PHI_FFDGAIN
explicitly define to zero to disable feed-forward rate term by default
void stabilization_attitude_set_rpy_setpoint_i(struct Int32Eulers *rpy)
struct Int32Rates accel
with REF_ACCEL_FRAC
int32_t phi
in rad with INT32_ANGLE_FRAC
#define INT_EULERS_ZERO(_e)
API to get/set the generic vehicle states.
struct Int32Eulers euler
with REF_ANGLE_FRAC
#define EULERS_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:295
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:32
int32_t p
in rad/s with INT32_RATE_FRAC
void stabilization_attitude_read_rc_setpoint_eulers(struct Int32Eulers *sp, bool in_flight, bool in_carefree, bool coordinated_turn)
Read attitude setpoint from RC as euler angles.
#define EULERS_ADD(_a, _b)
Definition: pprz_algebra.h:281
#define MAX_PPRZ
Definition: paparazzi.h:8
static void send_att(struct transport_tx *trans, struct link_device *dev)
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition: state.h:1125
int32_t q
in rad/s with INT32_RATE_FRAC
void stabilization_attitude_set_earth_cmd_i(struct Int32Vect2 *cmd, int32_t heading)
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
#define PPRZ_ITRIG_COS(_c, _a)
Paparazzi fixed point algebra.
struct Int32Eulers stab_att_sp_euler
with INT32_ANGLE_FRAC