Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "subsystems/imu.h"
39 
40 #define MAX_SUM_ERR 4000000
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 #define OFFSET_AND_ROUND(_a, _b) (((_a)+(1<<((_b)-1)))>>(_b))
64 #define OFFSET_AND_ROUND2(_a, _b) (((_a)+(1<<((_b)-1))-((_a)<0?1:0))>>(_b))
65 
70 
72 
73 #ifndef STABILIZATION_RATE_DEADBAND_P
74 #define STABILIZATION_RATE_DEADBAND_P 0
75 #endif
76 #ifndef STABILIZATION_RATE_DEADBAND_Q
77 #define STABILIZATION_RATE_DEADBAND_Q 0
78 #endif
79 #ifndef STABILIZATION_RATE_DEADBAND_R
80 #define STABILIZATION_RATE_DEADBAND_R 200
81 #endif
82 
83 #define ROLL_RATE_DEADBAND_EXCEEDED() \
84  (radio_control.values[RADIO_ROLL] > STABILIZATION_RATE_DEADBAND_P || \
85  radio_control.values[RADIO_ROLL] < -STABILIZATION_RATE_DEADBAND_P)
86 
87 #define PITCH_RATE_DEADBAND_EXCEEDED() \
88  (radio_control.values[RADIO_PITCH] > STABILIZATION_RATE_DEADBAND_Q || \
89  radio_control.values[RADIO_PITCH] < -STABILIZATION_RATE_DEADBAND_Q)
90 
91 #define YAW_RATE_DEADBAND_EXCEEDED() \
92  (radio_control.values[RADIO_YAW] > STABILIZATION_RATE_DEADBAND_R || \
93  radio_control.values[RADIO_YAW] < -STABILIZATION_RATE_DEADBAND_R)
94 
95 #if PERIODIC_TELEMETRY
97 
98 static void send_rate(struct transport_tx *trans, struct link_device *dev)
99 {
100  pprz_msg_send_RATE_LOOP(trans, dev, AC_ID,
110  &stabilization_cmd[COMMAND_THRUST]);
111 }
112 #endif
113 
115 {
116 
118 
120  STABILIZATION_RATE_GAIN_P,
121  STABILIZATION_RATE_GAIN_Q,
122  STABILIZATION_RATE_GAIN_R);
127 
129 
130 #if PERIODIC_TELEMETRY
131  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_RATE_LOOP, send_rate);
132 #endif
133 }
134 
135 
137 {
138 
141  } else {
143  }
144 
147  } else {
149  }
150 
153  } else {
155  }
156 }
157 
158 //Read rc with roll and yaw sitcks switched if the default orientation is vertical but airplane sticks are desired
160 {
161 
163  stabilization_rate_sp.r = (int32_t) - radio_control.values[RADIO_ROLL] * RATE_BFP_OF_REAL(STABILIZATION_RATE_SP_MAX_P) / MAX_PPRZ;
164  } else {
166  }
167 
170  } else {
172  }
173 
176  } else {
178  }
179 }
180 
182 {
184 }
185 
186 void stabilization_rate_run(bool_t in_flight)
187 {
188  /* compute feed-back command */
189  struct Int32Rates _error;
190  struct Int32Rates *body_rate = stateGetBodyRates_i();
191  RATES_DIFF(_error, stabilization_rate_sp, (*body_rate));
192  if (in_flight) {
193  /* update integrator */
196  } else {
198  }
199 
200  /* PI */
203 
206 
209 
210  stabilization_cmd[COMMAND_ROLL] = stabilization_rate_fb_cmd.p >> 11;
211  stabilization_cmd[COMMAND_PITCH] = stabilization_rate_fb_cmd.q >> 11;
212  stabilization_cmd[COMMAND_YAW] = stabilization_rate_fb_cmd.r >> 11;
213 
214  /* bound the result */
215  BoundAbs(stabilization_cmd[COMMAND_ROLL], MAX_PPRZ);
216  BoundAbs(stabilization_cmd[COMMAND_PITCH], MAX_PPRZ);
217  BoundAbs(stabilization_cmd[COMMAND_YAW], MAX_PPRZ);
218 
219 }
#define STABILIZATION_RATE_IGAIN_R
angular rates
void stabilization_rate_run(bool_t in_flight)
#define MAX_SUM_ERR
Generic transmission transport header.
Definition: transport.h:89
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:343
#define RADIO_ROLL
Definition: spektrum_arch.h:43
#define RADIO_YAW
Definition: spektrum_arch.h:45
Periodic telemetry system header (includes downlink utility and generated code).
struct Int32Rates stabilization_rate_sum_err
void stabilization_rate_enter(void)
#define INT_RATES_ZERO(_e)
struct Int32Rates stabilization_rate_gain
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:329
#define OFFSET_AND_ROUND2(_a, _b)
#define YAW_RATE_DEADBAND_EXCEEDED()
struct Int32Rates stabilization_rate_igain
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:371
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:58
#define THROTTLE_STICK_DOWN()
Rate stabilization for rotorcrafts.
int32_t r
in rad/s with INT32_RATE_FRAC
static struct Int32Rates * stateGetBodyRates_i(void)
Get vehicle body angular rate (int).
Definition: state.h:1162
#define RATES_BOUND_CUBE(_v, _min, _max)
Definition: pprz_algebra.h:400
#define STABILIZATION_RATE_IGAIN_P
void stabilization_rate_read_rc(void)
#define RADIO_PITCH
Definition: spektrum_arch.h:44
void stabilization_rate_read_rc_switched_sticks(void)
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
struct RadioControl radio_control
Definition: radio_control.c:30
Inertial Measurement Unit interface.
#define STABILIZATION_RATE_IGAIN_Q
signed long int32_t
Definition: types.h:19
#define PITCH_RATE_DEADBAND_EXCEEDED()
Some helper functions to check RC sticks.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
API to get/set the generic vehicle states.
General stabilization interface for rotorcrafts.
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:28
int32_t p
in rad/s with INT32_RATE_FRAC
struct Int32Rates stabilization_rate_fb_cmd
#define MAX_PPRZ
Definition: paparazzi.h:8
void stabilization_rate_init(void)
#define ROLL_RATE_DEADBAND_EXCEEDED()
struct Int32Rates stabilization_rate_sp
int32_t q
in rad/s with INT32_RATE_FRAC
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 RATE_BFP_OF_REAL(_af)