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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
| """ Emergency Safety Field (ESF) 无响应驾驶员干预系统 论文:An Emergency Driving Intervention System Designed for Driver Disability Scenarios """
import numpy as np from typing import List, Tuple, Dict from dataclasses import dataclass from enum import Enum
class ObstacleType(Enum): """障碍物类型""" VEHICLE = 1 PEDESTRIAN = 2 BICYCLE = 3 BARRIER = 4 FUEL_TANK_TRUCK = 5
@dataclass class Obstacle: """障碍物""" type: ObstacleType position: np.ndarray velocity: np.ndarray mass: float is_static: bool = False
@dataclass class RoadBoundary: """道路边界""" left_boundary: np.ndarray right_boundary: np.ndarray lane_width: float
class EmergencySafetyField: """ 紧急安全场计算器 量化道路环境风险,规划安全停车路径 """ def __init__(self, road_weight: float = 1.0, obstacle_weight: float = 2.0, target_weight: float = -1.5): """ Args: road_weight: 道路边界权重 obstacle_weight: 障碍物权重 target_weight: 目标位置权重(负值表示吸引力) """ self.w_road = road_weight self.w_obs = obstacle_weight self.w_target = target_weight def compute_road_field(self, position: np.ndarray, road: RoadBoundary) -> float: """ 计算道路边界风险场 Args: position: 车辆位置 (x, y) road: 道路边界信息 Returns: risk: 道路边界风险值 """ dist_left = np.min(np.linalg.norm( road.left_boundary - position, axis=1 )) dist_right = np.min(np.linalg.norm( road.right_boundary - position, axis=1 )) threshold = road.lane_width * 0.3 risk_left = 1.0 / (dist_left + 0.1) if dist_left < threshold else 0 risk_right = 1.0 / (dist_right + 0.1) if dist_right < threshold else 0 return risk_left + risk_right def compute_obstacle_field(self, position: np.ndarray, ego_velocity: np.ndarray, obstacles: List[Obstacle], prediction_time: float = 3.0) -> float: """ 计算障碍物风险场 Args: position: 车辆位置 ego_velocity: 车辆速度 obstacles: 障碍物列表 prediction_time: 预测时间(秒) Returns: risk: 总障碍物风险 """ total_risk = 0 for obs in obstacles: if obs.is_static: obs_position = obs.position else: obs_position = obs.position + obs.velocity * prediction_time ego_predicted = position + ego_velocity * prediction_time distance = np.linalg.norm(obs_position - ego_predicted) base_risk = 1.0 / (distance + 1.0) type_factor = { ObstacleType.VEHICLE: 1.0, ObstacleType.PEDESTRIAN: 1.5, ObstacleType.BICYCLE: 1.2, ObstacleType.BARRIER: 0.8, ObstacleType.FUEL_TANK_TRUCK: 3.0 }.get(obs.type, 1.0) relative_velocity = np.linalg.norm(ego_velocity - obs.velocity) ttc = distance / (relative_velocity + 0.1) if relative_velocity > 0 else 999 if ttc < 5: ttc_factor = 5.0 / ttc else: ttc_factor = 1.0 mass_factor = np.sqrt(obs.mass / 1500) risk = base_risk * type_factor * ttc_factor * mass_factor total_risk += risk return total_risk def compute_target_field(self, position: np.ndarray, target: np.ndarray, max_distance: float = 50.0) -> float: """ 计算目标位置吸引场 Args: position: 当前位置 target: 目标位置(安全停车点) max_distance: 最大影响距离 Returns: attraction: 吸引力(负值) """ distance = np.linalg.norm(target - position) if distance > max_distance: return 0 attraction = -distance / max_distance return attraction def compute_esf(self, position: np.ndarray, velocity: np.ndarray, road: RoadBoundary, obstacles: List[Obstacle], target: np.ndarray) -> Tuple[float, Dict]: """ 计算总紧急安全场 Returns: total_risk: 总风险值 components: 各分量风险 """ E_road = self.compute_road_field(position, road) E_obs = self.compute_obstacle_field(position, velocity, obstacles) E_target = self.compute_target_field(position, target) total = self.w_road * E_road + self.w_obs * E_obs + self.w_target * E_target components = { 'road_risk': E_road, 'obstacle_risk': E_obs, 'target_attraction': E_target, 'total_risk': total } return total, components
class EmergencyMotionPlanner: """ 紧急停车路径规划器 基于ESF生成安全停车轨迹 """ def __init__(self, max_decel: float = 4.0, max_lateral_accel: float = 2.0): """ Args: max_decel: 最大减速度 (m/s²) max_lateral_accel: 最大横向加速度 (m/s²) """ self.max_decel = max_decel self.max_lateral_accel = max_lateral_accel self.esf = EmergencySafetyField() def find_safe_stop_location(self, position: np.ndarray, road: RoadBoundary, obstacles: List[Obstacle], search_distance: float = 200.0) -> np.ndarray: """ 寻找安全停车位置 Args: position: 当前位置 road: 道路信息 obstacles: 障碍物列表 search_distance: 搜索距离 Returns: target: 安全停车位置 """ min_risk = float('inf') best_target = position + np.array([50, 0]) for distance in np.linspace(10, search_distance, 20): for lateral_offset in np.linspace(-road.lane_width, road.lane_width, 5): candidate = position + np.array([distance, lateral_offset]) risk, _ = self.esf.compute_esf( candidate, np.array([0, 0]), road, obstacles, candidate ) if risk < min_risk: min_risk = risk best_target = candidate return best_target def plan_trajectory(self, start_pos: np.ndarray, start_vel: np.ndarray, target: np.ndarray, duration: float = 10.0) -> np.ndarray: """ 规划停车轨迹 Args: start_pos: 起始位置 start_vel: 起始速度 target: 目标位置 duration: 总时间 Returns: trajectory: 轨迹点序列 (N, 4) - (x, y, vx, vy) """ n_points = int(duration * 10) t = np.linspace(0, duration, n_points) trajectory = np.zeros((n_points, 4)) for i, ti in enumerate(t): alpha = ti / duration alpha_smooth = 3 * alpha**2 - 2 * alpha**3 pos = start_pos * (1 - alpha_smooth) + target * alpha_smooth vel = start_vel * (1 - alpha)**2 trajectory[i, :2] = pos trajectory[i, 2:] = vel return trajectory def compute_control_commands(self, current_pos: np.ndarray, current_vel: np.ndarray, trajectory: np.ndarray, dt: float = 0.1) -> Tuple[float, float]: """ 计算控制命令 Returns: steering: 转向角 (rad) deceleration: 减速度 (m/s²) """ target_pos = trajectory[1, :2] target_vel = trajectory[1, 2:] pos_error = target_pos - current_pos vel_error = target_vel - current_vel lateral_error = pos_error[1] heading_error = np.arctan2(vel_error[1], vel_error[0] + 0.1) steering = np.clip(heading_error + 0.5 * lateral_error, -0.5, 0.5) speed = np.linalg.norm(current_vel) if speed > 1: deceleration = min(self.max_decel, speed / dt) else: deceleration = 0 return steering, deceleration
class EmergencyInterventionSystem: """ 紧急干预系统 检测无响应驾驶员并执行紧急停车 """ def __init__(self, unresponsive_threshold: float = 10.0, check_count: int = 3): """ Args: unresponsive_threshold: 无响应判定时间(秒) check_count: 确认次数 """ self.threshold = unresponsive_threshold self.check_count = check_count self.planner = EmergencyMotionPlanner() self.unresponsive_counter = 0 self.is_intervening = False def check_driver_response(self, gaze_on_road: bool, hands_on_wheel: bool, input_detected: bool) -> bool: """ 检查驾驶员响应 Returns: is_responsive: 驾驶员是否有响应 """ is_responsive = gaze_on_road or hands_on_wheel or input_detected if not is_responsive: self.unresponsive_counter += 1 else: self.unresponsive_counter = 0 return is_responsive or self.unresponsive_counter < self.check_count def trigger_emergency_stop(self, position: np.ndarray, velocity: np.ndarray, road: RoadBoundary, obstacles: List[Obstacle]) -> Dict: """ 触发紧急停车 Returns: intervention: 干预命令 """ self.is_intervening = True target = self.planner.find_safe_stop_location(position, road, obstacles) trajectory = self.planner.plan_trajectory(position, velocity, target) steering, deceleration = self.planner.compute_control_commands( position, velocity, trajectory ) intervention = { 'type': 'EMERGENCY_STOP', 'target_position': target.tolist(), 'steering': steering, 'deceleration': deceleration, 'duration': 10.0, 'trajectory': trajectory.tolist() } return intervention
if __name__ == "__main__": esf = EmergencySafetyField() planner = EmergencyMotionPlanner() system = EmergencyInterventionSystem() position = np.array([0.0, 0.0]) velocity = np.array([25.0, 0.0]) road = RoadBoundary( left_boundary=np.array([[0, -1.75], [100, -1.75]]), right_boundary=np.array([[0, 1.75], [100, 1.75]]), lane_width=3.5 ) obstacles = [ Obstacle( type=ObstacleType.VEHICLE, position=np.array([50, 0]), velocity=np.array([15, 0]), mass=1500 ) ] risk, components = esf.compute_esf(position, velocity, road, obstacles, np.array([100, 0])) print(f"总风险: {risk:.2f}") print(f"风险分量: {components}") print("\n模拟无响应驾驶员...") for i in range(5): is_responsive = system.check_driver_response( gaze_on_road=False, hands_on_wheel=False, input_detected=False ) print(f"检查 {i+1}: 响应={is_responsive}") if not is_responsive: intervention = system.trigger_emergency_stop(position, velocity, road, obstacles) print(f"\n触发紧急停车!") print(f"目标位置: {intervention['target_position']}") print(f"减速度: {intervention['deceleration']:.1f} m/s²") break
|