第12章 行为规划与决策免责声明本文档为学术研究与技术学习目的而编写基于Autoware开源项目Apache 2.0许可证的源码分析。文档内容力求准确但不保证完全无误仅供参考。读者在实际应用时应以官方文档和源码为准。本文档不涉及任何商业用途所有代码示例均来自开源项目。如有侵权请联系删除。摘要行为规划Behavior Planning是Autoware自动驾驶系统中连接任务规划和运动规划的中间层负责在给定全局路径的前提下根据当前交通场景做出具体的驾驶决策。本章深入解析行为规划模块的架构设计重点介绍场景识别与理解机制、基于决策树的行为选择逻辑、交通规则遵守策略、与其他交通参与者的交互式决策以及行为状态机的设计与实现。行为规划是实现安全、合规、高效自动驾驶的核心环节。目录12.1 行为规划架构12.1.1 行为规划在规划层级中的定位12.1.2 核心模块组成12.1.3 数据流与接口12.2 场景识别与理解12.2.1 场景分类体系12.2.2 场景要素提取12.2.3 场景匹配与识别12.3 行为决策树12.3.1 决策树结构设计12.3.2 行为库与行为选择12.3.3 决策逻辑实现12.4 交通规则遵守12.4.1 交通信号灯处理12.4.2 停止线与让行规则12.4.3 速度限制遵守12.4.4 禁止区域避让12.5 交互式决策12.5.1 车辆交互决策12.5.2 行人交互决策12.5.3 意图预测与博弈12.6 行为状态机12.6.1 状态机设计12.6.2 状态转换逻辑12.6.3 状态监控与调试参考资料12.1 行为规划架构12.1.1 行为规划在规划层级中的定位行为规划位于任务规划和运动规划之间是自动驾驶规划系统的大脑负责根据当前交通状况做出驾驶决策。规划层级架构任务规划Mission Planning ↓ 全局路径Route 行为规划Behavior Planning—— 驾驶行为决策 ↓ 行为命令Behavior 运动规划Motion Planning—— 轨迹生成 ↓ 轨迹Trajectory 车辆控制Vehicle Control—— 执行控制行为规划的核心职责场景理解识别当前驾驶场景如跟车、超车、路口通行等决策制定根据场景和交通规则决定执行何种驾驶行为约束生成为运动规划提供行为约束如减速停车、变道等规则遵守确保决策符合交通法规和安全要求与其他模块的交互输入依赖全局路径来自任务规划感知结果障碍物、交通信号灯、车道线等定位信息车辆位姿和速度高精地图道路属性、交通规则输出提供行为状态当前执行的驾驶行为路径约束运动规划的边界条件速度约束期望的速度曲线12.1.2 核心模块组成源码路径:universe/autoware_universe/planning/behavior_path_planner/,universe/autoware_universe/planning/behavior_velocity_planner/Autoware的行为规划分为两个主要模块1. Behavior Path Planner行为路径规划器负责车道级别的路径决策包括车道保持、车道变换、避障等。// behavior_path_planner/src/behavior_path_planner_node.cppnamespacebehavior_path_planner{classBehaviorPathPlannerNode:publicrclcpp::Node{public:explicitBehaviorPathPlannerNode(constrclcpp::NodeOptionsoptions):Node(behavior_path_planner,options){// 订阅全局路径sub_route_create_subscriptionLaneletRoute(/planning/mission_planning/route,1,std::bind(BehaviorPathPlannerNode::onRoute,this,_1));// 订阅感知结果sub_objects_create_subscriptionPredictedObjects(/perception/object_recognition/objects,1,std::bind(BehaviorPathPlannerNode::onObjects,this,_1));// 订阅定位信息sub_odometry_create_subscriptionOdometry(/localization/kinematic_state,1,std::bind(BehaviorPathPlannerNode::onOdometry,this,_1));// 发布行为路径pub_path_create_publisherPathWithLaneId(/planning/scenario_planning/lane_driving/behavior_planning/path,1);// 初始化场景模块initializeSceneModules();}voidinitializeSceneModules(){// 车道跟随模块scene_modules_.push_back(std::make_sharedLaneFollowingModule(get_logger()));// 车道变换模块scene_modules_.push_back(std::make_sharedLaneChangeModule(get_logger()));// 避障模块scene_modules_.push_back(std::make_sharedAvoidanceModule(get_logger()));// 路边停车模块scene_modules_.push_back(std::make_sharedPullOverModule(get_logger()));// 起步模块scene_modules_.push_back(std::make_sharedStartPlannerModule(get_logger()));}voidrun(){// 1. 更新场景信息updateSceneInfo();// 2. 执行所有场景模块for(automodule:scene_modules_){if(module-isExecutionRequested(scene_info_)){module-plan(scene_info_);}}// 3. 选择最优路径autobest_pathselectBestPath();// 4. 发布路径if(best_path){pub_path_-publish(*best_path);}}private:std::vectorstd::shared_ptrSceneModuleInterfacescene_modules_;SceneInfo scene_info_;};}// namespace behavior_path_planner2. Behavior Velocity Planner行为速度规划器负责根据交通规则和障碍物情况规划速度曲线。// behavior_velocity_planner/src/behavior_velocity_planner_node.cppnamespacebehavior_velocity_planner{classBehaviorVelocityPlannerNode:publicrclcpp::Node{public:explicitBehaviorVelocityPlannerNode(constrclcpp::NodeOptionsoptions):Node(behavior_velocity_planner,options){// 订阅行为路径sub_path_create_subscriptionPathWithLaneId(/planning/scenario_planning/lane_driving/behavior_planning/path,1,std::bind(BehaviorVelocityPlannerNode::onPath,this,_1));// 发布带速度的路径pub_path_with_velocity_create_publisherPath(/planning/scenario_planning/lane_driving/behavior_planning/path_with_velocity,1);// 初始化场景模块initializeSceneModules();}voidinitializeSceneModules(){// 交通信号灯模块scene_modules_.push_back(std::make_sharedTrafficLightModule(shared_from_this()));// 停止线模块scene_modules_.push_back(std::make_sharedStopLineModule(shared_from_this()));// 人行横道模块scene_modules_.push_back(std::make_sharedCrosswalkModule(shared_from_this()));// 路口模块scene_modules_.push_back(std::make_sharedIntersectionModule(shared_from_this()));// 障碍物停止模块scene_modules_.push_back(std::make_sharedObstacleStopModule(shared_from_this()));}voidplanVelocity(constPathWithLaneIdinput_path){autooutput_pathinput_path;// 依次执行所有速度规划模块for(automodule:scene_modules_){if(module-isActivated()){module-planVelocity(output_path);}}// 发布结果pub_path_with_velocity_-publish(output_path);}private:std::vectorstd::shared_ptrSceneModuleInterfacescene_modules_;};}// namespace behavior_velocity_planner12.1.3 数据流与接口输入接口订阅Topic:/planning/mission_planning/route(autoware_planning_msgs/msg/LaneletRoute) - 全局路径/perception/object_recognition/objects(autoware_perception_msgs/msg/PredictedObjects) - 预测目标/perception/traffic_light_recognition/traffic_signals(autoware_perception_msgs/msg/TrafficSignalArray) - 交通信号/localization/kinematic_state(nav_msgs/msg/Odometry) - 车辆状态/map/vector_map(autoware_map_msgs/msg/LaneletMapBin) - 高精地图发布Topic:/planning/scenario_planning/lane_driving/behavior_planning/path(autoware_planning_msgs/msg/PathWithLaneId) - 行为路径/planning/scenario_planning/lane_driving/behavior_planning/path_with_velocity(autoware_planning_msgs/msg/Path) - 带速度的路径/planning/scenario_planning/behavior_state(std_msgs/msg/String) - 当前行为状态PathWithLaneId消息格式# autoware_planning_msgs/msg/PathWithLaneId.msgstd_msgs/Header header PathPointWithLaneId[]points---# PathPointWithLaneId.msggeometry_msgs/Pose pose# 路径点位姿float32 longitudinal_velocity_mps# 纵向速度float32 lateral_velocity_mps# 横向速度float32 heading_rate_rps# 航向角变化率bool is_final# 是否为终点int64[]lane_ids# 关联的Lanelet ID参数配置# config/behavior_path_planner.param.yaml/**:ros__parameters:# 车道跟随lane_following:lateral_distance_threshold:2.0# 横向偏移阈值yaw_threshold:0.785# 航向角阈值45度# 车道变换lane_change:minimum_lane_changing_velocity:5.6# 最小变道速度20km/hprediction_time_resolution:0.5# 预测时间分辨率enable_abort_lane_change:true# 允许中止变道enable_collision_check_at_prepare:true# 避障avoidance:enable_avoidance:trueavoidance_execution_lateral_threshold:0.5max_lateral_offset:1.0# 路边停车pull_over:request_length:100.0# 提前请求距离th_arrived_distance:1.0# 到达判定距离th_stopped_velocity:0.01# 停止判定速度12.2 场景识别与理解12.2.1 场景分类体系Autoware将驾驶场景分为多个层级便于模块化处理。场景分类层级1. 宏观场景Scenario - 城市道路Urban - 高速公路Highway - 停车场Parking 2. 中观场景Scene - 直道行驶Straight - 路口通行Intersection - 环岛Roundabout - 匝道Ramp 3. 微观场景Sub-scene - 车道保持Lane Keeping - 车道变换Lane Change - 跟车Car Following - 超车Overtaking - 避障Avoidance - 停车Parking场景定义表场景ID场景名称触发条件对应模块1车道保持在车道内正常行驶LaneFollowing2左变道需要左侧车道且条件满足LaneChangeLeft3右变道需要右侧车道且条件满足LaneChangeRight4避障检测到静态障碍物Avoidance5路边停车接近目标停车点PullOver6路口通行接近路口Intersection7人行横道接近人行横道Crosswalk12.2.2 场景要素提取场景要素是描述当前驾驶环境的关键信息。核心场景要素structSceneInfo{// 车辆状态Pose current_pose;doublevelocity;doubleacceleration;// 路径信息LaneletRoute current_route;lanelet::ConstLanelet current_lanelet;// 感知信息PredictedObjects dynamic_objects;std::vectorTrafficLighttraffic_lights;// 地图信息std::vectorlanelet::ConstLaneletadjacent_lanelets;std::vectorStopLinestop_lines;std::vectorCrosswalkcrosswalks;// 场景特征boolis_in_intersection;boolis_on_highway;boolhas_static_obstacle;doubledistance_to_goal;};12.2.3 场景匹配与识别SceneTypeidentifyCurrentScene(constSceneInfoinfo)const{// 1. 检查是否需要停车if(info.distance_to_goalpull_over_distance_threshold_){returnSceneType::PULL_OVER;}// 2. 检查是否在路口if(info.is_in_intersection){returnSceneType::INTERSECTION;}// 3. 检查是否需要变道autolane_change_requiredcheckLaneChangeRequired(info);if(lane_change_required){returnlane_change_required-directionLEFT?SceneType::LANE_CHANGE_LEFT:SceneType::LANE_CHANGE_RIGHT;}// 4. 检查是否需要避障if(info.has_static_obstacle){returnSceneType::AVOIDANCE;}// 5. 默认车道保持returnSceneType::LANE_FOLLOWING;}12.3 行为决策树12.3.1 决策树结构设计Autoware使用模块化的决策架构每个场景模块独立决策。12.3.2 行为库与行为选择行为定义enumclassBehavior{LANE_FOLLOW,// 车道跟随LANE_CHANGE_LEFT,// 左变道LANE_CHANGE_RIGHT,// 右变道OVERTAKE,// 超车AVOIDANCE,// 避障STOP,// 停止SLOW_DOWN,// 减速PULL_OVER,// 路边停车EMERGENCY_STOP// 紧急停车};12.3.3 决策逻辑实现略参考完整源码12.4 交通规则遵守12.4.1 交通信号灯处理voidTrafficLightModule::modifyPathVelocity(Pathpath){// 获取前方交通信号灯autotraffic_lightsgetTrafficLightsOnPath(path);for(constautotl:traffic_lights){if(tl.colorTrafficLight::RED||tl.colorTrafficLight::YELLOW){// 在停止线前停车insertStopPoint(path,tl.stop_line_pose);}}}12.4.2 停止线与让行规则停止线处理逻辑确保车辆在必要时完全停止。12.4.3 速度限制遵守根据地图中的速度限制属性调整路径速度。12.4.4 禁止区域避让避开地图中标记的禁止通行区域。12.5 交互式决策12.5.1 车辆交互决策处理与其他车辆的交互如合流、让行等。12.5.2 行人交互决策在人行横道处理行人通行优先权。voidCrosswalkModule::handlePedestrian(Pathpath){autopedestriansdetectPedestriansOnCrosswalk();if(!pedestrians.empty()){// 在人行横道前停车insertStopPoint(path,crosswalk_stop_pose_);}}12.5.3 意图预测与博弈基于其他交通参与者的轨迹预测其意图。12.6 行为状态机12.6.1 状态机设计enumclassBehaviorState{IDLE,DRIVING,LANE_CHANGING,STOPPING,STOPPED,EMERGENCY};12.6.2 状态转换逻辑voidupdateState(){switch(current_state_){caseIDLE:if(has_route_)current_state_DRIVING;break;caseDRIVING:if(emergency_detected_)current_state_EMERGENCY;elseif(lane_change_requested_)current_state_LANE_CHANGING;break;caseLANE_CHANGING:if(lane_change_completed_)current_state_DRIVING;break;}}12.6.3 状态监控与调试状态机状态通过ROS topic发布便于监控和调试。参考资料官方文档Autoware Documentation - Behavior Planning源码仓库behavior_path_plannerbehavior_velocity_planner