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;
}
相关推荐
老黄编程3 小时前
Gazebo Classic 11 与 Gazebo Harmonic(Gazebo Sim) 的功能对比总结
ros·gazebo
kyle~4 小时前
虚拟仪器LabView(VI)
c++·python·ros·labview
BestOrNothing_20156 小时前
Ubuntu 一键安装 ROS 全流程( 鱼香ROS 一键脚本)
linux·ubuntu·ros·ros环境搭建·鱼香ros·一键安装ros
老黄编程1 天前
旧版 gazebo11 和 新版Gazebo Jetty差异性比较
ros·gazebo
老黄编程1 天前
Gazebo Jetty vs Gazebo Harmonic 差异性分析
ros·jetty·harmonic
Mr.Winter`1 天前
深度强化学习 | 基于PPO算法的移动机器人路径跟踪(附Pytorch实现)
人工智能·pytorch·深度学习·机器学习·机器人·自动驾驶·ros
爱尔兰的楠小楠3 天前
matlab/simulink与WLS2下ROS2联合仿真环境搭建
matlab·机器人·ros·px4
暮色念了红尘20 天前
Ros1 Noetic(本地)和Ros2 Humble(docker)之间相互通信及设置初始位姿
docker·容器·ros·humble·noetic·ros1_bridge
Mr.Winter`22 天前
无人船 | 图解基于MPC控制的路径跟踪算法(以全驱动无人艇WAMV为例)
人工智能·算法·机器人·自动驾驶·ros·路径规划
不懂音乐的欣赏者25 天前
Windows 下 ROS/ROS2 开发环境最优解:WSL 比直接安装、虚拟机、双系统更优雅!
linux·windows·ubuntu·ros·wsl·ros2·双系统