pizero_tracker/tracker/code/main/dynamics_t.h

98 wiersze
1.9 KiB
C
Czysty Zwykły widok Historia

2020-04-13 17:52:30 +00:00
#pragma once
#include <string>
#include <chrono>
#include <limits>
#include <algorithm>
2020-04-13 17:52:30 +00:00
// record value, min, max, dV/dT, averaged dV/dT
//
// average value
template<typename T>
class average_t
{
public:
average_t(size_t max_count, T init_val=0) : sum_(0), count_(0), max_count_(std::max(size_t(1),max_count))
{
add(init_val);
};
T add(const T& val)
{
T diff = get() - val;
if(count_ == max_count_)
{
sum_ = get() * (max_count_-1) + val;
}
else
{
++count_;
sum_ += val;
}
return diff;
}
T get() const
{
if(!count_)
return sum_;
return T(sum_)/count_;
}
// operator double() const { T val = get(); return val; }
void reset(const T& val)
{
sum_ = val;
count_ = 1;
}
private:
T sum_;
size_t count_;
size_t max_count_;
};
2020-04-13 17:52:30 +00:00
class dynamics_t
{
public:
using tp = std::chrono::steady_clock::time_point;
2020-04-13 17:52:30 +00:00
private:
bool initialised_ = false;
2020-04-16 10:37:30 +00:00
float val_ = 0; // updated on every add()
float val_prev_ = 0; // updated after min_dT_
2020-04-16 10:38:49 +00:00
tp val_prev_timestamp_; // timestamp of val_prev_ update
2020-04-13 17:52:30 +00:00
float val_min_ = std::numeric_limits<float>::max();
float val_max_ = -std::numeric_limits<float>::min();
float dVdT_ = 0;
average_t<float> dVdT_avg_ = average_t<float>(5);
2020-04-13 17:52:30 +00:00
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_; }
void add(const tp timestamp, const float val);
2020-04-13 17:52:30 +00:00
float val() const { return val_; }
2020-04-13 17:52:30 +00:00
float min() const { return val_min_; }
float max() const { return val_max_; }
float dVdT() const { return dVdT_; }
float dVdT_avg() const { return dVdT_avg_.get(); }
2020-04-13 17:52:30 +00:00
std::string str() const;
std::string json() const;
2020-04-13 17:52:30 +00:00
};