ros2 action client waiting for result

asked 2022-02-08 13:14:44 -0500

Flash gravatar image

updated 2022-02-08 14:12:08 -0500

Hi all, I am using Depth First search algorithm where I try to call an action. I would like the algorithm to wait until I get the required response from the server, but my action client calls the server but doesn't wait for the response or required result. I am trying to understand how the action client and server works. Any help would be greatly appreciated. Here is the code for action client

class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('minimal_publisher')
        self.subscription = self.create_subscription(
            String,
            '/algo',
            self.listener_callback,
            10)

        self._action_client = ActionClient(self, Gotopoint, 'moving_robot')
        self.direction = [(-1,0),(0,1),(1,0),(0,-1)]

    def listener_callback(self, msg):
        self.algo_floor(0, 0, 0)
        self.get_logger().info('Floor Cleaning successful')

    def move_robot(self, x, y, d): #Asking robot to move 
        self.get_logger().info('Moving robot to x:%s, y:%s, d:%s' %(x,y,d))
        #call service/action to wait until robot is moved to that location
        goal_msg = Gotopoint.Goal()
        goal_msg.goal = [int(x), int(y), int(d)]
        self._action_client.wait_for_server()
        self._send_goal_future = self._action_client.send_goal_async(goal_msg, feedback_callback=self.feedback_callback)
        self._send_goal_future.add_done_callback(self.goal_response_callback)

    def goal_response_callback(self, future):
        goal_handle = future.result()
        if not goal_handle.accepted:
            self.get_logger().info('Goal rejected ')
            return
        self.get_logger().info('Goal accepted')

        self._get_result_future = goal_handle.get_result_async()
        self._get_result_future.add_done_callback(self.get_result_callback)

    def get_result_callback(self, future):
        result = future.result().result
        self.get_logger().info('result: {0}'.format(result.robot_objective))

    def feedback_callback(self, feedback_msg):
        feedback = feedback_msg.feedback
        self.get_logger().info('Received feedback: {0}'.format(feedback.robot_feedback))


    def algo_floor(self, robot_grid_x, robot_grid_y, robot_t): 
        for i in range(4):
            new_d = (robot_t+i)%4
            self.move_robot(new_x, new_y,new_d)
            self.algo_floor(new_x, new_y, new_d)


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

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

And here is my action server

class GoalActionServer(Node):

    def __init__(self):
        super().__init__('goal_action_server')
        self._action_server = ActionServer(self,Gotopoint,'moving_robot',self.execute_callback)

    def execute_callback(self, goal_handler):
        self.get_logger().info('Executing goal...%s'%goal_handler.request.goal)

        # feedback_msg = Gotopoint.Feedback()
        # feedback_msg.robot_feedback = goal_handler.request.goal
        # goal_handler.publish_feedback(feedback_msg)
        time.sleep(5)
        goal_handler.succeed()
        result = Gotopoint.Result()
        # result.robot_objective = feedback_msg.robot_feedback
        return result

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

    goal_action_server = GoalActionServer()

    rclpy.spin(goal_action_server)

    goal_action_server.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()
edit retag flag offensive close merge delete