Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
quaternion_filter.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 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 */
21
27#ifndef QUATERNION_FILTER_H
28#define QUATERNION_FILTER_H
29
30#include "std.h"
32
43
53 struct QuatSecondOrderLowPass *filter,
54 const float omega,
55 const float damp,
56 const float sample_time,
57 const struct FloatQuat q0)
58{
59 filter->quat = q0;
60 FLOAT_RATES_ZERO(filter->rate);
61 FLOAT_RATES_ZERO(filter->accel);
62 filter->two_zeta_omega = 2.f * damp * omega;
63 filter->two_omega2 = 2.f * omega * omega;
64 filter->dt = sample_time;
65}
66
73 struct QuatSecondOrderLowPass *filter,
74 const struct FloatQuat q0)
75{
76 filter->quat = q0;
77 FLOAT_RATES_ZERO(filter->rate);
78 FLOAT_RATES_ZERO(filter->accel);
79}
80
88 struct QuatSecondOrderLowPass *filter,
89 const struct FloatQuat q)
90{
91 /* integrate quaternion */
92 struct FloatQuat qdot;
93 float_quat_derivative(&qdot, &filter->rate, &filter->quat);
94 QUAT_SMUL(qdot, qdot, filter->dt);
95 QUAT_ADD(filter->quat, qdot);
96 float_quat_normalize(&filter->quat);
97
98 /* integrate rotational speed */
100 RATES_SMUL(delta_rate, filter->accel, filter->dt);
101 RATES_ADD(filter->rate, delta_rate);
102
103 /* compute angular accelerations */
104 struct FloatQuat err;
105 /* attitude error */
106 float_quat_inv_comp(&err, &q, &filter->quat);
107 /* wrap it in the shortest direction */
109 /* propagate the 2nd order linear model: xdotdot = -2*zeta*omega*xdot - omega^2*x */
110 /* since error quaternion contains the half-angles we get 2*omega^2*err */
111 filter->accel.p = -filter->two_zeta_omega * filter->rate.p - filter->two_omega2 * err.qx;
112 filter->accel.q = -filter->two_zeta_omega * filter->rate.q - filter->two_omega2 * err.qy;
113 filter->accel.r = -filter->two_zeta_omega * filter->rate.r - filter->two_omega2 * err.qz;
114
115 return filter->quat;
116}
117
124 const struct QuatSecondOrderLowPass *filter)
125{
126 return filter->quat;
127}
128
129
133
146 const float omega,
147 const float sample_time,
148 const struct FloatQuat q0)
149{
150 init_quat_second_order_low_pass((struct QuatSecondOrderLowPass *)filter, omega, 0.7071f, sample_time, q0);
151}
152
161 const struct FloatQuat quat)
162{
163 return update_quat_second_order_low_pass((struct QuatSecondOrderLowPass *)filter, quat);
164}
165
174 const struct FloatQuat quat)
175{
177}
178
186{
187 return get_quat_second_order_low_pass((const struct QuatSecondOrderLowPass *)filter);
188}
189
190#endif
191
static void float_quat_normalize(struct FloatQuat *q)
void float_quat_inv_comp(struct FloatQuat *b2c, const struct FloatQuat *a2b, const struct FloatQuat *a2c)
Composition (multiplication) of two quaternions.
static void float_quat_wrap_shortest(struct FloatQuat *q)
#define FLOAT_RATES_ZERO(_r)
void float_quat_derivative(struct FloatQuat *qd, const struct FloatRates *r, const struct FloatQuat *q)
Quaternion derivative from rotational velocity.
Roation quaternion.
angular rates
#define RATES_SMUL(_ro, _ri, _s)
#define RATES_ADD(_a, _b)
#define QUAT_SMUL(_qo, _qi, _s)
#define QUAT_ADD(_qo, _qi)
uint16_t foo
Definition main_demo5.c:58
Paparazzi floating point algebra.
static struct FloatQuat update_quat_butterworth_low_pass(QuatButterworthLowPass *filter, const struct FloatQuat quat)
Update second order quaternion Butterworth low pass filter state with a new value.
static struct FloatQuat update_quat_second_order_low_pass(struct QuatSecondOrderLowPass *filter, const struct FloatQuat q)
Update second order quaterion low pass filter state with a new value.
static struct FloatQuat get_quat_butterworth_low_pass(const QuatButterworthLowPass *filter)
Get current value of the second order quaternion Butterworth low pass filter.
static void reset_quat_butterworth_low_pass(QuatButterworthLowPass *filter, const struct FloatQuat quat)
Reset a quaternion Butterworth low-pass filter to a specific value.
static void reset_quat_second_order_low_pass(struct QuatSecondOrderLowPass *filter, const struct FloatQuat q0)
Reset the second order quaternion low-pass filter to a specific value.
static void init_quat_butterworth_low_pass(QuatButterworthLowPass *filter, const float omega, const float sample_time, const struct FloatQuat q0)
Init a second order quaternion Butterworth filter.
struct FloatRates accel
static struct FloatQuat get_quat_second_order_low_pass(const struct QuatSecondOrderLowPass *filter)
Get current value of the second order quaterion low pass filter.
static void init_quat_second_order_low_pass(struct QuatSecondOrderLowPass *filter, const float omega, const float damp, const float sample_time, const struct FloatQuat q0)
Init second order quaternion low pass filter.
Quaternion second order filter model (float)