deflaserCallback(self, scan_data): ifnotisinstance(scan_data, LaserScan): return # 记录激光扫描并发布最近物体的位置(或指向某点) # Record the laser scan and publish the position of the nearest object (or point to a point) ranges = np.array(scan_data.ranges) # 创建距离列表,将检测范围内的有效距离放入列表中 # create distance list, put the effective distance within the detection range into the list minDistList = [] # 创建序列号,将有效距离对应的ID放入列表中 # Create a serial number and place the ID corresponding to the valid distance in the list minDistIDList = [] # 按距离排序以检查从较近的点到较远的点是否是真实的东西 # if we already have a last scan to compare to: for i inrange(len(ranges)): angle = (scan_data.angle_min + scan_data.angle_increment * i) * RAD2DEG # if angle > 90: print "i: {},angle: {},dist: {}".format(i, angle, scan_data.ranges[i]) # 通过清除不需要的扇区的数据来保留有效的数据 ifabs(angle) > (180 - self.laserAngle): minDistList.append(ranges[i]) minDistIDList.append(angle) iflen(minDistList) == 0: return # 找到最小距离 # Find the minimum distance minDist = min(minDistList) # 找到最小距离对应的ID # Find the ID corresponding to the minimum distance minDistID = minDistIDList[minDistList.index(minDist)] self.get_logger().info(f"find minimum {minDist}{minDistID}")
if minDist <= self.ResponseDist: if self.Buzzer_state == False: b = Bool() b.data = True self.pub_Buzzer.publish(b) self.Buzzer_state = True else: if self.Buzzer_state == True: self.pub_Buzzer.publish(Bool()) self.Buzzer_state = False