Esempio n. 1
0
    def test_clock_change(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)
        time_source.ros_time_is_active = True

        pre_cb = Mock()
        post_cb = Mock()
        threshold = JumpThreshold(min_forward=None, min_backward=None, on_clock_change=True)
        handler = clock.create_jump_callback(
            threshold, pre_callback=pre_cb, post_callback=post_cb)

        time_source.ros_time_is_active = False
        pre_cb.assert_called()
        post_cb.assert_called()
        assert post_cb.call_args[0][0].clock_change == ClockChange.ROS_TIME_DEACTIVATED

        pre_cb.reset_mock()
        post_cb.reset_mock()

        time_source.ros_time_is_active = True
        pre_cb.assert_called()
        post_cb.assert_called()
        assert post_cb.call_args[0][0].clock_change == ClockChange.ROS_TIME_ACTIVATED
        handler.unregister()
Esempio n. 2
0
    def test_no_post_callback(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)
        time_source.ros_time_is_active = True

        pre_cb = Mock()
        threshold = JumpThreshold(min_forward=None, min_backward=None, on_clock_change=True)
        handler = clock.create_jump_callback(
            threshold, pre_callback=pre_cb, post_callback=None)

        time_source.ros_time_is_active = False
        pre_cb.assert_called_once()
        handler.unregister()
Esempio n. 3
0
    def test_time_source_using_sim_time(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)

        # Setting ROS time active on a time source should also cause attached clocks' use of ROS
        # time to be set to active.
        self.assertFalse(time_source.ros_time_is_active)
        self.assertFalse(clock.ros_time_is_active)
        time_source.ros_time_is_active = True
        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)
        assert clock.now() <= Time(seconds=5, clock_type=ClockType.ROS_TIME)

        # Check that attached clocks get the cached message
        clock2 = Clock(clock_type=ClockType.ROS_TIME)
        time_source.attach_clock(clock2)
        assert clock2.now() > Time(seconds=0, clock_type=ClockType.ROS_TIME)
        assert clock2.now() <= Time(seconds=5, clock_type=ClockType.ROS_TIME)

        # Check detaching the node
        time_source.detach_node()
        node2 = rclpy.create_node('TestTimeSource2', namespace='/rclpy')
        time_source.attach_node(node2)
        node2.destroy_node()
        assert time_source._node == node2
        assert time_source._clock_sub is not None
Esempio n. 4
0
 def test_log_throttle_ros_clock(self):
     message_was_logged = []
     ros_clock = ROSClock()
     time_source = TimeSource()
     time_source.attach_clock(ros_clock)
     time_source.ros_time_is_active = True
     for i in range(5):
         message_was_logged.append(
             rclpy.logging._root_logger.log(
                 'message_' + inspect.stack()[0][3] + '_' + str(i),
                 LoggingSeverity.INFO,
                 throttle_duration_sec=1,
                 throttle_time_source_type=ros_clock,
             ))
         time.sleep(0.4)
     self.assertEqual(
         message_was_logged,
         [
             False,  # t=0, throttled
             False,  # t=0.4, throttled
             False,  # t=0.8, throttled
             False,  # t=1.2, throttled
             False  # t=1.6, throttled
         ])
     message_was_logged = []
     for i in range(5):
         message_was_logged.append(
             rclpy.logging._root_logger.log(
                 'message_' + inspect.stack()[0][3] + '_' + str(i),
                 LoggingSeverity.INFO,
                 throttle_duration_sec=2,
                 throttle_time_source_type=ros_clock,
             ))
         ros_clock.set_ros_time_override(
             Time(
                 seconds=i + 1,
                 nanoseconds=0,
                 clock_type=ros_clock.clock_type,
             ))
     self.assertEqual(
         message_was_logged,
         [
             False,  # t=0, throttled
             False,  # t=1.0, throttled
             True,  # t=2.0, not throttled
             False,  # t=3.0, throttled
             True  # t=4.0, not throttled
         ])
Esempio n. 5
0
    def test_backwards_jump(self):
        time_source = TimeSource(node=self.node)
        clock = ROSClock()
        time_source.attach_clock(clock)
        time_source.ros_time_is_active = True

        pre_cb = Mock()
        post_cb = Mock()
        threshold = JumpThreshold(
            min_forward=None, min_backward=Duration(seconds=-0.5), on_clock_change=False)
        handler = clock.create_jump_callback(
            threshold, pre_callback=pre_cb, post_callback=post_cb)

        self.publish_reversed_clock_messages()

        pre_cb.assert_called()
        post_cb.assert_called()
        assert post_cb.call_args[0][0].clock_change == ClockChange.ROS_TIME_NO_CHANGE
        handler.unregister()