Creating ROS1 Action
Below are the steps to create a ROS1 action:
Step 1: Add the following in package.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<build_depend>roscpp</build_depend> # Use this if the action is in C++ <build_depend>rospy</build_depend> # Use this if the action is in Python <build_depend>actionlib</build_depend> <build_depend>std_msgs</build_depend> <build_depend>message_generation</build_depend> <build_depend>actionlib_msgs</build_depend> <run_depend>roscpp</run_depend> # Use this if the action is in C++ <run_depend>rospy</run_depend> # Use this if the action is in Python <run_depend>actionlib</run_depend> <run_depend>std_msgs</run_depend> <run_depend>message_runtime</run_depend> <run_depend>actionlib_msgs</run_depend> |
Step 2: Add the following in CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
find_package(catkin REQUIRED COMPONENTS roscpp # Use this if the action is in C++ rospy # Use this if the action is in Python std_msgs message_generation actionlib_msgs actionlib ) add_action_files(FILES ActionFile.action) generate_messages(DEPENDENCIES actionlib_msgs std_msgs) catkin_package( LIBRARIES action_name CATKIN_DEPENDS std_msgs actionlib_msgs actionlib roscpp ) include_directories(${catkin_INCLUDE_DIRS}) # SKIP ALL BELOW IF THE ACTION SERVER & CLIENT ARE WRITTEN IN PYTHON add_executable(action_server src/action_server.cpp) add_dependencies(action_server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) target_link_libraries(action_server ${catkin_LIBRARIES}) add_executable(action_client src/action_client.cpp) add_dependencies(action_client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) target_link_libraries(action_client ${catkin_LIBRARIES}) |
Step 3: Create a folder called “action” (if you still don’t have it) inside your package folder. And then make the action file (.action) called “ActionFile.action”. For example, if the action is to compute Fibonacci numbers, you may call it “Fibonacci.action” which looks like below:
1 2 3 4 5 6 7 8 |
#goal definition int32 order --- #result definition int32[] sequence --- #feedback int32[] sequence |
The lines of three dashes above separate between the request variable(s), the result variable(s), and the feedback variable(s).
Step 4: Write the “action_server” node.
Using C++, write the action_server node in a file namely “action_server.cpp”, inside “src” folder of the package. An code example can be found here: http://wiki.ros.org/actionlib_tutorials/Tutorials/SimpleActionServer%28ExecuteCallbackMethod%29
Using Python, write the action_server node in a file namely “action_server.py”, inside “script” folder of the package. An code example can be found here: http://wiki.ros.org/actionlib_tutorials/Tutorials/Writing%20a%20Simple%20Action%20Server%20using%20the%20Execute%20Callback%20%28Python%29
Step 5: Write the “action_client” node.
Using C++, write the action_client node in a file namely “action_client.cpp”, inside “src” folder of the package. An code example can be found here: http://wiki.ros.org/actionlib_tutorials/Tutorials/SimpleActionClient
Using Python, write the action_client node in a file namely “action_server.py”, inside “script” folder of the package. An code example can be found here: http://wiki.ros.org/actionlib_tutorials/Tutorials/Writing%20a%20Simple%20Action%20Client%20%28Python%29
Step 6: Build the package by using “catkin_make” command.
Running the action
After making sure that ROS Master is running (unless roslaunch is used), type the following in the terminal to run the “action_server” node:
1 2 |
For C++ action: rosrun package_name action_server For Python action: rosrun package_name action_server.py |
To call the action, run the “action_client” node:
1 2 |
For C++ action: rosrun package_name action_client For Python action: rosrun package_name action_client.py |
To show the stream of the action feedback:
1 |
rostopic echo /action_name/feedback |