*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
#include <ao_log.h>
#endif
-#if HAS_MPU6000
+#if HAS_MPU6000 || HAS_MPU9250
#include <ao_quaternion.h>
#endif
/* Main flight thread. */
-__pdata enum ao_flight_state ao_flight_state; /* current flight state */
-__pdata uint16_t ao_boost_tick; /* time of launch detect */
-__pdata uint16_t ao_motor_number; /* number of motors burned so far */
+enum ao_flight_state ao_flight_state; /* current flight state */
+uint16_t ao_boost_tick; /* time of most recent boost detect */
+uint16_t ao_launch_tick; /* time of first boost detect */
+uint16_t ao_motor_number; /* number of motors burned so far */
#if HAS_SENSOR_ERRORS
/* Any sensor can set this to mark the flight computer as 'broken' */
-__xdata uint8_t ao_sensor_errors;
+uint8_t ao_sensor_errors;
#endif
/*
* track min/max data over a long interval to detect
* resting
*/
-static __data uint16_t ao_interval_end;
-static __data int16_t ao_interval_min_height;
-static __data int16_t ao_interval_max_height;
+static uint16_t ao_interval_end;
+static ao_v_t ao_interval_min_height;
+static ao_v_t ao_interval_max_height;
#if HAS_ACCEL
-static __data int16_t ao_coast_avg_accel;
+static ao_v_t ao_coast_avg_accel;
#endif
-__pdata uint8_t ao_flight_force_idle;
+uint8_t ao_flight_force_idle;
/* We also have a clock, which can be used to sanity check things in
* case of other failures
{
/* Set pad mode - we can fly! */
ao_flight_state = ao_flight_pad;
-#if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE
+#if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE && !DEBUG
/* Disable the USB controller in flight mode
* to save power
*/
#endif
}
/* wakeup threads due to state change */
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
break;
case ao_flight_pad:
)
{
ao_flight_state = ao_flight_boost;
- ao_boost_tick = ao_sample_tick;
+ ao_launch_tick = ao_boost_tick = ao_sample_tick;
/* start logging data */
ao_log_start();
ao_wakeup(&ao_gps_new);
#endif
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
}
break;
case ao_flight_boost:
* deceleration, or by waiting until the maximum burn duration
* (15 seconds) has past.
*/
- if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
+ if ((ao_accel < AO_MSS_TO_ACCEL(-2.5)) ||
(int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
{
#if HAS_ACCEL
ao_flight_state = ao_flight_coast;
#endif
++ao_motor_number;
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
}
break;
#if HAS_ACCEL
if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
{
ao_flight_state = ao_flight_coast;
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
} else
goto check_re_boost;
break;
* number of seconds.
*/
if (ao_config.apogee_lockout) {
- if ((ao_sample_tick - ao_boost_tick) <
+ if ((int16_t) (ao_sample_tick - ao_launch_tick) <
AO_SEC_TO_TICKS(ao_config.apogee_lockout))
break;
}
* the measured altitude reasonably closely; otherwise
* we're probably transsonic.
*/
+#define AO_ERROR_BOUND 100
+
if (ao_speed < 0
#if !HAS_ACCEL
- && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
+ && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < AO_ERROR_BOUND)
#endif
)
{
/* and enter drogue state */
ao_flight_state = ao_flight_drogue;
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
}
#if HAS_ACCEL
else {
check_re_boost:
- ao_coast_avg_accel = ao_coast_avg_accel - (ao_coast_avg_accel >> 6) + (ao_accel >> 6);
+ ao_coast_avg_accel = ao_coast_avg_accel + ((ao_accel - ao_coast_avg_accel) >> 5);
if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
ao_boost_tick = ao_sample_tick;
ao_flight_state = ao_flight_boost;
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
}
}
#endif
ao_interval_min_height = ao_interval_max_height = ao_avg_height;
ao_flight_state = ao_flight_main;
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
}
break;
{
ao_flight_state = ao_flight_landed;
+#if HAS_ADC
/* turn off the ADC capture */
ao_timer_set_adc_interval(0);
+#endif
- ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
+ ao_wakeup(&ao_flight_state);
}
ao_interval_min_height = ao_interval_max_height = ao_avg_height;
ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
}
#if HAS_FLIGHT_DEBUG
-static inline int int_part(int16_t i) { return i >> 4; }
-static inline int frac_part(int16_t i) { return ((i & 0xf) * 100 + 8) / 16; }
+static inline int int_part(ao_v_t i) { return i >> 4; }
+static inline int frac_part(ao_v_t i) { return ((i & 0xf) * 100 + 8) / 16; }
static void
ao_flight_dump(void)
{
#if HAS_ACCEL
- int16_t accel;
+ ao_v_t accel;
accel = ((ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale) >> 16;
#endif
printf (" max_height %d\n", ao_max_height);
printf (" avg_height %d\n", ao_avg_height);
printf (" error_h %d\n", ao_error_h);
+#if !HAS_ACCEL
printf (" error_avg %d\n", ao_error_h_sq_avg);
+#endif
}
static void
ao_orient_test = !ao_orient_test;
}
-__code struct ao_cmds ao_flight_cmds[] = {
+const struct ao_cmds ao_flight_cmds[] = {
{ ao_flight_dump, "F\0Dump flight status" },
{ ao_gyro_test, "G\0Test gyro code" },
{ ao_orient_test_select,"O\0Test orientation code" },
};
#endif
-static __xdata struct ao_task flight_task;
+static struct ao_task flight_task;
void
ao_flight_init(void)