Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
ahrs_float_cmpl_wrapper.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Felix Ruess <felix.ruess@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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include "modules/ahrs/ahrs.h"
29 #include "modules/core/abi.h"
30 #include "state.h"
31 
32 PRINT_CONFIG_VAR(AHRS_FC_TYPE)
33 
34 
36 
39 
40 static void compute_body_orientation_and_rates(void);
41 
42 #if PERIODIC_TELEMETRY
44 #include "mcu_periph/sys_time.h"
45 #include "state.h"
46 
47 static void send_euler(struct transport_tx *trans, struct link_device *dev)
48 {
49  struct FloatEulers ltp_to_body_euler;
50  float_eulers_of_quat(&ltp_to_body_euler, &ahrs_fc.ltp_to_body_quat);
51  pprz_msg_send_AHRS_EULER(trans, dev, AC_ID,
52  &ltp_to_body_euler.phi,
53  &ltp_to_body_euler.theta,
54  &ltp_to_body_euler.psi,
55  &ahrs_fc_id);
56 }
57 
58 static void send_bias(struct transport_tx *trans, struct link_device *dev)
59 {
60  struct Int32Rates gyro_bias;
62  pprz_msg_send_AHRS_GYRO_BIAS_INT(trans, dev, AC_ID,
63  &gyro_bias.p, &gyro_bias.q, &gyro_bias.r, &ahrs_fc_id);
64 }
65 
66 static void send_euler_int(struct transport_tx *trans, struct link_device *dev)
67 {
68  /* compute eulers in int (IMU frame) */
69  struct FloatEulers ltp_to_body_euler;
70  float_eulers_of_quat(&ltp_to_body_euler, &ahrs_fc.ltp_to_body_quat);
71  struct Int32Eulers eulers_body;
72  EULERS_BFP_OF_REAL(eulers_body, ltp_to_body_euler);
73 
74  pprz_msg_send_AHRS_EULER_INT(trans, dev, AC_ID,
75  &eulers_body.phi,
76  &eulers_body.theta,
77  &eulers_body.psi,
78  &eulers_body.phi,
79  &eulers_body.theta,
80  &eulers_body.psi,
81  &ahrs_fc_id);
82 }
83 
84 static void send_geo_mag(struct transport_tx *trans, struct link_device *dev)
85 {
86  pprz_msg_send_GEO_MAG(trans, dev, AC_ID,
88 }
89 
90 static void send_filter_status(struct transport_tx *trans, struct link_device *dev)
91 {
92  uint8_t mde = 3;
93  uint16_t val = 0;
94  if (!ahrs_fc.is_aligned) { mde = 2; }
96  /* set lost if no new gyro measurements for 50ms */
97  if (t_diff > 50000) { mde = 5; }
98  pprz_msg_send_STATE_FILTER_STATUS(trans, dev, AC_ID, &ahrs_fc_id, &mde, &val);
99 }
100 #endif
101 
102 
106 #ifndef AHRS_FC_IMU_ID
107 #define AHRS_FC_IMU_ID ABI_BROADCAST
108 #endif
113 #ifndef AHRS_FC_MAG_ID
114 #define AHRS_FC_MAG_ID AHRS_FC_IMU_ID
115 #endif
120 #ifndef AHRS_FC_GPS_ID
121 #define AHRS_FC_GPS_ID GPS_MULTI_ID
122 #endif
130 
131 
132 static void gyro_cb(uint8_t __attribute__((unused)) sender_id,
133  uint32_t stamp, struct Int32Rates *gyro)
134 {
135  ahrs_fc_last_stamp = stamp;
136  struct FloatRates gyro_f;
137  RATES_FLOAT_OF_BFP(gyro_f, *gyro);
138 
139 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_PROPAGATE_FREQUENCY)
140  PRINT_CONFIG_MSG("Calculating dt for AHRS_FC propagation.")
141  /* timestamp in usec when last callback was received */
142  static uint32_t last_stamp = 0;
143 
144  if (last_stamp > 0 && ahrs_fc.is_aligned) {
145  float dt = (float)(stamp - last_stamp) * 1e-6;
146  ahrs_fc_propagate(&gyro_f, dt);
148  }
149  last_stamp = stamp;
150 #else
151  PRINT_CONFIG_MSG("Using fixed AHRS_PROPAGATE_FREQUENCY for AHRS_FC propagation.")
153  if (ahrs_fc.status == AHRS_FC_RUNNING) {
154  const float dt = 1. / (AHRS_PROPAGATE_FREQUENCY);
155  ahrs_fc_propagate(&gyro_f, dt);
157  }
158 #endif
159 }
160 
161 static void accel_cb(uint8_t __attribute__((unused)) sender_id,
162  uint32_t __attribute__((unused)) stamp,
163  struct Int32Vect3 *accel)
164 {
165  struct FloatVect3 accel_f;
166  ACCELS_FLOAT_OF_BFP(accel_f, *accel);
167 
168 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_CORRECT_FREQUENCY)
169  PRINT_CONFIG_MSG("Calculating dt for AHRS float_cmpl accel update.")
170  static uint32_t last_stamp = 0;
171  if (last_stamp > 0 && ahrs_fc.is_aligned) {
172  float dt = (float)(stamp - last_stamp) * 1e-6;
173  ahrs_fc_update_accel(&accel_f, dt);
174  }
175  last_stamp = stamp;
176 #else
177  PRINT_CONFIG_MSG("Using fixed AHRS_CORRECT_FREQUENCY for AHRS float_cmpl accel update.")
178  PRINT_CONFIG_VAR(AHRS_CORRECT_FREQUENCY)
179  if (ahrs_fc.is_aligned) {
180  const float dt = 1. / (AHRS_CORRECT_FREQUENCY);
181  ahrs_fc_update_accel(&accel_f, dt);
182  }
183 #endif
184 }
185 
186 static void mag_cb(uint8_t __attribute__((unused)) sender_id,
187  uint32_t __attribute__((unused)) stamp,
188  struct Int32Vect3 *mag)
189 {
190  struct FloatVect3 mag_f;
191  MAGS_FLOAT_OF_BFP(mag_f, *mag);
192 
193 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_MAG_CORRECT_FREQUENCY)
194  PRINT_CONFIG_MSG("Calculating dt for AHRS float_cmpl mag update.")
195  static uint32_t last_stamp = 0;
196  if (last_stamp > 0 && ahrs_fc.is_aligned) {
197  float dt = (float)(stamp - last_stamp) * 1e-6;
198  ahrs_fc_update_mag(&mag_f, dt);
199  }
200  last_stamp = stamp;
201 #else
202  PRINT_CONFIG_MSG("Using fixed AHRS_MAG_CORRECT_FREQUENCY for AHRS float_cmpl mag update.")
203  PRINT_CONFIG_VAR(AHRS_MAG_CORRECT_FREQUENCY)
204  if (ahrs_fc.is_aligned) {
205  const float dt = 1. / (AHRS_MAG_CORRECT_FREQUENCY);
206  ahrs_fc_update_mag(&mag_f, dt);
207  }
208 #endif
209 }
210 
211 static void aligner_cb(uint8_t __attribute__((unused)) sender_id,
212  uint32_t stamp __attribute__((unused)),
213  struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel,
214  struct Int32Vect3 *lp_mag)
215 {
216  if (!ahrs_fc.is_aligned) {
217  /* convert to float */
218  struct FloatRates gyro_f;
219  RATES_FLOAT_OF_BFP(gyro_f, *lp_gyro);
220  struct FloatVect3 accel_f;
221  ACCELS_FLOAT_OF_BFP(accel_f, *lp_accel);
222  struct FloatVect3 mag_f;
223  MAGS_FLOAT_OF_BFP(mag_f, *lp_mag);
224  /* use low passed values to align */
225  if (ahrs_fc_align(&gyro_f, &accel_f, &mag_f)) {
227  }
228  }
229 }
230 
231 static void geo_mag_cb(uint8_t sender_id __attribute__((unused)), struct FloatVect3 *h)
232 {
233  ahrs_fc.mag_h = *h;
234 }
235 
236 static void gps_cb(uint8_t sender_id __attribute__((unused)),
237  uint32_t stamp __attribute__((unused)),
238  struct GpsState *gps_s)
239 {
240  ahrs_fc_update_gps(gps_s);
242 }
243 
247 #if (defined MODULE_AHRS_FLOAT_CMPL_QUAT_ID)
248 #define MODULE_AHRS_FLOAT_CMPL_ID MODULE_AHRS_FLOAT_CMPL_QUAT_ID
249 #elif (defined MODULE_AHRS_FLOAT_CMPL_RMAT_ID)
250 #define MODULE_AHRS_FLOAT_CMPL_ID MODULE_AHRS_FLOAT_CMPL_RMAT_ID
251 #else
252 #error "wrong ahrs_cmpl module type"
253 #endif
255 {
256  stateSetNedToBodyQuat_f(MODULE_AHRS_FLOAT_CMPL_ID, &ahrs_fc.ltp_to_body_quat);
257  stateSetBodyRates_f(MODULE_AHRS_FLOAT_CMPL_ID, &ahrs_fc.body_rate);
258 }
259 
261 {
262  ahrs_fc_init();
263  if (AHRS_FC_TYPE == AHRS_PRIMARY) {
265  } else {
267  }
268 
269  /*
270  * Subscribe to scaled IMU measurements and attach callbacks
271  */
272  AbiBindMsgIMU_GYRO(AHRS_FC_IMU_ID, &gyro_ev, gyro_cb);
273  AbiBindMsgIMU_ACCEL(AHRS_FC_IMU_ID, &accel_ev, accel_cb);
274  AbiBindMsgIMU_MAG(AHRS_FC_MAG_ID, &mag_ev, mag_cb);
275  AbiBindMsgIMU_LOWPASSED(ABI_BROADCAST, &aligner_ev, aligner_cb);
276  AbiBindMsgGEO_MAG(ABI_BROADCAST, &geo_mag_ev, geo_mag_cb);
277  AbiBindMsgGPS(AHRS_FC_GPS_ID, &gps_ev, gps_cb);
278 
279 #if PERIODIC_TELEMETRY
280  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_EULER, send_euler);
281  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_GYRO_BIAS_INT, send_bias);
282  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_EULER_INT, send_euler_int);
284  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STATE_FILTER_STATUS, send_filter_status);
285 #endif
286 }
287 
289 {
290  if (enable) {
291  stateSetInputFilter(STATE_INPUT_ATTITUDE, MODULE_AHRS_FLOAT_CMPL_ID);
292  stateSetInputFilter(STATE_INPUT_RATES, MODULE_AHRS_FLOAT_CMPL_ID);
293  }
294  ahrs_fc_enable = enable;
295 }
296 
static void h(const real32_T x[7], const real32_T q[4], real32_T y[6])
Main include for ABI (AirBorneInterface).
#define ABI_BROADCAST
Broadcast address.
Definition: abi_common.h:58
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
#define AHRS_COMP_ID_FC
Definition: ahrs.h:40
#define AHRS_PRIMARY
Definition: ahrs.h:32
struct AhrsFloatCmpl ahrs_fc
void ahrs_fc_init(void)
void ahrs_fc_update_mag(struct FloatVect3 *mag, float dt)
bool ahrs_fc_align(struct FloatRates *lp_gyro, struct FloatVect3 *lp_accel, struct FloatVect3 *lp_mag)
void ahrs_fc_propagate(struct FloatRates *gyro, float dt)
void ahrs_fc_update_accel(struct FloatVect3 *accel, float dt)
void ahrs_fc_update_gps(struct GpsState *gps_s)
struct FloatRates gyro_bias
struct FloatRates body_rate
@ AHRS_FC_RUNNING
enum AhrsFCStatus status
struct FloatVect3 mag_h
struct FloatQuat ltp_to_body_quat
static uint8_t ahrs_fc_id
uint8_t ahrs_fc_enable
if TRUE with push the estimation results to the state interface
static void gps_cb(uint8_t sender_id, uint32_t stamp, struct GpsState *gps_s)
static void compute_body_orientation_and_rates(void)
Compute body orientation and rates from imu orientation and rates.
static abi_event mag_ev
static abi_event accel_ev
static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro)
static void mag_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *mag)
#define AHRS_FC_MAG_ID
ABI binding for magnetometer data.
void ahrs_fc_wrapper_init(void)
static abi_event gyro_ev
#define AHRS_FC_IMU_ID
ABI binding for IMU data.
static void send_geo_mag(struct transport_tx *trans, struct link_device *dev)
static abi_event aligner_ev
static abi_event geo_mag_ev
static void send_filter_status(struct transport_tx *trans, struct link_device *dev)
static void send_euler_int(struct transport_tx *trans, struct link_device *dev)
static void accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel)
static void geo_mag_cb(uint8_t sender_id, struct FloatVect3 *h)
static void aligner_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel, struct Int32Vect3 *lp_mag)
static void send_euler(struct transport_tx *trans, struct link_device *dev)
static uint32_t ahrs_fc_last_stamp
static void send_bias(struct transport_tx *trans, struct link_device *dev)
void ahrs_float_cmp_quat_wrapper_enable(uint8_t enable)
#define AHRS_FC_GPS_ID
ABI binding for gps data.
static abi_event gps_ev
Paparazzi specific wrapper to run floating point complementary filter.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
data structure for GPS information
Definition: gps.h:87
#define STATE_INPUT_RATES
Definition: state.h:145
#define STATE_INPUT_ATTITUDE
Definition: state.h:144
float phi
in radians
float theta
in radians
float psi
in radians
void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q)
euler rotation 'ZYX'
euler angles
angular rates
#define MAGS_FLOAT_OF_BFP(_ef, _ei)
Definition: pprz_algebra.h:807
#define EULERS_BFP_OF_REAL(_ei, _ef)
Definition: pprz_algebra.h:715
#define RATES_BFP_OF_REAL(_ri, _rf)
Definition: pprz_algebra.h:765
#define RATES_FLOAT_OF_BFP(_rf, _ri)
Definition: pprz_algebra.h:759
#define ACCELS_FLOAT_OF_BFP(_ef, _ei)
Definition: pprz_algebra.h:795
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
euler angles
angular rates
static void stateSetNedToBodyQuat_f(uint16_t id, struct FloatQuat *ned_to_body_quat)
Set vehicle body attitude from quaternion (float).
Definition: state.h:1253
void stateSetInputFilter(uint8_t type, uint16_t flag)
set the input filter for a specified type of data.
Definition: state.c:85
static void stateSetBodyRates_f(uint16_t id, struct FloatRates *body_rate)
Set vehicle body angular rate (float).
Definition: state.h:1346
#define AHRS_PROPAGATE_FREQUENCY
Definition: hf_float.c:55
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
Architecture independent timing functions.
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
uint16_t val[TCOUPLE_NB]
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98