Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
ahrs_int_cmpl_quat_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 #ifndef AHRS_ICQ_OUTPUT_ENABLED
33 #define AHRS_ICQ_OUTPUT_ENABLED TRUE
34 #endif
35 PRINT_CONFIG_VAR(AHRS_ICQ_OUTPUT_ENABLED)
36 
37 
41 
42 static void set_body_state_from_quat(void);
43 
44 #if PERIODIC_TELEMETRY
46 #include "mcu_periph/sys_time.h"
47 #include "state.h"
48 
49 static void send_quat(struct transport_tx *trans, struct link_device *dev)
50 {
51  struct Int32Quat *quat = stateGetNedToBodyQuat_i();
52  pprz_msg_send_AHRS_QUAT_INT(trans, dev, AC_ID,
58  &(quat->qi),
59  &(quat->qx),
60  &(quat->qy),
61  &(quat->qz),
62  &ahrs_icq_id);
63 }
64 
65 static void send_euler(struct transport_tx *trans, struct link_device *dev)
66 {
67  struct Int32Eulers ltp_to_body_euler;
68  int32_eulers_of_quat(&ltp_to_body_euler, &ahrs_icq.ltp_to_body_quat);
69  struct Int32Eulers *eulers = stateGetNedToBodyEulers_i();
70  pprz_msg_send_AHRS_EULER_INT(trans, dev, AC_ID,
71  &ltp_to_body_euler.phi,
72  &ltp_to_body_euler.theta,
73  &ltp_to_body_euler.psi,
74  &(eulers->phi),
75  &(eulers->theta),
76  &(eulers->psi),
77  &ahrs_icq_id);
78 }
79 
80 static void send_bias(struct transport_tx *trans, struct link_device *dev)
81 {
82  pprz_msg_send_AHRS_GYRO_BIAS_INT(trans, dev, AC_ID,
85 }
86 
87 static void send_geo_mag(struct transport_tx *trans, struct link_device *dev)
88 {
89  struct FloatVect3 h_float;
90  h_float.x = MAG_FLOAT_OF_BFP(ahrs_icq.mag_h.x);
91  h_float.y = MAG_FLOAT_OF_BFP(ahrs_icq.mag_h.y);
92  h_float.z = MAG_FLOAT_OF_BFP(ahrs_icq.mag_h.z);
93  pprz_msg_send_GEO_MAG(trans, dev, AC_ID,
94  &h_float.x, &h_float.y, &h_float.z, &ahrs_icq_id);
95 }
96 
97 static void send_filter_status(struct transport_tx *trans, struct link_device *dev)
98 {
99  uint8_t mde = 3;
100  uint16_t val = 0;
101  if (!ahrs_icq.is_aligned) { mde = 2; }
103  /* set lost if no new gyro measurements for 50ms */
104  if (t_diff > 50000) { mde = 5; }
105  pprz_msg_send_STATE_FILTER_STATUS(trans, dev, AC_ID, &ahrs_icq_id, &mde, &val);
106 }
107 #endif
108 
109 
113 #ifndef AHRS_ICQ_IMU_ID
114 #define AHRS_ICQ_IMU_ID ABI_BROADCAST
115 #endif
116 PRINT_CONFIG_VAR(AHRS_ICQ_IMU_ID)
120 #ifndef AHRS_ICQ_MAG_ID
121 #define AHRS_ICQ_MAG_ID AHRS_ICQ_IMU_ID
122 #endif
123 PRINT_CONFIG_VAR(AHRS_ICQ_MAG_ID)
127 #ifndef AHRS_ICQ_GPS_ID
128 #define AHRS_ICQ_GPS_ID GPS_MULTI_ID
129 #endif
130 PRINT_CONFIG_VAR(AHRS_ICQ_GPS_ID)
137 
138 
139 static void gyro_cb(uint8_t __attribute__((unused)) sender_id,
140  uint32_t stamp, struct Int32Rates *gyro)
141 {
142  ahrs_icq_last_stamp = stamp;
143 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_PROPAGATE_FREQUENCY)
144  PRINT_CONFIG_MSG("Calculating dt for AHRS_ICQ propagation.")
145  /* timestamp in usec when last callback was received */
146  static uint32_t last_stamp = 0;
147 
148  if (last_stamp > 0 && ahrs_icq.is_aligned) {
149  float dt = (float)(stamp - last_stamp) * 1e-6;
150  ahrs_icq_propagate(gyro, dt);
152  }
153  last_stamp = stamp;
154 #else
155  PRINT_CONFIG_MSG("Using fixed AHRS_PROPAGATE_FREQUENCY for AHRS_ICQ propagation.")
156  PRINT_CONFIG_VAR(AHRS_PROPAGATE_FREQUENCY)
158  const float dt = 1. / (AHRS_PROPAGATE_FREQUENCY);
159  ahrs_icq_propagate(gyro, dt);
161  }
162 #endif
163 }
164 
165 static void accel_cb(uint8_t __attribute__((unused)) sender_id,
166  uint32_t __attribute__((unused)) stamp,
167  struct Int32Vect3 *accel)
168 {
169 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_CORRECT_FREQUENCY)
170  PRINT_CONFIG_MSG("Calculating dt for AHRS int_cmpl_quat accel update.")
171  static uint32_t last_stamp = 0;
172  if (last_stamp > 0 && ahrs_icq.is_aligned) {
173  float dt = (float)(stamp - last_stamp) * 1e-6;
174  ahrs_icq_update_accel(accel, dt);
176  }
177  last_stamp = stamp;
178 #else
179  PRINT_CONFIG_MSG("Using fixed AHRS_CORRECT_FREQUENCY for AHRS int_cmpl_quat accel update.")
180  PRINT_CONFIG_VAR(AHRS_CORRECT_FREQUENCY)
181  if (ahrs_icq.is_aligned) {
182  const float dt = 1. / (AHRS_CORRECT_FREQUENCY);
183  ahrs_icq_update_accel(accel, dt);
185  }
186 #endif
187 }
188 
189 static void mag_cb(uint8_t __attribute__((unused)) sender_id,
190  uint32_t __attribute__((unused)) stamp,
191  struct Int32Vect3 *mag)
192 {
193 #if USE_AUTO_AHRS_FREQ || !defined(AHRS_MAG_CORRECT_FREQUENCY)
194  PRINT_CONFIG_MSG("Calculating dt for AHRS int_cmpl_quat mag update.")
195  static uint32_t last_stamp = 0;
196  if (last_stamp > 0 && ahrs_icq.is_aligned) {
197  float dt = (float)(stamp - last_stamp) * 1e-6;
198  ahrs_icq_update_mag(mag, dt);
200  }
201  last_stamp = stamp;
202 #else
203  PRINT_CONFIG_MSG("Using fixed AHRS_MAG_CORRECT_FREQUENCY for AHRS int_cmpl_quat mag update.")
204  PRINT_CONFIG_VAR(AHRS_MAG_CORRECT_FREQUENCY)
205  if (ahrs_icq.is_aligned) {
206  const float dt = 1. / (AHRS_MAG_CORRECT_FREQUENCY);
207  ahrs_icq_update_mag(mag, dt);
209  }
210 #endif
211 }
212 
213 static void aligner_cb(uint8_t __attribute__((unused)) sender_id,
214  uint32_t stamp __attribute__((unused)),
215  struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel,
216  struct Int32Vect3 *lp_mag)
217 {
218  if (!ahrs_icq.is_aligned) {
219  if (ahrs_icq_align(lp_gyro, lp_accel, lp_mag)) {
221  }
222  }
223 }
224 
225 static void geo_mag_cb(uint8_t sender_id __attribute__((unused)), struct FloatVect3 *h)
226 {
228  MAG_BFP_OF_REAL(h->z));
229 }
230 
231 static void gps_cb(uint8_t sender_id __attribute__((unused)),
232  uint32_t stamp __attribute__((unused)),
233  struct GpsState *gps_s)
234 {
235  ahrs_icq_update_gps(gps_s);
236 }
237 
238 static bool ahrs_icq_enable_output(bool enable)
239 {
240  ahrs_icq_output_enabled = enable;
242 }
243 
245 static void set_body_state_from_quat(void)
246 {
248  /* Set state */
251  }
252 }
253 
255 {
257  ahrs_icq_init();
259 
260  /*
261  * Subscribe to scaled IMU measurements and attach callbacks
262  */
263  AbiBindMsgIMU_GYRO(AHRS_ICQ_IMU_ID, &gyro_ev, gyro_cb);
264  AbiBindMsgIMU_ACCEL(AHRS_ICQ_IMU_ID, &accel_ev, accel_cb);
265  AbiBindMsgIMU_MAG(AHRS_ICQ_MAG_ID, &mag_ev, mag_cb);
266  AbiBindMsgIMU_LOWPASSED(ABI_BROADCAST, &aligner_ev, aligner_cb);
267  AbiBindMsgGEO_MAG(ABI_BROADCAST, &geo_mag_ev, geo_mag_cb);
268  AbiBindMsgGPS(AHRS_ICQ_GPS_ID, &gps_ev, gps_cb);
269 
270 #if PERIODIC_TELEMETRY
271  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_QUAT_INT, send_quat);
272  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_EULER_INT, send_euler);
273  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_AHRS_GYRO_BIAS_INT, send_bias);
275  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_STATE_FILTER_STATUS, send_filter_status);
276 #endif
277 }
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
void ahrs_register_impl(AhrsEnableOutput enable)
Register an AHRS implementation.
Definition: ahrs.c:62
Dispatcher to register actual AHRS implementations.
#define AHRS_COMP_ID_ICQ
Definition: ahrs.h:35
bool ahrs_icq_align(struct Int32Rates *lp_gyro, struct Int32Vect3 *lp_accel, struct Int32Vect3 *lp_mag)
void ahrs_icq_update_accel(struct Int32Vect3 *accel, float dt)
struct AhrsIntCmplQuat ahrs_icq
Default Rate filter Low pass.
void ahrs_icq_init(void)
void ahrs_icq_update_gps(struct GpsState *gps_s)
void ahrs_icq_update_mag(struct Int32Vect3 *mag, float dt)
void ahrs_icq_propagate(struct Int32Rates *gyro, float dt)
struct Int32Rates gyro_bias
@ AHRS_ICQ_RUNNING
struct Int32Vect3 mag_h
struct Int32Rates body_rate
struct Int32Quat ltp_to_body_quat
enum AhrsICQStatus status
status of the AHRS, AHRS_ICQ_UNINIT or AHRS_ICQ_RUNNING
static bool ahrs_icq_enable_output(bool enable)
static void gps_cb(uint8_t sender_id, uint32_t stamp, struct GpsState *gps_s)
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)
static abi_event gyro_ev
static bool ahrs_icq_output_enabled
if TRUE with push the estimation results to the state interface
static uint8_t ahrs_icq_id
static void send_geo_mag(struct transport_tx *trans, struct link_device *dev)
#define AHRS_ICQ_IMU_ID
ABI binding for IMU data.
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_quat(struct transport_tx *trans, struct link_device *dev)
#define AHRS_ICQ_GPS_ID
ABI binding for gps data.
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 uint32_t ahrs_icq_last_stamp
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 void set_body_state_from_quat(void)
Rotate angles and rates from imu to body frame and set state.
static void send_bias(struct transport_tx *trans, struct link_device *dev)
#define AHRS_ICQ_MAG_ID
ABI binding for magnetometer data.
#define AHRS_ICQ_OUTPUT_ENABLED
void ahrs_icq_register(void)
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:86
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
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
void int32_eulers_of_quat(struct Int32Eulers *e, struct Int32Quat *q)
#define MAG_BFP_OF_REAL(_af)
#define MAG_FLOAT_OF_BFP(_ai)
euler angles
Rotation quaternion.
angular rates
static struct Int32Quat * stateGetNedToBodyQuat_i(void)
Get vehicle body attitude quaternion (int).
Definition: state.h:1113
static void stateSetNedToBodyQuat_i(struct Int32Quat *ned_to_body_quat)
Set vehicle body attitude from quaternion (int).
Definition: state.h:1075
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition: state.h:1125
static void stateSetBodyRates_i(struct Int32Rates *body_rate)
Set vehicle body angular rate (int).
Definition: state.h:1173
#define AHRS_PROPAGATE_FREQUENCY
Definition: hf_float.c:55
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
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