Skip to content

Commit 71f87a7

Browse files
committed
ros2: ensenso_camera: Fix ros2 python durations in compatibility layer
* Implement add and sub functions for durations, because ROS2 does not support them yet. * Implement helper function duration_to_seconds becuase ROS2 durations only return nanoseconds. ROS1: * Sleep accepts both numeric values and Duration objects * Arithmetic on Time objects results in Duration objects * Arithmetic on Duration objects results in Duration objects ROS2: * Sleep not existing, implemented in our ros2.py * Arithmetic on Time objects results in Duration objects * Arithmetic on Duration objects not implemented yet * Overloads implemented in rclpy #1387 (next release)
1 parent 4cfe726 commit 71f87a7

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

ensenso_camera/scripts/calibrate_handeye

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def calibrate_hand_eye(node, client):
4141
def _main(node_name):
4242
node = ros2py.create_node(node_name, args=sys.argv)
4343

44-
capture_wait = ros2py.get_param(node, "capture_wait", 10.0)
44+
capture_wait = ros2py.Duration(ros2py.get_param(node, "capture_wait", 10.0))
4545
count_poses = max(ros2py.get_param(node, "count_poses", 5), 6)
4646
timeout = ros2py.get_param(node, "timeout", 60)
4747

@@ -61,12 +61,14 @@ def _main(node_name):
6161
break
6262

6363
# ---- Move the robot to the next position.
64-
time_until_capture = capture_wait - (node.get_clock().now() - start_capture)
65-
while time_until_capture > 0.8:
64+
time_until_capture = ros2py.sub_durations(capture_wait, (node.get_clock().now() - start_capture))
65+
while time_until_capture > ros2py.Duration(0.8):
6666
start_sleep = node.get_clock().now()
67-
node.get_logger().info("Capturing next pattern in {}s.".format(time_until_capture))
67+
node.get_logger().info(
68+
"Capturing next pattern in {}s".format(ros2py.duration_to_seconds(time_until_capture))
69+
)
6870
ros2py.sleep(node, 0.75)
69-
time_until_capture -= node.get_clock().now() - start_sleep
71+
time_until_capture = ros2py.sub_durations(time_until_capture, (node.get_clock().now() - start_sleep))
7072
ros2py.sleep(node, time_until_capture)
7173

7274
calibrate_hand_eye(node, calibrate_hand_eye_client)

ensenso_camera/src/ensenso_camera/ros2.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def send_goal(self):
7979
def wait_for_response(self, timeout_sec=None):
8080
# Separate sending goal and waiting for response so that we can handle several clients at once by sending
8181
# all the goals first and then waiting until all clients have received the response.
82-
self._event.wait(timeout_sec)
82+
self._event.wait(duration_to_seconds(timeout_sec))
8383
self._response = ActionResponse(self._result.status, self._result.result)
8484

8585
def response_callback(self, future):
@@ -102,11 +102,26 @@ def get_response(self):
102102
def Duration(seconds):
103103
return rclpy.duration.Duration(seconds=seconds)
104104

105+
def add_durations(a, b):
106+
return rclpy.duration.Duration(nanoseconds=a.nanoseconds + b.nanoseconds)
107+
108+
def sub_durations(a, b):
109+
return rclpy.duration.Duration(nanoseconds=a.nanoseconds - b.nanoseconds)
110+
111+
def duration_to_seconds(duration_or_seconds):
112+
if duration_or_seconds is None:
113+
return None
114+
elif isinstance(duration_or_seconds, rclpy.duration.Duration):
115+
return duration_or_seconds.nanoseconds / 1e9
116+
else:
117+
return duration_or_seconds
118+
105119
def duration_from_seconds(seconds):
106120
return seconds
107121

108-
def sleep(node, secs):
109-
frequency = 1.0 / secs
122+
def sleep(node, duration_or_seconds):
123+
seconds = duration_to_seconds(duration_or_seconds)
124+
frequency = 1.0 / seconds
110125
rate = node.create_rate(frequency)
111126
rate.sleep()
112127

@@ -156,7 +171,7 @@ def wait_for_server(node, client, timeout_sec=None, exit=False):
156171
Returns True if the client established a server connection in the given amount of time, False otherwise.
157172
"""
158173
node.get_logger().info("Connecting to action server {} ...".format(client._action_name))
159-
if not client.wait_for_server(timeout_sec):
174+
if not client.wait_for_server(duration_to_seconds(timeout_sec)):
160175
node.get_logger().error(SERVER_TIMEOUT_ERROR_MESSAGE)
161176
if exit:
162177
sys.exit()
@@ -288,6 +303,15 @@ def now(self):
288303
def Duration(seconds):
289304
return rospy.Duration(seconds)
290305

306+
def add_durations(a, b):
307+
return a + b
308+
309+
def sub_durations(a, b):
310+
return a - b
311+
312+
def duration_to_seconds(d):
313+
return d.to_sec()
314+
291315
def duration_from_seconds(sec):
292316
return Duration(sec)
293317

0 commit comments

Comments
 (0)