From e26028f57291004a36e089de2d037a4a309f8a8c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 21 Jul 2018 06:24:52 +1000 Subject: [PATCH] AP_Stats: make singleton and add flighttime accessor --- libraries/AP_Stats/AP_Stats.cpp | 23 +++++++++++++++++++++++ libraries/AP_Stats/AP_Stats.h | 28 +++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/libraries/AP_Stats/AP_Stats.cpp b/libraries/AP_Stats/AP_Stats.cpp index 4426cb50bf..a6a159f2c0 100644 --- a/libraries/AP_Stats/AP_Stats.cpp +++ b/libraries/AP_Stats/AP_Stats.cpp @@ -42,11 +42,20 @@ const AP_Param::GroupInfo AP_Stats::var_info[] = { AP_GROUPEND }; +AP_Stats *AP_Stats::_singleton; + +// constructor +AP_Stats::AP_Stats(void) +{ + _singleton = this; +} + void AP_Stats::copy_variables_from_parameters() { flttime = params.flttime; runtime = params.runtime; reset = params.reset; + flttime_boot = flttime; } void AP_Stats::init() @@ -121,3 +130,17 @@ void AP_Stats::set_flying(const bool is_flying) _flying_ms = 0; } } + +/* + get time in flight since boot + */ +uint32_t AP_Stats::get_flight_time_s(void) +{ + update_flighttime(); + return flttime - flttime_boot; +} + +AP_Stats *AP::stats(void) +{ + return AP_Stats::get_singleton(); +} diff --git a/libraries/AP_Stats/AP_Stats.h b/libraries/AP_Stats/AP_Stats.h index 3bf49ac82f..94cc232610 100644 --- a/libraries/AP_Stats/AP_Stats.h +++ b/libraries/AP_Stats/AP_Stats.h @@ -9,14 +9,17 @@ class AP_Stats { public: - + // constructor + AP_Stats(); + // these variables are periodically written into the actual // parameters. If you add a variable here, make sure to update // init() to set initial values from the parameters! uint32_t flttime; // seconds in flight (or driving) uint32_t runtime; // total wallclock time spent running ArduPilot (seconds) uint32_t reset; // last time parameters were reset - + uint32_t flttime_boot; // seconds in flight (or driving), at boot + void init(); // copy state into underlying parameters: @@ -28,10 +31,25 @@ public: void set_flying(bool b); + // accessor for is_flying + bool get_is_flying(void) { + return _flying_ms != 0; + } + + // accessor for flighttime. Returns 0 if not flying, otherwise + // total time flying since boot in seconds + uint32_t get_flight_time_s(void); + + // get singleton + static AP_Stats *get_singleton(void) { + return _singleton; + } + static const struct AP_Param::GroupInfo var_info[]; private: - + static AP_Stats *_singleton; + struct { AP_Int16 bootcount; AP_Int32 flttime; @@ -51,3 +69,7 @@ private: void update_runtime(); }; + +namespace AP { + AP_Stats *stats(); +};