本篇文章通过订阅/scan话题获取障碍物的距离信息,达到避障目的,提供部分代码,仅供参考
1.单线激光雷达避障
(1)ros接入激光雷达
以sick激光为例,在之前的文章已经介绍过ROS 入门(七),此处不再赘叙,获取/scan数据
seq 是消息的顺序标识发布节点在发布消息时,会自动累加
stamp 是消息中与数据相关联的时间戳
frame_id 是消息中与数据相关联的参考系id
angle_min 起始角度(rad)
angle_max 结束角度(rad)
angle_increment 角度分辨率(rad)
time_increment 每个角度扫描时间
scan_time 扫描间隔
range_min 最小距离
range_max 最大距离
ranges 各个角度的距离
intensities 各个角度的强度
(2)代码实现
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
class Obstacle():
def __init__(self):
self.lidar_detect_distance = 1.5 #避障距离为1.5m
self.scan_error = 0.2 #过滤掉0.2m内的点云数据
sub = rospy.Subscriber('/scan',LaserScan,self.getScan,queue_size=10)
rospy.spin()
def getScan(self,msg):
self.scan_filter =[]
for i in range(360):
if i < 45 or i > 135: # 取激光扫描角度
if msg.ranges[i] >= self.scan_error:
self.scan_filter.append(msg.ranges[i])
# print(len(self.scan_filter))
if min(self.scan_filter) < self.lidar_detect_distance:
self.update_status()
def update_status(self):
cmd_pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
twist = Twist()
twist.linear.x = 0
twist.linear.y = 0
twist.linear.z = 0
twist.angular.x = 0
twist.angular.y = 0
twist.angular.z = 0
cmd_pub.publish(twist)
rospy.loginfo("distance of Obstacle {}".format(self.lidar_detect_distance))
if __name__ == '__main__':
rospy.init_node('stop')
# sub = rospy.Subscriber('/scan',LaserScan,doMsg,queue_size=10)
# rospy.spin()
try:
Obstacle()
except Exception as e:
pass
2.多线激光避障
多线激光避障思路和单线激光避障类似,其中要注意的是激光扫描角度的获取和第几线束的配置
(1)镭神16线激光
通过分析源码,设置参数:angle_disable_min,angle_disable_max,扫描角度10°~350°不输出扫描数据, channel_num:=0, 取0线束
<param name="point_num" value="2000"/>
<param name="channel_num" value="0"/>
<param name="angle_disable_min" value="10"/>
<param name="angle_disable_max" value="350"/>
涉及源码lslidar_c16_decoder_node.cpp如图:
源码地址:https://github.com/ncnynl/lslidar_C16
学习地址:https://www.ncnynl.com/archives/201810/2762.html
(2) velodyne16 激光
<arg name="laserscan_ring" default="-1" />
涉及源码velodyne_laserscan如图:
实时获取激光扫描的位置信息,点击‘2D Pose Estimate’,在rviz中输出
参考链接:
障碍物检测之基于激光雷达的障碍物检测及基于视觉和激光雷达融合的障碍物检测
ROS入门–基于激光的避障
ROS简单实现订阅/scan激光雷达的距离信息
版权归原作者 wqm-story027 所有, 如有侵权,请联系我们删除。