1919import rclpy
2020from rclpy .clock import Clock
2121from rclpy .clock import ClockType
22+ from rclpy .clock import ROSClock
2223from rclpy .time import Time
2324from rclpy .time_source import CLOCK_TOPIC
2425from rclpy .time_source import TimeSource
@@ -46,9 +47,26 @@ def publish_clock_messages(self):
4647 # TODO(dhood): use rate once available
4748 time .sleep (1 )
4849
50+ def test_time_source_attach_clock (self ):
51+ time_source = TimeSource (node = self .node )
52+
53+ # ROSClock is a specialization of Clock with ROS time methods.
54+ time_source .attach_clock (ROSClock ())
55+
56+ # A clock of type ROS_TIME can be attached. It will be converted to a ROSClock for storage.
57+ time_source .attach_clock (Clock (clock_type = ClockType .ROS_TIME ))
58+
59+ assert all ((isinstance (clock , ROSClock ) for clock in time_source ._associated_clocks ))
60+
61+ with self .assertRaises (ValueError ):
62+ time_source .attach_clock (Clock (clock_type = ClockType .SYSTEM_TIME ))
63+
64+ with self .assertRaises (ValueError ):
65+ time_source .attach_clock (Clock (clock_type = ClockType .STEADY_TIME ))
66+
4967 def test_time_source_not_using_sim_time (self ):
5068 time_source = TimeSource (node = self .node )
51- clock = Clock ( clock_type = ClockType . ROS_TIME )
69+ clock = ROSClock ( )
5270 time_source .attach_clock (clock )
5371
5472 # When not using sim time, ROS time should look like system time
@@ -66,15 +84,15 @@ def test_time_source_not_using_sim_time(self):
6684 # Whether or not an attached clock is using ROS time should be determined by the time
6785 # source managing it.
6886 self .assertFalse (time_source .ros_time_is_active )
69- clock2 = Clock ( clock_type = ClockType . ROS_TIME )
87+ clock2 = ROSClock ( )
7088 clock2 ._set_ros_time_is_active (True )
7189 time_source .attach_clock (clock2 )
7290 self .assertFalse (clock2 .ros_time_is_active )
7391 assert time_source ._clock_sub is None
7492
7593 def test_time_source_using_sim_time (self ):
7694 time_source = TimeSource (node = self .node )
77- clock = Clock ( clock_type = ClockType . ROS_TIME )
95+ clock = ROSClock ( )
7896 time_source .attach_clock (clock )
7997
8098 # Setting ROS time active on a time source should also cause attached clocks' use of ROS
0 commit comments