ao_kalman_predict(void)
{
#ifdef AO_FLIGHT_TEST
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 50) {
ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_1 +
(ao_k_t) ao_accel * AO_K_STEP_2_2_1) >> 4;
ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_1;
return;
}
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 5) {
ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_10 +
(ao_k_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_10;
{
ao_kalman_err_height();
#ifdef AO_FLIGHT_TEST
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 50) {
ao_k_height += (ao_k_t) AO_BARO_K0_1 * ao_error_h;
ao_k_speed += (ao_k_t) AO_BARO_K1_1 * ao_error_h;
ao_k_accel += (ao_k_t) AO_BARO_K2_1 * ao_error_h;
return;
}
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 5) {
ao_k_height += (ao_k_t) AO_BARO_K0_10 * ao_error_h;
ao_k_speed += (ao_k_t) AO_BARO_K1_10 * ao_error_h;
ao_k_accel += (ao_k_t) AO_BARO_K2_10 * ao_error_h;
accel = (ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale;
/* Can't use ao_accel here as it is the pre-prediction value still */
- ao_error_a = (accel - ao_k_accel) >> 16;
+ ao_error_a = (ao_v_t) ((accel - ao_k_accel) >> 16);
}
#if !defined(FORCE_ACCEL) && HAS_BARO
ao_kalman_err_accel();
#ifdef AO_FLIGHT_TEST
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 50) {
if (ao_flight_debug) {
printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
ao_k_speed / (65536.0 * 16.0),
(ao_k_t) AO_BOTH_K21_1 * ao_error_a;
return;
}
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 5) {
if (ao_flight_debug) {
printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
ao_k_speed / (65536.0 * 16.0),
ao_kalman_err_accel();
#ifdef AO_FLIGHT_TEST
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 5) {
ao_k_height +=(ao_k_t) AO_ACCEL_K0_10 * ao_error_a;
ao_k_speed += (ao_k_t) AO_ACCEL_K1_10 * ao_error_a;
ao_k_accel += (ao_k_t) AO_ACCEL_K2_10 * ao_error_a;
#endif /* else FORCE_ACCEL */
#endif /* HAS_ACCEL */
+#if !HAS_BARO
+static ao_k_t ao_k_height_prev;
+static ao_k_t ao_k_speed_prev;
+
+/*
+ * While in pad mode without a barometric sensor, remove accumulated
+ * speed and height values to reduce the effect of systematic sensor
+ * error
+ */
+void
+ao_kalman_reset_accumulate(void)
+{
+ ao_k_height -= ao_k_height_prev;
+ ao_k_speed -= ao_k_speed_prev;
+ ao_k_height_prev = ao_k_height;
+ ao_k_speed_prev = ao_k_speed;
+}
+#endif
+
void
ao_kalman(void)
{
ao_kalman_correct_accel();
#endif
#endif
- ao_height = from_fix(ao_k_height);
- ao_speed = from_fix(ao_k_speed);
- ao_accel = from_fix(ao_k_accel);
+ ao_height = (ao_v_t) from_fix(ao_k_height);
+ ao_speed = (ao_v_t) from_fix(ao_k_speed);
+ ao_accel = (ao_v_t) from_fix(ao_k_accel);
if (ao_height > ao_max_height)
ao_max_height = ao_height;
#if HAS_BARO
ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_height;
#endif
#ifdef AO_FLIGHT_TEST
- if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50)
+ if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 50)
ao_avg_height = (ao_avg_height_scaled + 1) >> 1;
- else if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5)
+ else if ((AO_TICK_SIGNED) (ao_sample_tick - ao_sample_prev_tick) > 5)
ao_avg_height = (ao_avg_height_scaled + 7) >> 4;
else
#endif
- ao_avg_height = (ao_avg_height_scaled + 63) >> 7;
+ ao_avg_height = (ao_v_t) ((ao_avg_height_scaled + 63) >> 7);
}