如何使用代码发布导航需要的传感器信息

在导航过程中,传感器的信息至关重要,这些传感器可以是激光雷达、摄像机、声纳、红外线、碰撞开关,但是归根结底,导航功能包要求机器人必须发布sensor_msgs/laserscan或sensor_msgs/pointcloud格式的传感器信息,本篇将详细介绍如何使用代码发布所需要的消息。
1、ros的消息头信息
无论是 sensor_msgs/laserscan,还是sensor_msgs/pointcloud ,都和ros中tf帧信息等时间相关的消息一样,带标准格式的头信息。
#standard metadata for higher-level flow data types
#sequence id: consecutively increasing id
uint32 seq
#two-integer timestamp that is expressed as:
# * stamp.secs: seconds (stamp_secs) since epoch
# * stamp.nsecs: nanoseconds since stamp_secs
# time-handling sugar is provided by the client library
time stamp
#frame this data is associated with
# 0: no frame
# 1: global frame
string frame_id
以上是标准头信息的主要部分。seq是消息的顺序标识,不需要手动设置,发布节点在发布消息时,会自动累加。stamp 是消息中与数据相关联的时间戳,例如激光数据中,时间戳对应激光数据的采集时间点。frame_id 是消息中与数据相关联的参考系id,例如在在激光数据中,frame_id对应激光数据采集的参考系。
2、如何发布激光扫描消息
2.1、激光消息的结构
针对激光雷达,ros在sensor_msgs 包中定义了专用了数据结构来存储激光消息的相关信息,成为laserscan。laserscan消息的格式化定义,为虚拟的激光雷达数据采集提供了方便,在我们讨论如何使用他之前,来看看该消息的结构是什么样的:
#
# laser scans angles are measured counter clockwise, with 0 facing forward
# (along the x-axis) of the device frame
#
header header
float32 angle_min # start angle of the scan [rad]
float32 angle_max # end angle of the scan [rad]
float32 angle_increment # angular distance between measurements [rad]
float32 time_increment # time between measurements [seconds]
float32 scan_time # time between scans [seconds]
float32 range_min # minimum range value [m]
float32 range_max # maximum range value [m]
float32[] ranges # range data [m] (note: values range_max should be discarded)
float32[] intensities # intensity data [device-specific units]
备注中已经详细介绍了每个参数的意义。
2.2、通过代码发布laserscan消息
使用ros发布laserscan格式的激光消息非常简洁,下边是一个简单的例程:
#include
#include
int main(int argc, char** argv){
ros::init(argc, argv, laser_scan_publisher);
ros::nodehandle n;
ros::publisher scan_pub = n.advertise(scan, 50);
unsigned int num_readings = 100;
double laser_frequency = 40;
double ranges[num_readings];
double intensities[num_readings];
int count = 0;
ros::rate r(1.0);
while(n.ok()){
//generate some fake data for our laser scan
for(unsigned int i = 0; i < num_readings; ++i){
ranges[i] = count;
intensities[i] = 100 + count;
}
ros::time scan_time = ros::time::now();
//populate the laserscan message
sensor_msgs::laserscan scan;
scan.header.stamp = scan_time;
scan.header.frame_id = laser_frame;
scan.angle_min = -1.57;
scan.angle_max = 1.57;
scan.angle_increment = 3.14 / num_readings;
scan.time_increment = (1 / laser_frequency) / (num_readings);
scan.range_min = 0.0;
scan.range_max = 100.0;
scan.ranges.resize(num_readings);
scan.intensities.resize(num_readings);
for(unsigned int i = 0; i < num_readings; ++i){
scan.ranges[i] = ranges[i];
scan.intensities[i] = intensities[i];
}
scan_pub.publish(scan);
++count;
r.sleep();
}
}
我们将代码分解以便于分析:
#include
首先我们需要先包含laserscan的数据结构。
ros::publisher scan_pub = n.advertise(scan, 50);
创建一个发布者,以便于后边发布针对scan主题的laserscan消息。
unsigned int num_readings = 100;
double laser_frequency = 40;
double ranges[num_readings];
double intensities[num_readings];
int count = 0;
ros::rate r(1.0);
while(n.ok()){
//generate some fake data for our laser scan
for(unsigned int i = 0; i < num_readings; ++i){
ranges[i] = count;
intensities[i] = 100 + count;
}
ros::time scan_time = ros::time::now();
这里的例程中我们虚拟一些激光雷达的数据,如果使用真是的激光雷达,这部分数据需要从驱动中获取。
//populate the laserscan message
sensor_msgs::laserscan scan;
scan.header.stamp = scan_time;
scan.header.frame_id = laser_frame;
scan.angle_min = -1.57;
scan.angle_max = 1.57;
scan.angle_increment = 3.14 / num_readings;
scan.time_increment = (1 / laser_frequency) / (num_readings);
scan.range_min = 0.0;
scan.range_max = 100.0;
scan.ranges.resize(num_readings);
scan.intensities.resize(num_readings);
for(unsigned int i = 0; i < num_readings; ++i){
scan.ranges[i] = ranges[i];
scan.intensities[i] = intensities[i];
}
创建scan_msgs::laserscan数据类型的变量scan,把我们之前伪造的数据填入格式化的消息结构中。
scan_pub.publish(scan);
数据填充完毕后,通过前边定义的发布者,将数据发布。
3、如何发布点云数据
3.1、点云消息的结构
为存储和发布点云消息,ros定义了sensor_msgs/pointcloud消息结构:
#this message holds a collection of 3d points, plus optional additional information about each point.
#each point32 should be interpreted as a 3d point in the frame given in the header
header header
geometry_msgs/point32[] points #array of 3d points
channelfloat32[] channels #each channel should have the same number of elements as points array, and the data in each channel should correspond 1:1 with each point
如上所示,点云消息的结构支持存储三维环境的点阵列,而且channels参数中,可以设置这些点云相关的数据,例如可以设置一个强度通道,存储每个点的数据强度,还可以设置一个系数通道,存储每个点的反射系数,等等。
3.2、通过代码发布点云数据
ros发布点云数据同样简洁:
#include
#include
int main(int argc, char** argv){
ros::init(argc, argv, point_cloud_publisher);
ros::nodehandle n;
ros::publisher cloud_pub = n.advertise(cloud, 50);
unsigned int num_points = 100;
int count = 0;
ros::rate r(1.0);
while(n.ok()){
sensor_msgs::pointcloud cloud;
cloud.header.stamp = ros::time::now();
cloud.header.frame_id = sensor_frame;
cloud.points.resize(num_points);
//we'll also add an intensity channel to the cloud
cloud.channels.resize(1);
cloud.channels[0].name = intensities;
cloud.channels[0].values.resize(num_points);
//generate some fake data for our point cloud
for(unsigned int i = 0; i < num_points; ++i){
cloud.points[i].x = 1 + count;
cloud.points[i].y = 2 + count;
cloud.points[i].z = 3 + count;
cloud.channels[0].values[i] = 100 + count;
}
cloud_pub.publish(cloud);
++count;
r.sleep();
}
}
分解代码来分析:
#include
首先也是要包含sensor_msgs/pointcloud消息结构。
ros::publisher cloud_pub = n.advertise(cloud, 50);
定义一个发布点云消息的发布者。
sensor_msgs::pointcloud cloud;
cloud.header.stamp = ros::time::now();
cloud.header.frame_id = sensor_frame;
为点云消息填充头信息,包括时间戳和相关的参考系id。
cloud.points.resize(num_points);
设置存储点云数据的空间大小。
//we'll also add an intensity channel to the cloud
cloud.channels.resize(1);
cloud.channels[0].name = intensities;
cloud.channels[0].values.resize(num_points);
设置一个名为“intensity“的强度通道,并且设置存储每个点强度信息的空间大小。
//generate some fake data for our point cloud
for(unsigned int i = 0; i < num_points; ++i){
cloud.points[i].x = 1 + count;
cloud.points[i].y = 2 + count;
cloud.points[i].z = 3 + count;
cloud.channels[0].values[i] = 100 + count;
}
将我们伪造的数据填充到点云消息结构当中。
cloud_pub.publish(cloud);
最后,发布点云数据。

有了华为云大数据BI,企业数字化转型该如何做?
直流电源稳压电路
第6代柔性AMOLED生产线进展良好,京东方与三星、华为合作稳定
OPPO采用Super VOOC闪充技术,重新定义快充
Microchip推出首款集成16位ADC、10 Msps ADC、 DAC、USB和LCD的PIC单片机
如何使用代码发布导航需要的传感器信息
Mophie、Belkin和Anker无线充电器对比评测
魅蓝Note5:4GB高运存+手机快充,解决手机卡顿持久流畅度堪比华为荣耀V9,售价还不足千元!
区块链技术有望改变商业的四个方面
大厂抢占竞争激烈的VR市场,各自出招显神通
涡街流量计的常见问题及解决方法
苹果新机iPhone8正式发布:iphone6价格再度狂跌,比荣耀9还便宜,怎么选?
无线户外照明控制系统为广泛的物联网解决方案提供潜力
小米6最新消息:雷军微博说小米6 七年心血之作米粉:现货,怕耍猴.
金立S7拆机教程
AMD重回X86处理器领导者地位 并表示在核心数方面不会停止进步
拿下国家级信创认证!中科驭数KPU SWIFT-2200N成为国内首款满足金融业严苛要求的DPU产品
易图通自动驾驶仿真测试解决方案介绍
加密货币怎样让被动和半被动方从中获取收益
先楫半导体与好上好信息达成战略合作,聚焦工业汽车市场