首先, 安装就不说了, 戳这里:
http://wiki.ros.org/cn/ROS/Tutorials
中文的哦.
装好后, 第一时间创建一个catkin的workspace:
source /opt/ros/kinetic/setup.bash
执行环境变量的脚本.
mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src
创建workspace名字叫catkin_ws
对空目录进行初始化:
cd ~/catkin_ws/ catkin_make
看看是否目录是否被成功加入了, 不然的话ros不认识你的包
source devel/setup.bash echo $ROS_PACKAGE_PATH
会看到workspace被加进去了.
接着是建第一包:
cd ~/catkin_ws/src catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
把包加进环境变量中:
. ~/catkin_ws/devel/setup.bash
查看包的依赖:
rospack depends1 beginner_tutorials
然后建srv, 这个srv具体是个什么鬼, 我还没搞明白.
roscd beginner_tutorials mkdir srv
去包目录, 建srv目录.
然后从别的地方拷贝一个srv过来:
roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
内容其实很简单:
topeet@ubuntu:~/catkin_ws/src/beginner_tutorials/srv$ cat AddTwoInts.srv
int64 a
int64 b
---
int64 sum
接着修改CMakeLists.txt, 增加对message-generation的依赖:
去掉下面的注释:
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
跟这个注释:
add_service_files( FILES AddTwoInts.srv )
记得将srv名字改成你需要的srv文件名字.
检查srv内容:
rossrv show <service type>
把这个注释也去掉:
generate_messages( DEPENDENCIES std_msgs )
然后重新编译一下:
# In your catkin workspace $ cd ../.. $ catkin_make $ cd -
接下来是写服务器端:
cd ~/catkin_ws/src/beginner_tutorials vi src/add_two_ints_server.cpp
服务器端内容是:
#include "ros/ros.h" #include "beginner_tutorials/AddTwoInts.h" bool add(beginner_tutorials::AddTwoInts::Request &req, beginner_tutorials::AddTwoInts::Response &res) { res.sum = req.a + req.b; ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b); ROS_INFO("sending back response: [%ld]", (long int)res.sum); return true; } int main(int argc, char **argv) { ros::init(argc, argv, "add_two_ints_server"); ros::NodeHandle n; ros::ServiceServer service = n.advertiseService("add_two_ints", add); ROS_INFO("Ready to add two ints."); ros::spin(); return 0; }
然后是客户端:
src/add_two_ints_client.cpp
#include "ros/ros.h" #include "beginner_tutorials/AddTwoInts.h" #include <cstdlib> int main(int argc, char **argv) { ros::init(argc, argv, "add_two_ints_client"); if (argc != 3) { ROS_INFO("usage: add_two_ints_client X Y"); return 1; } ros::NodeHandle n; ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints"); beginner_tutorials::AddTwoInts srv; srv.request.a = atoll(argv[1]); srv.request.b = atoll(argv[2]); if (client.call(srv)) { ROS_INFO("Sum: %ld", (long int)srv.response.sum); } else { ROS_ERROR("Failed to call service add_two_ints"); return 1; } return 0; }
将下面的内容加入到CMakeLists.txt里面:
add_executable(add_two_ints_server src/add_two_ints_server.cpp) target_link_libraries(add_two_ints_server ${catkin_LIBRARIES}) add_dependencies(add_two_ints_server beginner_tutorials_gencpp) add_executable(add_two_ints_client src/add_two_ints_client.cpp) target_link_libraries(add_two_ints_client ${catkin_LIBRARIES}) add_dependencies(add_two_ints_client beginner_tutorials_gencpp)
开始编译:
# In your catkin workspace cd ~/catkin_ws catkin_make
运行服务端:
rosrun beginner_tutorials add_two_ints_server
客户端:
rosrun beginner_tutorials add_two_ints_client 1 3
ok, 接下来是建消息服务.