Skip to content

Commit

Permalink
Return a ROSClock from Clock constructor if appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
dhood committed Aug 3, 2018
1 parent 6a9d42b commit 9992b1f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
10 changes: 6 additions & 4 deletions rclpy/rclpy/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ class ClockType(IntEnum):

class Clock:

def __init__(self, *, clock_type=ClockType.SYSTEM_TIME):
def __new__(cls, *, clock_type=ClockType.SYSTEM_TIME):
if not isinstance(clock_type, ClockType):
raise TypeError('Clock type must be a ClockType enum')
if clock_type is ClockType.ROS_TIME:
self = super().__new__(ROSClock)
else:
self = super().__new__(cls)
self._clock_handle = _rclpy.rclpy_create_clock(clock_type)
self._clock_type = clock_type
return self

@property
def clock_type(self):
Expand All @@ -55,9 +60,6 @@ def now(self):

class ROSClock(Clock):

def __init__(self):
super(ROSClock, self).__init__(clock_type=ClockType.ROS_TIME)

@property
def ros_time_is_active(self):
return _rclpy.rclpy_clock_get_ros_time_override_is_enabled(self._clock_handle)
Expand Down
8 changes: 1 addition & 7 deletions rclpy/rclpy/time_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# limitations under the License.

import builtin_interfaces.msg
from rclpy.clock import Clock
from rclpy.clock import ClockType
from rclpy.clock import ROSClock
from rclpy.time import Time

Expand Down Expand Up @@ -73,12 +71,8 @@ def detach_node(self):
self._node = None

def attach_clock(self, clock):
if not isinstance(clock, Clock):
raise TypeError('Clock must be of type rclpy.clock.Clock')
if not clock.clock_type == ClockType.ROS_TIME:
if not isinstance(clock, ROSClock):
raise ValueError('Only clocks with type ROS_TIME can be attached.')
# "Cast" to ROSClock subclass so ROS time methods can be used.
clock.__class__ = ROSClock
if self._last_time_set is not None:
self._set_clock(self._last_time_set, clock)
clock._set_ros_time_is_active(self.ros_time_is_active)
Expand Down
7 changes: 5 additions & 2 deletions rclpy/test/test_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from rclpy.clock import Clock
from rclpy.clock import ClockType
from rclpy.clock import ROSClock
from rclpy.time import Time


Expand All @@ -28,12 +29,14 @@ def test_clock_construction(self):
with self.assertRaises(TypeError):
clock = Clock(clock_type='STEADY_TIME')

clock = Clock(clock_type=ClockType.ROS_TIME)
assert clock.clock_type == ClockType.ROS_TIME
clock = Clock(clock_type=ClockType.STEADY_TIME)
assert clock.clock_type == ClockType.STEADY_TIME
clock = Clock(clock_type=ClockType.SYSTEM_TIME)
assert clock.clock_type == ClockType.SYSTEM_TIME
# A subclass ROSClock is returned if ROS_TIME is specified.
clock = Clock(clock_type=ClockType.ROS_TIME)
assert clock.clock_type == ClockType.ROS_TIME
assert isinstance(clock, ROSClock)

def test_clock_now(self):
# System time should be roughly equal to time.time()
Expand Down
6 changes: 1 addition & 5 deletions rclpy/test/test_time_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ def test_time_source_attach_clock(self):
# ROSClock is a specialization of Clock with ROS time methods.
time_source.attach_clock(ROSClock())

# A clock of type ROS_TIME can be attached. It will be converted to a ROSClock for storage.
time_source.attach_clock(Clock(clock_type=ClockType.ROS_TIME))

assert all((isinstance(clock, ROSClock) for clock in time_source._associated_clocks))

# Other clock types are not supported.
with self.assertRaises(ValueError):
time_source.attach_clock(Clock(clock_type=ClockType.SYSTEM_TIME))

Expand Down

0 comments on commit 9992b1f

Please sign in to comment.