Need advise on executers and clbkGroups
There exist two simple nodes, one as a server and the other as a client. The server node just gives service to the client node request. The client node has a timer that calls the request and it has to shows the response of the request.
class MinimalService(Node):
def __init__(self):
super().__init__('minimal_service')
self.srv = self.create_service(AddTwoInts, 'add_two_ints', self.add_two_ints_callback)
def add_two_ints_callback(self, request, response):
response.sum = request.a + request.b
self.get_logger().info('Incoming request\na: %d b: %d' % (request.a, request.b))
return response
def main(args=None):
rclpy.init(args=args)
minimal_service = MinimalService()
rclpy.spin(minimal_service)
rclpy.shutdown()
and the client code:
class MinimalClientAsync(Node):
def __init__(self):
super().__init__('minimal_client_async')
self.cli = self.create_client(AddTwoInts, 'add_two_ints')
while not self.cli.wait_for_service(timeout_sec=1.0):
self.get_logger().info('service not available, waiting again...')
self.req = AddTwoInts.Request()
self.grp_timer = MutuallyExclusiveCallbackGroup()
self.create_timer(1.0, self.send_request, callback_group=self.grp_timer)
def send_request(self):
self.req.a = 2
self.req.b = 3
self.future = self.cli.call_async(self.req)
rclpy.spin_until_future_complete(self, self.future)
try:
response = self.future.result()
except Exception as e:
self.get_logger().info(
'Service call failed %r' % (e,))
else:
self.get_logger().info(
'Result of add_two_ints: for %d + %d = %d' %
(self.req.a, self.req.b, response.sum))
def main(args=None):
rclpy.init(args=args)
minimal_client = MinimalClientAsync()
while rclpy.ok():
rclpy.spin_once(minimal_client)
minimal_client.destroy_node()
rclpy.shutdown()
These two are working now, but the problem is that I do not know exactly why? I appreciate if someone gives me some resources about callback group and executers. When I try to use the MultiThreadExecuter by creating the service instance in the client's main function the code stuck in a deadlock. Can somebody shows how I can use a multithread executer for these two codes?
Asked by hosh on 2020-07-11 17:29:43 UTC
Comments