From 32d38c223603f9fb289cb05fa3cc5f3b43f554b5 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Fri, 17 Aug 2018 14:08:00 -0700 Subject: [PATCH 1/2] add Time::is_zero and Duration::seconds --- rclcpp/include/rclcpp/duration.hpp | 7 +++++++ rclcpp/include/rclcpp/time.hpp | 4 ++++ rclcpp/src/rclcpp/duration.cpp | 6 ++++++ rclcpp/src/rclcpp/time.cpp | 6 ++++++ 4 files changed, 23 insertions(+) diff --git a/rclcpp/include/rclcpp/duration.hpp b/rclcpp/include/rclcpp/duration.hpp index 6e635848ac..11fc2e298f 100644 --- a/rclcpp/include/rclcpp/duration.hpp +++ b/rclcpp/include/rclcpp/duration.hpp @@ -97,6 +97,13 @@ class Duration rcl_duration_value_t nanoseconds() const; + /// \return the duration in seconds as a floating point number. + /// \warning Depending on sizeof(double) there could be significant precision loss. + /// When an exact time is required use nanoseconds() instead. + RCLCPP_PUBLIC + double + seconds() const; + private: rcl_duration_t rcl_duration_; }; diff --git a/rclcpp/include/rclcpp/time.hpp b/rclcpp/include/rclcpp/time.hpp index e93cb64909..4db441ad57 100644 --- a/rclcpp/include/rclcpp/time.hpp +++ b/rclcpp/include/rclcpp/time.hpp @@ -113,6 +113,10 @@ class Time rcl_clock_type_t get_clock_type() const; + RCLCPP_PUBLIC + bool + is_zero() const; + private: rcl_time_point_t rcl_time_; friend Clock; // Allow clock to manipulate internal data diff --git a/rclcpp/src/rclcpp/duration.cpp b/rclcpp/src/rclcpp/duration.cpp index afbc2934a8..0455abecd2 100644 --- a/rclcpp/src/rclcpp/duration.cpp +++ b/rclcpp/src/rclcpp/duration.cpp @@ -214,4 +214,10 @@ Duration::nanoseconds() const return rcl_duration_.nanoseconds; } +double +Duration::seconds() const +{ + return std::chrono::duration(std::chrono::nanoseconds(rcl_duration_.nanoseconds)).count(); +} + } // namespace rclcpp diff --git a/rclcpp/src/rclcpp/time.cpp b/rclcpp/src/rclcpp/time.cpp index 9ab924a8b6..749b8a87bb 100644 --- a/rclcpp/src/rclcpp/time.cpp +++ b/rclcpp/src/rclcpp/time.cpp @@ -239,6 +239,12 @@ Time::get_clock_type() const return rcl_time_.clock_type; } +bool +Time::is_zero() const +{ + return 0 == rcl_time_.nanoseconds; +} + Time operator+(const rclcpp::Duration & lhs, const rclcpp::Time & rhs) { From 2fbf62b08d236ebd867c87e2ea133c1eb791fd9d Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Sat, 18 Aug 2018 10:54:16 -0700 Subject: [PATCH 2/2] remove is_zero() for now --- rclcpp/include/rclcpp/time.hpp | 4 ---- rclcpp/src/rclcpp/time.cpp | 6 ------ 2 files changed, 10 deletions(-) diff --git a/rclcpp/include/rclcpp/time.hpp b/rclcpp/include/rclcpp/time.hpp index 4db441ad57..e93cb64909 100644 --- a/rclcpp/include/rclcpp/time.hpp +++ b/rclcpp/include/rclcpp/time.hpp @@ -113,10 +113,6 @@ class Time rcl_clock_type_t get_clock_type() const; - RCLCPP_PUBLIC - bool - is_zero() const; - private: rcl_time_point_t rcl_time_; friend Clock; // Allow clock to manipulate internal data diff --git a/rclcpp/src/rclcpp/time.cpp b/rclcpp/src/rclcpp/time.cpp index 749b8a87bb..9ab924a8b6 100644 --- a/rclcpp/src/rclcpp/time.cpp +++ b/rclcpp/src/rclcpp/time.cpp @@ -239,12 +239,6 @@ Time::get_clock_type() const return rcl_time_.clock_type; } -bool -Time::is_zero() const -{ - return 0 == rcl_time_.nanoseconds; -} - Time operator+(const rclcpp::Duration & lhs, const rclcpp::Time & rhs) {