Skip to content

Commit dc39960

Browse files
committed
ros2: ensenso_camera: Fix ros2 python durations in compatibility layer
Replace the original compatibility function with duration_from_seconds. Reminder: This function returns a numeric value for ROS2, because there the Duration type is not (yet) supported in messages. The Duration keyword is now again available in the namespace. * 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 cd4f8e5 commit dc39960

4 files changed

Lines changed: 47 additions & 15 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/scripts/fit_primitive‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def _main(node_name):
9797
marker.id = marker_id
9898
marker.type = Marker.CUBE
9999
marker.action = Marker.ADD
100-
marker.lifetime = ros2py.Duration(1)
100+
marker.lifetime = ros2py.duration_from_seconds(1)
101101
marker.color.a = 1.0
102102
marker.color.r = 1.0
103103
marker.color.g = 0.0

‎ensenso_camera/scripts/pattern_marker‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _main(node_name):
5959
marker.id = i
6060
marker.type = Marker.CUBE
6161
marker.action = Marker.ADD
62-
marker.lifetime = ros2py.Duration(2)
62+
marker.lifetime = ros2py.duration_from_seconds(2)
6363

6464
marker.color.a = 1.0
6565
marker.color.r = 1.0

‎ensenso_camera/src/ensenso_camera/ros2.py‎

Lines changed: 38 additions & 8 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):
@@ -99,11 +99,29 @@ def result_callback(self, future):
9999
def get_response(self):
100100
return self._response
101101

102-
def Duration(sec):
103-
return sec
102+
def Duration(seconds):
103+
return rclpy.duration.Duration(seconds=seconds)
104104

105-
def sleep(node, secs):
106-
frequency = 1.0 / secs
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+
119+
def duration_from_seconds(seconds):
120+
return seconds
121+
122+
def sleep(node, duration_or_seconds):
123+
seconds = duration_to_seconds(duration_or_seconds)
124+
frequency = 1.0 / seconds
107125
rate = node.create_rate(frequency)
108126
rate.sleep()
109127

@@ -153,7 +171,7 @@ def wait_for_server(node, client, timeout_sec=None, exit=False):
153171
Returns True if the client established a server connection in the given amount of time, False otherwise.
154172
"""
155173
node.get_logger().info("Connecting to action server {} ...".format(client._action_name))
156-
if not client.wait_for_server(timeout_sec):
174+
if not client.wait_for_server(duration_to_seconds(timeout_sec)):
157175
node.get_logger().error(SERVER_TIMEOUT_ERROR_MESSAGE)
158176
if exit:
159177
sys.exit()
@@ -282,8 +300,20 @@ class Clock:
282300
def now(self):
283301
return rospy.Time.now()
284302

285-
def Duration(sec):
286-
return rospy.Duration(sec)
303+
def Duration(seconds):
304+
return rospy.Duration(seconds)
305+
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+
315+
def duration_from_seconds(sec):
316+
return Duration(sec)
287317

288318
def sleep(node, secs):
289319
rospy.sleep(secs)

0 commit comments

Comments
 (0)