当前位置:网站首页>Learning and understanding of ROS service programming
Learning and understanding of ROS service programming
2022-07-31 06:32:00 【xp_fangfei】
近期由于工作需要,需要学习service编成,So write this blog to record it,以便日后查询方便,It can also facilitate the learning of netizens.
下面开始正题....
创建工作空间
mkdir -p ros_practice/src
cd ros_practice/src/
catkin_init_workspace
cd ../
catkin_make
//添加环境变量
echo "source ~ros_practice/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
创建RosFeature Packs and Meta Feature Packs
cd ros_practice/src/
catkin_create_pkg learn_service roscpp rospy std_msgs // catkin_create_pkg [package_name] [dependency1] [dependency1] ...
cd ../ //回退到ros_prctice目录下
catkin_make //编译
创建服务
- Create a folder named srv的文件夹
- 在srv文件夹下创建multnum.srv文件
multnum.srv内容:
float32 a
float32 b
---
float32 c
string d
package.xml内容更改

Uncomment the above three lines
CMakeLists.txt内容修改
cmake_minimum_required(VERSION 3.0.2)
project(learn_service)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation //添加
)
## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)
## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )
## Generate services in the 'srv' folder
add_service_files(
FILES
multnum.srv //添加
)
## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES learn_service
CATKIN_DEPENDS roscpp rospy std_msgs message_generation //添加
# DEPENDS system_lib
)
include_directories(
# include
${
catkin_INCLUDE_DIRS}
)
add_executable(service src/service.cpp)
target_link_libraries(service ${
catkin_LIBRARIES})
add_dependencies(service learn_service_gencpp)
add_executable(client src/client.cpp)
target_link_libraries(client ${
catkin_LIBRARIES})
add_dependencies(client learn_service_gencpp)
编译:
cd 到learn_service目录下
catkin_make
查看服务:
[email protected]-virtual-machine:~/ros_practice$ rossrv show learn_service/multnum
float32 a
float32 b
---
float32 c
string d
The above shows that the compilation is successful,Next, you can write nodes to implement their own functions.
节点的编写:
client.cpp
#include <cstdlib>
#include "ros/ros.h"
#include "learn_service/multnum.h"
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "lient");
// 从终端命令行获取两个加数
if (argc != 3)
{
ROS_INFO("usage: add_two_ints_client X Y");
return 1;
}
// 创建节点句柄
ros::NodeHandle n;
// 创建一个client,请求add_two_int service,service消息类型是learning_communication::AddTwoInts
ros::ServiceClient client = n.serviceClient<learn_service::multnum>("add_two_ints");
// 创建learning_communication::AddTwoInts类型的service消息
learn_service::multnum addsrv;
addsrv.request.a = atoll(argv[1]);
addsrv.request.b = atoll(argv[2]);
// 发布service请求,等待加法运算的应答结果
if (client.call(addsrv))
{
ROS_INFO("Sum: %ld", (long int)addsrv.response.c);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}
service.cpp
#include "ros/ros.h"
#include "learn_service/multnum.h"
#include "std_msgs/String.h"
// service回调函数,输入参数req,输出参数res
bool AddCallback(learn_service::multnum::Request &req,
learn_service::multnum::Response &res)
{
// 将输入参数中的请求数据相加,结果放到应答变量中
std::stringstream ss;
ss << "hello world ";
res.c = req.a + req.b;
res.d = ss.str();
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("sending back response: [%ld]", (long int)res.c);
ROS_INFO("%s", res.d.c_str());
return true;
}
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "service");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个名为add_two_ints的server,注册回调函数add()
ros::ServiceServer service = n.advertiseService("add_two_ints", AddCallback);
// 循环等待回调函数
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
Compilation is complete and you can run it!
边栏推荐
- Nmap的下载与安装
- mysql 事务原理详解
- Embedding cutting-edge understanding
- Pytorch学习笔记13——Basic_RNN
- MySQL 入门:Case 语句很好用
- Phospholipids-Polyethylene Glycol-Active Esters for Scientific Research DSPE-PEG-NHS CAS: 1445723-73-8
- Getting Started with MySQL: The Case Statement Works Well
- Sourcery插件(自动提升代码质量)
- 自然语言处理相关list
- IDEA概述和安装及调试
猜你喜欢

Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400

朴素贝叶斯文本分类(代码实现)

Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use

计算图像数据集均值和方差

Cholesterol-PEG-DBCO 胆固醇-聚乙二醇-二苯基环辛炔化学试剂

科学研究用磷脂-聚乙二醇-活性酯 DSPE-PEG-NHS CAS:1445723-73-8

MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid

学习JDBC之获取数据库连接的方式

Cholesterol-PEG-NHS NHS-PEG-CLS 胆固醇-聚乙二醇-活性酯可修饰小分子材料

DC-CDN学习笔记
随机推荐
pytorch学习笔记10——卷积神经网络详解及mnist数据集多分类任务应用
Learn how to get a database connection with JDBC
JS写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数JS
深度学习知识点杂谈
Pytorch学习笔记09——多分类问题
VS2017 connects to MYSQL
活体检测PatchNet学习笔记
MW:3400 4-Arm PEG-DSPE 四臂-聚乙二醇-磷脂一种饱和的18碳磷脂
CAS: 1403744-37-5 DSPE-PEG-FA Phospholipid-Polyethylene Glycol-Folic Acid for Scientific Research
Embedding cutting-edge understanding
Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
Cholesterol-PEG-Azide CLS-PEG-N3 胆固醇-聚乙二醇-叠氮 MW:3400
会话和饼干,令牌
UR3机器人雅克比矩阵
钉钉企业内部-H5微应用开发
自己设置的私密文件,在哪找
DSPE-PEG-COOH CAS:1403744-37-5 磷脂-聚乙二醇-羧基脂质PEG共轭物
random.randint函数用法
About iframe
TransactionTemplate transaction programmatic way