Skip to content

Commit 0ccac1e

Browse files
Carl Delseyclalancette
Carl Delsey
authored andcommitted
Adding a factory method to create a Duration from seconds (#567)
* Adding a factory method to create a Duration from seconds There are many places in the ROS codebase where a time duration is specified as a floating point number of seconds. A factory function to create a Duration object from these values makes the code a bit simpler in many cases. Signed-off-by: Carl Delsey <carl.r.delsey@intel.com> * Adding some comments to clarify which constructors get matched. Signed-off-by: Carl Delsey <carl.r.delsey@intel.com>
1 parent 5a5a1fe commit 0ccac1e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

rclcpp/include/rclcpp/duration.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ class RCLCPP_PUBLIC Duration
2828
public:
2929
Duration(int32_t seconds, uint32_t nanoseconds);
3030

31+
// This constructor matches any numeric value - ints or floats
3132
explicit Duration(rcl_duration_value_t nanoseconds);
3233

3334
explicit Duration(std::chrono::nanoseconds nanoseconds);
3435

36+
// This constructor matches any std::chrono value other than nanoseconds
3537
// intentionally not using explicit to create a conversion constructor
3638
template<class Rep, class Period>
3739
// cppcheck-suppress noExplicitConstructor
@@ -94,6 +96,10 @@ class RCLCPP_PUBLIC Duration
9496
double
9597
seconds() const;
9698

99+
// Create a duration object from a floating point number representing seconds
100+
static Duration
101+
from_seconds(double seconds);
102+
97103
template<class DurationT>
98104
DurationT
99105
to_chrono() const

rclcpp/src/rclcpp/duration.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,10 @@ Duration::to_rmw_time() const
233233
return result;
234234
}
235235

236+
Duration
237+
Duration::from_seconds(double seconds)
238+
{
239+
return Duration(static_cast<int64_t>(RCL_S_TO_NS(seconds)));
240+
}
241+
236242
} // namespace rclcpp

rclcpp/test/test_duration.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,20 @@ TEST(TestDuration, maximum_duration) {
136136

137137
EXPECT_EQ(max_duration, max);
138138
}
139+
140+
static const int64_t HALF_SEC_IN_NS = 500 * 1000 * 1000;
141+
static const int64_t ONE_AND_HALF_SEC_IN_NS = 3 * HALF_SEC_IN_NS;
142+
143+
TEST(TestDuration, from_seconds) {
144+
EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration::from_seconds(0.0));
145+
EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration::from_seconds(0));
146+
EXPECT_EQ(rclcpp::Duration(1, HALF_SEC_IN_NS), rclcpp::Duration::from_seconds(1.5));
147+
EXPECT_EQ(rclcpp::Duration(-ONE_AND_HALF_SEC_IN_NS), rclcpp::Duration::from_seconds(-1.5));
148+
}
149+
150+
TEST(TestDuration, std_chrono_constructors) {
151+
EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration(0.0s));
152+
EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration(0s));
153+
EXPECT_EQ(rclcpp::Duration(1, HALF_SEC_IN_NS), rclcpp::Duration(1.5s));
154+
EXPECT_EQ(rclcpp::Duration(-1, 0), rclcpp::Duration(-1s));
155+
}

0 commit comments

Comments
 (0)