建立工作空间,并在工作空间中建立src文件夹,返回ws文件夹,编译工作空间
mkdir ws/src
cd ws
catkin_make
进入src文件夹
cd src
建立程序包
catkin_create_pkg my_pkg std_msgs rospy roscpp
进入程序包中,在程序包中建立msg和srv文件夹,在msg文件夹中建立msg1.msg文件(空的即可,本例中不需要message类型),在srv文件夹中建立两个文件:srv1.srv和srv2.srv
srv1.srv中输入以下代码:
int32 A
int32 B
int32 C
---
int32 sum
srv2.srv中输入以下代码:
int32 A
int32 B
int32 C
---
int32 multiply
---表示分割客户端与服务器的数据类型,前面三个是客户端的请求的数据类型,后面是服务器的数据类型。
打开package.xml文件,把以下两句取消注释
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
打开CMakeList.txt文件,做如下修改
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
add_message_files(
FILES
msg1.msg
)
add_service_files(
FILES
srv1.srv
srv2.srv
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES my_pkg
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(request src/request.cpp) #这里括号里面,前面是节点名,后面是对应的cpp文件
add_executable(response src/response.cpp)
add_executable(response2 src/response2.cpp)
add_dependencies(request my_pkg_generate_messages_cpp)
add_dependencies(response my_pkg_generate_messages_cpp)
add_dependencies(response2 my_pkg_generate_messages_cpp)
target_link_libraries(request
${catkin_LIBRARIES}
)
target_link_libraries(response
${catkin_LIBRARIES}
)
target_link_libraries(response2
${catkin_LIBRARIES}
)
在ws/src/my_pkg/src中建立3个cpp文件,
request.cpp
#include "ros/ros.h"
#include "my_pkg/srv1.h"
#include "my_pkg/srv2.h"
#include <cstdlib>
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_3_ints_client"); // ""中为节点名
if (argc != 4)
{
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<my_pkg::srv1>("add_3_ints");
// "add_3_ints" 为客户端名
my_pkg::srv1 srv1;
srv1.request.A = 1;
srv1.request.B = 3;
srv1.request.C = 8;
client.call(srv1);
ROS_INFO("call srv1");
ROS_INFO("Sum: %ld", (long int)srv1.response.sum);
ros::ServiceClient client2 = n.serviceClient<my_pkg::srv2>("multiply_3_ints");
my_pkg::srv2 srv2;
srv2.request.A = 2;
srv2.request.B = 4;
srv2.request.C = 6;
client2.call(srv2);
ROS_INFO("call srv1");
ROS_INFO("Multiply: %ld", (long int)srv2.response.multiply);
}
return 0;
}
response.cpp
#include "ros/ros.h"
#include "my_pkg/srv1.h"
bool add(my_pkg::srv1::Request &req,
my_pkg::srv1::Response &res)
{
res.sum = req.A + req.B + req.C;
ROS_INFO("request: A=%ld, B=%ld C=%ld", (int)req.A, (int)req.B, (int)req.C);
ROS_INFO("sending back response: [%ld]", (int)res.sum);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_3_ints_server"); //"add_3_ints_server" 为节点名
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("add_3_ints", add); //"add_3_ints"为服务器名
ROS_INFO("Ready to add 3 ints.");
ros::spin();
return 0;
}
response2.cpp
#include "ros/ros.h"
#include "my_pkg/srv2.h"
bool multiply(my_pkg::srv2::Request &req,
my_pkg::srv2::Response &res)
{
res.multiply = req.A * req.B * req.C;
ROS_INFO("request: A=%ld, B=%ld C=%ld", (int)req.A, (int)req.B, (int)req.C);
ROS_INFO("sending back response: [%ld]", (int)res.multiply);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "multiply_3_ints_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("multiply_3_ints", multiply);
ROS_INFO("Ready to multiply 3 ints.");
ros::spin();
return 0;
}
返回工作空间目录,编译工作空间
catkin_make
启动ROS
roscore
打开新的终端,进入工作空间目录 ws,并且source一下
source devel/setup.bash
rosrun my_pkg response
再打开新的终端,进入工作空间目录 ws,并且source一下
source devel/setup.bash
rosrun my_pkg response2
再打开新的终端,进入工作空间目录 ws,并且source一下,即可运行。
source devel/setup.bash
rosrun my_pkg request
总结,先建立工作空间,编译工作空间,创建程序包,创建msg srv文件,修改package.xml文件和CMakeList.txt文件,编写cpp或py文件,重新编译工作空间,最后运行ros即可。