ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Service call from python hangs

asked 2023-04-05 01:23:20 -0600

ALNA_Perera gravatar image

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/2016... 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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-04-05 09:22:36 -0600

Per Edwardsson gravatar image

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.

edit flag offensive delete link more

Comments

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!

ALNA_Perera gravatar image ALNA_Perera  ( 2023-04-05 17:13:07 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2023-04-05 01:23:20 -0600

Seen: 544 times

Last updated: Apr 05 '23