1、创建功能包
进入工作空间catkin_ws/src下面打开功能包
chen@ubuntu:~/catkin_ws/src$ catkin_create_pkg learning_service roscpp rospy std_msgs geometry_msgs turtlesim
usage: catkin_create_pkg [-h] [--meta] [-s [SYS_DEPS [SYS_DEPS ...]]]
[-b [BOOST_COMPS [BOOST_COMPS ...]]] [-V PKG_VERSION]
[-D DESCRIPTION] [-l LICENSE] [-a AUTHOR]
[-m MAINTAINER] [--rosdistro ROSDISTRO]
name [dependencies [dependencies ...]]
catkin_create_pkg: error: File exists: /home/chen/catkin_ws/src/learning_service/package.xml
需要依赖的第三方功能包:roscpp rospy std_msge geometry_msgs turtlesim
2、创建客户端代码C++
把客户端代码放入功能包的src中
/
**
* 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn
*/
#include
#include
int main(int argc, char** argv)
{
// 初始化ROS节点
ros::init(argc, argv, "turtle_spawn");
// 创建节点句柄
ros::NodeHandle node;
// 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
ros::service::waitForService("/spawn");
ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");
// 初始化turtlesim::Spawn的请求数据
turtlesim::Spawn srv;
srv.request.x = 2.0;
srv.request.y = 2.0;
srv.request.name = "turtle2";
// 请求服务调用
ROS_INFO("Call service to spwan turtle[x:%0.6f, y:%0.6f, name:%s]",
srv.request.x, srv.request.y, srv.request.name.c_str());
add_turtle.call(srv);
// 显示服务调用结果
ROS_INFO("Spwan turtle successfully [name:%s]", srv.response.name.c_str());
return 0;
};
add_executable(turtle_spawn src/turtle_spawn.cpp)
target_link_libraries(turtle_spawn ${catkin_LIBRARIES})
4、编译并运行客户端
chen@ubuntu:~/catkin_ws$ catkin_make
在devel中编译生成的可执行文件
5.运行
5.1、运行roscore
5.2运行仿真器显示节点
chen@ubuntu:~$ rosrun turtlesim
draw_square mimic turtlesim_node turtle_teleop_key
chen@ubuntu:~$ rosrun turtlesim turtlesim_node
[ INFO] [1582773496.228019937]: Starting turtlesim with node name /turtlesim
[ INFO] [1582773496.232371953]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]
5.3 运行客户端节点
按tab键补全,出现两只小海龟
chen@ubuntu:~$ rosrun learning_service turtle_spawn
[ INFO] [1582773599.365576304]: Call service to spwan turtle[x:2.000000, y:2.000000, name:turtle2]
//请求service
[ INFO] [1582773599.385445464]: Spwan turtle successfully [name:turtle2]
//应答
同时仿真器显示客户端界面也会出现海龟2信息
[ INFO] [1582773599.379247073]: Spawning turtle [turtle2] at x=[2.000000], y=[2.000000], theta=[0.000000]
二、14服务端Server的编程实现
1.1创建服务器代码(C++)
查看消息内容chen@ubuntu:~$ rossrv show std_srvs/Trigger
rossrv show std_srvs/Trigger`
1.2配置服务器代码编译规则
1.3编译并运行服务器
查看需不需source ,或者功能包找不到
vi .bashrc
添加下面脚本
配置编译规则
add_executable(turtle_command_server src/turtle_command_server.cpp)
target_link_libraries(turtle_command_server ${catkin_LIBRARIES})
打开服务器终端节点
chen@ubuntu:~$ rosrun learning_service turtle_command_server
[ INFO] [1582776144.242303889]: Ready to receive turtle command.
向server发送请求,让海龟动
chen@ubuntu:~$ rosservice call /turtle_command "{}"
enter后
chen@ubuntu:~$ rosservice call /turtle_command "{}"
success: True
message: "Change turtle command state!"
停下来
chen@ubuntu:~$ rosservice call /turtle_command "{}"
success: True
message: "Change turtle command state!"
三、服务数据的定义与使用
1.1自定义服务数据
通过"—“区分,”—“以上为request ,”—"以下为response.
创建srv文件夹和Person.srv文件,并写入内容
chen@ubuntu:~/catkin_ws/src/learning_service$ mkdir srv/
chen@ubuntu:~/catkin_ws/src/learning_service$ cd srv/
chen@ubuntu:~/catkin_ws/src/learning_service/srv$ touch Person.srv
chen@ubuntu:~/catkin_ws/src/learning_service/srv$
内容:
string name
uint8 sex
uint8 age
uint8 unknown=0
uint8 male =1
uint8 female =2
//以上通过client发给sever
---
string result
1.2在package.xml中添加功能包依赖
通过find_package找到这个文件,作为功能包的一个依赖。
message_generation
下面代码是为了让编译器知道,要根据 Person.srv文件来产生对应的头文件
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# geometry_msgs# std_msgs
# )
add_service_files(FILES Person.srv)
generate_messages(DEPENDENCIES std_msgs)
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
```bash
add_message_files(FILES Person.srv)
generate_messages(DEPENDENCIES std_msgs)
```bash
message_generation
message_runtime
CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_runtime
add_executable(turtle_command_server src/turtle_command_server.cpp)
target_link_libraries(turtle_command_server ${catkin_LIBRARIES})
add_executable(turtle_spawn src/turtle_spawn.cpp)
target_link_libraries(turtle_spawn ${catkin_LIBRARIES})
编译生成的头文件,request和response单独分开,并有一个总的头文件,C++文件
2 创建服务器代码 C++
3、配置服务器/客户端代码编译规则
add_executable(person_client src/person_client.cpp)
target_link_libraries(person_client ${catkin_LIBRARIES})
add_dependencies(person_client ${PROJECT_NAME}_generate_messages_cpp)
add_executable(person_server src/person_server.cpp)
target_link_libraries(person_server ${catkin_LIBRARIES})
add_dependencies(person_server ${PROJECT_NAME}_generate_messages_cpp)
再次编译:
chen@ubuntu:~/catkin_ws$ catkin_make
4、运行
4.1、rescore
4.2、运行server端
chen@ubuntu:~$ rosrun learning_service person_server
[ INFO] [1582788002.519354137]: Ready to show person informtion.
4.3、运行client端
chen@ubuntu:~$ rosrun learning_service person_client
[ INFO] [1582788108.790908017]: Call service to show person[name:Tom, age:20, sex:1]
[ INFO] [1582788108.794107997]: Show person result : OK
客户端请求数据,收到后显示ok
server端发送如下 :
chen@ubuntu:~$ rosrun learning_service person_server
[ INFO] [1582788002.519354137]: Ready to show person informtion.
[ INFO] [1582788108.792867917]: Person: name:Tom age:20 sex:1