当前位置: 首页 > news >正文

ros topic和service的使用

在做ldiar slam的时候,最常用的当属topic,偶尔也会用一下service,action则很少使用。现在一块来看一下topic的使用。

一、topic的使用

topic的消息订阅和发布

#include<ros/ros.h>
#include<rosbag/bag.h>
#include<rosbag/view.h>
#include<std_msgs/String.h>
#include<pcl/point_types.h>
#include<pcl/point_cloud.h>
#include<pcl/conversions.h>
#include<pcl_conversions/pcl_conversions.h>
#include<opencv2/opencv.hpp>#include<image_transport/image_transport.h>
#include<cv_bridge/cv_bridge.h>
#include<sensor_msgs/PointCloud2.h>
#include<sensor_msgs/Imu.h>
#include<nav_msgs/Odometry.h>
#include<sensor_msgs/LaserScan.h>
#include<sensor_msgs/Range.h>
#include<turtlesim/Spawn.h>
#include<std_srvs/Trigger.h>class DataProcess
{public:DataProcess(){}public:void processPointCloud2(const sensor_msgs::PointCloud2::ConstPtr& pc){}void imuCallback(const sensor_msgs::Imu::ConstPtr& imuMsg){std::cout<< imuMsg->linear_acceleration.x<< "   "<<  imuMsg->linear_acceleration.y<< "   "<<  imuMsg->linear_acceleration.z<< " \n";}pcl::PointCloud<pcl::PointXYZI> pointxyz;pointxyz.width = 100;pointxyz.height = 1;pointxyz.resize(pointxyz.height * pointxyz.width);srand(time(nullptr));for(int i = 0;i < 100; i ++){pointxyz.points[i].x = (rand()% 1000) / 10.0;pointxyz.points[i].y = (rand()% 1000) / 10.0;pointxyz.points[i].z = (rand()% 1000) / 10.0;}//-------------------------------------------点云的生成和转化----------------------------顺利完成---------------------// 这个函数在pcl_conversion./pcl_conversion.h文件中sensor_msgs::PointCloud2 pointCloudMsg;pointCloudMsg.header.stamp = ros::Time().now();pointCloudMsg.header.frame_id = "/camera_init";pcl::toROSMsg(pointxyz,pointCloudMsg);pcl::PointCloud<pcl::PointXYZI> pointxyzi2;pcl::fromROSMsg(pointCloudMsg, pointxyzi2);for( int i = 0; i < pointxyzi2.points.size();i ++ ){std::cout<< pointxyzi2.points[i].x<<"   "<< pointxyzi2.points[i].y<<"   "<< pointxyzi2.points[i].z<<"\n";}pcl::PointCloud<pcl::PointXYZI> point3;pcl::moveFromROSMsg(pointCloudMsg,point3);for( int i = 0; i < point3.points.size();i ++ ){std::cout<< point3.points[i].x<<"   "<< point3.points[i].y<<"   "<< point3.points[i].z<<"****\n";}}

二、service的使用

以spawn为例子,学习service的使用。这个例子比较简洁,这样很容易学会。

1)客户端

#include<ros/ros.h>
#include<turtlesim/spawn.h>int main(int argc, char** argv)
{turtlesim::Spawn  spawn;spawn.request.x = 0.0;spawn.request.y = 1.0;spawn.request.theta = 1.0;spawn.request.name = "turtle2";ros::service::waitForService("/showspawn");ros::ServiceClient client = nh_.serviceClient<turtlesim::Spawn>("/showspawn");client.call(spawn);
}

2)服务端

#include<ros/ros.h>
#include<turtlesim/Spawn.h>bool srvCallback1(turtlesim::Spawn::Request &req, turtlesim::Spawn::Response& respond){respond.name = req.name;std::cout<<  respond.name <<std::endl;return true;}
int main(int argc, char** argv)
{ros::init(argc, argv, "rosbaglearn1");ros::NodeHandle nh_;ros::ServiceServer srv = nh_.advertiseService("/showspawn",  srvCallback1 );ros::spin();return 0;
}

http://www.dtcms.com/a/274679.html

相关文章:

  • 深入浅出Redis:一文掌握Redis底层数据结构与实现原理
  • Java Stream流介绍及使用指南
  • GIC控制器 (三)
  • 猿人学js逆向比赛第一届第十八题
  • 【一起来学AI大模型】微调技术:LoRA(Low-Rank Adaptation) 的实战应用
  • Linux kernel regcache_cache_only()函数详解
  • pytest中mark的使用
  • SpringCloud之Feign
  • 深入探讨大模型的记忆机制及其前沿技术
  • 数据结构与算法——从递归入手一维动态规划【2】
  • 极端高温下的智慧出行:危险检测与救援
  • AI介入电商内容生产,会颠覆品牌运营吗?
  • 打破内网壁垒,轻松实现安防视频的云端汇聚与P2P超低延迟播放
  • 史上最详细Java并发多线程(面试必备,一篇足矣)
  • 进制转换小题
  • 5 大人工智能知识管理工具
  • 冒泡排序和快速排序
  • 云成本优化完整指南:从理论到实践的全方位解决方案
  • 聚焦数据资源建设与应用,浙江省质科院赴景联文科技调研交流
  • Python 异常处理机制详解:try-except 捕获异常
  • 奇哥面试:RabbitMQ工作模式深度剖析与Spring整合MQ
  • C++ auto与 for循环
  • 2022 年 12 月青少年软编等考 C 语言七级真题解析
  • Linux驱动(input子系统)
  • 使用Python将目录中的JPG图片按后缀数字从小到大顺序纵向拼接,很适合老师发的零散图片拼接一个图片
  • 垂直和领域 Agent 的护城河:上下文工程
  • python16——匿名函数
  • 基于RUP的软件过程深度解析:架构师的高效工程框架
  • 73、【OS】【Nuttx】【启动】深入理解 caller-saved 和 callee-saved(上)
  • TypeScript---泛型