Skip to content

Commit

Permalink
Only subscribe to clock topic if ROS time is active
Browse files Browse the repository at this point in the history
  • Loading branch information
dhood committed Jul 30, 2018
1 parent be9aaca commit 6e4b278
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion rclpy/rclpy/time_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def ros_time_is_active(self, enabled):
self._ros_time_is_active = enabled
for clock in self._associated_clocks:
clock._set_ros_time_is_active(True)
self._subscribe_to_clock_topic()

def _subscribe_to_clock_topic(self):
if self._clock_sub is not None and self._node is not None:
Expand All @@ -57,7 +58,8 @@ def attach_node(self, node):
if self._node is not None:
self.detach_node()
self._node = node
self._subscribe_to_clock_topic()
if self.ros_time_is_active:
self._subscribe_to_clock_topic()

def detach_node(self):
# Remove the subscription to the clock topic.
Expand Down
4 changes: 4 additions & 0 deletions rclpy/test/test_time_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def test_time_source_not_using_sim_time(self):
clock2._set_ros_time_is_active(True)
time_source.attach_clock(clock2)
self.assertFalse(clock2.ros_time_is_active)
assert time_source._clock_sub is None

def test_time_source_using_sim_time(self):
time_source = TimeSource(node=self.node)
Expand All @@ -84,6 +85,9 @@ def test_time_source_using_sim_time(self):
self.assertTrue(time_source.ros_time_is_active)
self.assertTrue(clock.ros_time_is_active)

# A subscriber should have been created
assert time_source._clock_sub is not None

# When using sim time, ROS time should look like the messages received on /clock
self.publish_clock_messages()
assert clock.now() > Time(seconds=0, clock_type=ClockType.ROS_TIME)
Expand Down

0 comments on commit 6e4b278

Please sign in to comment.