当前位置:网站首页>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"{}"
边栏推荐
- Arduino operation stm32
- Agile project management of project management
- Wheel 1:qcustomplot initialization template
- Esp8266 interrupt configuration
- Halcon shape_ trans
- MATLAB skills (28) Fuzzy Comprehensive Evaluation
- Halcon color recognition_ fuses. hdev:classify fuses by color
- Business modeling of software model | overview
- Halcon wood texture recognition
- Digital analog 2: integer programming
猜你喜欢
【NOI模拟赛】汁树(树形DP)
每日一题——输入一个日期,输出它是该年的第几天
Guess riddles (2)
Guess riddles (5)
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Classification of plastic surgery: short in long long long
Halcon blob analysis (ball.hdev)
TypeScript手把手教程,简单易懂
Daily question - input a date and output the day of the year
猜谜语啦(8)
随机推荐
【NOI模拟赛】汁树(树形DP)
MATLAB小技巧(28)模糊综合评价
多元线性回归(sklearn法)
猜谜语啦(6)
Some pitfalls of win10 network sharing
Guess riddles (8)
Shift operation of complement
Guess riddles (7)
Wheel 1:qcustomplot initialization template
[formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
One dimensional vector transpose point multiplication np dot
ECMAScript6介绍及环境搭建
猜谜语啦(9)
Business modeling of software model | object modeling
Daily question - input a date and output the day of the year
Business modeling of software model | vision
Run菜单解析
[牛客网刷题 Day4] JZ35 复杂链表的复制
【日常训练--腾讯精选50】557. 反转字符串中的单词 III
我从技术到产品经理的几点体会