一.激光雷达数据格式
图片来源:ROS-订阅与处理激光雷达scan话题_ros激光雷达数据处理_zhhao1326的博客-CSDN博客
# 测量的激光扫描角度,逆时针为正
# 设备坐标帧的0度面向前(沿着X轴方向)Header header # Header也是一个结构体,包含了seq,stamp,frame_id,其中seq指的是扫描顺序增加的id,stamp包含了开始扫描的时间和与开始扫描的时间差,frame_id是扫描的参考系名称.注意扫描是逆时针从正前方开始扫描的.
float32 angle_min # 开始扫描的角度(角度)
float32 angle_max # 结束扫描的角度(角度)
float32 angle_increment # 每一次扫描增加的角度(角度)
float32 time_increment # 测量的时间间隔(s)
float32 scan_time # 扫描的时间间隔(s)
float32 range_min # 距离最小值(m)
float32 range_max # 距离最大值(m)
float32[] ranges # 距离数组(长度360)(注意: 值 < range_min 或 > range_max 应当被丢弃)
float32[] intensities # 与设备有关,强度数组(长度360)
数据信息:逆时针0-360,每隔angle_increment存储一次。最终数据在ranges中。
参考:阿ROS消息sensor_msgs::LaserScan数据格式_sensor_msgs/laserscan_和道一文字_的博客-CSDN博客副
二.发送laser数据
文件名:pub_LaserScan.cpp
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
/**
@param num_readings:激光雷达扫描一圈发射的激光数量
**/
int main(int argc, char** argv){
ros::init(argc, argv, "laser_scan_publisher");
ros::NodeHandle n;
ros::Publisher scan_pub = n.advertise<sensor_msgs::LaserScan>("pub_scan", 50);
unsigned int num_readings = 100;
double laser_frequency = 40;
double ranges[num_readings];
double intensities[num_readings];
int count = 1;
ros::Rate r(1);
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 = "base_link";
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];
}
ROS_INFO("%d",count);
scan_pub.publish(scan);
//++count;
r.sleep();
}
}
CMakeLists .txt添加:
add_executable(pub_LaserScan src/pub_LaserScan.cpp)
target_link_libraries(pub_LaserScan ${catkin_LIBRARIES})
终端运行:
rosrun [your package] pub_LaserScan
具体代码解析:ROS与navigation教程-发布传感器数据 - 创客智造/爱折腾智能机器人
三.查看发布的laser数据
方法一:
rosrun rviz rviz
点击Add—>添加LaserScan—>选择Topic—>修改Fixed Frame为base_link;
方法二:
rostopic echo /pub_scan
四.订阅laser数据
文件名:
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
//回调函数
void LaserCallBack(const sensor_msgs::LaserScan::ConstPtr &msg){
int size=msg->ranges.size();
ROS_INFO("angle_min:[%f]",msg->angle_min);
ROS_INFO("angle_max:[%f]",msg->angle_max);//其余数据,需要什么写什么
//std::string ranges="";//每次进来初始化string
for(int i=0;i<size;i++)
ROS_INFO("ranges[%d]=%f",i,msg->ranges[i]);
}
int main(int argc, char** argv){
ros::init(argc, argv, "laser_scan_subscrible");
ros::NodeHandle n;
ros::Subscriber scan_sub =n.subscribe("pub_scan",1000,LaserCallBack);
ros::spin();//死循环执行LaserCallBack()
return 0;
}
CMakeLists .txt添加:
add_executable(sub_LaserScan src/sub_LaserScan.cpp)
target_link_libraries(sub_LaserScan ${catkin_LIBRARIES})
运行:
rosrun robot_setup_tf sub_LaserScan
结果: