Mastering ROS for Robotics Programming(Second Edition)
上QQ阅读APP看书,第一时间看更新

Building the ROS action server and client

After creating these two files in the src folder, we have to edit the package.xml and CMakeLists.txt to build the nodes.

The package.xml file should contain message generation and runtime packages, as we did for ROS service and messages.

We have to include the Boost library in CMakeLists.txt to build these nodes. Also, we have to add the action files that we wrote for this example:

find_package(catkin REQUIRED COMPONENTS 
 roscpp 
 rospy 
 std_msgs 
 actionlib  
 actionlib_msgs 
 message_generation 
) 

We should pass actionlib, actionlib_msgs, and message_generation in find_package():

## System dependencies are found with CMake's conventions 
find_package(Boost REQUIRED COMPONENTS system) 

We should add Boost as a system dependency:

## Generate actions in the 'action' folder 
 add_action_files( 
  FILES 
  Demo_action.action 
 ) 

We need to add our action file in add_action_files():

## Generate added messages and services with any dependencies listed here 
 generate_messages( 
  DEPENDENCIES 
  std_msgs 
  actionlib_msgs 
 ) 

We have to add actionlib_msgs in generate_messages():

catkin_package( 
 CATKIN_DEPENDS roscpp rospy std_msgs actionlib actionlib_msgs message_runtime  
) 
 
include_directories( 
 include 
 ${catkin_INCLUDE_DIRS} 
 ${Boost_INCLUDE_DIRS} 
) 

We have to add Boost to include the directory:

##Building action server and action client 
 
add_executable(demo_action_server src/demo_action_server.cpp) 
add_executable(demo_action_client src/demo_action_client.cpp) 
 
add_dependencies(demo_action_server mastering_ros_demo_pkg_generate_messages_cpp) 
add_dependencies(demo_action_client mastering_ros_demo_pkg_generate_messages_cpp) 
 
target_link_libraries(demo_action_server ${catkin_LIBRARIES} ) 
target_link_libraries(demo_action_client ${catkin_LIBRARIES}) 

After catkin_make, we can run these nodes using the following commands:

  • Run roscore:
    $ roscore  
  • Launch the action server node:
    $rosrun mastering_ros_demo_pkg demo_action_server  
  • Launch the action client node:
    $rosrun mastering_ros_demo_pkg demo_action_client 10 1  

The output of these process is shown as follows:

Figure 7: Running ROS actionlib server and client.