Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
stabilization_rate.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
3  * Copyright (C) 2010 Felix Ruess <felix.ruess@gmail.com>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
29 #include "generated/airframe.h"
30 
33 
34 #include "state.h"
35 
36 #include "modules/imu/imu.h"
39 
40 #define MAX_SUM_ERR 40000
41 
42 #ifndef STABILIZATION_RATE_IGAIN_P
43 #define STABILIZATION_RATE_IGAIN_P 0
44 #endif
45 
46 #ifndef STABILIZATION_RATE_IGAIN_Q
47 #define STABILIZATION_RATE_IGAIN_Q 0
48 #endif
49 
50 #ifndef STABILIZATION_RATE_IGAIN_R
51 #define STABILIZATION_RATE_IGAIN_R 0
52 #endif
53 
54 #if (STABILIZATION_RATE_GAIN_P < 0) || \
55  (STABILIZATION_RATE_GAIN_Q < 0) || \
56  (STABILIZATION_RATE_GAIN_R < 0) || \
57  (STABILIZATION_RATE_IGAIN_P < 0) || \
58  (STABILIZATION_RATE_IGAIN_Q < 0) || \
59  (STABILIZATION_RATE_IGAIN_R < 0)
60 #error "ALL control gains have to be positive!!!"
61 #endif
62 
63 // divide by 2^_b and round instead of floor
64 #define OFFSET_AND_ROUND(_a, _b) (((_a)+(1<<((_b)-1)))>>(_b))
65 #define OFFSET_AND_ROUND2(_a, _b) (((_a)+(1<<((_b)-1))-((_a)<0?1:0))>>(_b))
66 
67 static struct FloatRates stabilization_rate_sp;
72 
73 #if PERIODIC_TELEMETRY
75 
76 static void send_rate(struct transport_tx *trans, struct link_device *dev)
77 {
78  pprz_msg_send_RATE_LOOP(trans, dev, AC_ID,
88  &stabilization.cmd[COMMAND_THRUST]);
89 }
90 #endif
91 
93 {
94 
96 
98  STABILIZATION_RATE_GAIN_P,
99  STABILIZATION_RATE_GAIN_Q,
100  STABILIZATION_RATE_GAIN_R);
105 
107 
108 #if PERIODIC_TELEMETRY
109  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_RATE_LOOP, send_rate);
110 #endif
111 }
112 
114 {
116 }
117 
118 void stabilization_rate_run(bool in_flight, struct StabilizationSetpoint *rate_sp, struct ThrustSetpoint *thrust, int32_t *cmd)
119 {
120  /* compute feed-back command */
121  struct FloatRates _error;
122  struct FloatRates *body_rate = stateGetBodyRates_f();
124  RATES_DIFF(_error, stabilization_rate_sp, (*body_rate));
125  if (in_flight) {
126  /* update integrator */
127  //divide the sum_err_increment to make sure it doesn't accumulate to the max too fast
128  struct FloatRates sum_err_increment;
129  RATES_SDIV(sum_err_increment, _error, PERIODIC_FREQUENCY);
130  RATES_ADD(stabilization_rate_sum_err, sum_err_increment);
132  } else {
134  }
135 
136  /* PI */
139 
142 
145 
146  cmd[COMMAND_ROLL] = stabilization_rate_fb_cmd.p;
147  cmd[COMMAND_PITCH] = stabilization_rate_fb_cmd.q;
148  cmd[COMMAND_YAW] = stabilization_rate_fb_cmd.r;
149  cmd[COMMAND_THRUST] = th_sp_to_thrust_i(thrust, 0, THRUST_AXIS_Z);
150 
151  /* bound the result */
152  BoundAbs(cmd[COMMAND_ROLL], MAX_PPRZ);
153  BoundAbs(cmd[COMMAND_PITCH], MAX_PPRZ);
154  BoundAbs(cmd[COMMAND_YAW], MAX_PPRZ);
155  BoundAbs(cmd[COMMAND_THRUST], MAX_PPRZ);
156 }
157 
159 {
160  struct FloatRates rate_sp;
161  FLOAT_RATES_ZERO(rate_sp);
162  if (ROLL_RATE_DEADBAND_EXCEEDED(rc)) {
163  rate_sp.p = rc->values[RC_RATE_P] * STABILIZATION_RATE_SP_MAX_P / MAX_PPRZ;
164  }
166  rate_sp.q = rc->values[RC_RATE_Q] * STABILIZATION_RATE_SP_MAX_Q / MAX_PPRZ;
167  }
169  rate_sp.r = rc->values[RC_RATE_R] * STABILIZATION_RATE_SP_MAX_R / MAX_PPRZ;
170  }
171  return stab_sp_from_rates_f(&rate_sp);
172 }
173 
float q
in rad/s
float p
in rad/s
float r
in rad/s
#define FLOAT_RATES_ZERO(_r)
angular rates
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:344
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:372
#define RATES_BOUND_CUBE(_v, _min, _max)
Definition: pprz_algebra.h:401
#define RATES_SDIV(_ro, _ri, _s)
Definition: pprz_algebra.h:386
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition: state.h:1200
Inertial Measurement Unit interface.
#define MAX_PPRZ
Definition: paparazzi.h:8
Generic interface for radio control modules.
Some helper functions to check RC sticks.
#define THROTTLE_STICK_DOWN_FROM_RC(_rc)
struct Stabilization stabilization
Definition: stabilization.c:41
struct FloatRates stab_sp_to_rates_f(struct StabilizationSetpoint *sp)
struct StabilizationSetpoint stab_sp_from_rates_f(struct FloatRates *rates)
int32_t th_sp_to_thrust_i(struct ThrustSetpoint *th, int32_t thrust, uint8_t axis)
General stabilization interface for rotorcrafts.
#define THRUST_AXIS_Z
int32_t cmd[COMMANDS_NB]
output command vector, range from [-MAX_PPRZ:MAX_PPRZ] (store for messages)
#define STABILIZATION_RATE_IGAIN_P
static struct FloatRates stabilization_rate_fb_cmd
void stabilization_rate_enter(void)
struct StabilizationSetpoint stabilization_rate_read_rc(struct RadioControl *rc)
#define STABILIZATION_RATE_IGAIN_R
#define STABILIZATION_RATE_IGAIN_Q
#define MAX_SUM_ERR
void stabilization_rate_run(bool in_flight, struct StabilizationSetpoint *rate_sp, struct ThrustSetpoint *thrust, int32_t *cmd)
struct FloatRates stabilization_rate_igain
struct FloatRates stabilization_rate_gain
static void send_rate(struct transport_tx *trans, struct link_device *dev)
static struct FloatRates stabilization_rate_sp
static struct FloatRates stabilization_rate_sum_err
void stabilization_rate_init(void)
Rate stabilization for rotorcrafts.
#define RC_RATE_R
#define RC_RATE_Q
#define YAW_RATE_DEADBAND_EXCEEDED(_rc)
#define RC_RATE_P
#define ROLL_RATE_DEADBAND_EXCEEDED(_rc)
#define PITCH_RATE_DEADBAND_EXCEEDED(_rc)
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