Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions include/real_time_tools/hard_spinner.hpp
Original file line number Diff line number Diff line change
@@ -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 <unistd.h>
#include <chrono>
#include <real_time_tools/spinner.hpp>

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
2 changes: 1 addition & 1 deletion include/real_time_tools/spinner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Spinner
*/
double predict_sleeping_time();

private:
protected:
/**
* @brief period_sec_ is the period of the loop in seconds
*/
Expand Down
26 changes: 26 additions & 0 deletions src/hard_spinner.cpp
Original file line number Diff line number Diff line change
@@ -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 <real_time_tools/hard_spinner.hpp>
#include <real_time_tools/timer.hpp>

namespace real_time_tools
{
HardSpinner::HardSpinner()
: Spinner() { }

void HardSpinner::spin()
{
Timer::sleep_until_sec(next_date_sec_);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

problem:
is 3 tics are missed this method does sleep for 3 times which in a control loop means sending 3 commands without sleeping. Which might mean a robot communication break down.

next_date_sec_ = next_date_sec_ + period_sec_;
}

} // namespace real_time_tools