ROS study notes eight (programming implementation of the client client)

Service model

The picture below is from Gu Yue's "21 Lectures on ROS Introduction"
Insert picture description here

Through the ROS Master management node, the client turtle_spawn issues a request, generates a new turtle, sends it to the server turtlesim, and the server processes the request and returns a response indicating the processing result.

Create feature pack

$ cd~/catkin_ws/src
$ catkin_create_pkg learning_service roscpp rospy std_msgs geometry_msgs turtlesim

Create client code (C++)

/**
 * 该例程将请求/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服务后,创建一个服务客户端,连接名为/spwan的service
	
	//阻塞性函数,经会一直等待直到系统出现/spawn服务
	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 spawn 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("Spawn turtle sucessfully [name:%s]", srv.response.name.c_str());
	
	return 0;
	
}

Process combing

  • Initialize the ROS node
  • Create a Client instance
  • Publish service request data
  • Waiting for the response result after Server processing

Configure client code compilation rules

  • Set the code to be compiled and the executable file generated
  • Set up the link library
add_executable(turtle_spwan src/turtle_spawn.cpp)
target_link_libraries(turtle_spawn ${catkin_LIBRARIES})

Compile and run the client

$ cd~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun learning_service turtle_spawn

The results are as follows:
Insert picture description here

Client code (Python)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将请求/spawn服务,服务数据类型turtlesim::Spawn

import sys
import rospy
from turtlesim.srv import Spawn

def turtle_spawn():
	# ROS节点初始化
    rospy.init_node('turtle_spawn')

	# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
    rospy.wait_for_service('/spawn')
    try:
        add_turtle = rospy.ServiceProxy('/spawn', Spawn)

		# 请求服务调用,输入请求数据
        response = add_turtle(2.0, 2.0, 0.0, "turtle2")
        return response.name
    except rospy.ServiceException, e:
        print "Service call failed: %s"%e

if __name__ == "__main__":
	#服务调用并显示调用结果
    print "Spwan turtle successfully [name:%s]" %(turtle_spawn())


Part of the code in this article comes from Gu Yue "21 Lectures on ROS Introduction"

Previous link

ROS study notes seven (definition and use of topic messages)
ROS study notes six (programming implementation of subscribers)
ROS study notes five (programming implementation of publishers)
ROS study notes four (creating workspaces and functional packages)
ROS learning Note three (use of ROS command line tools)
ROS study notes two (core concepts of ROS )
ROS study notes one (basic Linux system operation)

Guess you like

Origin blog.csdn.net/weixin_44911075/article/details/114222595