[ROS in 5 mins] 035 – How to create a ROS Action Client

How to create a ROS Action Client

Written by Ruben Alves

10/09/2018

Hi ROS Developers,

in today’s post, we are going to learn how to create a ROS Action Client, how to call the Action Server and print the feedback and result messages.

Before we start, if you are new to ROS, I highly recommend you taking one of the following courses on Robot Ignite Academy:

Let’s get started.

Creating the ROS Action Server

In order to create a ROS Action Client, we first need to create the Action Server. We have already learned how to do that on the previous post: https://www.theconstruct.ai/ros-5-mins-033-create-ros-action-server/

Please make sure you follow that post first since the client we will create here will call that server.

Creating the ROS Action Client

If you followed the post on how to create an Action Server, we should have the following output after running the tree . command on the ~/catkin_ws/srcfolder.

user:~/catkin_ws/src$ tree .
.
├── actions_tutorial
│   ├── action
│   │   └── WashTheDishes.action
│   ├── CMakeLists.txt
│   ├── package.xml
│   ├── scripts
│   │   └── action_server.py
│   └── src
└── CMakeLists.txt -> /opt/ros/kinetic/share/catkin/cmake/toplevel.cmake

4 directories, 5 files

As you can see we have a scripts folder with our action_server.py there. We will create our action_client.py in the same folder. Let’s use the commands below for that:

cd ~/catkin_ws/src/actions_tutorial/scripts/
touch action_client.py
chmod +x action_client.py

With the code above we created an empty python file and gave it execute permissions. With the tree ~/catkin_ws/src command you should have something like:

tree ~/catkin_ws/src
/home/user/catkin_ws/src
├── actions_tutorial
│   ├── action
│   │   └── WashTheDishes.action
│   ├── CMakeLists.txt
│   ├── package.xml
│   ├── scripts
│   │   ├── action_client.py
│   │   └── action_server.py
│   └── src
└── CMakeLists.txt -> /opt/ros/kinetic/share/catkin/cmake/toplevel.cmake

4 directories, 6 files

Now let’s paste the following code on the action_client.py file:

#! /usr/bin/env python

import rospy
import actionlib
from actions_tutorial.msg import WashTheDishesAction, WashTheDishesGoal

def feedback_cb(msg):
 print 'Feedback received:', msg

def call_server():

    client = actionlib.SimpleActionClient('wash_dishes_as', WashTheDishesAction)

    client.wait_for_server()

    goal = WashTheDishesGoal()
    goal.number_of_minutes = 7

    client.send_goal(goal, feedback_cb=feedback_cb)

    client.wait_for_result()

    result = client.get_result()

    return result

if __name__ == '__main__':

    try:
        rospy.init_node('action_client')
        result = call_server()
        print 'The result is:', result
    except rospy.ROSInterruptException as e:
        print 'Something went wrong:', e

In this file what we do is basically call the Action Server defined in the previous post and print the feedback and the result messages sent by the server.

Running the ROS Action Client

With everything in place, let’s now launch our server and our client and see they work together. To launch the server we can use the command below:

source ~/catkin_ws/devel/setup.bash
rosrun actions_tutorial action_server.py

and in a different shell we run the next command:

source catkin_ws/devel/setup.bash
rosrun actions_tutorial action_client.py

Everything should have run smoothly and you should see in the output something like:

Feedback received: last_dish_washed: "bowl-0"
Feedback received: last_dish_washed: "bowl-1"
Feedback received: last_dish_washed: "bowl-2"
Feedback received: last_dish_washed: "bowl-3"
Feedback received: last_dish_washed: "bowl-4"
Feedback received: last_dish_washed: "bowl-5"
Feedback received: last_dish_washed: "bowl-6"
The result is: dishes_washed: [bowl-0, bowl-1, bowl-2, bowl-3, bowl-4, bowl-5, bowl-6]

So, congratulations, you now know how to create an Action Server as well as an Action Client.

To make your life easier, we have also prepared a video showing all the process explained in this post.

I hope you have enjoyed the post and the video, if so, please consider giving us a thumbs up. You can also subscribe to our channel and press the bell so that you won’t miss our updates.

We love feedback, so, whether you like the video or not, please share your thoughts on the comments section of the video.

Keep pushing your ROS Learning.

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This