iCub-main
mcEventDownsampler.cpp
Go to the documentation of this file.
1 // --------------------------------------------------------------------------------------------------------------------
2 // - public interface
3 // --------------------------------------------------------------------------------------------------------------------
4 
5 #include "mcEventDownsampler.h"
6 
7 namespace mced {
8 
9  YARP_LOG_COMPONENT(EVENT_DOWNSAMPLER, "event-downsampler")
10 
12  {
13  mutex = new std::mutex();
14  };
15 
17  {
18 
19  stop();
20 
21  if(nullptr != timer)
22  {
23  delete timer;
24  timer = nullptr;
25  }
26 
27  delete mutex;
28  }
29 
31  {
32  mutex->lock();
33 
34  counter++;
35  size_t diff = counter - latch_1;
36  bool cp = !isdownsampling;
37 
38  mutex->unlock();
39 
40  if(diff > 5)
41  {
42  return false;
43  }
44  else
45  {
46  return cp;
47  }
48  }
49 
51  {
52 
53  if(nullptr != timer)
54  {
55  timer->stop();
56  return true;
57  }
58 
59  return false;
60  }
61 
63  {
64  if(nullptr != timer)
65  {
66  stop();
67  }
68 
69  expire_time = yarp::os::Time::now();
70  yarp::os::TimerSettings ts(config.period);
71  timer = new yarp::os::Timer(ts, &mcEventDownsampler::step, this, true, mutex);
72 
73  if (timer != nullptr)
74  {
75  timer->start();
76  return true;
77  }
78  else
79  {
80  return false;
81  }
82  }
83 
84  bool mcEventDownsampler::step(const yarp::os::YarpTimerEvent &event)
85  {
86  if (yarp::os::Time::now() - expire_time >= 1)
87  {
88  expire_time = yarp::os::Time::now();
89 
90  isdownsampling = (counter - latch_1 > config.threshold);
91 
92  if (isdownsampling)
93  {
94  printreport();
95  latch_2 = counter;
96  }
97  latch_1 = counter;
98  }
99  return true;
100  }
101 
102  void mcEventDownsampler::printreport()
103  {
104  yCError(EVENT_DOWNSAMPLER) << config.subcomponent << config.info << "-"
105  << "detected" << counter - latch_2 << "events on aggregate since the last message";
106  }
107 
108 } // mced
~mcEventDownsampler()
Destroy the Event Downsampler object.
bool start()
Instantiates the yarp::os::Timer object and starts it.
bool canprint()
Called by the object that implements the downsampler.
bool stop()
Stops the timer.