当前位置:网站首页>Programming implementation of ROS learning 6 -service node
Programming implementation of ROS learning 6 -service node
2022-07-05 08:45:00 【m0_ forty-six million ninety-three thousand eight hundred and t】
One 、 Concept
service Service It is a way of synchronous communication between nodes ; Allow clients Client Node issue request Request; By the server Server Feedback response after node processing Response.
Two 、 establish Service Process overview of
1. establish Service Of .cpp Program
2. compile
3. Configure environment variables
4. perform Service Program
3、 ... and 、 The specific process
31. establish Service.cpp file
Create in the function pack of the corresponding workspace Service.cpp file , The file should include the following four parts :
1. initialization ROS node
2. establish Serve example ( Callable ros The service type data already defined in can also be defined by yourself , How to view ros For the defined service types, see :)
3. Wait for the service request of the client circularly , After receiving the request, enter the callback function
4. Complete the service function processing in the callback function , And feed back the response data
example : Simulate the following service model
establish turtle_command_server.cpp file , In this example ros The built-in trigger
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* The routine will execute /turtle_command service , Service data type std_srvs/Trigger
*/
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <std_srvs/Trigger.h>
ros::Publisher turtle_vel_pub;
bool pubCommand = false;
// service Callback function , Input parameters req, Output parameters res
bool commandCallback(std_srvs::Trigger::Request &req,
std_srvs::Trigger::Response &res)
{
pubCommand = !pubCommand;
// Display request data
ROS_INFO("Publish turtle velocity command [%s]", pubCommand==true?"Yes":"No");
// Set feedback data
res.success = true;
res.message = "Change turtle command state!"
return true;
}
int main(int argc, char **argv)
{
// ROS Node initialization
ros::init(argc, argv, "turtle_command_server");
// Create node handle
ros::NodeHandle n;
// Create a file called /turtle_command Of server, Register callback function commandCallback
ros::ServiceServer command_service = n.advertiseService("/turtle_command", commandCallback);
// Create a Publisher, The release is called /turtle1/cmd_vel Of topic, The message type is geometry_msgs::Twist, The queue length 10
turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
// Loop waiting for callback function
ROS_INFO("Ready to receive turtle command.");
// Set the frequency of the cycle
ros::Rate loop_rate(10);
while(ros::ok())
{
// Check the callback function queue once
ros::spinOnce();
// If the flag is true, Then issue speed command
if(pubCommand)
{
geometry_msgs::Twist vel_msg;
vel_msg.linear.x = 0.5;
vel_msg.angular.z = 0.2;
turtle_vel_pub.publish(vel_msg);
}
// Delay according to the cycle frequency
loop_rate.sleep();
}
return 0;
}
There is also the publisher's program in this program , Think about it , Our general request is to request a service , Then you give an answer . So in the process, assume that the request is for the service that makes the tortoise move , Then answer . In this program, you can pay attention to how the programs of publishers and servers are combined .
3.2 compile
Add the following code to CmakeList.txt Of install Just above
add_executeable(turtle_command_server src/turtle_command_server.cpp)
target_link_libraries(turtle_command_server ${catkin_LIBRARIES})
Then go back to the main directory of the workspace, open the terminal, and catkin_make once
3.3 Configure environment variables
Return to the main directory of the workspace and open the terminal , then source devel/setup.bash once
Four 、 perform
Because the service makes the little turtle move , So start the little turtle first
roscore
rosrun turtlesim turtlesim_node
Then directly execute your service File can , The instructions are as follows :
rosrun Function package name .cpp file name
rosrun learning_service turtle_command_server
But note that this is the server , Therefore, there needs to be a request before there is a service , So there must be a client to call your request . The name of the service just created in the program is :/turtle1/cmd_vel
Therefore, when requesting, you have to ask for the service area . The instructions are as follows :
rosservice call /turtle_command"{}"
边栏推荐
- Basic number theory - fast power
- Tips 1: Web video playback code
- C language data type replacement
- Warning: retrying occurs during PIP installation
- [牛客网刷题 Day4] JZ35 复杂链表的复制
- RT-Thread内核快速入门,内核实现与应用开发学习随笔记
- Classification of plastic surgery: short in long long long
- 【日常训练】1200. 最小绝对差
- Halcon shape_ trans
- L298N module use
猜你喜欢
Mathematical modeling: factor analysis
Classification of plastic surgery: short in long long long
C# LINQ源码分析之Count
EA introduction notes
Example 006: Fibonacci series
Halcon snap, get the area and position of coins
Explore the authentication mechanism of StarUML
IT冷知识(更新ing~)
It cold knowledge (updating ing~)
资源变现小程序添加折扣充值和折扣影票插件
随机推荐
Typical low code apaas manufacturer cases
Explore the authentication mechanism of StarUML
IT冷知识(更新ing~)
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
location search 属性获取登录用户名
Several problems to be considered and solved in the design of multi tenant architecture
Old Wang's esp8266 and old Wu's ws2818 light strip
Esp8266 interrupt configuration
An enterprise information integration system
Bit operation related operations
Guess riddles (7)
Speech recognition learning summary
容易混淆的基本概念 成员变量 局部变量 全局变量
Apaas platform of TOP10 abroad
One question per day - replace spaces
Wheel 1:qcustomplot initialization template
Count of C # LINQ source code analysis
猜谜语啦(2)
Meizu Bluetooth remote control temperature and humidity access homeassistant
287. 寻找重复数-快慢指针