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
| """ 干预决策模块 根据驾驶员状态选择合适的干预策略 """
import numpy as np from typing import Dict, List, Optional from dataclasses import dataclass from enum import Enum
class InterventionLevel(Enum): """干预级别""" NONE = 0 WARNING_VISUAL = 1 WARNING_AUDIO = 2 WARNING_HAPTIC = 3 ASSIST_ENHANCED = 4 SPEED_REDUCTION = 5 EMERGENCY_STOP = 6
@dataclass class InterventionAction: """干预动作""" level: InterventionLevel action_type: str parameters: Dict duration_sec: float escalation_time_sec: float
class InterventionDecisionEngine: """ 干预决策引擎 根据驾驶员状态和风险评估决定干预策略 """ WARNING_ESCALATION = { 'distraction': { 'first_warning': 2.0, 'escalation': 4.0, 'intervention': 6.0 }, 'drowsiness': { 'first_warning': 5.0, 'escalation': 10.0, 'intervention': 15.0 }, 'impairment': { 'first_warning': 10.0, 'escalation': 15.0, 'intervention': 20.0 }, 'unresponsive': { 'first_warning': 3.0, 'escalation': 6.0, 'emergency_stop': 10.0 } } def __init__( self, config: Optional[Dict] = None ): if config is None: config = {} self.config = config self.state_history: List[DriverStateEstimate] = [] self.intervention_history: List[InterventionAction] = [] def decide( self, state_estimate: DriverStateEstimate, vehicle_state: Dict, road_context: Dict ) -> InterventionAction: """ 决定干预策略 Args: state_estimate: 驾驶员状态估计 vehicle_state: 车辆状态 road_context: 道路上下文 Returns: action: 干预动作 """ self.state_history.append(state_estimate) state = state_estimate.state risk = state_estimate.risk_level duration = state_estimate.duration_sec if state == DriverState.NORMAL: return self._no_intervention() elif state == DriverState.DISTRACTED: return self._distraction_intervention( risk, duration, vehicle_state ) elif state == DriverState.DROWSY: return self._drowsiness_intervention( risk, duration, vehicle_state ) elif state == DriverState.IMPAIRED: return self._impairment_intervention( risk, duration, vehicle_state ) elif state == DriverState.UNRESPONSIVE: return self._unresponsive_intervention( risk, duration, vehicle_state, road_context ) elif state == DriverState.EMERGENCY: return self._emergency_intervention( vehicle_state, road_context ) return self._no_intervention() def _no_intervention(self) -> InterventionAction: """无干预""" return InterventionAction( level=InterventionLevel.NONE, action_type='none', parameters={}, duration_sec=0, escalation_time_sec=0 ) def _distraction_intervention( self, risk: str, duration: float, vehicle_state: Dict ) -> InterventionAction: """分心干预""" escalation_times = self.WARNING_ESCALATION['distraction'] if duration < escalation_times['first_warning']: return InterventionAction( level=InterventionLevel.WARNING_VISUAL, action_type='visual_alert', parameters={ 'display_type': 'icon_flash', 'color': 'amber', 'message': '请保持注意力集中' }, duration_sec=3.0, escalation_time_sec=escalation_times['first_warning'] ) elif duration < escalation_times['escalation']: return InterventionAction( level=InterventionLevel.WARNING_AUDIO, action_type='audio_haptic_alert', parameters={ 'sound_type': 'chime', 'volume': 0.7, 'vibration_pattern': 'pulse', 'message': '请注视道路' }, duration_sec=5.0, escalation_time_sec=escalation_times['escalation'] ) elif duration < escalation_times['intervention']: return InterventionAction( level=InterventionLevel.ASSIST_ENHANCED, action_type='enhanced_lka', parameters={ 'lka_sensitivity': 'high', 'ldw_threshold': 0.5, 'auto_steering': True }, duration_sec=10.0, escalation_time_sec=escalation_times['intervention'] ) else: return InterventionAction( level=InterventionLevel.SPEED_REDUCTION, action_type='gradual_slowdown', parameters={ 'target_speed': vehicle_state.get('speed', 60) * 0.8, 'deceleration': 0.5 }, duration_sec=30.0, escalation_time_sec=0 ) def _drowsiness_intervention( self, risk: str, duration: float, vehicle_state: Dict ) -> InterventionAction: """疲劳干预""" escalation_times = self.WARNING_ESCALATION['drowsiness'] if risk == 'medium': return InterventionAction( level=InterventionLevel.WARNING_AUDIO, action_type='drowsiness_alert', parameters={ 'sound_type': 'alert_tone', 'volume': 0.8, 'message': '您可能感到疲劳,请考虑休息', 'rest_stop_suggestion': True }, duration_sec=5.0, escalation_time_sec=escalation_times['first_warning'] ) elif risk == 'high': return InterventionAction( level=InterventionLevel.WARNING_HAPTIC, action_type='strong_drowsiness_alert', parameters={ 'sound_type': 'loud_alert', 'volume': 1.0, 'vibration_intensity': 'high', 'message': '检测到严重疲劳,请立即休息', 'auto_reduce_speed': True }, duration_sec=10.0, escalation_time_sec=escalation_times['escalation'] ) else: return InterventionAction( level=InterventionLevel.SPEED_REDUCTION, action_type='emergency_slowdown', parameters={ 'target_speed': 30, 'deceleration': 1.0, 'find_rest_area': True, 'auto_park_suggestion': True }, duration_sec=60.0, escalation_time_sec=0 ) def _impairment_intervention( self, risk: str, duration: float, vehicle_state: Dict ) -> InterventionAction: """损伤(酒驾/药驾)干预""" escalation_times = self.WARNING_ESCALATION['impairment'] return InterventionAction( level=InterventionLevel.ASSIST_ENHANCED, action_type='impairment_assist', parameters={ 'fcw_sensitivity': 'maximum', 'aeb_threshold': 0.3, 'safe_following_distance': 3.0, 'speed_limit_reduction': 0.8, 'message': '检测到驾驶能力受损,已增强安全辅助' }, duration_sec=float('inf'), escalation_time_sec=escalation_times['first_warning'] ) def _unresponsive_intervention( self, risk: str, duration: float, vehicle_state: Dict, road_context: Dict ) -> InterventionAction: """无响应驾驶员干预""" escalation_times = self.WARNING_ESCALATION['unresponsive'] if duration < escalation_times['first_warning']: return InterventionAction( level=InterventionLevel.WARNING_HAPTIC, action_type='wake_attempt', parameters={ 'sound_type': 'loud_siren', 'volume': 1.0, 'vibration_intensity': 'maximum', 'light_flash': True }, duration_sec=3.0, escalation_time_sec=escalation_times['first_warning'] ) elif duration < escalation_times['escalation']: return InterventionAction( level=InterventionLevel.SPEED_REDUCTION, action_type='prepare_stop', parameters={ 'target_speed': 20, 'deceleration': 2.0, 'hazard_lights': True }, duration_sec=6.0, escalation_time_sec=escalation_times['escalation'] ) else: return InterventionAction( level=InterventionLevel.EMERGENCY_STOP, action_type='emergency_stop', parameters={ 'stop_type': 'controlled', 'pull_over': road_context.get('shoulder_available', True), 'deceleration': 3.0, 'hazard_lights': True, 'unlock_doors': True, 'emergency_call': True, 'eCall_message': '驾驶员无响应,请求救援' }, duration_sec=float('inf'), escalation_time_sec=0 ) def _emergency_intervention( self, vehicle_state: Dict, road_context: Dict ) -> InterventionAction: """医疗紧急情况干预""" return InterventionAction( level=InterventionLevel.EMERGENCY_STOP, action_type='medical_emergency_stop', parameters={ 'stop_type': 'immediate', 'pull_over': True, 'deceleration': 4.0, 'hazard_lights': True, 'unlock_doors': True, 'emergency_call': True, 'eCall_message': '医疗紧急情况,请求急救', 'transmit_location': True }, duration_sec=float('inf'), escalation_time_sec=0 )
if __name__ == "__main__": engine = InterventionDecisionEngine() state_estimate = DriverStateEstimate( state=DriverState.UNRESPONSIVE, confidence=0.95, risk_level='critical', duration_sec=12.0, indicators={} ) vehicle_state = { 'speed': 80, 'lane_position': 0.1 } road_context = { 'shoulder_available': True, 'traffic_density': 'low' } action = engine.decide(state_estimate, vehicle_state, road_context) print("=== 干预决策测试 ===") print(f"干预级别: {action.level.name}") print(f"动作类型: {action.action_type}") print(f"参数: {action.parameters}") print(f"持续时间: {action.duration_sec}s")
|