当前位置:网站首页>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!
边栏推荐
- DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
- IDEA控制台不能输入信息的解决方法
- TransactionTemplate transaction programmatic way
- Redis-Hash
- UR3机器人运动学分析之逆运动学分析
- 活体检测FaceBagNet阅读笔记
- PyTorch学习笔记08——加载数据集
- CLS-PEG-FITC Fluorescein-PEG-CLS 胆固醇-聚乙二醇-荧光素简介
- VTK:Could not locate vtkTextRenderer object.
- 日志jar包冲突,及其解决方法
猜你喜欢
随机推荐
UR3机器人运动学分析之逆运动学分析
DSPE-PEG-Azide DSPE-PED-N3 Phospholipid-Polyethylene Glycol-Azide Lipid PFG
CNN的一点理解
Cholesterol-PEG-Acid CLS-PEG-COOH 胆固醇-聚乙二醇-羧基修饰肽类化合物
MW:3400 4-Arm PEG-DSPE 四臂-聚乙二醇-磷脂一种饱和的18碳磷脂
mPEG-DMPE Methoxy-polyethylene glycol-bismyristyl phosphatidylethanolamine for stealth liposome formation
MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid
科学研究用磷脂-聚乙二醇-活性酯 DSPE-PEG-NHS CAS:1445723-73-8
Word vector - demo
WeChat applet source code acquisition and decompilation method
Numpy常用函数
Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400
opencv之访问图像像素的三种方法
DSPE-PEG-Azide DSPE-PED-N3 磷脂-聚乙二醇-叠氮脂质PFG
cv2.resize()是反的
Pytorch常用函数
Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
mysql 事务原理详解
jenkins +miniprogram-ci upload WeChat applet with one click
cv2.imread()









