Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flights counter to stats #10020

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -5792,6 +5792,16 @@ General switch of the statistics recording feature (a.k.a. odometer)

---

### stats_flight_count

Total number of flights. The value is updated on every disarm when "stats" are enabled.

| Default | Min | Max |
| --- | --- | --- |
| 0 | | 65535 |

---

### stats_total_dist

Total flight distance [in meters]. The value is updated on every disarm when "stats" are enabled.
Expand Down
3 changes: 3 additions & 0 deletions src/main/fc/fc_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#include "fc/rc_controls.h"
#include "fc/runtime_config.h"
#include "fc/firmware_update.h"
#include "fc/stats.h"

#include "flight/failsafe.h"
#include "flight/imu.h"
Expand Down Expand Up @@ -720,5 +721,7 @@ void init(void)
persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE);
#endif

statsInit();
MrD-RC marked this conversation as resolved.
Show resolved Hide resolved

systemState |= SYSTEM_STATE_READY;
}
4 changes: 4 additions & 0 deletions src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3787,6 +3787,10 @@ groups:
max: INT32_MAX
condition: USE_ADC
default_value: 0
- name: stats_flight_count
description: "Total number of flights. The value is updated on every disarm when \"stats\" are enabled."
default_value: 0
max: UINT16_MAX

- name: PG_TIME_CONFIG
type: timeConfig_t
Expand Down
24 changes: 23 additions & 1 deletion src/main/fc/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

#include "fc/settings.h"
#include "fc/stats.h"
#include "fc/runtime_config.h"

#include "sensors/battery.h"
#include "sensors/sensors.h"

#include "drivers/time.h"
#include "navigation/navigation.h"
Expand All @@ -16,21 +18,24 @@
#include "config/parameter_group_ids.h"

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


PG_REGISTER_WITH_RESET_TEMPLATE(statsConfig_t, statsConfig, PG_STATS_CONFIG, 1);
PG_REGISTER_WITH_RESET_TEMPLATE(statsConfig_t, statsConfig, PG_STATS_CONFIG, 2);

PG_RESET_TEMPLATE(statsConfig_t, statsConfig,
.stats_enabled = SETTING_STATS_DEFAULT,
.stats_total_time = SETTING_STATS_TOTAL_TIME_DEFAULT,
.stats_total_dist = SETTING_STATS_TOTAL_DIST_DEFAULT,
.stats_flight_count = SETTING_STATS_FLIGHT_COUNT_DEFAULT,
#ifdef USE_ADC
.stats_total_energy = SETTING_STATS_TOTAL_ENERGY_DEFAULT
#endif
);

static uint32_t arm_millis;
static uint32_t arm_distance_cm;
static uint16_t prev_flight_count;

#ifdef USE_ADC
static uint32_t arm_mWhDrawn;
Expand All @@ -41,6 +46,11 @@ uint32_t getFlyingEnergy(void) {
}
#endif

void statsInit(void)
{
prev_flight_count = statsConfig()->stats_flight_count;
}

MrD-RC marked this conversation as resolved.
Show resolved Hide resolved
void statsOnArm(void)
{
arm_millis = millis();
Expand All @@ -57,6 +67,18 @@ void statsOnDisarm(void)
if (dt >= MIN_FLIGHT_TIME_TO_RECORD_STATS_S) {
statsConfigMutable()->stats_total_time += dt; //[s]
statsConfigMutable()->stats_total_dist += (getTotalTravelDistance() - arm_distance_cm) / 100; //[m]
#ifdef USE_GPS
// flight counter is incremented at most once per power on
if (sensors(SENSOR_GPS)) {
if ((getTotalTravelDistance() - arm_distance_cm) / 100 >= MIN_FLIGHT_DISTANCE_M) {
statsConfigMutable()->stats_flight_count = prev_flight_count + 1;
}
} else {
statsConfigMutable()->stats_flight_count = prev_flight_count + 1;
}
#else
statsConfigMutable()->stats_flight_count = prev_flight_count + 1;
#endif // USE_GPS
#ifdef USE_ADC
if (feature(FEATURE_VBAT) && isAmperageConfigured()) {
const uint32_t energy = getMWhDrawn() - arm_mWhDrawn;
Expand Down
3 changes: 3 additions & 0 deletions src/main/fc/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
typedef struct statsConfig_s {
uint32_t stats_total_time; // [Seconds]
uint32_t stats_total_dist; // [Metres]
uint16_t stats_flight_count;
#ifdef USE_ADC
uint32_t stats_total_energy; // deciWatt hour (x0.1Wh)
#endif
uint8_t stats_enabled;
} statsConfig_t;

uint32_t getFlyingEnergy(void);
void statsInit(void);
MrD-RC marked this conversation as resolved.
Show resolved Hide resolved
void statsOnArm(void);
void statsOnDisarm(void);

#else

#define statsInit() do {} while (0)
#define statsOnArm() do {} while (0)
#define statsOnDisarm() do {} while (0)

Expand Down