FrequencyReporter.cpp
Go to the documentation of this file.
1#include "FrequencyReporter.h"
2
3#include <algorithm>
4#include <cstdint>
5#include <numeric>
6
7#include <Eigen/Core>
8#include <Eigen/Geometry>
9
11
12namespace armarx
13{
14
16 const std::string& name) :
17 debugObserver(debugObserver), name(name)
18 {
19 task = new SimplePeriodicTask<>([&] { report(); }, 1000 / frequency);
20
21 task->start();
22 }
23
25 {
26 if (task)
27 {
28 task->stop();
29 }
30 }
31
32 void
34 {
35 std::lock_guard g{mtx};
36 timestamps.push_back(timestamp.toMicroSecondsSinceEpoch());
37 }
38
39 void
40 FrequencyReporter::report()
41 {
43 std::vector<int64_t> ts;
44
45 {
46 std::lock_guard g{mtx};
47
48 // check if received enough messages
49 if (timestamps.size() < 2)
50 {
52 debugObserver->setDebugChannel(name,
53 {{"frequency_mean", new armarx::Variant(0.F)},
54 {"frequency_max", new armarx::Variant(0.F)},
55 {"frequency_min", new armarx::Variant(0.F)},
56 {"frequency_stddev", new armarx::Variant(0.F)}});
57
58 return;
59 }
60
61 ts = timestamps;
62 timestamps.clear();
63 }
64
65 std::adjacent_difference(ts.begin(), ts.end(), ts.begin());
66
67 using Vector = Eigen::Matrix<std::int64_t, Eigen::Dynamic, 1>;
68
69 // the first element is the diff between the first element and 0
70 const Vector v = Eigen::Map<Vector>(ts.data() + 1, ts.size() - 1);
71 const Eigen::ArrayXf dts = v.cast<float>();
72
73 const float dtMean = dts.mean();
74 const float dtMin = dts.minCoeff();
75 const float dtMax = dts.maxCoeff();
76 const float dtStddev = std::sqrt((dts - dtMean).square().sum() / (dts.size() - 1));
77
78 const float frequencyMean = 1.0F / (dtMean / 1'000'000);
79 const float frequencyMax = 1.0F / (dtMin / 1'000'000);
80 const float frequencyMin = 1.0F / (dtMax / 1'000'000);
81 const float frequencyStddev = 1.0F / (dtStddev / 1'000'000);
82
83 debugObserver->setDebugChannel(
84 name,
85 {{"frequency_mean", new armarx::Variant(frequencyMean)},
86 {"frequency_max", new armarx::Variant(frequencyMax)},
87 {"frequency_min", new armarx::Variant(frequencyMin)},
88 {"frequency_stddev", new armarx::Variant(frequencyStddev)}});
89 }
90} // namespace armarx
std::string timestamp()
Eigen::Matrix< T, 3, 1 > Vector
void add(armarx::core::time::DateTime timestamp)
Add a new timestamp to the reporter.
FrequencyReporter(DebugObserverInterfacePrx debugObserver, const std::string &name)
Construct a new Frequency Reporter object.
The Variant class is described here: Variants.
Definition Variant.h:224
Represents a point in time.
Definition DateTime.h:25
double v(double t, double v0, double a0, double j)
Definition CtrlUtil.h:39
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
SimplePeriodicTask(Ts...) -> SimplePeriodicTask< std::function< void(void)> >
#define ARMARX_TRACE
Definition trace.h:77