rosros 0.2.5
Simple unified interface to ROS1 / ROS2 Python API
Loading...
Searching...
No Matches
clock.py
Go to the documentation of this file.
1"""
2Partial stand-in for `rclpy.clock` in ROS1, providing `ClockType` and `Clock`.
3
4------------------------------------------------------------------------------
5This file is part of rosros - simple unified interface to ROS1 / ROS2.
6Released under the BSD License.
7
8@author Erki Suurjaak
9@created 14.02.2022
10@modified 15.04.2022
11------------------------------------------------------------------------------
12"""
13## @namespace rosros.rclify.clock
14from enum import IntEnum
15import time
16
17import rospy
18
19# from .. import patch # Imported late to avoid circular import
20
21
22class ClockType(IntEnum):
23 """Enum for clock type."""
24
25
26 ROS_TIME = 1
27
28
29 SYSTEM_TIME = 2
30
31
32 STEADY_TIME = 3
33
34
35class Clock:
36 """Simple clock interface mimicking `rclpy.clock.Clock`, only providing `now()`."""
37
38 def __init__(self, *, clock_type=ClockType.SYSTEM_TIME):
39 """Raises error if unknown clock type."""
40 if not isinstance(clock_type, ClockType):
41 raise TypeError("Clock type must be a ClockType enum")
42 self.clock_type = clock_type
43
44 def __repr__(self):
45 return "Clock(clock_type={0})".format(self.clock_type.name)
46
47 def now(self):
48 """Returns `rospy.Time` according to clock type."""
49 if ClockType.ROS_TIME == self.clock_type:
50 result = rospy.get_rostime()
51 elif ClockType.STEADY_TIME == self.clock_type:
52 result = rospy.Time(nsecs=time.monotonic_ns())
53 else: # ClockType.SYSTEM_TIME
54 result = rospy.Time(nsecs=time.time_ns())
55 from .. import patch # Imported late to avoid circular import
56 patch.set_extra_attribute(result, "_clock_type", self.clock_type)
57 return result
58
59
60__all__ = ["ClockType", "Clock"]
Simple clock interface mimicking `rclpy.clock.Clock`, only providing `now()`.
Definition clock.py:35
__init__(self, *clock_type=ClockType.SYSTEM_TIME)
Raises error if unknown clock type.
Definition clock.py:39
now(self)
Returns `rospy.Time` according to clock type.
Definition clock.py:47
Enum for clock type.
Definition clock.py:22