Service service is also a common communication method between ros nodes. Different from the topic, the service has a server and a client. The client requests the server to provide services. After the server completes the service, it needs to return the service results. Also called a response. In this lesson, we will explain how to write a client node program.
The general creation steps are as follows:
In order to distinguish it from the learn_topic function package, we re-create a function package learn_service and enter it in the terminal.
cd ~/ros_ws/srccatkin_create_pkg learn_service std_msgs rospy roscpp geometry_msgs turtlesimThen compile,
xxxxxxxxxxcd ~/ros_wscatkin_makeIn the src folder of the function package learn_service, create a C++ file (the file suffix is .cpp), name it a_new_turtle.cpp, and paste the following content into a_new_turtle.cpp,
xxxxxxxxxx/*** This routine will request the /spawn service in the little turtle node, and a new little turtle will appear at the specified location.*/int main(int argc, char** argv){ ros::init(argc, argv, "a_nes_turtle");// Initialize ROS node ros::NodeHandle node; ros::service::waitForService("/spawn"); // Wait/spawn service ros::ServiceClient new_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");//Create a service client and connect to the service named /spawn // Initialize turtlesim::Spawn's request data turtlesim::Spawn new_turtle_srv; new_turtle_srv.request.x = 6.0; new_turtle_srv.request.y = 8.0; new_turtle_srv.request.name = "turtle2"; // Request the service to pass in the xy position parameters and name parameters ROS_INFO("Call service to create a new turtle name is %s,at the x:%.1f,y:%.1f", new_turtle_srv.request.name.c_str(), new_turtle_srv.request.x,new_turtle_srv.request.y); new_turtle.call(new_turtle_srv); //Request service ROS_INFO("Spwan turtle successfully [name:%s]", new_turtle_srv.response.name.c_str());// Display service call results return 0;};Configure in CMakelist.txt, under the build area, add the following content,
xxxxxxxxxxadd_executable(a_new_turtle src/a_new_turtle.cpp)target_link_libraries(a_new_turtle ${catkin_LIBRARIES})add_executable shows that the generated executable program file is a_new_turtle, and the compiled source code is a_new_turtle.cpp in the src directory.
target_link_libraries specifies the libraries that need to be linked when compiling and generating an executable file.
Terminal input,
xxxxxxxxxxcd ~/ros_wscatkin_make
After the compilation is passed, you need to re-source the current environment variables to find or update the program. Enter in the terminal.
xxxxxxxxxxcd ~/ros_wssource devel/setup.bash
Open roscore,
xxxxxxxxxxroscore
Run the little turtle node program,
xxxxxxxxxxrosrun turtlesim turtlesim_node
Run the client node program to generate a small turtle at the specified location.
xxxxxxxxxxrosrun learn_service a_new_turtle
After starting the little turtle node, and then running the a_new_turtle program, you will find that another little turtle will appear on the screen. This is because the little turtle node provides service/spawn, which corresponds to ros::ServiceClient new_turtle in the code. = node.serviceClient<turtlesim::Spawn>("/spawn");//Creating this service will generate another little turtle turtle2. To view the services provided by the little turtle, you can view it through the rosservice list command, as shown in the figure below.

You can view the parameters required by this service through rosservice info /spawn, as shown in the figure below.

It can be seen that 4 parameters are required: x, y, theta, name. These four parameters are initialized in a_new_turtle.cpp.
xxxxxxxxxxsrv.request.x = 6.0;srv.request.y = 8.0;srv.request.name = "turtle2";Note: theta is not assigned a value and defaults to 0
Create a new scripts folder under the function package learn_service, then create a new python file (file suffix .py) in this scripts folder, name it a_new_turtle.py, copy and paste the following program code into the a_new_turtle.py file,
xxxxxxxxxx#!/usr/bin/env python# -*- coding: utf-8 -*-import sysimport rospyfrom turtlesim.srv import Spawndef turtle_spawn(): rospy.init_node('new_turtle')# ROS node initialization rospy.wait_for_service('/spawn')# Wait/spawn service try: new_turtle = rospy.ServiceProxy('/spawn', Spawn) response = new_turtle(2.0, 2.0, 0.0, "turtle2")# Enter request data return response.name except rospy.ServiceException as e: print ("failed to call service : %s")if __name__ == "__main__": #Call the service and display the call results print ("a new turtle named %s." %(turtle_spawn()))The python program does not need to be compiled, but it needs to add executable permissions and enter it in the terminal.
xxxxxxxxxxcd ~/ros_ws/src/learn_service/scriptssudo chmod a+x a_new_turtle.pyOpen roscore,
xxxxxxxxxxrocore
Run the little turtle node,
xxxxxxxxxxrosrun turtlesim turtlesim_node
Run the publisher node program and continue to send speed to the little turtle.
xxxxxxxxxxrosrun learn_service a_new_turtle.py

Similarly, after running, a little turtle will appear, and the terminal will print the returned content.
