Skip to content

Commit

Permalink
Use rcl_time_point_valid
Browse files Browse the repository at this point in the history
Signed-off-by: methylDragon <methylDragon@gmail.com>
  • Loading branch information
methylDragon committed Nov 10, 2022
1 parent 5d6d65b commit a7eec28
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
9 changes: 2 additions & 7 deletions rclcpp/include/rclcpp/clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,8 @@ class Clock
* uninitialized time (i.e. with zero time.)
*
* Note:
* This notion of validity is different from the rcl notion of validity (rcl_clock_valid)!
* A clock that is rcl_clock_valid is a clock that can be used to get a time (i.e. not
* NULL, not RCL_CLOCK_UNINITIALIZED, or with a missing get_now() method.)
*
* A clock that is valid in the sense codified by this method is a clock with non-zero time.
* Consequently, a clock that is not rcl_clock_valid can never become valid, since it cannot
* be used to obtain a time.
* A valid clock must be both rcl_clock_valid and hold a time that is rcl_time_point_valid.
* An invalid clock can potentially become valid if it is rcl_clock_valid.
*
* \return true if clock was or became valid
*/
Expand Down
10 changes: 10 additions & 0 deletions rclcpp/include/rclcpp/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ class Time
rcl_clock_type_t
get_clock_type() const;

/// Check if time is valid.
/**
* A valid time is a time that is non-zero.
*
* \return true if the time is valid
*/
RCLCPP_PUBLIC
bool
is_valid() const;

private:
rcl_time_point_t rcl_time_;
friend Clock; // Allow clock to manipulate internal data
Expand Down
2 changes: 1 addition & 1 deletion rclcpp/src/rclcpp/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Clock::is_valid()
case RCL_ROS_TIME:
case RCL_STEADY_TIME:
case RCL_SYSTEM_TIME:
return now().nanoseconds() > 0;
return now().is_valid();

// By right we shouldn't even get to this block, but these cases are included for completeness
case RCL_CLOCK_UNINITIALIZED:
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/src/rclcpp/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ Time::get_clock_type() const
return rcl_time_.clock_type;
}

bool
Time::is_valid() const
{
return rcl_time_point_valid(const_cast<rcl_time_point_t *>(&rcl_time_));
}

Time
operator+(const rclcpp::Duration & lhs, const rclcpp::Time & rhs)
{
Expand Down
12 changes: 12 additions & 0 deletions rclcpp/test/rclcpp/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,18 @@ TEST_F(TestTime, test_overflow_underflow_throws) {
std::underflow_error("addition leads to int64_t underflow"));
}

TEST_F(TestTime, validity) {
EXPECT_FALSE(rclcpp::Time(0, 0, RCL_CLOCK_UNINITIALIZED).is_valid());
EXPECT_FALSE(rclcpp::Time(0, 0, RCL_SYSTEM_TIME).is_valid());
EXPECT_FALSE(rclcpp::Time(0, 0, RCL_STEADY_TIME).is_valid());
EXPECT_FALSE(rclcpp::Time(0, 0, RCL_ROS_TIME).is_valid());

EXPECT_TRUE(rclcpp::Time(0, 1, RCL_CLOCK_UNINITIALIZED).is_valid());
EXPECT_TRUE(rclcpp::Time(0, 1, RCL_SYSTEM_TIME).is_valid());
EXPECT_TRUE(rclcpp::Time(0, 1, RCL_STEADY_TIME).is_valid());
EXPECT_TRUE(rclcpp::Time(0, 1, RCL_ROS_TIME).is_valid());
}

class TestClockSleep : public ::testing::Test
{
protected:
Expand Down

0 comments on commit a7eec28

Please sign in to comment.