# Ros2 **Repository Path**: daweibiao/ros2 ## Basic Information - **Project Name**: Ros2 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-22 - **Last Updated**: 2025-09-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Ros2 #### 介绍 {**以下是 Gitee 平台说明,您可以替换此简介** Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) ------------------------------------------------------ #!usr/bin/env python import rclpy from rclpy.node import Node from sensor_msgs.msg import LaserScan from rclpy.qos import ReliabilityPolicy, QoSProfile import random class ObstacleDetectorNode(Node): def __init__(self): super().__init__('obstacle_detector_node') self.get_logger().info('obstacle_detector_node' + "Ready ...") self.subscriber = self.create_subscription( LaserScan, '/laser_scan', self.laserscan_callback, QoSProfile(depth=10, reliability=ReliabilityPolicy.RELIABLE)) def laserscan_callback(self,msg): min_distance = min(msg.ranges) sectors = { "Right_Rear":(0,33), "Right": (34, 66), "Front_Right": (67, 100), "Front_Left": (101, 133), "Left": (134, 166), "Left_Rear": (167, 199) } min_distances = {key: float('inf') for key in sectors.keys()} # Determine detected obstacles for sector, (start_idx, end_idx) in sectors.items(): # Ensure the index range is within bounds and not empty if start_idx < len(msg.ranges) and end_idx < len(msg.ranges): sector_ranges = msg.ranges[start_idx:end_idx + 1] if sector_ranges: min_distances[sector] = min(sector_ranges) # Log the minimum distances for sector, min_distance in min_distances.items(): self.get_logger().info(f'{sector}: {min_distance:.2f} meters') # Define the threshold for obstacle detection obstacle_threshold = 0.8 # meters detections = {sector: min_distance < obstacle_threshold for sector, min_distance in min_distances.items()} if detections["Front_Left"] and detections["Front_Right"]: arbitrary_direction = "Right" action = "Selected Turn Arbitrary Direction "+arbitrary_direction elif detections["Front_Left"] and not detections["Front_Right"]: action = "Turn Right to avoid obstacle on the front-left" elif detections["Front_Right"] and not detections["Front_Left"]: action = "Turn Left to avoid obstacle on the front-right" # Priority 2 elif detections["Left"]: action = "Go Forwards turning slightly right to avoid obstacle on the left" elif detections["Right"]: action = "Go Forwards turning slightly left to avoid obstacle on the right" # Priority 3 elif detections["Right_Rear"]: action = "Go Forwards, BUT DONT reverse Right" elif detections["Left_Rear"]: action = "Go Forwards, BUT DONT reverse left" else: action = "Go Forwards" self.get_logger().info(f'Minimum distance: {min_distance:.2f} meters') def main(args=None): rclpy.init(args=args) node = ObstacleDetectorNode() rclpy.spin(node) rclpy.shutdown() if __name__ == '__main__': main() ---------------------------------