当前位置:网站首页>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!
边栏推荐
- Redis-Hash
- DingTalk Enterprise Internal-H5 Micro Application Development
- 科学研究用磷脂-聚乙二醇-活性酯 DSPE-PEG-NHS CAS:1445723-73-8
- DSPE-PEG-Azide DSPE-PED-N3 磷脂-聚乙二醇-叠氮脂质PFG
- Numpy常用函数
- Evaluating Machine Learning Models - Excerpt
- 数据分析之SQL面试真题
- 2022 SQL big factory high-frequency practical interview questions (detailed analysis)
- IDEA控制台不能输入信息的解决方法
- UR3机器人雅克比矩阵
猜你喜欢
MW:3400 4-Arm PEG-DSPE 四臂-聚乙二醇-磷脂一种饱和的18碳磷脂
Embedding cutting-edge understanding
wangeditor富文本编辑器上传图片以及跨域问题解决
朴素贝叶斯文本分类(代码实现)
Pytorch常用函数
random.randint函数用法
PyTorch学习笔记08——加载数据集
化学试剂磷脂-聚乙二醇-氨基,DSPE-PEG-amine,CAS:474922-26-4
mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids
MySQL 入门:Case 语句很好用
随机推荐
opencv之图像二值化处理
Rejection sampling note
MySQL 主从切换步骤
Research reagents Cholesterol-PEG-Maleimide, CLS-PEG-MAL, Cholesterol-PEG-Maleimide
mPEG-DMPE 甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺用于形成隐形脂质体
cocos2d-x implements cross-platform directory traversal
DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用
用pytorch里的children方法自定义网络
这些数组技巧,我爱了
Tensorflow——demo
jenkins +miniprogram-ci upload WeChat applet with one click
科研试剂Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺
2021-09-30
应用usb_cam同时打开多个摄像头方法
Getting Started with MySQL: The Case Statement Works Well
CAS:474922-22-0 Maleimide-PEG-DSPE 磷脂-聚乙二醇-马来酰亚胺简述
解决background-size:cover时图片铺满但显示不完整?
Virtual machine view port number process
Solution for MySQL The table is full
VTK:Could not locate vtkTextRenderer object.