From f21a521a00fbf202103c8270f61a63cd8385e172 Mon Sep 17 00:00:00 2001 From: Michal Fratczak Date: Mon, 13 Apr 2020 19:52:30 +0200 Subject: [PATCH] dynamics --- tracker/code/main/CMakeLists.txt | 1 + tracker/code/main/GLOB.cpp | 26 +++++++++++++++ tracker/code/main/GLOB.h | 10 ++++++ tracker/code/main/dynamics_t.cpp | 55 ++++++++++++++++++++++++++++++++ tracker/code/main/dynamics_t.h | 37 +++++++++++++++++++++ tracker/code/main/main.cpp | 32 ++++++++++++++++--- 6 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 tracker/code/main/dynamics_t.cpp create mode 100644 tracker/code/main/dynamics_t.h diff --git a/tracker/code/main/CMakeLists.txt b/tracker/code/main/CMakeLists.txt index 494dd65..2414bea 100644 --- a/tracker/code/main/CMakeLists.txt +++ b/tracker/code/main/CMakeLists.txt @@ -10,6 +10,7 @@ find_package(cppzmq) set (tracker_src GLOB.h GLOB.cpp cli.h cli.cpp + dynamics_t.h dynamics_t.cpp ssdv_t.cpp main.cpp ../mtx2/mtx2.cpp diff --git a/tracker/code/main/GLOB.cpp b/tracker/code/main/GLOB.cpp index 261e565..39df4d5 100644 --- a/tracker/code/main/GLOB.cpp +++ b/tracker/code/main/GLOB.cpp @@ -1,6 +1,7 @@ #include "GLOB.h" #include +#include std::string GLOB::str() const { @@ -18,3 +19,28 @@ std::string GLOB::str() const return s.str(); } + + +bool GLOB::dynamics_add(const std::string& name, const dynamics_t::tp timestamp, const float value) +{ + auto d = dynamics_.find(name); + if( d == dynamics_.end() ) + { + dynamics_[name] = dynamics_t(); + return dynamics_[name].add(timestamp, value); + } + else + { + return d->second.add(timestamp, value); + } + +} + +dynamics_t GLOB::dynamics_get(const std::string& name) +{ + auto d = dynamics_.find(name); + if( d == dynamics_.end() ) + return dynamics_t(); // default, uninitialised + else + return d->second; +} diff --git a/tracker/code/main/GLOB.h b/tracker/code/main/GLOB.h index cebb8fa..067bfc7 100644 --- a/tracker/code/main/GLOB.h +++ b/tracker/code/main/GLOB.h @@ -1,10 +1,13 @@ #pragma once #include +#include #include #include "mtx2/mtx2.h" #include "nmea/nmea.h" +#include "dynamics_t.h" + // global options - singleton @@ -18,6 +21,9 @@ private: std::atomic nmea; // GPS data + // sensors dynamics + std::map dynamics_; // index: value name (alt, temp1, etc.) + public: static GLOB& get() { @@ -44,6 +50,10 @@ public: // sensors std::atomic temperature{0}; + bool dynamics_add(const std::string& name, const dynamics_t::tp timestamp, const float value); + dynamics_t dynamics_get(const std::string& name); + + nmea_t nmea_get() { nmea_t ret = get().nmea; return ret; } void nmea_set(const nmea_t& in_nmea) { get().nmea = in_nmea; } std::string str() const; diff --git a/tracker/code/main/dynamics_t.cpp b/tracker/code/main/dynamics_t.cpp new file mode 100644 index 0000000..e1ba966 --- /dev/null +++ b/tracker/code/main/dynamics_t.cpp @@ -0,0 +1,55 @@ + +#include "dynamics_t.h" + +#include +#include + +// utc_seconds is HHMMSS converted to seconds. return true if sample was accepted +bool dynamics_t::add(const tp timestamp, const float val) +{ + if( !initialised_ && timestamp.time_since_epoch().count() ) + { + val_ = val; + val_prev_ = val; + val_prev_timestamp_ = timestamp; + dVdT_ = 0; + + if(val > val_max_) + val_max_ = val; + if(val < val_min_) + val_min_ = val; + + initialised_ = true; + return true; + } + + if( (timestamp - val_prev_timestamp_).count() < min_dT_ ) + return false; + + dVdT_ = (val - val_prev_) / float((timestamp - val_prev_timestamp_).count()); + + if(val > val_max_) + val_max_ = val; + + if(val < val_min_) + val_min_ = val; + + val_prev_ = val; + val_prev_timestamp_ = timestamp; + + return true; +} + +std::chrono::system_clock::time_point dynamics_t::utc2tp(const std::string utc) +{ + using namespace std; + + // init with *now* + time_t _now = time(nullptr); // "now: as long int + tm tm = *localtime(&_now); // init all fields + + istringstream ss(utc); + ss >> get_time( &tm, "%H%M%S" ); // get HHMMSS + + return chrono::system_clock::from_time_t( mktime(&tm) ); +} \ No newline at end of file diff --git a/tracker/code/main/dynamics_t.h b/tracker/code/main/dynamics_t.h new file mode 100644 index 0000000..6575d19 --- /dev/null +++ b/tracker/code/main/dynamics_t.h @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include + +// record min, max, dV/dT for a value +class dynamics_t +{ +public: + using tp = std::chrono::system_clock::time_point; + +private: + bool initialised_ = false; + float val_ = 0; + float val_prev_ = 0; + tp val_prev_timestamp_; // timestamp of previous value + float val_min_ = std::numeric_limits::max(); + float val_max_ = -std::numeric_limits::min(); + float dVdT_ = 0; + +public: + int min_dT_ = 10; // minimum time difference to compute dV/dT. Sample too close to previous will be rejected + + bool initialised() const { return initialised_; } + + bool add(const tp timestamp, const float val); // utc_seconds is HHMMSS converted to seconds. return true if sample was accepted + + float min() const { return val_min_; } + float max() const { return val_max_; } + float dVdT() const { return dVdT_; } + + static std::chrono::system_clock::time_point utc2tp(const std::string utc); + +}; + + diff --git a/tracker/code/main/main.cpp b/tracker/code/main/main.cpp index 91e9447..b10a10d 100644 --- a/tracker/code/main/main.cpp +++ b/tracker/code/main/main.cpp @@ -16,6 +16,7 @@ #include "ublox/ublox_cmds.h" #include "ublox/ublox.h" #include "ds18b20/ds18b20.h" +#include "dynamics_t.h" #include "ssdv_t.h" #include "cli.h" @@ -213,6 +214,7 @@ int main1(int argc, char** argv) } cout<<"ds18b20_device "<