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
ahrs_float_mlkf_wrapper.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 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, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
29 #include "subsystems/ahrs.h"
30 #include "subsystems/abi.h"
31 #include "state.h"
32 
33 #ifndef AHRS_MLKF_OUTPUT_ENABLED
34 #define AHRS_MLKF_OUTPUT_ENABLED TRUE
35 #endif
36 PRINT_CONFIG_VAR(AHRS_MLKF_OUTPUT_ENABLED)
37 
38 
42 
43 static void set_body_state_from_quat(void);
44 
45 #if PERIODIC_TELEMETRY
47 #include "mcu_periph/sys_time.h"
48 
49 static void send_euler(struct transport_tx *trans, struct link_device *dev)
50 {
51  struct FloatEulers ltp_to_imu_euler;
52  float_eulers_of_quat(&ltp_to_imu_euler, &ahrs_mlkf.ltp_to_imu_quat);
53  pprz_msg_send_AHRS_EULER(trans, dev, AC_ID,
54  &ltp_to_imu_euler.phi,
55  &ltp_to_imu_euler.theta,
56  &ltp_to_imu_euler.psi,
57  &ahrs_mlkf_id);
58 }
59 
60 static void send_bias(struct transport_tx *trans, struct link_device *dev)
61 {
62  struct Int32Rates gyro_bias;
64  pprz_msg_send_AHRS_GYRO_BIAS_INT(trans, dev, AC_ID,
65  &gyro_bias.p, &gyro_bias.q, &gyro_bias.r, &ahrs_mlkf_id);
66 }
67 
68 static void send_geo_mag(struct transport_tx *trans, struct link_device *dev)
69 {
70  pprz_msg_send_GEO_MAG(trans, dev, AC_ID,
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_mlkf.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_mlkf_id, &mde, &val);
83 }
84 #endif
85 
86 
90 #ifndef AHRS_MLKF_IMU_ID
91 #define AHRS_MLKF_IMU_ID ABI_BROADCAST
92 #endif
93 PRINT_CONFIG_VAR(AHRS_MLKF_IMU_ID)
97 #ifndef AHRS_MLKF_MAG_ID
98 #define AHRS_MLKF_MAG_ID AHRS_MLKF_IMU_ID
99 #endif
100 PRINT_CONFIG_VAR(AHRS_MLKF_MAG_ID)
107 
108 
109 static void gyro_cb(uint8_t __attribute__((unused)) sender_id,
110  uint32_t stamp, struct Int32Rates *gyro)
111 {
112  ahrs_mlkf_last_stamp = stamp;
113 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_PROPAGATE_FREQUENCY)
114  PRINT_CONFIG_MSG("Calculating dt for AHRS_MLKF propagation.")
115  /* timestamp in usec when last callback was received */
116  static uint32_t last_stamp = 0;
117 
118  if (last_stamp > 0 && ahrs_mlkf.is_aligned) {
119  float dt = (float)(stamp - last_stamp) * 1e-6;
120  ahrs_mlkf_propagate(gyro, dt);
122  }
123  last_stamp = stamp;
124 #else
125  PRINT_CONFIG_MSG("Using fixed AHRS_PROPAGATE_FREQUENCY for AHRS_MLKF propagation.")
126  PRINT_CONFIG_VAR(AHRS_PROPAGATE_FREQUENCY)
128  const float dt = 1. / (AHRS_PROPAGATE_FREQUENCY);
129  ahrs_mlkf_propagate(gyro, dt);
131  }
132 #endif
133 }
134 
135 static void accel_cb(uint8_t sender_id __attribute__((unused)),
136  uint32_t stamp __attribute__((unused)),
137  struct Int32Vect3 *accel)
138 {
139  if (ahrs_mlkf.is_aligned) {
140  ahrs_mlkf_update_accel(accel);
142  }
143 }
144 
145 static void mag_cb(uint8_t sender_id __attribute__((unused)),
146  uint32_t stamp __attribute__((unused)),
147  struct Int32Vect3 *mag)
148 {
149  if (ahrs_mlkf.is_aligned) {
152  }
153 }
154 
155 static void aligner_cb(uint8_t __attribute__((unused)) sender_id,
156  uint32_t stamp __attribute__((unused)),
157  struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel,
158  struct Int32Vect3 *lp_mag)
159 {
160  if (!ahrs_mlkf.is_aligned) {
161  /* set initial body orientation in state interface if alignment was successful */
162  if (ahrs_mlkf_align(lp_gyro, lp_accel, lp_mag)) {
164  }
165  }
166 }
167 
168 static void body_to_imu_cb(uint8_t sender_id __attribute__((unused)),
169  struct FloatQuat *q_b2i_f)
170 {
172 }
173 
174 static void geo_mag_cb(uint8_t sender_id __attribute__((unused)), struct FloatVect3 *h)
175 {
176  ahrs_mlkf.mag_h = *h;
177 }
178 
179 static bool_t ahrs_mlkf_enable_output(bool_t enable)
180 {
181  ahrs_mlkf_output_enabled = enable;
183 }
184 
188 static void set_body_state_from_quat(void)
189 {
190  if (ahrs_mlkf_output_enabled) {
191  struct FloatQuat *body_to_imu_quat = orientationGetQuat_f(&ahrs_mlkf.body_to_imu);
192  struct FloatRMat *body_to_imu_rmat = orientationGetRMat_f(&ahrs_mlkf.body_to_imu);
193 
194  /* Compute LTP to BODY quaternion */
195  struct FloatQuat ltp_to_body_quat;
196  float_quat_comp_inv(&ltp_to_body_quat, &ahrs_mlkf.ltp_to_imu_quat, body_to_imu_quat);
197  /* Set in state interface */
198  stateSetNedToBodyQuat_f(&ltp_to_body_quat);
199 
200  /* compute body rates */
201  struct FloatRates body_rate;
202  float_rmat_transp_ratemult(&body_rate, body_to_imu_rmat, &ahrs_mlkf.imu_rate);
203  /* Set state */
204  stateSetBodyRates_f(&body_rate);
205  }
206 }
207 
209 {
210  ahrs_mlkf_output_enabled = AHRS_MLKF_OUTPUT_ENABLED;
211  ahrs_mlkf_init();
213 
214  /*
215  * Subscribe to scaled IMU measurements and attach callbacks
216  */
217  AbiBindMsgIMU_GYRO_INT32(AHRS_MLKF_IMU_ID, &gyro_ev, gyro_cb);
218  AbiBindMsgIMU_ACCEL_INT32(AHRS_MLKF_IMU_ID, &accel_ev, accel_cb);
219  AbiBindMsgIMU_MAG_INT32(AHRS_MLKF_MAG_ID, &mag_ev, mag_cb);
220  AbiBindMsgIMU_LOWPASSED(ABI_BROADCAST, &aligner_ev, aligner_cb);
221  AbiBindMsgBODY_TO_IMU_QUAT(ABI_BROADCAST, &body_to_imu_ev, body_to_imu_cb);
222  AbiBindMsgGEO_MAG(ABI_BROADCAST, &geo_mag_ev, geo_mag_cb);
223 
224 #if PERIODIC_TELEMETRY
225  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_EULER, send_euler);
226  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_GYRO_BIAS_INT, send_bias);
227  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_GEO_MAG, send_geo_mag);
228  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STATE_FILTER_STATUS, send_filter_status);
229 #endif
230 }
231 
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
bool_t ahrs_mlkf_align(struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel, struct Int32Vect3 *lp_mag)
unsigned short uint16_t
Definition: types.h:16
void float_quat_comp_inv(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions.
static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro)
angular rates
#define RATES_BFP_OF_REAL(_ri, _rf)
Definition: pprz_algebra.h:698
Dispatcher to register actual AHRS implementations.
static abi_event mag_ev
float phi
in radians
Generic transmission transport header.
Definition: transport.h:89
#define AHRS_MLKF_IMU_ID
ABI binding for IMU data.
#define AHRS_PROPAGATE_FREQUENCY
Definition: hf_float.c:51
static abi_event body_to_imu_ev
#define AHRS_MLKF_OUTPUT_ENABLED
Periodic telemetry system header (includes downlink utility and generated code).
static struct FloatRMat * orientationGetRMat_f(struct OrientationReps *orientation)
Get vehicle body attitude rotation matrix (float).
struct FloatVect3 mag_h
Paparazzi specific wrapper to run MLKF filter.
struct FloatRates imu_rate
Rotational velocity in IMU frame.
static abi_event accel_ev
Main include for ABI (AirBorneInterface).
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 mag_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *mag)
static void stateSetNedToBodyQuat_f(struct FloatQuat *ned_to_body_quat)
Set vehicle body attitude from quaternion (float).
Definition: state.h:1064
float psi
in radians
struct FloatRates gyro_bias
static bool_t ahrs_mlkf_enable_output(bool_t enable)
void ahrs_mlkf_update_accel(struct Int32Vect3 *accel)
static void body_to_imu_cb(uint8_t sender_id, struct FloatQuat *q_b2i_f)
enum AhrsMlkfStatus status
static void set_body_state_from_quat(void)
Compute body orientation and rates from imu orientation and rates.
static void accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel)
static abi_event geo_mag_ev
static uint32_t ahrs_mlkf_last_stamp
static abi_event aligner_ev
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
euler angles
Roation quaternion.
float theta
in radians
static abi_event gyro_ev
void ahrs_register_impl(AhrsEnableOutput enable)
Register an AHRS implementation.
Definition: ahrs.c:57
Architecture independent timing functions.
void ahrs_mlkf_set_body_to_imu_quat(struct FloatQuat *q_b2i)
uint16_t val[TCOUPLE_NB]
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
void ahrs_mlkf_propagate(struct Int32Rates *gyro, float dt)
void ahrs_mlkf_init(void)
#define AHRS_COMP_ID_MLKF
Definition: ahrs.h:40
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
struct AhrsMlkf ahrs_mlkf
bool_t is_aligned
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
void float_rmat_transp_ratemult(struct FloatRates *rb, struct FloatRMat *m_b2a, struct FloatRates *ra)
rotate anglular rates by transposed rotation matrix.
void ahrs_mlkf_update_mag(struct Int32Vect3 *mag)
struct OrientationReps body_to_imu
body_to_imu rotation
static uint8_t ahrs_mlkf_id
struct FloatQuat ltp_to_imu_quat
Rotation from LocalTangentPlane to IMU frame as unit quaternion.
rotation matrix
#define AHRS_MLKF_MAG_ID
ABI binding for magnetometer data.
#define ABI_BROADCAST
Broadcast address.
Definition: abi_common.h:56
static void stateSetBodyRates_f(struct FloatRates *body_rate)
Set vehicle body angular rate (float).
Definition: state.h:1152
static bool_t ahrs_mlkf_output_enabled
if TRUE with push the estimation results to the state interface
void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q)
angular rates
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
static struct FloatQuat * orientationGetQuat_f(struct OrientationReps *orientation)
Get vehicle body attitude quaternion (float).
static void geo_mag_cb(uint8_t sender_id, struct FloatVect3 *h)
void ahrs_mlkf_register(void)