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_invariant_wrapper.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 Jean-Philippe Condomines, Gautier Hattenberger
3  * 2015 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, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
29 #include "subsystems/ahrs.h"
30 #include "subsystems/abi.h"
31 #include "mcu_periph/sys_time.h"
32 #include "message_pragmas.h"
33 #include "state.h"
34 
35 #ifndef AHRS_FINV_OUTPUT_ENABLED
36 #define AHRS_FINV_OUTPUT_ENABLED TRUE
37 #endif
38 PRINT_CONFIG_VAR(AHRS_INV_OUTPUT_ENABLED)
39 
40 
45 
46 static void compute_body_orientation_and_rates(void);
47 
48 #if PERIODIC_TELEMETRY
50 
51 static void send_att(struct transport_tx *trans, struct link_device *dev)
52 {
53  /* compute eulers in int (IMU frame) */
54  struct FloatEulers ltp_to_imu_euler;
55  float_eulers_of_quat(&ltp_to_imu_euler, &ahrs_float_inv.state.quat);
56  struct Int32Eulers eulers_imu;
57  EULERS_BFP_OF_REAL(eulers_imu, ltp_to_imu_euler);
58 
59  /* compute Eulers in int (body frame) */
60  struct FloatQuat ltp_to_body_quat;
61  struct FloatQuat *body_to_imu_quat = orientationGetQuat_f(&ahrs_float_inv.body_to_imu);
62  float_quat_comp_inv(&ltp_to_body_quat, &ahrs_float_inv.state.quat, body_to_imu_quat);
63  struct FloatEulers ltp_to_body_euler;
64  float_eulers_of_quat(&ltp_to_body_euler, &ltp_to_body_quat);
65  struct Int32Eulers eulers_body;
66  EULERS_BFP_OF_REAL(eulers_body, ltp_to_body_euler);
67 
68  pprz_msg_send_AHRS_EULER_INT(trans, dev, AC_ID,
69  &eulers_imu.phi,
70  &eulers_imu.theta,
71  &eulers_imu.psi,
72  &eulers_body.phi,
73  &eulers_body.theta,
74  &eulers_body.psi,
75  &ahrs_finv_id);
76 }
77 
78 static void send_geo_mag(struct transport_tx *trans, struct link_device *dev)
79 {
80  pprz_msg_send_GEO_MAG(trans, dev, AC_ID,
84 }
85 
86 static void send_filter_status(struct transport_tx *trans, struct link_device *dev)
87 {
88  uint8_t mde = 3;
89  uint16_t val = 0;
90  if (!ahrs_float_inv.is_aligned) { mde = 2; }
92  /* set lost if no new gyro measurements for 50ms */
93  if (t_diff > 50000) { mde = 5; }
94  pprz_msg_send_STATE_FILTER_STATUS(trans, dev, AC_ID, &ahrs_finv_id, &mde, &val);
95 }
96 #endif
97 
98 
99 /*
100  * ABI bindings
101  */
103 #ifndef AHRS_FINV_IMU_ID
104 #define AHRS_FINV_IMU_ID ABI_BROADCAST
105 #endif
106 PRINT_CONFIG_VAR(AHRS_FINV_IMU_ID)
107 
108 
109 #ifndef AHRS_FINV_MAG_ID
110 #define AHRS_FINV_MAG_ID AHRS_FINV_IMU_ID
111 #endif
112 PRINT_CONFIG_VAR(AHRS_FINV_MAG_ID)
113 
120 
126 static void gyro_cb(uint8_t sender_id __attribute__((unused)),
127  uint32_t stamp, struct Int32Rates *gyro)
128 {
129 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_PROPAGATE_FREQUENCY)
130  PRINT_CONFIG_MSG("Calculating dt for AHRS float_invariant propagation.")
131  /* timestamp in usec when last callback was received */
132  static uint32_t last_stamp = 0;
133 
134  if (last_stamp > 0 && ahrs_float_inv.is_aligned) {
135  float dt = (float)(stamp - last_stamp) * 1e-6;
138  }
139  last_stamp = stamp;
140 #else
141  PRINT_CONFIG_MSG("Using fixed AHRS_PROPAGATE_FREQUENCY for AHRS float_invariant propagation.")
142  PRINT_CONFIG_VAR(AHRS_PROPAGATE_FREQUENCY)
143  const float dt = 1. / (AHRS_PROPAGATE_FREQUENCY);
147  }
148 #endif
149 
150  ahrs_finv_last_stamp = stamp;
151 }
152 
153 static void accel_cb(uint8_t sender_id __attribute__((unused)),
154  uint32_t stamp __attribute__((unused)),
155  struct Int32Vect3 *accel)
156 {
159  }
160 }
161 
162 static void mag_cb(uint8_t sender_id __attribute__((unused)),
163  uint32_t stamp __attribute__((unused)),
164  struct Int32Vect3 *mag)
165 {
168  }
169 }
170 
171 static void aligner_cb(uint8_t __attribute__((unused)) sender_id,
172  uint32_t stamp __attribute__((unused)),
173  struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel,
174  struct Int32Vect3 *lp_mag)
175 {
176  if (!ahrs_float_inv.is_aligned) {
177  ahrs_float_invariant_align(lp_gyro, lp_accel, lp_mag);
179  }
180 }
181 
182 static void body_to_imu_cb(uint8_t sender_id __attribute__((unused)),
183  struct FloatQuat *q_b2i_f)
184 {
186 }
187 
188 static void geo_mag_cb(uint8_t sender_id __attribute__((unused)), struct FloatVect3 *h)
189 {
190  ahrs_float_inv.mag_h = *h;
191 }
192 
193 static bool_t ahrs_float_invariant_enable_output(bool_t enable)
194 {
195  ahrs_finv_output_enabled = enable;
197 }
198 
203 {
204  if (ahrs_finv_output_enabled) {
205  /* Compute LTP to BODY quaternion */
206  struct FloatQuat ltp_to_body_quat;
207  struct FloatQuat *body_to_imu_quat = orientationGetQuat_f(&ahrs_float_inv.body_to_imu);
208  float_quat_comp_inv(&ltp_to_body_quat, &ahrs_float_inv.state.quat, body_to_imu_quat);
209  /* Set state */
210  stateSetNedToBodyQuat_f(&ltp_to_body_quat);
211 
212  /* compute body rates */
213  struct FloatRates body_rate;
215  struct FloatRMat *body_to_imu_rmat = orientationGetRMat_f(&ahrs_float_inv.body_to_imu);
216  float_rmat_transp_ratemult(&body_rate, body_to_imu_rmat, &body_rate);
217  stateSetBodyRates_f(&body_rate);
218 
219  }
220 }
221 
222 
224 {
225  ahrs_finv_output_enabled = AHRS_FINV_OUTPUT_ENABLED;
228 
229  /*
230  * Subscribe to scaled IMU measurements and attach callbacks
231  */
232  AbiBindMsgIMU_MAG_INT32(AHRS_FINV_MAG_ID, &mag_ev, mag_cb);
233  AbiBindMsgIMU_GYRO_INT32(AHRS_FINV_IMU_ID, &gyro_ev, gyro_cb);
234  AbiBindMsgIMU_ACCEL_INT32(AHRS_FINV_IMU_ID, &accel_ev, accel_cb);
235  AbiBindMsgIMU_LOWPASSED(AHRS_FINV_IMU_ID, &aligner_ev, aligner_cb);
236  AbiBindMsgBODY_TO_IMU_QUAT(AHRS_FINV_IMU_ID, &body_to_imu_ev, body_to_imu_cb);
237  AbiBindMsgGEO_MAG(ABI_BROADCAST, &geo_mag_ev, geo_mag_cb);
238 
239 #if PERIODIC_TELEMETRY
240  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_EULER_INT, send_att);
241  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_GEO_MAG, send_geo_mag);
242  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STATE_FILTER_STATUS, send_filter_status);
243 #endif
244 }
int32_t psi
in rad with INT32_ANGLE_FRAC
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
static abi_event geo_mag_ev
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.
angular rates
#define AHRS_COMP_ID_FINV
Definition: ahrs.h:39
Dispatcher to register actual AHRS implementations.
static uint8_t ahrs_finv_id
static void mag_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *mag)
Generic transmission transport header.
Definition: transport.h:89
struct OrientationReps body_to_imu
body_to_imu rotation
#define AHRS_PROPAGATE_FREQUENCY
Definition: hf_float.c:51
void ahrs_float_invariant_init(void)
#define EULERS_BFP_OF_REAL(_ei, _ef)
Definition: pprz_algebra.h:648
Periodic telemetry system header (includes downlink utility and generated code).
int32_t theta
in rad with INT32_ANGLE_FRAC
static abi_event mag_ev
struct FloatQuat quat
Estimated attitude (quaternion)
static struct FloatRMat * orientationGetRMat_f(struct OrientationReps *orientation)
Get vehicle body attitude rotation matrix (float).
static abi_event accel_ev
Main include for ABI (AirBorneInterface).
static void stateSetNedToBodyQuat_f(struct FloatQuat *ned_to_body_quat)
Set vehicle body attitude from quaternion (float).
Definition: state.h:1064
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 geo_mag_cb(uint8_t sender_id, struct FloatVect3 *h)
#define RATES_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:371
void ahrs_float_invariant_propagate(struct Int32Rates *gyro, float dt)
struct inv_command cmd
command vector
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.
static uint32_t ahrs_finv_last_stamp
last gyro msg timestamp
void ahrs_float_inv_set_body_to_imu_quat(struct FloatQuat *q_b2i)
static bool_t ahrs_float_invariant_enable_output(bool_t enable)
void ahrs_register_impl(AhrsEnableOutput enable)
Register an AHRS implementation.
Definition: ahrs.c:57
Architecture independent timing functions.
static void compute_body_orientation_and_rates(void)
Compute body orientation and rates from imu orientation and rates.
euler angles
uint16_t val[TCOUPLE_NB]
unsigned long uint32_t
Definition: types.h:18
Paparazzi specific wrapper to run INVARIANT ahrs filter.
struct FloatRates rates
Input gyro rates.
static bool_t ahrs_finv_output_enabled
if TRUE with push the estimation results to the state interface
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
void ahrs_float_invariant_align(struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel, struct Int32Vect3 *lp_mag)
struct FloatRates bias
Estimated gyro biases.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
#define AHRS_FINV_IMU_ID
IMU (gyro, accel)
static void accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel)
int32_t phi
in rad with INT32_ANGLE_FRAC
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.
static abi_event aligner_ev
void ahrs_float_invariant_update_accel(struct Int32Vect3 *accel)
rotation matrix
#define AHRS_FINV_OUTPUT_ENABLED
#define ABI_BROADCAST
Broadcast address.
Definition: abi_common.h:56
struct FloatVect3 mag_h
static void gyro_cb(uint8_t sender_id, uint32_t stamp, struct Int32Rates *gyro)
Call ahrs_float_invariant_propagate on new gyro measurements.
static abi_event body_to_imu_ev
struct AhrsFloatInv ahrs_float_inv
static void stateSetBodyRates_f(struct FloatRates *body_rate)
Set vehicle body angular rate (float).
Definition: state.h:1152
void float_eulers_of_quat(struct FloatEulers *e, struct FloatQuat *q)
#define AHRS_FINV_MAG_ID
magnetometer
void ahrs_float_invariant_register(void)
angular rates
static void body_to_imu_cb(uint8_t sender_id, struct FloatQuat *q_b2i_f)
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 abi_event gyro_ev
struct inv_state state
state vector
static struct FloatQuat * orientationGetQuat_f(struct OrientationReps *orientation)
Get vehicle body attitude quaternion (float).
void ahrs_float_invariant_update_mag(struct Int32Vect3 *mag)