diff --git a/rclpy/rclpy/duration.py b/rclpy/rclpy/duration.py index c59fc559e..4fde06f38 100644 --- a/rclpy/rclpy/duration.py +++ b/rclpy/rclpy/duration.py @@ -34,6 +34,11 @@ def nanoseconds(self): def __repr__(self): return 'Duration(nanoseconds={0})'.format(self.nanoseconds) + def __str__(self): + if self == Infinite: + return 'Infinite' + return f'{self.nanoseconds} nanoseconds' + def __eq__(self, other): if isinstance(other, Duration): return self.nanoseconds == other.nanoseconds @@ -79,3 +84,7 @@ def from_msg(cls, msg): def get_c_duration(self): return self._duration_handle + + +# Constant representing an infinite amount of time. +Infinite = Duration(nanoseconds=_rclpy.RMW_DURATION_INFINITE) diff --git a/rclpy/rclpy/topic_endpoint_info.py b/rclpy/rclpy/topic_endpoint_info.py index be4ceb773..7569a926d 100644 --- a/rclpy/rclpy/topic_endpoint_info.py +++ b/rclpy/rclpy/topic_endpoint_info.py @@ -158,17 +158,18 @@ def __eq__(self, other): for slot in self.__slots__) def __str__(self): - result = 'Node name: %s\n' % self.node_name - result += 'Node namespace: %s\n' % self.node_namespace - result += 'Topic type: %s\n' % self.topic_type - result += 'Endpoint type: %s\n' % self.endpoint_type.name - result += 'GID: %s\n' % '.'.join(format(x, '02x') for x in self.endpoint_gid) - result += 'QoS profile:\n' - result += ' Reliability: %s\n' % self.qos_profile.reliability.name - result += ' Durability: %s\n' % self.qos_profile.durability.name - result += ' Lifespan: %d nanoseconds\n' % self.qos_profile.lifespan.nanoseconds - result += ' Deadline: %d nanoseconds\n' % self.qos_profile.deadline.nanoseconds - result += ' Liveliness: %s\n' % self.qos_profile.liveliness.name - result += ' Liveliness lease duration: %d nanoseconds' % \ - self.qos_profile.liveliness_lease_duration.nanoseconds - return result + gid = '.'.join(format(x, '02x') for x in self.endpoint_gid) + return '\n'.join([ + f'Node name: {self.node_name}', + f'Node namespace: {self.node_namespace}', + f'Topic type: {self.topic_type}', + f'Endpoint type: {self.endpoint_type.name}', + f'GID: {gid}', + 'QoS profile:', + f' Reliability: {self.qos_profile.reliability.name}', + f' Durability: {self.qos_profile.durability.name}', + f' Lifespan: {self.qos_profile.lifespan}', + f' Deadline: {self.qos_profile.deadline}', + f' Liveliness: {self.qos_profile.liveliness.name}', + f' Liveliness lease duration: {self.qos_profile.liveliness_lease_duration}', + ]) diff --git a/rclpy/src/rclpy/_rclpy_pybind11.cpp b/rclpy/src/rclpy/_rclpy_pybind11.cpp index 2fbacb1ae..01df8143c 100644 --- a/rclpy/src/rclpy/_rclpy_pybind11.cpp +++ b/rclpy/src/rclpy/_rclpy_pybind11.cpp @@ -58,6 +58,7 @@ PYBIND11_MODULE(_rclpy_pybind11, m) { .value("STEADY_TIME", RCL_STEADY_TIME); m.attr("RCL_DEFAULT_DOMAIN_ID") = py::int_(RCL_DEFAULT_DOMAIN_ID); + m.attr("RMW_DURATION_INFINITE") = py::int_(rmw_time_total_nsec(RMW_DURATION_INFINITE)); py::enum_(m, "ClockChange") .value( diff --git a/rclpy/test/test_time.py b/rclpy/test/test_time.py index d00178107..c89aa0a4b 100644 --- a/rclpy/test/test_time.py +++ b/rclpy/test/test_time.py @@ -16,6 +16,7 @@ from rclpy.clock import ClockType from rclpy.duration import Duration +from rclpy.duration import Infinite from rclpy.time import Time from test_msgs.msg import Builtins @@ -228,3 +229,7 @@ def test_seconds_nanoseconds(self): assert (1, int(5e8)) == Time(seconds=1, nanoseconds=5e8).seconds_nanoseconds() assert (1, int(5e8)) == Time(seconds=0, nanoseconds=15e8).seconds_nanoseconds() assert (0, 0) == Time().seconds_nanoseconds() + + def test_infinite_duration(self): + duration = Infinite + assert str(duration) == 'Infinite'