31 #include "modulesettings.h"
36 #include "airspeedactual.h"
37 #include "flightbatterystate.h"
38 #include "flightstatus.h"
39 #include "flightstats.h"
40 #include "flightstatssettings.h"
42 #include "positionactual.h"
43 #include "velocityactual.h"
46 #define STACK_SIZE_BYTES 600
47 #define TASK_PRIORITY PIOS_THREAD_PRIO_LOW
54 static volatile FlightStatsSettingsData
settings;
76 #ifdef MODULE_FLIGHTSTATS_BUILTIN
79 uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
80 ModuleSettingsAdminStateGet(module_state);
81 if (module_state[MODULESETTINGS_ADMINSTATE_FLIGHTSTATS] == MODULESETTINGS_ADMINSTATE_ENABLED) {
91 if (FlightStatsInitialize() == -1 || FlightStatsSettingsInitialize() == -1) {
97 FlightStatsSettingsConnectCopy(&
settings);
126 FlightStatsData flightStatsData;
127 bool first_run =
true;
130 flightStatsData.State = FLIGHTSTATS_STATE_IDLE;
136 switch (flightStatsData.State) {
137 case FLIGHTSTATS_STATE_IDLE:
140 case FLIGHTSTATSSETTINGS_STATSBEHAVIOR_RESETONBOOT:
141 flightStatsData.State = FLIGHTSTATS_STATE_COLLECTING;
143 case FLIGHTSTATSSETTINGS_STATSBEHAVIOR_RESETONARM:
144 flightStatsData.State = FLIGHTSTATS_STATE_RESET;
150 case FLIGHTSTATS_STATE_RESET:
152 flightStatsData.State = FLIGHTSTATS_STATE_COLLECTING;
154 case FLIGHTSTATS_STATE_COLLECTING:
160 if (FlightBatteryStateHandle()) {
165 if (
settings.StatsBehavior == FLIGHTSTATSSETTINGS_STATSBEHAVIOR_RESETONARM) {
173 if ((
settings.StatsBehavior == FLIGHTSTATSSETTINGS_STATSBEHAVIOR_RESETONARM)\
174 || (flightStatsData.InitialBatteryVoltage == 0)){
176 FlightBatteryStateVoltageGet(&voltage);
177 flightStatsData.InitialBatteryVoltage = roundf(1000.
f * voltage);
184 flightStatsData.State = FLIGHTSTATS_STATE_IDLE;
186 FlightStatsSet(&flightStatsData);
199 FlightStatusArmedGet(&arm_status);
200 return (arm_status == FLIGHTSTATUS_ARMED_ARMED);
209 memset((
void*)stats, 0,
sizeof(FlightStatsData));
218 PositionActualData positionActual;
219 PositionActualGet(&positionActual);
222 stats->DistanceTravelled += sqrtf(powf(positionActual.North -
lastPositionActual.North, 2.f) \
226 tmp = sqrtf(powf(positionActual.North, 2.f) + powf(positionActual.East, 2.f));
227 stats->MaxDistanceToHome =
MAX(stats->MaxDistanceToHome, tmp);
230 stats->MaxAltitude =
MAX(stats->MaxAltitude, -1.f * positionActual.Down);
232 VelocityActualData velocityActual;
233 VelocityActualGet(&velocityActual);
236 tmp = sqrtf(powf(velocityActual.North, 2.f) + powf(velocityActual.East, 2.f));
237 stats->MaxGroundSpeed =
MAX(stats->MaxGroundSpeed, tmp);
240 stats->MaxClimbRate =
MAX(stats->MaxClimbRate, -1.f * velocityActual.Down);
243 stats->MaxDescentRate =
MAX(stats->MaxDescentRate, velocityActual.Down);
246 if (AirspeedActualHandle()) {
247 AirspeedActualTrueAirspeedGet(&tmp);
248 stats->MaxAirSpeed =
MAX(stats->MaxAirSpeed, tmp);
254 stats->MaxPitchRate =
MAX(stats->MaxPitchRate, fabsf(gyros.y));
255 stats->MaxRollRate =
MAX(stats->MaxRollRate, fabsf(gyros.x));
256 stats->MaxYawRate =
MAX(stats->MaxYawRate, fabsf(gyros.z));
259 if (FlightBatteryStateHandle()) {
260 FlightBatteryStateConsumedEnergyGet(&tmp);
static void resetStats(FlightStatsData *stats)
static struct pios_thread * flightStatsTaskHandle
static float initial_consumed_energy
static PositionActualData lastPositionActual
static bool module_enabled
static void collectStats(FlightStatsData *stats)
MODULE_INITCALL(FlightStatsModuleInitialize, FlightStatModuleStart)
static volatile FlightStatsSettingsData settings
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
static void flightStatsTask(void *parameters)
void PIOS_Thread_Sleep(uint32_t time_ms)
static float previous_consumed_energy
int32_t FlightStatsModuleInitialize(void)
int32_t FlightStatModuleStart(void)
Includes PiOS and core architecture components.