Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 "std.h"
29 #include "generated/airframe.h"
30 
33 #include "paparazzi.h"
34 #include "state.h"
35 
37 #ifndef USE_ATTITUDE_REF
38 #define USE_ATTITUDE_REF 1
39 #endif
40 
42 #ifndef STABILIZATION_ATTITUDE_PHI_FFDGAIN
43 #define STABILIZATION_ATTITUDE_PHI_FFDGAIN 0
44 #endif
45 #ifndef STABILIZATION_ATTITUDE_THETA_FFDGAIN
46 #define STABILIZATION_ATTITUDE_THETA_FFDGAIN 0
47 #endif
48 #ifndef STABILIZATION_ATTITUDE_PSI_FFDGAIN
49 #define STABILIZATION_ATTITUDE_PSI_FFDGAIN 0
50 #endif
51 
53 
54 /* warn if some gains are still negative */
55 #if (STABILIZATION_ATTITUDE_PHI_PGAIN < 0) || \
56  (STABILIZATION_ATTITUDE_THETA_PGAIN < 0) || \
57  (STABILIZATION_ATTITUDE_PSI_PGAIN < 0) || \
58  (STABILIZATION_ATTITUDE_PHI_DGAIN < 0) || \
59  (STABILIZATION_ATTITUDE_THETA_DGAIN < 0) || \
60  (STABILIZATION_ATTITUDE_PSI_DGAIN < 0) || \
61  (STABILIZATION_ATTITUDE_PHI_IGAIN < 0) || \
62  (STABILIZATION_ATTITUDE_THETA_IGAIN < 0) || \
63  (STABILIZATION_ATTITUDE_PSI_IGAIN < 0)
64 #error "ALL control gains have to be positive!!!"
65 #endif
66 
68 
71 
72 static struct Int32Eulers stab_att_sp_euler;
73 static struct AttRefEulerInt att_ref_euler_i;
74 
75 static inline void reset_psi_ref_from_body(void)
76 {
77  //sp has been set from body using stabilization_attitude_get_yaw_i, use that value
81 }
82 
83 #if PERIODIC_TELEMETRY
85 
86 static void send_att(struct transport_tx *trans, struct link_device *dev)
87 {
88  struct Int32Rates *body_rate = stateGetBodyRates_i();
89  struct Int32Eulers *att = stateGetNedToBodyEulers_i();
90  pprz_msg_send_STAB_ATTITUDE_INT(trans, dev, AC_ID,
91  &(body_rate->p), &(body_rate->q), &(body_rate->r),
92  &(att->phi), &(att->theta), &(att->psi),
99  &stabilization_att_fb_cmd[COMMAND_ROLL],
100  &stabilization_att_fb_cmd[COMMAND_PITCH],
101  &stabilization_att_fb_cmd[COMMAND_YAW],
102  &stabilization_att_ff_cmd[COMMAND_ROLL],
103  &stabilization_att_ff_cmd[COMMAND_PITCH],
104  &stabilization_att_ff_cmd[COMMAND_YAW],
105  &stabilization.cmd[COMMAND_ROLL],
106  &stabilization.cmd[COMMAND_PITCH],
107  &stabilization.cmd[COMMAND_YAW]);
108 }
109 
110 static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
111 {
112  pprz_msg_send_STAB_ATTITUDE_REF_INT(trans, dev, AC_ID,
125 }
126 #endif
127 
129 {
130 
132 
134 
135 
137  STABILIZATION_ATTITUDE_PHI_PGAIN,
138  STABILIZATION_ATTITUDE_THETA_PGAIN,
139  STABILIZATION_ATTITUDE_PSI_PGAIN);
140 
142  STABILIZATION_ATTITUDE_PHI_DGAIN,
143  STABILIZATION_ATTITUDE_THETA_DGAIN,
144  STABILIZATION_ATTITUDE_PSI_DGAIN);
145 
147  STABILIZATION_ATTITUDE_PHI_IGAIN,
148  STABILIZATION_ATTITUDE_THETA_IGAIN,
149  STABILIZATION_ATTITUDE_PSI_IGAIN);
150 
152  STABILIZATION_ATTITUDE_PHI_DDGAIN,
153  STABILIZATION_ATTITUDE_THETA_DDGAIN,
154  STABILIZATION_ATTITUDE_PSI_DDGAIN);
155 
160 
162 
163 #if PERIODIC_TELEMETRY
164  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_INT, send_att);
165  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STAB_ATTITUDE_REF_INT, send_att_ref);
166 #endif
167 }
168 
170 {
173 }
174 
175 #define OFFSET_AND_ROUND(_a, _b) (((_a)+(1<<((_b)-1)))>>(_b))
176 #define OFFSET_AND_ROUND2(_a, _b) (((_a)+(1<<((_b)-1))-((_a)<0?1:0))>>(_b))
177 
178 #define MAX_SUM_ERR 4000000
179 
180 void stabilization_attitude_run(bool in_flight, struct StabilizationSetpoint *sp, struct ThrustSetpoint *thrust, int32_t *cmd)
181 {
183 
184  /* update reference */
185 #if USE_ATTITUDE_REF
187 #else
191 #endif
192 
193  const struct Int32Eulers att_ref_scaled = {
197  };
198  const struct Int32Rates rate_ref_scaled = {
202  };
203 
204  /* compute feedforward command */
205  stabilization_att_ff_cmd[COMMAND_ROLL] =
207  stabilization_gains.ffd.x * rate_ref_scaled.p;
208  stabilization_att_ff_cmd[COMMAND_PITCH] =
210  stabilization_gains.ffd.y * rate_ref_scaled.q;
211  stabilization_att_ff_cmd[COMMAND_YAW] =
213  stabilization_gains.ffd.z * rate_ref_scaled.r;
214 
215  /* with FFD gain of 100, reference rate of 180deg/s (3.14 rad/s)
216  * INT32_RATE_FRAC = 12, CMD_SHIFT = 11
217  * fb cmd: 100 * 3.14 * 2^INT32_RATE_FRAC / 2^CMD_SHIFT = 628
218  * max possible command is 9600
219  */
220 
221  /* compute feedback command */
222  /* attitude error */
223  struct Int32Eulers att_err;
224  struct Int32Eulers *ltp_to_body_euler = stateGetNedToBodyEulers_i();
225  EULERS_DIFF(att_err, att_ref_scaled, (*ltp_to_body_euler));
226  INT32_ANGLE_NORMALIZE(att_err.psi);
227 
228  if (in_flight) {
229  /* update integrator */
232  } else {
234  }
235 
236  /* rate error */
237  struct Int32Rates rate_err;
238  struct Int32Rates *body_rate = stateGetBodyRates_i();
239  RATES_DIFF(rate_err, rate_ref_scaled, (*body_rate));
240 
241  /* PID */
242  stabilization_att_fb_cmd[COMMAND_ROLL] =
243  stabilization_gains.p.x * att_err.phi +
244  stabilization_gains.d.x * rate_err.p +
246 
247  stabilization_att_fb_cmd[COMMAND_PITCH] =
248  stabilization_gains.p.y * att_err.theta +
249  stabilization_gains.d.y * rate_err.q +
251 
252  stabilization_att_fb_cmd[COMMAND_YAW] =
253  stabilization_gains.p.z * att_err.psi +
254  stabilization_gains.d.z * rate_err.r +
256 
257 
258  /* with P gain of 100, att_err of 180deg (3.14 rad)
259  * fb cmd: 100 * 3.14 * 2^12 / 2^CMD_SHIFT = 628
260  * max possible command is 9600
261  */
262 #define CMD_SHIFT 11
263 
264  /* sum feedforward and feedback */
265  cmd[COMMAND_ROLL] =
267 
268  cmd[COMMAND_PITCH] =
270 
271  cmd[COMMAND_YAW] =
273 
274  cmd[COMMAND_THRUST] = th_sp_to_thrust_i(thrust, 0, THRUST_AXIS_Z);
275 
276  /* bound the result */
277  BoundAbs(cmd[COMMAND_ROLL], MAX_PPRZ);
278  BoundAbs(cmd[COMMAND_PITCH], MAX_PPRZ);
279  BoundAbs(cmd[COMMAND_YAW], MAX_PPRZ);
280  BoundAbs(cmd[COMMAND_THRUST], MAX_PPRZ);
281 
282 }
#define EULERS_BOUND_CUBE(_v, _min, _max)
Definition: pprz_algebra.h:317
#define EULERS_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:295
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
#define EULERS_ADD(_a, _b)
Definition: pprz_algebra.h:281
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:372
int32_t p
in rad/s with INT32_RATE_FRAC
int32_t r
in rad/s with INT32_RATE_FRAC
int32_t phi
in rad with INT32_ANGLE_FRAC
int32_t q
in rad/s with INT32_RATE_FRAC
int32_t psi
in rad with INT32_ANGLE_FRAC
int32_t theta
in rad with INT32_ANGLE_FRAC
#define INT_RATES_ZERO(_e)
#define INT32_ANGLE_FRAC
#define INT32_EULERS_LSHIFT(_o, _i, _r)
#define INT32_RATE_FRAC
#define INT_EULERS_ZERO(_e)
#define INT32_ANGLE_NORMALIZE(_a)
euler angles
angular rates
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition: state.h:1125
static struct Int32Rates * stateGetBodyRates_i(void)
Get vehicle body angular rate (int).
Definition: state.h:1191
#define MAX_PPRZ
Definition: paparazzi.h:8
struct Stabilization stabilization
Definition: stabilization.c:41
struct Int32Eulers stab_sp_to_eulers_i(struct StabilizationSetpoint *sp)
int32_t th_sp_to_thrust_i(struct ThrustSetpoint *th, int32_t thrust, uint8_t axis)
#define THRUST_AXIS_Z
int32_t cmd[COMMANDS_NB]
output command vector, range from [-MAX_PPRZ:MAX_PPRZ] (store for messages)
void stabilization_attitude_run(bool in_flight, struct StabilizationSetpoint *sp, struct ThrustSetpoint *thrust, int32_t *cmd)
Attitude control run function.
#define OFFSET_AND_ROUND2(_a, _b)
void stabilization_attitude_enter(void)
Attitude control enter function.
int32_t stabilization_att_ff_cmd[COMMANDS_NB]
static void send_att_ref(struct transport_tx *trans, struct link_device *dev)
#define STABILIZATION_ATTITUDE_PSI_FFDGAIN
#define OFFSET_AND_ROUND(_a, _b)
static struct Int32Eulers stab_att_sp_euler
static void reset_psi_ref_from_body(void)
struct Int32AttitudeGains stabilization_gains
#define CMD_SHIFT
struct Int32Eulers stabilization_att_sum_err
int32_t stabilization_att_fb_cmd[COMMANDS_NB]
#define STABILIZATION_ATTITUDE_THETA_FFDGAIN
void stabilization_attitude_euler_int_init(void)
#define STABILIZATION_ATTITUDE_PHI_FFDGAIN
explicitly define to zero to disable feed-forward rate term by default
static struct AttRefEulerInt att_ref_euler_i
static void send_att(struct transport_tx *trans, struct link_device *dev)
void attitude_ref_euler_int_update(struct AttRefEulerInt *ref, struct Int32Eulers *sp_euler)
Propagate reference model.
void attitude_ref_euler_int_init(struct AttRefEulerInt *ref)
Rotorcraft attitude reference generation (euler int version)
struct Int32Rates accel
with REF_ACCEL_FRAC
struct Int32Rates rate
with REF_RATE_FRAC
struct Int32Eulers euler
with REF_ANGLE_FRAC
Attitude reference models and state/output (euler int)
#define REF_RATE_FRAC
#define REF_ANGLE_FRAC
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
Stabilization setpoint.
Definition: stabilization.h:53
Thrust setpoint // TODO to a setpoint header Structure to store the desired thrust vector with differ...
Definition: stabilization.h:82
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83