vtx: fix VTX_SETTINGS_POWER_COUNT and add dummy entries to saPowerNames
[inav.git] / src / main / fc / stats.c
blobf719ccb0d61eb582bc364996c2c0d03230f41766
2 #include "platform.h"
4 #ifdef USE_STATS
6 #include "fc/settings.h"
7 #include "fc/stats.h"
9 #include "sensors/battery.h"
11 #include "drivers/time.h"
12 #include "navigation/navigation.h"
14 #include "fc/config.h"
15 #include "config/parameter_group.h"
16 #include "config/parameter_group_ids.h"
18 #define MIN_FLIGHT_TIME_TO_RECORD_STATS_S 10 //prevent recording stats for that short "flights" [s]
21 PG_REGISTER_WITH_RESET_TEMPLATE(statsConfig_t, statsConfig, PG_STATS_CONFIG, 1);
23 PG_RESET_TEMPLATE(statsConfig_t, statsConfig,
24 .stats_enabled = SETTING_STATS_DEFAULT,
25 .stats_total_time = SETTING_STATS_TOTAL_TIME_DEFAULT,
26 .stats_total_dist = SETTING_STATS_TOTAL_DIST_DEFAULT,
27 #ifdef USE_ADC
28 .stats_total_energy = SETTING_STATS_TOTAL_ENERGY_DEFAULT
29 #endif
32 static uint32_t arm_millis;
33 static uint32_t arm_distance_cm;
35 #ifdef USE_ADC
36 static uint32_t arm_mWhDrawn;
37 static uint32_t flyingEnergy; // energy drawn during flying up to last disarm (ARMED) mWh
39 uint32_t getFlyingEnergy(void) {
40 return flyingEnergy;
42 #endif
44 void statsOnArm(void)
46 arm_millis = millis();
47 arm_distance_cm = getTotalTravelDistance();
48 #ifdef USE_ADC
49 arm_mWhDrawn = getMWhDrawn();
50 #endif
53 void statsOnDisarm(void)
55 if (statsConfig()->stats_enabled) {
56 uint32_t dt = (millis() - arm_millis) / 1000;
57 if (dt >= MIN_FLIGHT_TIME_TO_RECORD_STATS_S) {
58 statsConfigMutable()->stats_total_time += dt; //[s]
59 statsConfigMutable()->stats_total_dist += (getTotalTravelDistance() - arm_distance_cm) / 100; //[m]
60 #ifdef USE_ADC
61 if (feature(FEATURE_VBAT) && isAmperageConfigured()) {
62 const uint32_t energy = getMWhDrawn() - arm_mWhDrawn;
63 statsConfigMutable()->stats_total_energy += energy;
64 flyingEnergy += energy;
66 #endif
67 saveConfigAndNotify();
72 #endif