1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| """ 安全停车路径规划
目标: 1. 安全减速 2. 保持车道 3. 避免碰撞 4. 选择安全停车点 """
import numpy as np from typing import List, Tuple, Optional from dataclasses import dataclass
@dataclass class VehicleState: """车辆状态""" x: float y: float yaw: float speed: float acceleration: float
@dataclass class SafeStopPlan: """安全停车计划""" stop_distance: float deceleration: float lane_change_required: bool target_lane: int estimated_time: float
class SafeStopPlanner: """ 安全停车规划器 策略: 1. 高速:保持车道,减速停车 2. 城市道路:寻找路边停车点 3. 紧急情况:立即停车 """ def __init__( self, max_deceleration: float = 3.0, target_deceleration: float = 2.0, min_stop_distance: float = 50.0 ): self.max_deceleration = max_deceleration self.target_deceleration = target_deceleration self.min_stop_distance = min_stop_distance def plan_stop( self, vehicle_state: VehicleState, lane_info: dict, traffic_info: dict ) -> SafeStopPlan: """ 规划安全停车 Args: vehicle_state: 车辆状态 lane_info: 车道信息 traffic_info: 交通信息 Returns: plan: 停车计划 """ speed = vehicle_state.speed stop_distance = speed ** 2 / (2 * self.target_deceleration) stop_distance = max(stop_distance, self.min_stop_distance) stop_time = speed / self.target_deceleration lane_change_required = self._check_lane_change( vehicle_state, lane_info, traffic_info ) target_lane = self._select_target_lane( vehicle_state, lane_info, traffic_info ) return SafeStopPlan( stop_distance=stop_distance, deceleration=self.target_deceleration, lane_change_required=lane_change_required, target_lane=target_lane, estimated_time=stop_time ) def _check_lane_change( self, vehicle_state: VehicleState, lane_info: dict, traffic_info: dict ) -> bool: """判断是否需要变道""" current_lane = lane_info.get('current_lane', 1) num_lanes = lane_info.get('num_lanes', 3) if current_lane < num_lanes - 1: right_lane_clear = traffic_info.get('right_lane_clear', True) return right_lane_clear return False def _select_target_lane( self, vehicle_state: VehicleState, lane_info: dict, traffic_info: dict ) -> int: """选择目标车道""" num_lanes = lane_info.get('num_lanes', 3) return num_lanes - 1 def generate_trajectory( self, vehicle_state: VehicleState, plan: SafeStopPlan, horizon: float = 5.0, dt: float = 0.1 ) -> List[Tuple[float, float, float]]: """ 生成停车轨迹 Args: vehicle_state: 车辆状态 plan: 停车计划 horizon: 规划时域(秒) dt: 时间步长 Returns: trajectory: 轨迹点列表 [(x, y, yaw), ...] """ trajectory = [] x, y, yaw = vehicle_state.x, vehicle_state.y, vehicle_state.yaw speed = vehicle_state.speed num_steps = int(horizon / dt) for _ in range(num_steps): speed = max(0, speed - plan.deceleration * dt) x += speed * np.cos(yaw) * dt y += speed * np.sin(yaw) * dt trajectory.append((x, y, yaw)) if speed < 0.1: break return trajectory
class SafeStopController: """ 安全停车控制器 执行减速、车道保持、停车操作 """ def __init__(self): self.planner = SafeStopPlanner() self.is_active = False self.current_plan: Optional[SafeStopPlan] = None def activate(self, vehicle_state: VehicleState): """激活安全停车""" self.is_active = True print("⚠️ 激活安全停车系统") def execute( self, vehicle_state: VehicleState, lane_info: dict, traffic_info: dict ) -> dict: """ 执行控制 Returns: control: 控制指令 """ if not self.is_active: return {'throttle': 0, 'brake': 0, 'steering': 0} if self.current_plan is None: self.current_plan = self.planner.plan_stop( vehicle_state, lane_info, traffic_info ) brake_pressure = self._calculate_brake(vehicle_state) steering_angle = self._calculate_steering(vehicle_state, lane_info) if vehicle_state.speed < 0.1: self._on_stop_complete() return { 'throttle': 0, 'brake': brake_pressure, 'steering': steering_angle } def _calculate_brake(self, vehicle_state: VehicleState) -> float: """计算刹车压力""" if self.current_plan is None: return 0.5 speed_factor = min(vehicle_state.speed / 30.0, 1.0) brake_pressure = 0.3 + 0.5 * speed_factor return min(brake_pressure, 1.0) def _calculate_steering( self, vehicle_state: VehicleState, lane_info: dict ) -> float: """计算转向角度""" lane_center = lane_info.get('lane_center', 0) current_offset = vehicle_state.y - lane_center steering = -0.1 * current_offset return np.clip(steering, -0.5, 0.5) def _on_stop_complete(self): """停车完成""" print("✅ 安全停车完成") print("📞 激活 eCall...") self.is_active = False
|