modify rates to allow ctrl-c to interrupt sleep

This commit is contained in:
William Woodall 2014-08-29 17:50:47 -07:00
parent b78164e4fe
commit 680e536f10

View file

@ -21,6 +21,7 @@
#include <thread> #include <thread>
#include <rclcpp/macros.hpp> #include <rclcpp/macros.hpp>
#include <rclcpp/utilities.hpp>
namespace rclcpp namespace rclcpp
{ {
@ -37,18 +38,23 @@ public:
virtual void reset() = 0; virtual void reset() = 0;
}; };
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
template<class Clock = std::chrono::high_resolution_clock> template<class Clock = std::chrono::high_resolution_clock>
class GenericRate : public RateBase class GenericRate : public RateBase
{ {
public: public:
RCLCPP_MAKE_SHARED_DEFINITIONS(GenericRate); RCLCPP_MAKE_SHARED_DEFINITIONS(GenericRate);
GenericRate(double rate) : rate_(rate), last_interval_(Clock::now()) GenericRate(double rate)
{ : GenericRate<Clock>(
typedef std::chrono::nanoseconds nanoseconds; duration_cast<nanoseconds>(duration<double>(1.0/rate)))
auto tmp = std::chrono::duration<double>(1.0f/rate_); {}
period_ = std::chrono::duration_cast<nanoseconds>(tmp); GenericRate(std::chrono::nanoseconds period)
} : period_(period), last_interval_(Clock::now())
{}
virtual bool virtual bool
sleep() sleep()
@ -80,8 +86,8 @@ public:
// Either way do not sleep and return false // Either way do not sleep and return false
return false; return false;
} }
// Sleep // Sleep (will get interrupted by ctrl-c, may not sleep full time)
std::this_thread::sleep_for(time_to_sleep); rclcpp::utilities::sleep_for(time_to_sleep);
return true; return true;
} }
@ -100,7 +106,6 @@ public:
private: private:
RCLCPP_DISABLE_COPY(GenericRate); RCLCPP_DISABLE_COPY(GenericRate);
double rate_;
std::chrono::nanoseconds period_; std::chrono::nanoseconds period_;
std::chrono::time_point<Clock> last_interval_; std::chrono::time_point<Clock> last_interval_;