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<>([&]
20  {
21  report();
22  }, 1000 / frequency);
23 
24  task->start();
25  }
26 
28  {
29  if (task)
30  {
31  task->stop();
32  }
33  }
34 
36  {
37  std::lock_guard g{mtx};
38  timestamps.push_back(timestamp.toMicroSecondsSinceEpoch());
39  }
40 
41  void FrequencyReporter::report()
42  {
44  std::vector<int64_t> ts;
45 
46  {
47  std::lock_guard g{mtx};
48 
49  // check if received enough messages
50  if (timestamps.size() < 2)
51  {
53  debugObserver->setDebugChannel(name,
54  {
55  {"frequency_mean", new armarx::Variant(0.F)},
56  {"frequency_max", new armarx::Variant(0.F)},
57  {"frequency_min", new armarx::Variant(0.F)},
58  {"frequency_stddev", new armarx::Variant(0.F)}
59  });
60 
61  return;
62  }
63 
64  ts = timestamps;
65  timestamps.clear();
66  }
67 
68  std::adjacent_difference(ts.begin(), ts.end(), ts.begin());
69 
71 
72  // the first element is the diff between the first element and 0
73  const Vector v = Eigen::Map<Vector>(ts.data() + 1, ts.size() - 1);
74  const Eigen::ArrayXf dts = v.cast<float>();
75 
76  const float dtMean = dts.mean();
77  const float dtMin = dts.minCoeff();
78  const float dtMax = dts.maxCoeff();
79  const float dtStddev = std::sqrt((dts - dtMean).square().sum() / (dts.size() - 1));
80 
81  const float frequencyMean = 1.0F / (dtMean / 1'000'000);
82  const float frequencyMax = 1.0F / (dtMin / 1'000'000);
83  const float frequencyMin = 1.0F / (dtMax / 1'000'000);
84  const float frequencyStddev = 1.0F / (dtStddev / 1'000'000);
85 
86  debugObserver->setDebugChannel(
87  name,
88  {
89  {"frequency_mean", new armarx::Variant(frequencyMean)},
90  {"frequency_max", new armarx::Variant(frequencyMax)},
91  {"frequency_min", new armarx::Variant(frequencyMin)},
92  {"frequency_stddev", new armarx::Variant(frequencyStddev)}
93  });
94  }
95 } // namespace armarx
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::core::time::DateTime::toMicroSecondsSinceEpoch
std::int64_t toMicroSecondsSinceEpoch() const
Definition: DateTime.cpp:95
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::FrequencyReporter::~FrequencyReporter
virtual ~FrequencyReporter()
Definition: FrequencyReporter.cpp:27
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:16
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:28
armarx::FrequencyReporter::add
void add(armarx::core::time::DateTime timestamp)
Add a new timestamp to the reporter.
Definition: FrequencyReporter.cpp:35
armarx::SimplePeriodicTask
Usage:
Definition: ApplicationNetworkStats.h:32