ROS 激光数据 点云数据的发送以及 接受

时间:2024-02-21 06:58:30

1.The LaserScan Message

   、ROS 中提供了一种很特殊的  Message type 在sensor-MSG  包当中,他是LaserScan ,他使得编码已获得的信息变得极为方便,首先看一下信息的类型。

A:激光扫描的角度是被逆时针测量的

 1 Header header
 2 float32 angle_min        # start angle of the scan [rad]
 3 float32 angle_max        # end angle of the scan [rad]
 4 float32 angle_increment  # angular distance between measurements [rad]
 5 float32 time_increment   # time between measurements [seconds]
 6 float32 scan_time        # time between scans [seconds]
 7 float32 range_min        # minimum range value [m]
 8 float32 range_max        # maximum range value [m]
 9 float32[] ranges         # range data [m] (Note: values < range_min or > range_max should be discarded)
10 float32[] intensities    # intensity data [device-specific units]

2. Writing Code to Publish a LaserScan Message

 1 #include <ros/ros.h>
 2 #include <sensor_msgs/LaserScan.h>  
 3 
 4 int main(int argc, char** argv){
 5   ros::init(argc, argv, "laser_scan_publisher");
 6 
 7   ros::NodeHandle n;
 8   ros::Publisher scan_pub = n.advertise<sensor_msgs::LaserScan>("scan", 50);  #这是要传递的信息。
 9    
10   unsigned int num_readings = 100;
11   double laser_frequency = 40;
12   double ranges[num_readings];
13   double intensities[num_readings];  # 准备创建虚拟的激光信息,这分别是平率,不太懂。。。
14  
15   int count = 0;
16   ros::Rate r(1.0);     #发送数据的速率
17   while(n.ok()){
18     //generate some fake data for our laser scan
19     for(unsigned int i = 0; i < num_readings; ++i){
20       ranges[i] = count;
21       intensities[i] = 100 + count;
22     }
23     ros::Time scan_time = ros::Time::now();      #每隔一秒产生一个虚假的数据
24 
25     //populate the LaserScan message
26     sensor_msgs::LaserScan scan;
27     scan.header.stamp = scan_time;
28     scan.header.frame_id = "laser_frame";
29     scan.angle_min = -1.57;
30     scan.angle_max = 1.57;
31     scan.angle_increment = 3.14 / num_readings;
32     scan.time_increment = (1 / laser_frequency) / (num_readings);
33     scan.range_min = 0.0;
34     scan.range_max = 100.0;                   #这些是消息内容。与前一节交相辉映
35 
36     scan.ranges.resize(num_readings);
37     scan.intensities.resize(num_readings);
38     for(unsigned int i = 0; i < num_readings; ++i){
39       scan.ranges[i] = ranges[i];
40       scan.intensities[i] = intensities[i];
41     }
42 
43     scan_pub.publish(scan);
44     ++count;
45     r.sleep();
46   }
47 }

3. Publishing PointCloud Message

 1 #include <ros/ros.h>
 2 #include <sensor_msgs/PointCloud.h>
 3 
 4 int main(int argc, char** argv){
 5   ros::init(argc, argv, "point_cloud_publisher");
 6 
 7   ros::NodeHandle n;
 8   ros::Publisher cloud_pub = n.advertise<sensor_msgs::PointCloud>("cloud", 50);
 9 
10   unsigned int num_points = 100;
11 
12   int count = 0;
13   ros::Rate r(1.0);
14   while(n.ok()){
15     sensor_msgs::PointCloud cloud;
16     cloud.header.stamp = ros::Time::now();
17     cloud.header.frame_id = "sensor_frame";
18 
19     cloud.points.resize(num_points);
20 
21     //we\'ll also add an intensity channel to the cloud
22     cloud.channels.resize(1);
23     cloud.channels[0].name = "intensities";
24     cloud.channels[0].values.resize(num_points);
25 
26     //generate some fake data for our point cloud
27     for(unsigned int i = 0; i < num_points; ++i){
28       cloud.points[i].x = 1 + count;
29       cloud.points[i].y = 2 + count;
30       cloud.points[i].z = 3 + count;
31       cloud.channels[0].values[i] = 100 + count;
32     }
33 
34     cloud_pub.publish(cloud);
35     ++count;
36     r.sleep();
37   }
38 }