2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
25 #ifdef USE_PERSISTENT_STATS
27 #include "config/config.h"
29 #include "drivers/time.h"
31 #include "fc/dispatch.h"
32 #include "fc/runtime_config.h"
34 #include "io/beeper.h"
39 #ifdef USE_BATTERY_CONTINUE
40 #include "sensors/battery.h"
43 #include "sensors/gyro.h"
47 #define STATS_SAVE_DELAY_US 500000 // Let disarming complete and save stats after this time
49 static timeMs_t arm_millis
;
50 static uint32_t arm_distance_cm
;
52 static bool saveRequired
= false;
54 static void writeStats(dispatchEntry_t
*self
);
55 dispatchEntry_t writeStatsEntry
= { writeStats
, 0, NULL
, false };
58 #define DISTANCE_FLOWN_CM (GPS_distanceFlownInCm)
60 #define DISTANCE_FLOWN_CM (0)
68 static void writeStats(dispatchEntry_t
*self
)
72 if (!ARMING_FLAG(ARMED
)) {
73 // Don't save if the user made config changes that have not yet been saved.
74 if (!isConfigDirty()) {
76 const bool gyroIsStill
= fabsf(gyro
.gyroADCf
[FD_ROLL
]) < statsConfig()->statsSaveMoveLimit
&&
77 fabsf(gyro
.gyroADCf
[FD_PITCH
]) < statsConfig()->statsSaveMoveLimit
&&
78 fabsf(gyro
.gyroADCf
[FD_YAW
]) < statsConfig()->statsSaveMoveLimit
;
80 if (gyroIsStill
|| statsConfig()->statsSaveMoveLimit
== 0) {
82 // Repeat disarming beep indicating the stats save is complete
83 beeper(BEEPER_DISARMING
);
85 dispatchAdd(&writeStatsEntry
, STATS_SAVE_DELAY_US
);
95 arm_millis
= millis();
96 arm_distance_cm
= DISTANCE_FLOWN_CM
;
99 void statsOnDisarm(void)
101 int8_t minArmedTimeS
= statsConfig()->stats_min_armed_time_s
;
102 if (minArmedTimeS
>= 0) {
103 uint32_t dtS
= (millis() - arm_millis
) / 1000;
104 if (dtS
>= (uint8_t)minArmedTimeS
) {
105 statsConfigMutable()->stats_total_flights
+= 1; // arm / flight counter
106 statsConfigMutable()->stats_total_time_s
+= dtS
;
107 statsConfigMutable()->stats_total_dist_m
+= (DISTANCE_FLOWN_CM
- arm_distance_cm
) / 100;
108 #ifdef USE_BATTERY_CONTINUE
109 statsConfigMutable()->stats_mah_used
= getMAhDrawn();
116 /* signal that stats need to be saved but don't execute time consuming flash operation
117 now - let the disarming process complete and then execute the actual save */
118 dispatchAdd(&writeStatsEntry
, STATS_SAVE_DELAY_US
);