Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* @file spinner.hpp |
3 |
|
|
* @author Maximilien Naveau (maximilien.naveau@gmail.com) |
4 |
|
|
* license License BSD-3-Clause |
5 |
|
|
* @copyright Copyright (c) 2019, New York University and Max Planck |
6 |
|
|
* Gesellschaft. |
7 |
|
|
* @date 2019-05-22 |
8 |
|
|
* |
9 |
|
|
* @brief Tools for maintaining the timing on a while loop. |
10 |
|
|
*/ |
11 |
|
|
|
12 |
|
|
#ifndef SPINNER_HPP |
13 |
|
|
#define SPINNER_HPP |
14 |
|
|
|
15 |
|
|
#include <unistd.h> |
16 |
|
|
#include <chrono> |
17 |
|
|
|
18 |
|
|
namespace real_time_tools |
19 |
|
|
{ |
20 |
|
|
/** |
21 |
|
|
* @brief Class to have threads / loops running at a desired frequency |
22 |
|
|
*/ |
23 |
|
|
class Spinner |
24 |
|
|
{ |
25 |
|
|
public: |
26 |
|
|
// create a spinner for the desired frequency |
27 |
|
|
Spinner(); |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* @brief set_period sets the period of the loop in !!seconds!! |
31 |
|
|
* @param period in seconds. |
32 |
|
|
*/ |
33 |
|
2 |
void set_period(double period) |
34 |
|
|
{ |
35 |
|
2 |
period_sec_ = period; |
36 |
|
2 |
} |
37 |
|
|
|
38 |
|
|
/** |
39 |
|
|
* @brief Set the frequency of the loop [Hz] |
40 |
|
|
* |
41 |
|
|
* @param frequency |
42 |
|
|
*/ |
43 |
|
✗ |
void set_frequency(double frequency) |
44 |
|
|
{ |
45 |
|
✗ |
period_sec_ = 1.0 / frequency; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* @brief To be called at the beginning of the loop if the spinner is not |
50 |
|
|
* created just before. |
51 |
|
|
*/ |
52 |
|
|
void initialize(); |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* @brief spin waits for the time such that successive calls to spin |
56 |
|
|
* will result in spin being called at the desired frequency |
57 |
|
|
*/ |
58 |
|
|
void spin(); |
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* @brief Predict the time the current thread is going to sleep. |
62 |
|
|
*/ |
63 |
|
|
double predict_sleeping_time(); |
64 |
|
|
|
65 |
|
|
private: |
66 |
|
|
/** |
67 |
|
|
* @brief period_sec_ is the period of the loop in seconds |
68 |
|
|
*/ |
69 |
|
|
double period_sec_; |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* @brief next_date_sec_ is the date when the loop needs to wake up. |
73 |
|
|
*/ |
74 |
|
|
double next_date_sec_; |
75 |
|
|
}; |
76 |
|
|
|
77 |
|
|
} // namespace real_time_tools |
78 |
|
|
|
79 |
|
|
#endif // SPINNER_HPP |
80 |
|
|
|