目录
- 如何实现一个客户端
- C++
- 创建功能包
- 编写程序
- 配置CMakeLists.txt
- 编译并运行
- python
- 创建并编写脚本
- 运行
平台:华硕 Thinker Edge R 瑞芯微 RK3399Pro
固件版本:Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615
记录自【古月居】古月·ROS入门21讲 | 一学就会的ROS机器人入门教程 —— 古月居GYH P13 13.客户端Client的编程实现
如何实现一个客户端
- 初始化ROS节点;
- 创建一个Client实例;
- 发布服务请求数据;
- 等待Server处理之后的应答结果。
C++
创建功能包
cd ~/catkin_workspace/src
catkin_create_pkg learning_service roscpp rospy std_msgs geometry_msgs turtlesim
编写程序
nano ~/catkin_workspace/src/learning_service/src/turtle_spawn.cpp
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************//*** 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn*/#include <ros/ros.h>
#include <turtlesim/Spawn.h>int main(int argc, char** argv)
{// 初始化ROS节点ros::init(argc, argv, "turtle_spawn");// 创建节点句柄ros::NodeHandle node;// 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的serviceros::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;
};
配置CMakeLists.txt
nano ~/catkin_workspace/src/learning_service/CMakeLists.txt
添加
add_executable(turtle_spawn src/turtle_spawn.cpp)
target_link_libraries(turtle_spawn ${catkin_LIBRARIES})
编译并运行
cd ~/catkin_workspace
catkin_make# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun turtlesim turtlesim_node
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_service turtle_spawn
python
创建并编写脚本
cd ~/catkin_workspace/src/learning_service
mkdir scripts
cd ~/catkin_workspace/src/learning_service/scripts
nano turtle_spawn.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-########################################################################
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
######################################################################### 该例程将请求/spawn服务,服务数据类型turtlesim::Spawnimport sys
import rospy
from turtlesim.srv import Spawndef turtle_spawn():# ROS节点初始化rospy.init_node('turtle_spawn')# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的servicerospy.wait_for_service('/spawn')try:add_turtle = rospy.ServiceProxy('/spawn', Spawn)# 请求服务调用,输入请求数据response = add_turtle(2.0, 2.0, 0.0, "turtle2")return response.nameexcept rospy.ServiceException, e:print "Service call failed: %s"%eif __name__ == "__main__":#服务调用并显示调用结果print "Spwan turtle successfully [name:%s]" %(turtle_spawn())
运行
sudo chmod +x ~/catkin_workspace/src/learning_service/scripts/turtle_spawn.py# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun turtlesim turtlesim_node
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_service turtle_spawn.py