Cheetah Software  1.0
PeriodicTask.cpp
Go to the documentation of this file.
1 
9 #include <sys/timerfd.h>
10 #include <unistd.h>
11 #include <cmath>
13 
15  std::string name)
16  : _period(period), _name(name) {
17  taskManager->addTask(this);
18 }
19 
21  if (_running) {
22  printf("[PeriodicTask] Tried to start %s but it was already running!\n",
23  _name.c_str());
24  return;
25  }
26  init();
27  _running = true;
28  _thread = std::thread(&PeriodicTask::loopFunction, this);
29 }
30 
32  if (!_running) {
33  printf("[PeriodicTask] Tried to stop %s but it wasn't running!\n",
34  _name.c_str());
35  return;
36  }
37  _running = false;
38  printf("[PeriodicTask] Waiting for %s to stop...\n", _name.c_str());
39  _thread.join();
40  printf("[PeriodicTask] Done!\n");
41  cleanup();
42 }
43 
45  return _maxPeriod > _period * 1.3f || _maxRuntime > _period;
46 }
47 
49  _maxPeriod = 0;
50  _maxRuntime = 0;
51 }
52 
54  if (!_running) return;
55  if (isSlow()) {
56  printf_color(PrintColor::Red, "|%-20s|%6.4f|%6.4f|%6.4f|%6.4f|%6.4f\n",
59  } else {
60  printf("|%-20s|%6.4f|%6.4f|%6.4f|%6.4f|%6.4f\n", _name.c_str(),
62  }
63 }
64 
66  auto timerFd = timerfd_create(CLOCK_MONOTONIC, 0);
67  int seconds = (int)_period;
68  int nanoseconds = (int)(1e9 * std::fmod(_period, 1.f));
69 
70  Timer t;
71 
72  itimerspec timerSpec;
73  timerSpec.it_interval.tv_sec = seconds;
74  timerSpec.it_value.tv_sec = seconds;
75  timerSpec.it_value.tv_nsec = nanoseconds;
76  timerSpec.it_interval.tv_nsec = nanoseconds;
77 
78  timerfd_settime(timerFd, 0, &timerSpec, nullptr);
79  unsigned long long missed = 0;
80 
81  printf("[PeriodicTask] Start %s (%d s, %d ns)\n", _name.c_str(), seconds,
82  nanoseconds);
83  while (_running) {
84  _lastPeriodTime = (float)t.getSeconds();
85  t.start();
86  run();
87  _lastRuntime = (float)t.getSeconds();
88  int m = read(timerFd, &missed, sizeof(missed));
89  (void)m;
92  }
93  printf("[PeriodicTask] %s has stopped!\n", _name.c_str());
94 }
95 
97 
99  _tasks.push_back(task);
100 }
101 
103  printf("\n----------------------------TASKS----------------------------\n");
104  printf("|%-20s|%-6s|%-6s|%-6s|%-6s|%-6s\n", "name", "rt", "rt-max", "T-des",
105  "T-act", "T-max");
106  printf("-----------------------------------------------------------\n");
107  for (auto& task : _tasks) {
108  task->printStatus();
109  task->clearMax();
110  }
111  printf("-------------------------------------------------------------\n\n");
112 }
113 
115  for (auto& task : _tasks) {
116  if (task->isSlow()) {
117  task->printStatus();
118  task->clearMax();
119  }
120  }
121 }
122 
124  for (auto& task : _tasks) {
125  task->stop();
126  }
127 }
float _lastRuntime
Definition: PeriodicTask.h:46
Definition: Timer.h:12
Implementation of a periodic function running in a separate thread. Periodic tasks have a task manage...
PeriodicTask(PeriodicTaskManager *taskManager, float period, std::string name)
void start()
Definition: Timer.h:16
void loopFunction()
volatile bool _running
Definition: PeriodicTask.h:45
float _lastPeriodTime
Definition: PeriodicTask.h:47
double getSeconds()
Definition: Timer.h:27
Timer for measuring how long things take.
float _maxPeriod
Definition: PeriodicTask.h:48
std::string _name
Definition: PeriodicTask.h:50
void printf_color(PrintColor color, const char *fmt,...)
virtual void init()=0
virtual void run()=0
float _maxRuntime
Definition: PeriodicTask.h:49
std::thread _thread
Definition: PeriodicTask.h:51
virtual void cleanup()=0
void printStatus()
void addTask(PeriodicTask *task)
MX f(const MX &x, const MX &u)