Creating ROS1 Action
Below are the steps to create a ROS1 action:
Step 1: Add the following in package.xml
roscpp # Use this if the action is in C++rospy # Use this if the action is in Pythonactionlib std_msgs message_generation actionlib_msgs roscpp # Use this if the action is in C++rospy # Use this if the action is in Pythonactionlib std_msgs message_runtime actionlib_msgs
Step 2: Add the following in CMakeLists.txt
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:
#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:
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:
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:
rostopic echo /action_name/feedback
