36#include "generated/airframe.h"
45#define PRINT_DBG(_l, _p) { \
46 if (DBG_LEVEL >= _l) \
50#define PRINT_DBG(_l, _p) {}
54#ifndef AHRS_PROPAGATE_FREQUENCY
55#define AHRS_PROPAGATE_FREQUENCY PERIODIC_FREQUENCY
59#if AHRS_PROPAGATE_FREQUENCY == 512
60#define HFF_PRESCALER 16
61#elif AHRS_PROPAGATE_FREQUENCY == 500
62#define HFF_PRESCALER 10
63#elif AHRS_PROPAGATE_FREQUENCY == 200
64#define HFF_PRESCALER 6
66#error "HFF_PRESCALER not set, needs to be a divisor of AHRS_PROPAGATE_FREQUENCY"
71#define HFF_FREQ (AHRS_PROPAGATE_FREQUENCY / HFF_PRESCALER)
72#define HFF_DT (1./HFF_FREQ)
75#define HFF_INIT_PXX 1.
77#ifndef HFF_ACCEL_NOISE
78#define HFF_ACCEL_NOISE 0.5
80#define HFF_Q HFF_ACCEL_NOISE
81#define HFF_Qdotdot HFF_ACCEL_NOISE
88#define HFF_R_POS_MIN 3.
91#ifndef HFF_R_GPS_SPEED
92#define HFF_R_GPS_SPEED 2.
94#ifndef HFF_R_GPS_SPEED_MIN
95#define HFF_R_GPS_SPEED_MIN 0.25
98#ifndef HFF_UPDATE_GPS_SPEED
99#define HFF_UPDATE_GPS_SPEED TRUE
102#ifndef HFF_LOWPASS_CUTOFF_FREQUENCY
103#define HFF_LOWPASS_CUTOFF_FREQUENCY 14
106#if HFF_LOWPASS_CUTOFF_FREQUENCY < 8
107#error "It is not allowed to use a cutoff frequency lower than 8Hz due to overflow issues."
142#define HFF_Qbiasbias 1e-7
154#define GPS_LAG_N ((int) (GPS_LAG * HFF_FREQ + 0.5))
156#define GPS_DT_N ((int) (HFF_FREQ / 4))
158#define GPS_LAG_TOLERANCE 0.08
159#define GPS_LAG_TOL_N ((int) (GPS_LAG_TOLERANCE * HFF_FREQ + 0.5))
165#define MAX_PP_STEPS 6
168#define ACC_BUF_MAXN (GPS_LAG_N+10)
169#define INC_ACC_IDX(idx) { idx = (idx + 1) < ACC_BUF_MAXN ? (idx + 1) : 0; }
182#define HFF_RB_MAXN ((int) (GPS_LAG * 4))
183#define INC_RB_POINTER(ptr) { \
184 if (ptr == &hff_rb[HFF_RB_MAXN-1]) \
209#define HFF_LOST_LIMIT 1000
232#if PERIODIC_TELEMETRY
311#if PERIODIC_TELEMETRY
374 PRINT_DBG(1, (
"Cannot go back zero steps!\n"));
384 PRINT_DBG(3, (
"get past accel. buf_n: %2d \tbuf_w: %2d \tback_n: %2d \ti: %2d \txdd: %f \tydd: %f\n",
acc_buf_n,
413 PRINT_DBG(2, (
"hff ringbuffer empty!\n"));
564#if HFF_UPDATE_GPS_SPEED
580#if HFF_UPDATE_GPS_SPEED
589 PRINT_DBG(2, (
"try next saved state\n"));
724 const float K1 =
filt->xP[0][0] * 1 /
S;
725 const float K2 =
filt->xP[1][0] * 1 /
S;
726 const float K3 =
filt->xP[2][0] * 1 /
S;
732 const float P11 = (1. -
K1) *
filt->xP[0][0];
733 const float P12 = (1. -
K1) *
filt->xP[0][1];
734 const float P13 = (1. -
K1) *
filt->xP[0][2];
759 const float K1 =
filt->yP[0][0] * 1 /
S;
760 const float K2 =
filt->yP[1][0] * 1 /
S;
761 const float K3 =
filt->yP[2][0] * 1 /
S;
767 const float P11 = (1. -
K1) *
filt->yP[0][0];
768 const float P12 = (1. -
K1) *
filt->yP[0][1];
769 const float P13 = (1. -
K1) *
filt->yP[0][2];
815 if (
Rvel.x >= 0.f ||
Rvel.y >= 0.f) {
824 const float yd = vel -
filt->xdot;
826 const float K1 =
filt->xP[0][1] * 1 /
S;
827 const float K2 =
filt->xP[1][1] * 1 /
S;
828 const float K3 =
filt->xP[2][1] * 1 /
S;
837 const float P21 = (1. -
K2) *
filt->xP[1][0];
838 const float P22 = (1. -
K2) *
filt->xP[1][1];
839 const float P23 = (1. -
K2) *
filt->xP[1][2];
859 const float yd = vel -
filt->ydot;
861 const float K1 =
filt->yP[0][1] * 1 /
S;
862 const float K2 =
filt->yP[1][1] * 1 /
S;
863 const float K3 =
filt->yP[2][1] * 1 /
S;
872 const float P21 = (1. -
K2) *
filt->yP[1][0];
873 const float P22 = (1. -
K2) *
filt->yP[1][1];
874 const float P23 = (1. -
K2) *
filt->yP[1][2];
struct GpsState gps
global GPS state
Device independent GPS code (interface)
uint32_t sacc
speed accuracy in cm/s
uint32_t pacc
position accuracy in cm
void int32_rmat_transp_vmult(struct Int32Vect3 *vb, struct Int32RMat *m_b2a, struct Int32Vect3 *va)
rotate 3D vector by transposed rotation matrix.
#define ACCEL_FLOAT_OF_BFP(_ai)
static struct Int32RMat * stateGetNedToBodyRMat_i(void)
Get vehicle body attitude rotation matrix (int).
static struct Int32Vect3 * stateGetAccelBody_i(void)
Get acceleration in Body coordinates (int).
static int past_save_counter
struct HfilterFloat * hff_rb_last
ringbuffer read pointer
#define PRINT_DBG(_l, _p)
static void hff_update_ydot(struct HfilterFloat *filt, float vel, float Rvel)
void hff_init(float init_x, float init_xdot, float init_y, float init_ydot)
Butterworth2LowPass_int filter_z
static uint16_t hff_lost_limit
static void hff_update_x(struct HfilterFloat *filt, float x_meas, float Rpos)
static void hff_init_y(float init_y, float init_ydot, float init_ybias)
static int16_t lag_counter_err
by how many steps the estimated GPS validity point in time differed from GPS_LAG_N
static void hff_propagate_x(struct HfilterFloat *filt, float dt)
Propagate the filter in time.
static float hff_ydd_meas
Butterworth2LowPass_int filter_y
#define HFF_LOWPASS_CUTOFF_FREQUENCY
static void hff_update_y(struct HfilterFloat *filt, float y_meas, float Rpos)
void hff_update_gps(struct FloatVect2 *pos_ned, struct FloatVect2 *speed_ned)
static void hff_propagate_y(struct HfilterFloat *filt, float dt)
static void hff_init_x(float init_x, float init_xdot, float init_xbias)
#define AHRS_PROPAGATE_FREQUENCY
static int16_t save_counter
counts down the propagation steps until the filter state is saved again
#define HFF_R_GPS_SPEED_MIN
static void hff_update_xdot(struct HfilterFloat *filt, float vel, float Rvel)
void hff_update_vel(struct FloatVect2 vel, struct FloatVect2 Rvel)
static void send_hff_debug(struct transport_tx *trans, struct link_device *dev)
static uint16_t hff_lost_counter
static void send_hff(struct transport_tx *trans, struct link_device *dev)
static float hff_xdd_meas
void hff_realign(struct FloatVect2 pos, struct FloatVect2 vel)
#define HFF_INIT_PXX
initial covariance diagonal
static int hff_ps_counter
counter for hff propagation
static int hff_rb_n
ringbuffer fill count
void hff_update_pos(struct FloatVect2 pos, struct FloatVect2 Rpos)
Update position.
Butterworth2LowPass_int filter_x
static uint16_t hff_speed_lost_counter
Horizontal filter (x,y) to estimate position and velocity.
float yP[HFF_STATE_SIZE][HFF_STATE_SIZE]
float xP[HFF_STATE_SIZE][HFF_STATE_SIZE]
Inertial Measurement Unit interface.
Simple first order low pass filter with bilinear transform.
static void init_butterworth_2_low_pass_int(Butterworth2LowPass_int *filter, float cut_off, float sample_time, int32_t value)
Init a second order Butterworth filter.
static int32_t update_butterworth_2_low_pass_int(Butterworth2LowPass_int *filter, int32_t value)
Update second order Butterworth low pass filter state with a new value(fixed point version).
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
short int16_t
Typedef defining 16 bit short type.