Cheetah Software  1.0
PeriodicTask Class Referenceabstract

#include <PeriodicTask.h>

+ Inheritance diagram for PeriodicTask:
+ Collaboration diagram for PeriodicTask:

Public Member Functions

 PeriodicTask (PeriodicTaskManager *taskManager, float period, std::string name)
 
void start ()
 
void stop ()
 
void printStatus ()
 
void clearMax ()
 
bool isSlow ()
 
virtual void init ()=0
 
virtual void run ()=0
 
virtual void cleanup ()=0
 
virtual ~PeriodicTask ()
 
float getPeriod ()
 
float getRuntime ()
 
float getMaxPeriod ()
 
float getMaxRuntime ()
 

Private Member Functions

void loopFunction ()
 

Private Attributes

float _period
 
volatile bool _running = false
 
float _lastRuntime = 0
 
float _lastPeriodTime = 0
 
float _maxPeriod = 0
 
float _maxRuntime = 0
 
std::string _name
 
std::thread _thread
 

Detailed Description

A single periodic task which will call run() at the given frequency

Definition at line 19 of file PeriodicTask.h.

Constructor & Destructor Documentation

PeriodicTask::PeriodicTask ( PeriodicTaskManager taskManager,
float  period,
std::string  name 
)

Definition at line 14 of file PeriodicTask.cpp.

References PeriodicTaskManager::addTask().

16  : _period(period), _name(name) {
17  taskManager->addTask(this);
18 }
std::string _name
Definition: PeriodicTask.h:50
void addTask(PeriodicTask *task)

+ Here is the call graph for this function:

virtual PeriodicTask::~PeriodicTask ( )
inlinevirtual

Definition at line 31 of file PeriodicTask.h.

References stop().

31 { stop(); }

+ Here is the call graph for this function:

Member Function Documentation

virtual void PeriodicTask::cleanup ( )
pure virtual

Implemented in PeriodicMemberFunction< T >, PrintTaskStatus, PeriodicFunction, RobotInterface, RobotRunner, and TestPeriodicTask.

+ Here is the caller graph for this function:

void PeriodicTask::clearMax ( )

Definition at line 48 of file PeriodicTask.cpp.

References _maxPeriod, and _maxRuntime.

48  {
49  _maxPeriod = 0;
50  _maxRuntime = 0;
51 }
float _maxPeriod
Definition: PeriodicTask.h:48
float _maxRuntime
Definition: PeriodicTask.h:49
float PeriodicTask::getMaxPeriod ( )
inline

Definition at line 37 of file PeriodicTask.h.

References _maxPeriod.

37 { return _maxPeriod; }
float _maxPeriod
Definition: PeriodicTask.h:48
float PeriodicTask::getMaxRuntime ( )
inline

Definition at line 39 of file PeriodicTask.h.

References _maxRuntime, and loopFunction().

39 { return _maxRuntime; }
float _maxRuntime
Definition: PeriodicTask.h:49

+ Here is the call graph for this function:

float PeriodicTask::getPeriod ( )
inline

Definition at line 33 of file PeriodicTask.h.

References _period.

33 { return _period; }
float PeriodicTask::getRuntime ( )
inline

Definition at line 35 of file PeriodicTask.h.

References _lastRuntime.

35 { return _lastRuntime; }
float _lastRuntime
Definition: PeriodicTask.h:46
virtual void PeriodicTask::init ( )
pure virtual

Implemented in PeriodicMemberFunction< T >, PrintTaskStatus, PeriodicFunction, RobotInterface, RobotRunner, and TestPeriodicTask.

+ Here is the caller graph for this function:

bool PeriodicTask::isSlow ( )

Definition at line 44 of file PeriodicTask.cpp.

References _maxPeriod, _maxRuntime, and _period.

44  {
45  return _maxPeriod > _period * 1.3f || _maxRuntime > _period;
46 }
float _maxPeriod
Definition: PeriodicTask.h:48
float _maxRuntime
Definition: PeriodicTask.h:49

+ Here is the caller graph for this function:

void PeriodicTask::loopFunction ( )
private

Definition at line 65 of file PeriodicTask.cpp.

References _lastPeriodTime, _lastRuntime, _maxPeriod, _maxRuntime, _name, _period, _running, f(), Timer::getSeconds(), run(), and Timer::start().

65  {
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 }
float _lastRuntime
Definition: PeriodicTask.h:46
Definition: Timer.h:12
void start()
Definition: Timer.h:16
volatile bool _running
Definition: PeriodicTask.h:45
float _lastPeriodTime
Definition: PeriodicTask.h:47
double getSeconds()
Definition: Timer.h:27
float _maxPeriod
Definition: PeriodicTask.h:48
std::string _name
Definition: PeriodicTask.h:50
virtual void run()=0
float _maxRuntime
Definition: PeriodicTask.h:49
MX f(const MX &x, const MX &u)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void PeriodicTask::printStatus ( )

Definition at line 53 of file PeriodicTask.cpp.

References _lastPeriodTime, _lastRuntime, _maxPeriod, _maxRuntime, _name, _period, _running, isSlow(), printf_color(), and Red.

53  {
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 }
float _lastRuntime
Definition: PeriodicTask.h:46
volatile bool _running
Definition: PeriodicTask.h:45
float _lastPeriodTime
Definition: PeriodicTask.h:47
float _maxPeriod
Definition: PeriodicTask.h:48
std::string _name
Definition: PeriodicTask.h:50
void printf_color(PrintColor color, const char *fmt,...)
float _maxRuntime
Definition: PeriodicTask.h:49

+ Here is the call graph for this function:

virtual void PeriodicTask::run ( )
pure virtual

Implemented in PeriodicMemberFunction< T >, PrintTaskStatus, PeriodicFunction, RobotInterface, RobotRunner, and TestPeriodicTask.

+ Here is the caller graph for this function:

void PeriodicTask::start ( )

Definition at line 20 of file PeriodicTask.cpp.

References _name, _running, _thread, init(), and loopFunction().

20  {
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 }
void loopFunction()
volatile bool _running
Definition: PeriodicTask.h:45
std::string _name
Definition: PeriodicTask.h:50
virtual void init()=0
std::thread _thread
Definition: PeriodicTask.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void PeriodicTask::stop ( )

Definition at line 31 of file PeriodicTask.cpp.

References _name, _running, _thread, and cleanup().

31  {
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 }
volatile bool _running
Definition: PeriodicTask.h:45
std::string _name
Definition: PeriodicTask.h:50
std::thread _thread
Definition: PeriodicTask.h:51
virtual void cleanup()=0

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

float PeriodicTask::_lastPeriodTime = 0
private

Definition at line 47 of file PeriodicTask.h.

float PeriodicTask::_lastRuntime = 0
private

Definition at line 46 of file PeriodicTask.h.

float PeriodicTask::_maxPeriod = 0
private

Definition at line 48 of file PeriodicTask.h.

float PeriodicTask::_maxRuntime = 0
private

Definition at line 49 of file PeriodicTask.h.

std::string PeriodicTask::_name
private

Definition at line 50 of file PeriodicTask.h.

float PeriodicTask::_period
private

Definition at line 44 of file PeriodicTask.h.

volatile bool PeriodicTask::_running = false
private

Definition at line 45 of file PeriodicTask.h.

std::thread PeriodicTask::_thread
private

Definition at line 51 of file PeriodicTask.h.


The documentation for this class was generated from the following files: