event_counter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Simon Rasmussen (refactor)
3  *
4  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <chrono>
22 #include <cstdlib>
23 #include "ur_modern_driver/log.h"
25 
27 {
28 private:
29  typedef std::chrono::high_resolution_clock Clock;
30  Clock::time_point events_[250];
31  size_t idx_ = 0;
32 
33  Clock::time_point last_;
34 
35 public:
36  void trigger()
37  {
38  // auto now = Clock::now();
39  // LOG_INFO("Time diff: %d ms", std::chrono::duration_cast<std::chrono::microseconds>(now - last_));
40  // last_ = now;
41  // return;
42 
43  events_[idx_] = Clock::now();
44  idx_ += 1;
45 
46  if (idx_ > 250)
47  {
48  std::chrono::time_point<std::chrono::high_resolution_clock> t_min =
49  std::chrono::time_point<std::chrono::high_resolution_clock>::max();
50  std::chrono::time_point<std::chrono::high_resolution_clock> t_max =
51  std::chrono::time_point<std::chrono::high_resolution_clock>::min();
52 
53  for (auto const& e : events_)
54  {
55  if (e < t_min)
56  t_min = e;
57  if (e > t_max)
58  t_max = e;
59  }
60 
61  auto diff = t_max - t_min;
62  auto secs = std::chrono::duration_cast<std::chrono::seconds>(diff).count();
63  auto ms = std::chrono::duration_cast<std::chrono::microseconds>(diff).count();
64  std::chrono::duration<double> test(t_max - t_min);
65  LOG_INFO("Recieved 250 messages at %f Hz", (250.0 / test.count()));
66  idx_ = 0;
67  }
68  }
69 
70 public:
71  bool consume(RTState_V1_6__7& state)
72  {
73  trigger();
74  return true;
75  }
76  bool consume(RTState_V1_8& state)
77  {
78  trigger();
79  return true;
80  }
81  bool consume(RTState_V3_0__1& state)
82  {
83  trigger();
84  return true;
85  }
86  bool consume(RTState_V3_2__3& state)
87  {
88  trigger();
89  return true;
90  }
91 
93  {
94  last_ = Clock::now();
95  }
97  {
98  }
99  void stopConsumer()
100  {
101  }
102 };
void trigger()
Definition: event_counter.h:36
bool consume(RTState_V1_6__7 &state)
Definition: event_counter.h:71
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
void teardownConsumer()
Definition: event_counter.h:96
#define LOG_INFO(format,...)
Definition: log.h:35
Clock::time_point last_
Definition: event_counter.h:33
bool consume(RTState_V3_2__3 &state)
Definition: event_counter.h:86
Clock::time_point events_[250]
Definition: event_counter.h:30
bool consume(RTState_V1_8 &state)
Definition: event_counter.h:76
void stopConsumer()
Definition: event_counter.h:99
void setupConsumer()
Definition: event_counter.h:92
bool consume(RTState_V3_0__1 &state)
Definition: event_counter.h:81
std::chrono::high_resolution_clock Clock
Definition: event_counter.h:29


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00