Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ahrs_int_cmpl_euler.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2010 The Paparazzi Team
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 
22 #include "ahrs_int_cmpl_euler.h"
23 
24 #include "subsystems/imu.h"
26 #include "math/pprz_trig_int.h"
27 #include "math/pprz_algebra_int.h"
28 
29 #include "generated/airframe.h"
30 
31 #ifndef FACE_REINJ_1
32 #define FACE_REINJ_1 1024
33 #endif
34 
35 
37 
38 static inline void get_phi_theta_measurement_fom_accel(int32_t* phi_meas, int32_t* theta_meas, struct Int32Vect3 accel);
39 static inline void get_psi_measurement_from_mag(int32_t* psi_meas, int32_t phi_est, int32_t theta_est, struct Int32Vect3 mag);
40 static inline void compute_imu_quat_and_rmat_from_euler(void);
41 static inline void compute_body_orientation(void);
42 
43 #define F_UPDATE 512
44 
45 #define PI_INTEG_EULER (INT32_ANGLE_PI * F_UPDATE)
46 #define TWO_PI_INTEG_EULER (INT32_ANGLE_2_PI * F_UPDATE)
47 #define INTEG_EULER_NORMALIZE(_a) { \
48  while (_a > PI_INTEG_EULER) _a -= TWO_PI_INTEG_EULER; \
49  while (_a < -PI_INTEG_EULER) _a += TWO_PI_INTEG_EULER; \
50  }
51 
52 void ahrs_init(void) {
54 
55  /* set ltp_to_body to zero */
60 
61  /* set ltp_to_imu so that body is zero */
66 
69 
70 #ifdef IMU_MAG_OFFSET
71  ahrs_mag_offset = IMU_MAG_OFFSET;
72 #else
73  ahrs_mag_offset = 0.;
74 #endif
75 }
76 
77 void ahrs_align(void) {
78 
82 
85 
86  /* Compute LTP to IMU eulers */
88 
90 
92 
95 
96 }
97 
98 //#define USE_NOISE_CUT 1
99 //#define USE_NOISE_FILTER 1
100 #define NOISE_FILTER_GAIN 50
101 
102 #if USE_NOISE_CUT
103 #include "led.h"
104 static inline bool_t cut_rates (struct Int32Rates i1, struct Int32Rates i2, int32_t threshold) {
105  struct Int32Rates diff;
106  RATES_DIFF(diff, i1, i2);
107  if (diff.p < -threshold || diff.p > threshold ||
108  diff.q < -threshold || diff.q > threshold ||
109  diff.r < -threshold || diff.r > threshold) {
110  return TRUE;
111  } else {
112  return FALSE;
113  }
114 }
115 #define RATE_CUT_THRESHOLD RATE_BFP_OF_REAL(1)
116 
117 static inline bool_t cut_accel (struct Int32Vect3 i1, struct Int32Vect3 i2, int32_t threshold) {
118  struct Int32Vect3 diff;
119  VECT3_DIFF(diff, i1, i2);
120  if (diff.x < -threshold || diff.x > threshold ||
121  diff.y < -threshold || diff.y > threshold ||
122  diff.z < -threshold || diff.z > threshold) {
123  LED_ON(4);
124  return TRUE;
125  } else {
126  LED_OFF(4);
127  return FALSE;
128  }
129 }
130 #define ACCEL_CUT_THRESHOLD ACCEL_BFP_OF_REAL(20)
131 
132 #endif
133 
134 /*
135  *
136  * fc = 1/(2*pi*tau)
137  *
138  * alpha = dt / ( tau + dt )
139  *
140  *
141  * y(i) = alpha x(i) + (1-alpha) y(i-1)
142  * or
143  * y(i) = y(i-1) + alpha * (x(i) - y(i-1))
144  *
145  *
146  */
147 
148 void ahrs_propagate(void) {
149 
150  /* unbias gyro */
151  struct Int32Rates uf_rate;
153 #if USE_NOISE_CUT
154  static struct Int32Rates last_uf_rate = { 0, 0, 0 };
155  if (!cut_rates(uf_rate, last_uf_rate, RATE_CUT_THRESHOLD)) {
156 #endif
157  /* low pass rate */
158 #if USE_NOISE_FILTER
161 #else
162  RATES_ADD(ahrs.imu_rate, uf_rate);
164 #endif
165 #if USE_NOISE_CUT
166  }
167  RATES_COPY(last_uf_rate, uf_rate);
168 #endif
169 
170  /* integrate eulers */
171  struct Int32Eulers euler_dot;
173  EULERS_ADD(ahrs_impl.hi_res_euler, euler_dot);
174 
175  /* low pass measurement */
178 
179  /* compute residual */
182 
183  struct Int32Eulers correction;
184  /* compute a correction */
186  /* correct estimation */
187  EULERS_ADD(ahrs_impl.hi_res_euler, correction);
189 
190 
191  /* Compute LTP to IMU eulers */
193 
195 
197 
198 }
199 
200 void ahrs_update_accel(void) {
201 
202 #if USE_NOISE_CUT || USE_NOISE_FILTER
203  static struct Int32Vect3 last_accel = { 0, 0, 0 };
204 #endif
205 #if USE_NOISE_CUT
206  if (!cut_accel(imu.accel, last_accel, ACCEL_CUT_THRESHOLD)) {
207 #endif
208 #if USE_NOISE_FILTER
211 #endif
213 #if USE_NOISE_CUT
214  }
215  VECT3_COPY(last_accel, imu.accel);
216 #endif
217 
218 }
219 
220 
221 void ahrs_update_mag(void) {
222 
224 
225 }
226 
227 void ahrs_update_gps(void) {
228 
229 }
230 
231 /* measures phi and theta assuming no dynamic acceleration ?!! */
232 __attribute__ ((always_inline)) static inline void get_phi_theta_measurement_fom_accel(int32_t* phi_meas, int32_t* theta_meas, struct Int32Vect3 accel) {
233 
234  INT32_ATAN2(*phi_meas, -accel.y, -accel.z);
235  int32_t cphi;
236  PPRZ_ITRIG_COS(cphi, *phi_meas);
237  int32_t cphi_ax = -INT_MULT_RSHIFT(cphi, accel.x, INT32_TRIG_FRAC);
238  INT32_ATAN2(*theta_meas, -cphi_ax, -accel.z);
239  *phi_meas *= F_UPDATE;
240  *theta_meas *= F_UPDATE;
241 
242 }
243 
244 /* measure psi by projecting magnetic vector in local tangeant plan */
245 __attribute__ ((always_inline)) static inline void get_psi_measurement_from_mag(int32_t* psi_meas, int32_t phi_est, int32_t theta_est, struct Int32Vect3 mag) {
246 
247  int32_t sphi;
248  PPRZ_ITRIG_SIN(sphi, phi_est);
249  int32_t cphi;
250  PPRZ_ITRIG_COS(cphi, phi_est);
251  int32_t stheta;
252  PPRZ_ITRIG_SIN(stheta, theta_est);
253  int32_t ctheta;
254  PPRZ_ITRIG_COS(ctheta, theta_est);
255 
256  int32_t sphi_stheta = (sphi*stheta)>>INT32_TRIG_FRAC;
257  int32_t cphi_stheta = (cphi*stheta)>>INT32_TRIG_FRAC;
258  //int32_t sphi_ctheta = (sphi*ctheta)>>INT32_TRIG_FRAC;
259  //int32_t cphi_ctheta = (cphi*ctheta)>>INT32_TRIG_FRAC;
260 
261  const int32_t mn = ctheta * mag.x + sphi_stheta * mag.y + cphi_stheta * mag.z;
262  const int32_t me = 0 * mag.x + cphi * mag.y - sphi * mag.z;
263  //const int32_t md =
264  // -stheta * imu.mag.x +
265  // sphi_ctheta * imu.mag.y +
266  // cphi_ctheta * imu.mag.z;
267  float m_psi = -atan2(me, mn);
268  *psi_meas = ((m_psi - ahrs_mag_offset)*(float)(1<<(INT32_ANGLE_FRAC))*F_UPDATE);
269 
270 }
271 
272 /* Compute ltp to imu rotation in quaternion and rotation matrice representation
273  from the euler angle representation */
274 __attribute__ ((always_inline)) static inline void compute_imu_quat_and_rmat_from_euler(void) {
275 
276  /* Compute LTP to IMU quaternion */
278  /* Compute LTP to IMU rotation matrix */
280 
281 }
282 
283 __attribute__ ((always_inline)) static inline void compute_body_orientation(void) {
284 
285  /* Compute LTP to BODY quaternion */
287  /* Compute LTP to BODY rotation matrix */
289  /* compute LTP to BODY eulers */
291  /* compute body rates */
293 
294 }
295 
296 
297 #ifdef AHRS_UPDATE_FW_ESTIMATOR
298 // TODO use ahrs result directly
299 #include "estimator.h"
300 // remotely settable
301 #ifndef INS_ROLL_NEUTRAL_DEFAULT
302 #define INS_ROLL_NEUTRAL_DEFAULT 0
303 #endif
304 #ifndef INS_PITCH_NEUTRAL_DEFAULT
305 #define INS_PITCH_NEUTRAL_DEFAULT 0
306 #endif
307 float ins_roll_neutral = INS_ROLL_NEUTRAL_DEFAULT;
308 float ins_pitch_neutral = INS_PITCH_NEUTRAL_DEFAULT;
309 void ahrs_update_fw_estimator(void)
310 {
311  struct FloatEulers att;
312  // export results to estimator
314 
315  estimator_phi = att.phi - ins_roll_neutral;
316  estimator_theta = att.theta - ins_pitch_neutral;
317  estimator_psi = att.psi;
318 
319  struct FloatRates rates;
321  estimator_p = rates.p;
322  estimator_q = rates.q;
323  estimator_r = rates.r;
324 
325 }
326 #endif //AHRS_UPDATE_FW_ESTIMATOR
struct Int32Rates body_rate
Rotational velocity in body frame.
Definition: ahrs.h:52
int32_t phi
in rad with INT32_ANGLE_FRAC
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:338
__attribute__((always_inline))
float estimator_theta
pitch angle in rad, + = up
Definition: estimator.c:51
float estimator_q
Definition: estimator.c:55
#define VECT3_SUM_SCALED(_c, _a, _b, _s)
Definition: pprz_algebra.h:148
float estimator_r
Definition: estimator.c:56
#define INT32_RMAT_TRANSP_RATEMULT(_vb, _m_b2a, _va)
#define INT_MULT_RSHIFT(_a, _b, _r)
#define EULERS_ADD(_a, _b)
Definition: pprz_algebra.h:249
#define EULERS_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:263
#define FACE_REINJ_1
int32_t p
in rad/s^2 with INT32_RATE_FRAC
void ahrs_init(void)
AHRS initialization.
#define INT32_QUAT_ZERO(_q)
angular rates
void ahrs_update_accel(void)
Update AHRS state with accerleration measurements.
float estimator_phi
roll angle in rad, + = right
Definition: estimator.c:49
static void get_phi_theta_measurement_fom_accel(int32_t *phi_meas, int32_t *theta_meas, struct Int32Vect3 accel)
#define INTEG_EULER_NORMALIZE(_a)
static void compute_imu_quat_and_rmat_from_euler(void)
struct Int32RMat body_to_imu_rmat
rotation from body to imu frame as a rotation matrix
Definition: imu.h:52
struct Int32Vect3 lp_accel
Definition: ahrs_aligner.h:36
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:303
struct Ahrs ahrs
global AHRS state (fixed point version)
Definition: ahrs.c:24
struct Int32Vect3 accel
accelerometer measurements
Definition: imu.h:41
#define LED_ON(i)
Definition: led_hw.h:28
void ahrs_update_gps(void)
#define INT_RATES_ZERO(_e)
int32_t theta
in rad with INT32_ANGLE_FRAC
#define RATES_SUM_SCALED(_c, _a, _b, _s)
Definition: pprz_algebra.h:331
#define INT_EULERS_ZERO(_e)
#define INT32_TRIG_FRAC
struct Int32Rates imu_rate
Rotational velocity in IMU frame.
Definition: ahrs.h:47
void ahrs_align(void)
Aligns the AHRS.
euler angles
float estimator_p
Definition: estimator.c:54
struct Int32Vect3 lp_mag
Definition: ahrs_aligner.h:37
#define FALSE
Definition: imu_chimu.h:141
struct Int32RMat ltp_to_body_rmat
Rotation from LocalTangentPlane to body frame as Rotation Matrix.
Definition: ahrs.h:51
#define INT32_QUAT_COMP_INV(_a2b, _a2c, _b2c)
#define RATES_SDIV(_ro, _ri, _s)
Definition: pprz_algebra.h:352
struct AhrsIntCmplEuler ahrs_impl
static void compute_body_orientation(void)
struct Int32Quat ltp_to_imu_quat
Rotation from LocalTangentPlane to IMU frame as unit quaternion.
Definition: ahrs.h:44
#define PPRZ_ITRIG_SIN(_s, _a)
Definition: pprz_trig_int.h:42
#define INT32_QUAT_OF_EULERS(_q, _e)
#define INT32_EULERS_DOT_OF_RATES(_ed, _e, _r)
#define INT32_ANGLE_FRAC
struct AhrsAligner ahrs_aligner
Definition: ahrs_aligner.c:30
#define RMAT_COPY(_o, _i)
Definition: pprz_algebra.h:564
#define INT32_RMAT_ZERO(_rm)
uint8_t status
status of the AHRS, AHRS_UNINIT or AHRS_RUNNING
Definition: ahrs.h:54
struct Int32Quat ltp_to_body_quat
Rotation from LocalTangentPlane to body frame as unit quaternion.
Definition: ahrs.h:49
#define INT32_ATAN2(_a, _y, _x)
#define INT32_EULERS_OF_RMAT(_e, _rm)
#define F_UPDATE
#define RATES_ADD(_a, _b)
Definition: pprz_algebra.h:310
#define EULERS_SDIV(_eo, _ei, _s)
Definition: pprz_algebra.h:278
Inertial Measurement Unit interface.
angular rates
#define RATES_FLOAT_OF_BFP(_rf, _ri)
Definition: pprz_algebra.h:619
int32_t int32_t accel
float ins_pitch_neutral
Definition: ins_arduimu.c:15
#define INT32_RMAT_COMP_INV(_m_a2b, _m_a2c, _m_b2c)
signed long int32_t
Definition: types.h:19
#define AHRS_UNINIT
Definition: ahrs.h:33
#define TRUE
Definition: imu_chimu.h:144
struct Int32Vect3 mag
magnetometer measurements
Definition: imu.h:42
void ahrs_update_fw_estimator(void)
struct Int32Eulers ltp_to_body_euler
Rotation from LocalTangentPlane to body frame as Euler angles.
Definition: ahrs.h:50
#define NOISE_FILTER_GAIN
struct Int32Quat body_to_imu_quat
rotation from body to imu frame as a unit quaternion
Definition: imu.h:51
#define QUAT_COPY(_qo, _qi)
Definition: pprz_algebra.h:478
struct Int32Eulers ltp_to_imu_euler
Rotation from LocalTangentPlane to IMU frame as Euler angles.
Definition: ahrs.h:45
struct Int32Eulers measure
#define VECT3_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:155
int32_t psi
in rad with INT32_ANGLE_FRAC
void ahrs_update_mag(void)
Update AHRS state with magnetometer measurements.
float estimator_psi
heading in rad, CW, 0 = N
Definition: estimator.c:50
State estimation, fusioning sensors.
#define EULERS_FLOAT_OF_BFP(_ef, _ei)
Definition: pprz_algebra.h:569
int32_t q
in rad/s^2 with INT32_RATE_FRAC
struct Int32Rates gyro
gyroscope measurements
Definition: imu.h:40
#define LED_OFF(i)
Definition: led_hw.h:29
arch independent LED (Light Emitting Diodes) API
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:113
struct Int32Eulers hi_res_euler
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:50
float ins_roll_neutral
Definition: ins_arduimu.c:14
struct Int32Rates gyro_bias
int32_t r
in rad/s^2 with INT32_RATE_FRAC
void ahrs_propagate(void)
Propagation.
#define VECT3_SDIV(_vo, _vi, _s)
Definition: pprz_algebra.h:169
struct Int32RMat ltp_to_imu_rmat
Rotation from LocalTangentPlane to IMU frame as Rotation Matrix.
Definition: ahrs.h:46
#define INT32_RMAT_OF_EULERS(_rm, _e)
#define EULERS_COPY(_a, _b)
Definition: pprz_algebra.h:236
#define AHRS_RUNNING
Definition: ahrs.h:34
struct Int32Eulers measurement
float ahrs_mag_offset
Definition: ahrs.c:27
#define PPRZ_ITRIG_COS(_c, _a)
Definition: pprz_trig_int.h:52
struct Int32Rates lp_gyro
Definition: ahrs_aligner.h:35
Paparazzi fixed point algebra.
static void get_psi_measurement_from_mag(int32_t *psi_meas, int32_t phi_est, int32_t theta_est, struct Int32Vect3 mag)
struct Int32Eulers residual
euler angles