diff --git a/CMakeLists.txt b/CMakeLists.txt index 622b46c..f44bdb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ add_library(${LIBRARY_NAME} src/realtime_check.cpp src/thread.cpp src/spinner.cpp + src/hard_spinner.cpp src/timer.cpp src/iostream.cpp src/usb_stream.cpp diff --git a/include/real_time_tools/hard_spinner.hpp b/include/real_time_tools/hard_spinner.hpp new file mode 100644 index 0000000..7a525b7 --- /dev/null +++ b/include/real_time_tools/hard_spinner.hpp @@ -0,0 +1,46 @@ +/** + * @file hard_spinner.cpp + * @author Julian Viereck (jviereck@tuebingen.mpg.de) + * license License BSD-3-Clause + * @copyright Copyright (c) 2020, New York University and Max Planck + * Gesellschaft. + * @date 2020-11-26 + * + * @brief This file implements a hard spinner to time a loop. Compared to the + * Spinner class, this spinner uses the desired frequency for how often to + * spin instead of the desired waiting time. + * + * \code{.txt} + * Spinner: |-----1 ms-----| |-work-| |-----1 ms-----| |-work-| + * HardSpinner: |-|-work-|-----| |-|-work-|-----| |-|-work-|-----| + * \endcode + */ + +#ifndef HARD_SPINNER_HPP +#define HARD_SPINNER_HPP + +#include +#include +#include + +namespace real_time_tools +{ +/** + * @brief Class to have threads / loops running at a desired frequency + */ +class HardSpinner: Spinner +{ +public: + // create a spinner for the desired frequency + HardSpinner(); + + /** + * @brief spin waits for the time such that successive calls to spin + * will result in spin being called at the desired frequency. + */ + void spin(); +}; + +} // namespace real_time_tools + +#endif // SPINNER_HPP diff --git a/include/real_time_tools/spinner.hpp b/include/real_time_tools/spinner.hpp index 290af91..a0c4c40 100644 --- a/include/real_time_tools/spinner.hpp +++ b/include/real_time_tools/spinner.hpp @@ -62,7 +62,7 @@ class Spinner */ double predict_sleeping_time(); -private: +protected: /** * @brief period_sec_ is the period of the loop in seconds */ diff --git a/src/hard_spinner.cpp b/src/hard_spinner.cpp new file mode 100644 index 0000000..dd67cff --- /dev/null +++ b/src/hard_spinner.cpp @@ -0,0 +1,26 @@ +/** + * @file hard_spinner.cpp + * @author Julian Viereck (jviereck@tuebingen.mpg.de) + * license License BSD-3-Clause + * @copyright Copyright (c) 2020, New York University and Max Planck + * Gesellschaft. + * @date 2020-11-26 + * + * @brief This file implements a hard spinner to time a loop. + */ + +#include +#include + +namespace real_time_tools +{ +HardSpinner::HardSpinner() +: Spinner() { } + +void HardSpinner::spin() +{ + Timer::sleep_until_sec(next_date_sec_); + next_date_sec_ = next_date_sec_ + period_sec_; +} + +} // namespace real_time_tools