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 
12 namespace 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 
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
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::core::time::DateTime::toMicroSecondsSinceEpoch
std::int64_t toMicroSecondsSinceEpoch() const
Definition: DateTime.cpp:87
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
armarx::FrequencyReporter::~FrequencyReporter
virtual ~FrequencyReporter()
Definition: FrequencyReporter.cpp:24
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
FrequencyReporter.h
IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface >
F
Definition: ExportDialogControllerTest.cpp:18
armarx::FrequencyReporter::FrequencyReporter
FrequencyReporter(DebugObserverInterfacePrx debugObserver, const std::string &name)
Construct a new Frequency Reporter object.
Definition: FrequencyReporter.cpp:15
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
Variant.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::FrequencyReporter::add
void add(armarx::core::time::DateTime timestamp)
Add a new timestamp to the reporter.
Definition: FrequencyReporter.cpp:33
armarx::SimplePeriodicTask
Usage:
Definition: ApplicationNetworkStats.h:32