注意:代碼可在github中找到
https://github.com/heyanwei/rosdemo
- 在工程目錄下增加srv的文件夾,並創建add_srv.srv文件,在裏面定義請求參數和返回結果
wilson@ubuntu:~/code/catkin/src/hello_demo$ ls
CMakeLists.txt include package.xml src srv
wilson@ubuntu:~/code/catkin/src/hello_demo$ cd srv/
wilson@ubuntu:~/code/catkin/src/hello_demo/srv$ ls
add_srv.srv
wilson@ubuntu:~/code/catkin/src/hello_demo/srv$ cat add_srv.srv
int32 A
int32 B
int32 C
---
int32 sum
- 在package.xml中增加以下兩行定義
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
- 在CMakeLists.txt中添加以下定義
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
## Generate services in the 'srv' folder
add_service_files(
FILES
add_srv.srv
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES hello_demo
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
- 返回工作空間進行編譯
wilson@ubuntu:~/code/catkin$ catkin_make
Base path: /home/wilson/code/catkin
Source space: /home/wilson/code/catkin/src
Build space: /home/wilson/code/catkin/build
Devel space: /home/wilson/code/catkin/devel
Install space: /home/wilson/code/catkin/install
- 此時,會產生請求信息的頭文件
wilson@ubuntu:~/code/catkin/devel/include/hello_demo$ ls
add_srv.h add_srvRequest.h add_srvResponse.h
- 編寫service代碼
#include "ros/ros.h"
#include "hello_demo/add_srv.h"
bool add(hello_demo::add_srv::Request &req,
hello_demo::add_srv::Response &resp)
{
resp.sum = req.A+req.B;
ROS_INFO("service add handle...");
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "service_add");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("serviceadd", add);
ros::spin();
return 0;
}
- 編寫客戶端代碼
#include "ros/ros.h"
#include "hello_demo/add_srv.h"
int main(int argc, char **argv)
{
ros::init(argc, argv, "client_add");
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<hello_demo::add_srv>("serviceadd");
hello_demo::add_srv srv;
srv.request.A = 1;
srv.request.B = 2;
if(client.call(srv))
{
ROS_INFO("client_add success, result: %d", srv.response.sum);
return 1;
}
else
{
ROS_INFO("client_add failed...");
return 0;
}
}
- 在CMakeLists.txt中添加以下定義
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(calcadd src/calcadd.cpp)
target_link_libraries(calcadd ${catkin_LIBRARIES})
add_dependencies(calcadd hello_demo_generate_messages_cpp)
add_executable(clientadd src/clientadd.cpp)
target_link_libraries(clientadd ${catkin_LIBRARIES})
add_dependencies(clientadd hello_demo_generate_messages_cpp)
- 返回工作空間進行編譯
wilson@ubuntu:~/code/catkin$ catkin_make
Base path: /home/wilson/code/catkin
Source space: /home/wilson/code/catkin/src
Build space: /home/wilson/code/catkin/build
Devel space: /home/wilson/code/catkin/devel
Install space: /home/wilson/code/catkin/install
- 運行roscore
wilson@ubuntu:~/code/catkin$ roscore
... logging to /home/wilson/.ros/log/d5fa44f2-3d2e-11ea-ba87-000c29eec410/roslaunch-ubuntu-21874.log
- 激活並運行服務
wilson@ubuntu:~/code/catkin$ source devel/setup.bash
wilson@ubuntu:~/code/catkin$ rosrun hello_demo calcadd
[ INFO] [1579708280.921439498]: service add handle...
- 激活並運行客戶端
wilson@ubuntu:~/code/catkin$ rosrun hello_demo clientadd
[ INFO] [1579708288.662391813]: client_add success, result: 3