完整代码获取
访问GitHub仓库获取最新代码:
https://github.com/YonderL/Bug-Algorithms
代码结构说明
BugAlgorithms/
├── Bug0/ # Bug0算法完整实现
│ ├── boundary_detect.m # 边界碰撞检测模块
│ ├── bug0.m # 主算法逻辑入口
│ ├── isPointOnSegment.m # 几何关系判断工具
│ └── next_cal.m # 运动矢量计算
├── Bug1/ # Bug1算法完整实现
├── checkCollision_pro.m # 增强型碰撞检测
├── bug1.m # 主算法逻辑入口
├── followBoundary.m # 边界跟踪核心算法
├── calClosetPoint.m # 最近点计算模块
├── rotationWithPoint.m # 障碍物拐点处理
├── pointToLineDistance.m # 距离计算工具
└── generate_rotated_coordinates.m # 坐标系变换
关键模块解析
Bug0核心组件
-
bug0.m
- 主控制循环
- 状态机管理(自由移动/沿障移动)
- 调用传感器模拟模块
-
boundary_detect.m
function [state, k_obs] = boundary_detect(obstaclelist, now, next) % 输入:障碍物列表/当前位置/下一位置 % 输出:碰撞状态/障碍物边界斜率
- 多障碍物碰撞检测
- 边界斜率计算(k_obs=Inf处理垂直边界)
Bug1增强功能
-
checkCollision_pro.m
% 支持非凸多边形投影检测 [collision, new_pos] = checkCollision_pro(obstaclelist, current, target)
- 基于射线法的精确碰撞检测
- 自动计算安全退出位置
-
rotationWithPoint.m
function path = rotationWithPoint(current, obstacle, step, path, last_close) % 处理障碍物顶点的"拐角难题"
- 动态调整绕行方向
- 防止顶点处震荡
通用工具
- isPointOnSegment.m
function flag = isPointOnSegment(p, p1, p2, tol) % 判断点p是否在线段p1p2上(带容差)
- 用于精确的路径终点判断
- 支持浮点数容差(tol参数)