Skip to content

Commit de322c4

Browse files
Merge pull request #10020 from TransientTetra/master
Add flights counter to stats
2 parents 400f8f9 + f3de09f commit de322c4

5 files changed

Lines changed: 43 additions & 1 deletion

File tree

docs/Settings.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6272,6 +6272,16 @@ General switch of the statistics recording feature (a.k.a. odometer)
62726272

62736273
---
62746274

6275+
### stats_flight_count
6276+
6277+
Total number of flights. The value is updated on every disarm when "stats" are enabled.
6278+
6279+
| Default | Min | Max |
6280+
| --- | --- | --- |
6281+
| 0 | | 65535 |
6282+
6283+
---
6284+
62756285
### stats_total_dist
62766286

62776287
Total flight distance [in meters]. The value is updated on every disarm when "stats" are enabled.

src/main/fc/fc_init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#include "fc/rc_controls.h"
9090
#include "fc/runtime_config.h"
9191
#include "fc/firmware_update.h"
92+
#include "fc/stats.h"
9293

9394
#include "flight/failsafe.h"
9495
#include "flight/imu.h"
@@ -746,5 +747,7 @@ void init(void)
746747
persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
747748
#endif
748749

750+
statsInit();
751+
749752
systemState |= SYSTEM_STATE_READY;
750753
}

src/main/fc/settings.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3951,6 +3951,10 @@ groups:
39513951
max: INT32_MAX
39523952
condition: USE_ADC
39533953
default_value: 0
3954+
- name: stats_flight_count
3955+
description: "Total number of flights. The value is updated on every disarm when \"stats\" are enabled."
3956+
default_value: 0
3957+
max: UINT16_MAX
39543958

39553959
- name: PG_TIME_CONFIG
39563960
type: timeConfig_t

src/main/fc/stats.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
#include "fc/settings.h"
77
#include "fc/stats.h"
8+
#include "fc/runtime_config.h"
89

910
#include "sensors/battery.h"
11+
#include "sensors/sensors.h"
1012

1113
#include "drivers/time.h"
1214
#include "navigation/navigation.h"
@@ -16,21 +18,24 @@
1618
#include "config/parameter_group_ids.h"
1719

1820
#define MIN_FLIGHT_TIME_TO_RECORD_STATS_S 10 //prevent recording stats for that short "flights" [s]
21+
#define MIN_FLIGHT_DISTANCE_M 30 // minimum distance flown for a flight to be registered [m]
1922

2023

21-
PG_REGISTER_WITH_RESET_TEMPLATE(statsConfig_t, statsConfig, PG_STATS_CONFIG, 1);
24+
PG_REGISTER_WITH_RESET_TEMPLATE(statsConfig_t, statsConfig, PG_STATS_CONFIG, 2);
2225

2326
PG_RESET_TEMPLATE(statsConfig_t, statsConfig,
2427
.stats_enabled = SETTING_STATS_DEFAULT,
2528
.stats_total_time = SETTING_STATS_TOTAL_TIME_DEFAULT,
2629
.stats_total_dist = SETTING_STATS_TOTAL_DIST_DEFAULT,
30+
.stats_flight_count = SETTING_STATS_FLIGHT_COUNT_DEFAULT,
2731
#ifdef USE_ADC
2832
.stats_total_energy = SETTING_STATS_TOTAL_ENERGY_DEFAULT
2933
#endif
3034
);
3135

3236
static uint32_t arm_millis;
3337
static uint32_t arm_distance_cm;
38+
static uint16_t prev_flight_count;
3439

3540
#ifdef USE_ADC
3641
static uint32_t arm_mWhDrawn;
@@ -41,6 +46,11 @@ uint32_t getFlyingEnergy(void) {
4146
}
4247
#endif
4348

49+
void statsInit(void)
50+
{
51+
prev_flight_count = statsConfig()->stats_flight_count;
52+
}
53+
4454
void statsOnArm(void)
4555
{
4656
arm_millis = millis();
@@ -57,6 +67,18 @@ void statsOnDisarm(void)
5767
if (dt >= MIN_FLIGHT_TIME_TO_RECORD_STATS_S) {
5868
statsConfigMutable()->stats_total_time += dt; //[s]
5969
statsConfigMutable()->stats_total_dist += (getTotalTravelDistance() - arm_distance_cm) / 100; //[m]
70+
#ifdef USE_GPS
71+
// flight counter is incremented at most once per power on
72+
if (sensors(SENSOR_GPS)) {
73+
if ((getTotalTravelDistance() - arm_distance_cm) / 100 >= MIN_FLIGHT_DISTANCE_M) {
74+
statsConfigMutable()->stats_flight_count = prev_flight_count + 1;
75+
}
76+
} else {
77+
statsConfigMutable()->stats_flight_count = prev_flight_count + 1;
78+
}
79+
#else
80+
statsConfigMutable()->stats_flight_count = prev_flight_count + 1;
81+
#endif // USE_GPS
6082
#ifdef USE_ADC
6183
if (feature(FEATURE_VBAT) && isAmperageConfigured()) {
6284
const uint32_t energy = getMWhDrawn() - arm_mWhDrawn;

src/main/fc/stats.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
typedef struct statsConfig_s {
66
uint32_t stats_total_time; // [Seconds]
77
uint32_t stats_total_dist; // [Metres]
8+
uint16_t stats_flight_count;
89
#ifdef USE_ADC
910
uint32_t stats_total_energy; // deciWatt hour (x0.1Wh)
1011
#endif
1112
uint8_t stats_enabled;
1213
} statsConfig_t;
1314

1415
uint32_t getFlyingEnergy(void);
16+
void statsInit(void);
1517
void statsOnArm(void);
1618
void statsOnDisarm(void);
1719

1820
#else
1921

22+
#define statsInit() do {} while (0)
2023
#define statsOnArm() do {} while (0)
2124
#define statsOnDisarm() do {} while (0)
2225

0 commit comments

Comments
 (0)