Skip to content

Commit 3aebe9f

Browse files
authored
ROS clock storage initially set to zero (#283)
1 parent adc0190 commit 3aebe9f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

rcl/src/rcl/time.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ rcl_ros_clock_init(
137137
rcl_init_generic_clock(clock);
138138
clock->data = allocator->allocate(sizeof(rcl_ros_clock_storage_t), allocator->state);
139139
rcl_ros_clock_storage_t * storage = (rcl_ros_clock_storage_t *)clock->data;
140+
// 0 is a special value meaning time has not been set
141+
atomic_init(&(storage->current_time), 0);
140142
storage->active = false;
141143
clock->get_now = rcl_get_ros_time;
142144
clock->type = RCL_ROS_TIME;

rcl/test/rcl/test_time.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <thread>
2121

2222
#include "osrf_testing_tools_cpp/memory_tools/memory_tools.hpp"
23+
#include "osrf_testing_tools_cpp/scope_exit.hpp"
2324
#include "rcl/error_handling.h"
2425
#include "rcl/time.h"
2526

@@ -175,6 +176,19 @@ TEST_F(CLASSNAME(TestTimeFixture, RMW_IMPLEMENTATION), test_rcl_init_for_clock_a
175176
EXPECT_EQ(retval, RCL_RET_OK) << rcl_get_error_string_safe();
176177
}
177178

179+
TEST_F(CLASSNAME(TestTimeFixture, RMW_IMPLEMENTATION), test_ros_clock_initially_zero) {
180+
rcl_allocator_t allocator = rcl_get_default_allocator();
181+
rcl_clock_t ros_clock;
182+
ASSERT_EQ(RCL_RET_OK, rcl_ros_clock_init(&ros_clock, &allocator)) << rcl_get_error_string_safe();
183+
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({
184+
EXPECT_EQ(RCL_RET_OK, rcl_clock_fini(&ros_clock)) << rcl_get_error_string_safe();
185+
});
186+
ASSERT_EQ(RCL_RET_OK, rcl_enable_ros_time_override(&ros_clock)) << rcl_get_error_string_safe();
187+
rcl_time_point_value_t query_now = 5;
188+
ASSERT_EQ(RCL_RET_OK, rcl_clock_get_now(&ros_clock, &query_now)) << rcl_get_error_string_safe();
189+
EXPECT_EQ(0, query_now);
190+
}
191+
178192
TEST(CLASSNAME(rcl_time, RMW_IMPLEMENTATION), clock_validation) {
179193
ASSERT_FALSE(rcl_clock_valid(NULL));
180194
rcl_clock_t uninitialized;

0 commit comments

Comments
 (0)