SICKAG / sick_scan_xd

Based on the sick_scan drivers for ROS1, sick_scan_xd merges sick_scan, sick_scan2 and sick_scan_base repositories. The driver supports both Linux (native, ROS1, ROS2) and Windows (native and ROS2).
Apache License 2.0
99 stars 84 forks source link

How to call service from Python using ros2? #189

Closed Kaju-Bubanja closed 1 year ago

Kaju-Bubanja commented 1 year ago

Looking at this guide it looks like we need to import from from sick_scan.srv._cola_msg_srv import ColaMsgSrv_Request but the underscore indicates that this method shouldn't be used and is private, also the ColaMsgSrv_Request looks auto generated and not meant for programmatic use. Which brings me to my question, how would I call the /ColaMsg service from python instead of the cli?

Kaju-Bubanja commented 1 year ago

While the _ indicates that these methods should not be used, here is an example how I got it working:

from sick_scan.srv._cola_msg_srv import ColaMsgSrv, ColaMsgSrv_Request
...
service_client = my_ros_node.create_client(ColaMsgSrv, "/ColaMsg")
future = service_client.call_async(ColaMsgSrv_Request(request='sRN DeviceIdent'))
rclpy.spin_until_future_complete(my_ros_node, future)
result = future.result()
print(result)