Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
ahrs_madgwick_wrapper.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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 "mcu_periph/sys_time.h"
31 #include "message_pragmas.h"
32 #include "state.h"
33 
34 #ifndef AHRS_MADGWICK_OUTPUT_ENABLED
35 #define AHRS_MADGWICK_OUTPUT_ENABLED TRUE
36 #endif
38 
39 
44 
45 static void compute_body_orientation_and_rates(void);
46 
47 #if PERIODIC_TELEMETRY
49 
50 static void send_att(struct transport_tx *trans, struct link_device *dev)
51 {
52  /* compute eulers in int (IMU frame) */
53  struct FloatEulers ltp_to_imu_euler;
54  float_eulers_of_quat(&ltp_to_imu_euler, &ahrs_madgwick.quat);
55  struct Int32Eulers eulers_imu;
56  EULERS_BFP_OF_REAL(eulers_imu, ltp_to_imu_euler);
57 
58  /* compute Eulers in int (body frame) */
59  struct FloatEulers ltp_to_body_euler;
60  float_eulers_of_quat(&ltp_to_body_euler, &ahrs_madgwick.quat);
61  struct Int32Eulers eulers_body;
62  EULERS_BFP_OF_REAL(eulers_body, ltp_to_body_euler);
63 
64  pprz_msg_send_AHRS_EULER_INT(trans, dev, AC_ID,
65  &eulers_imu.phi,
66  &eulers_imu.theta,
67  &eulers_imu.psi,
68  &eulers_body.phi,
69  &eulers_body.theta,
70  &eulers_body.psi,
72 }
73 
74 static void send_filter_status(struct transport_tx *trans, struct link_device *dev)
75 {
76  uint8_t mde = 3;
77  uint16_t val = 0;
78  if (!ahrs_madgwick.is_aligned) { mde = 2; }
80  /* set lost if no new gyro measurements for 50ms */
81  if (t_diff > 50000) { mde = 5; }
82  pprz_msg_send_STATE_FILTER_STATUS(trans, dev, AC_ID, &ahrs_madgwick_id, &mde, &val);
83 }
84 #endif
85 
86 
87 /*
88  * ABI bindings
89  */
91 #ifndef AHRS_MADGWICK_IMU_ID
92 #define AHRS_MADGWICK_IMU_ID ABI_BROADCAST
93 #endif
95 
96 
97 #ifndef AHRS_MADGWICK_MAG_ID
98 #define AHRS_MADGWICK_MAG_ID AHRS_MADGWICK_IMU_ID
99 #endif
101 
105 
111 static void gyro_cb(uint8_t sender_id __attribute__((unused)),
112  uint32_t stamp, struct Int32Rates *gyro)
113 {
114  struct FloatRates gyro_f;
115  RATES_FLOAT_OF_BFP(gyro_f, *gyro);
116 
117 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_PROPAGATE_FREQUENCY)
118  PRINT_CONFIG_MSG("Calculating dt for AHRS Madgwick propagation.")
119  /* timestamp in usec when last callback was received */
120  static uint32_t last_stamp = 0;
121 
122  if (last_stamp > 0 && ahrs_madgwick.is_aligned) {
123  float dt = (float)(stamp - last_stamp) * 1e-6;
124  ahrs_madgwick_propagate(&gyro_f, dt);
126  }
127  last_stamp = stamp;
128 #else
129  PRINT_CONFIG_MSG("Using fixed AHRS_PROPAGATE_FREQUENCY for AHRS Madgwick propagation.")
131  const float dt = 1. / (AHRS_PROPAGATE_FREQUENCY);
133  ahrs_madgwick_propagate(&gyro_f, dt);
135  }
136 #endif
137 
138  ahrs_madgwick_last_stamp = stamp;
139 }
140 
141 static void accel_cb(uint8_t sender_id __attribute__((unused)),
142  uint32_t stamp __attribute__((unused)),
143  struct Int32Vect3 *accel)
144 {
146  struct FloatVect3 accel_f;
147  ACCELS_FLOAT_OF_BFP(accel_f, *accel);
148  ahrs_madgwick_update_accel(&accel_f);
149  }
150 }
151 
152 static void aligner_cb(uint8_t __attribute__((unused)) sender_id,
153  uint32_t stamp __attribute__((unused)),
154  struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel,
155  struct Int32Vect3 *lp_mag __attribute__((unused)))
156 {
157  if (!ahrs_madgwick.is_aligned) {
158  /* convert to float */
159  struct FloatRates gyro_f;
160  RATES_FLOAT_OF_BFP(gyro_f, *lp_gyro);
161  struct FloatVect3 accel_f;
162  ACCELS_FLOAT_OF_BFP(accel_f, *lp_accel);
163  ahrs_madgwick_align(&gyro_f, &accel_f);
165  }
166 }
167 
168 static bool ahrs_madgwick_enable_output(bool enable)
169 {
172 }
173 
178 {
180  /* Set state */
182 
183  /* compute body rates */
185  }
186 }
187 
188 
190 {
194 
195  /*
196  * Subscribe to scaled IMU measurements and attach callbacks
197  */
198  AbiBindMsgIMU_GYRO(AHRS_MADGWICK_IMU_ID, &gyro_ev, gyro_cb);
199  AbiBindMsgIMU_ACCEL(AHRS_MADGWICK_IMU_ID, &accel_ev, accel_cb);
200  AbiBindMsgIMU_LOWPASSED(ABI_BROADCAST, &aligner_ev, aligner_cb);
201 
202 #if PERIODIC_TELEMETRY
203  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_EULER_INT, send_att);
204  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STATE_FILTER_STATUS, send_filter_status);
205 #endif
206 }
207 
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
void ahrs_register_impl(AhrsEnableOutput enable)
Register an AHRS implementation.
Definition: ahrs.c:62
Dispatcher to register actual AHRS implementations.
#define AHRS_COMP_ID_MADGWICK
Definition: ahrs.h:45
struct AhrsMadgwick ahrs_madgwick
Definition: ahrs_madgwick.c:39
void ahrs_madgwick_propagate(struct FloatRates *gyro, float dt)
Definition: ahrs_madgwick.c:69
void ahrs_madgwick_update_accel(struct FloatVect3 *accel)
void ahrs_madgwick_align(struct FloatRates *lp_gyro, struct FloatVect3 *lp_accel)
Definition: ahrs_madgwick.c:59
void ahrs_madgwick_init(void)
Definition: ahrs_madgwick.c:50
bool is_aligned
aligned flag
Definition: ahrs_madgwick.h:44
struct FloatRates rates
Measured gyro rates.
Definition: ahrs_madgwick.h:40
struct FloatQuat quat
Estimated attitude (quaternion)
Definition: ahrs_madgwick.h:39
static void compute_body_orientation_and_rates(void)
Compute body orientation and rates from imu orientation and rates.
#define AHRS_MADGWICK_IMU_ID
IMU (gyro, accel)
static uint8_t ahrs_madgwick_id
static abi_event accel_ev
static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro)
Call ahrs_madgwick_propagate on new gyro measurements.
static uint32_t ahrs_madgwick_last_stamp
last gyro msg timestamp
static abi_event gyro_ev
static abi_event aligner_ev
static void send_filter_status(struct transport_tx *trans, struct link_device *dev)
#define AHRS_MADGWICK_MAG_ID
magnetometer
static bool ahrs_madgwick_enable_output(bool enable)
static void accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel)
void ahrs_madgwick_register(void)
static void aligner_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel, struct Int32Vect3 *lp_mag)
#define AHRS_MADGWICK_OUTPUT_ENABLED
static void send_att(struct transport_tx *trans, struct link_device *dev)
static bool ahrs_madgwick_output_enabled
if TRUE with push the estimation results to the state interface
Paparazzi specific wrapper to run Madgwick ahrs filter.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q)
euler rotation 'ZYX'
euler angles
angular rates
#define EULERS_BFP_OF_REAL(_ei, _ef)
Definition: pprz_algebra.h:715
#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 phi
in rad with INT32_ANGLE_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(struct FloatQuat *ned_to_body_quat)
Set vehicle body attitude from quaternion (float).
Definition: state.h:1093
static void stateSetBodyRates_f(struct FloatRates *body_rate)
Set vehicle body angular rate (float).
Definition: state.h:1181
#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