Cheetah Software  1.0
PeriodicTask.h
Go to the documentation of this file.
1 
7 #ifndef PROJECT_PERIODICTASK_H
8 #define PROJECT_PERIODICTASK_H
9 
10 #include <string>
11 #include <thread>
12 #include <vector>
13 
15 
19 class PeriodicTask {
20  public:
21  PeriodicTask(PeriodicTaskManager* taskManager, float period,
22  std::string name);
23  void start();
24  void stop();
25  void printStatus();
26  void clearMax();
27  bool isSlow();
28  virtual void init() = 0;
29  virtual void run() = 0;
30  virtual void cleanup() = 0;
31  virtual ~PeriodicTask() { stop(); }
32 
33  float getPeriod() { return _period; }
34 
35  float getRuntime() { return _lastRuntime; }
36 
37  float getMaxPeriod() { return _maxPeriod; }
38 
39  float getMaxRuntime() { return _maxRuntime; }
40 
41  private:
42  void loopFunction();
43 
44  float _period;
45  volatile bool _running = false;
46  float _lastRuntime = 0;
47  float _lastPeriodTime = 0;
48  float _maxPeriod = 0;
49  float _maxRuntime = 0;
50  std::string _name;
51  std::thread _thread;
52 };
53 
58  public:
59  PeriodicTaskManager() = default;
61  void addTask(PeriodicTask* task);
62  void printStatus();
63  void printStatusOfSlowTasks();
64  void stopAll();
65 
66  private:
67  std::vector<PeriodicTask*> _tasks;
68 };
69 
74  public:
75  PeriodicFunction(PeriodicTaskManager* taskManager, float period,
76  std::string name, void (*function)())
77  : PeriodicTask(taskManager, period, name), _function(function) {}
78  void cleanup() {}
79  void init() {}
80  void run() { _function(); }
81 
83 
84  private:
85  void (*_function)() = nullptr;
86 };
87 
91 class PrintTaskStatus : public PeriodicTask {
92  public:
94  : PeriodicTask(tm, period, "print-tasks"), _tm(tm) {}
95  void run() override { _tm->printStatus(); }
96 
97  void init() override {}
98 
99  void cleanup() override {}
100 
101  private:
103 };
104 
108 template <typename T>
110  public:
111  PeriodicMemberFunction(PeriodicTaskManager* taskManager, float period,
112  std::string name, void (T::*function)(), T* obj)
113  : PeriodicTask(taskManager, period, name),
114  _function(function),
115  _obj(obj) {}
116 
117  void cleanup() {}
118  void init() {}
119  void run() { (_obj->*_function)(); }
120 
121  private:
122  void (T::*_function)();
123  T* _obj;
124 };
125 
126 #endif // PROJECT_PERIODICTASK_H
float _lastRuntime
Definition: PeriodicTask.h:46
float getRuntime()
Definition: PeriodicTask.h:35
void init() override
Definition: PeriodicTask.h:97
float getMaxPeriod()
Definition: PeriodicTask.h:37
std::vector< PeriodicTask * > _tasks
Definition: PeriodicTask.h:67
virtual ~PeriodicTask()
Definition: PeriodicTask.h:31
PeriodicMemberFunction(PeriodicTaskManager *taskManager, float period, std::string name, void(T::*function)(), T *obj)
Definition: PeriodicTask.h:111
float getPeriod()
Definition: PeriodicTask.h:33
PeriodicTask(PeriodicTaskManager *taskManager, float period, std::string name)
void loopFunction()
volatile bool _running
Definition: PeriodicTask.h:45
float _lastPeriodTime
Definition: PeriodicTask.h:47
void run() override
Definition: PeriodicTask.h:95
PrintTaskStatus(PeriodicTaskManager *tm, float period)
Definition: PeriodicTask.h:93
float _maxPeriod
Definition: PeriodicTask.h:48
PeriodicTaskManager * _tm
Definition: PeriodicTask.h:102
std::string _name
Definition: PeriodicTask.h:50
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 cleanup() override
Definition: PeriodicTask.h:99
PeriodicFunction(PeriodicTaskManager *taskManager, float period, std::string name, void(*function)())
Definition: PeriodicTask.h:75
float getMaxRuntime()
Definition: PeriodicTask.h:39