当前位置:网站首页>ROS之service编程的学习和理解
ROS之service编程的学习和理解
2022-07-31 05:16:00 【xp_fangfei】
近期由于工作需要,需要学习service编成,故写该博客记录一下,以便日后查询方便,也可以方便各位网友的学习。
下面开始正题。。。。
创建工作空间
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
创建Ros功能包和元功能包
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 //编译
创建服务
- 在元功能包文件夹下创建名为srv的文件夹
- 在srv文件夹下创建multnum.srv文件
multnum.srv内容:
float32 a
float32 b
---
float32 c
string d
package.xml内容更改
以上三行取消注释
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
以上就说明编译成功,接下来就可以写节点实现自己的功能了。
节点的编写:
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;
}
编译完成就可以运行啦!
边栏推荐
猜你喜欢
Xiaomi mobile phone SMS location service activation failed
Access database query
QT VS中双击ui文件无法打开的问题
Gradle sync failed: Uninitialized object exists on backward branch 142
多元线性回归方程原理及其推导
动态规划(一)| 斐波那契数列和归递
This in js points to the prototype object
变分自编码器VAE实现MNIST数据集生成by Pytorch
unicloud 云开发记录
Why does read in bash need to cooperate with while to read the contents of /dev/stdin
随机推荐
Principle analysis of famous website msdn.itellyou.cn
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装
ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
unicloud 云开发记录
VS通过ODBC连接MYSQL(一)
VS通过ODBC连接MYSQL(二)
The latest MySql installation teaching, very detailed
QT VS中双击ui文件无法打开的问题
TransactionTemplate transaction programmatic way
一个简单的bash转powershell案例
VTK:Could not locate vtkTextRenderer object.
podspec 校验依赖出错问题 pod lib lint ,需要指定源
Powershell中UTF-8环境中文乱码解决办法
通信原理——纠错编码 | 汉明码(海明码)手算详解
cocos2d-x-3.2 不能混合颜色修改
SQLite 查询表中每天插入的数量
sql add default constraint
人脸识别AdaFace学习笔记
[swagger close] The production environment closes the swagger method
break and continue exit in js