0
$\begingroup$

Rosanswers logo

Hi,

I am trying to call an existing service (/reinitialise_global_localization from AMCL). I tried creating a client node, but the client gets to request and hangs. I am not sure what is wrong.

The code is given below. It prints '1', then '2', and then, nothing. There are no errors.

If possible, I would like to find a solution like https://answers.ros.org/question/201617/how-to-call-a-service-inside-a-node/ but I can't seem to translate that to ros2 either.

Thanks

    import rclpy
    from rclpy.node import Node
    
    from std_srvs.srv import Empty
    
    
    class MinimalClientAsync(Node):
    
        def __init__(self):
            print("1")
            super().__init__('minimal_client_async')
            self.cli = self.create_client(Empty, 'reinitialize_global_localization')
            while not self.cli.wait_for_service(timeout_sec=1.0):
                self.get_logger().info('service not available, waiting again...')
            print("2")
            self.req = Empty.Request()

    def send_request(self):
        print("3")
        self.future = self.cli.call_async(self.req)
        rclpy.spin_until_future_complete(self, self.future)
        return self.future.result()


def main(args=None):
    rclpy.init(args=args)

    minimal_client = MinimalClientAsync()
    
    

    rclpy.spin_once(minimal_client)
    print ("4")

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_client.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

Originally posted by ALNA_Perera on ROS Answers with karma: 33 on 2023-04-05

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Do you call self.send_request at some point? I can't see that you do. I also believe you should rclpy.spin rather than rclpy.spin_once in main.


Originally posted by Per Edwardsson with karma: 501 on 2023-04-05

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by ALNA_Perera on 2023-04-05:
Turns out, I didn't have to call rclpy.spin(). You were right about me not calling self.send_request, calling it solved the problem. Thanks!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.