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
| """ 接管准备度评估模型
综合多维度判断驾驶员接管能力 """
import numpy as np
class TakeoverReadinessModel: """ 接管准备度模型 输出:0-100 分,越高越准备好接管 """ def __init__(self): self.weights = { 'attention': 0.4, 'posture': 0.3, 'responsiveness': 0.3 } self.thresholds = { 'ready': 80, 'warning': 50, 'emergency': 30 } def evaluate( self, attention_state: dict, posture_state: dict, responsiveness_state: dict ) -> dict: """ 评估接管准备度 Args: attention_state: { 'eyes_on_road': bool, 'gaze_deviation': float, 'secondary_task': bool } posture_state: { 'hands_near_wheel': bool, 'seat_position': str, 'seatbelt_fastened': bool } responsiveness_state: { 'fatigue_level': float, 'reaction_time': float } Returns: { 'score': float, 'level': str, 'recommendation': str } """ attention_score = self._evaluate_attention(attention_state) posture_score = self._evaluate_posture(posture_state) responsiveness_score = self._evaluate_responsiveness(responsiveness_state) total_score = ( attention_score * self.weights['attention'] + posture_score * self.weights['posture'] + responsiveness_score * self.weights['responsiveness'] ) if total_score >= self.thresholds['ready']: level = 'ready' recommendation = '可以安全接管' elif total_score >= self.thresholds['warning']: level = 'warning' recommendation = '需要准备时间,延长接管窗口' else: level = 'emergency' recommendation = '无法安全接管,需要紧急停车' return { 'score': total_score, 'level': level, 'recommendation': recommendation, 'breakdown': { 'attention': attention_score, 'posture': posture_score, 'responsiveness': responsiveness_score } } def _evaluate_attention(self, state: dict) -> float: """评估注意力""" score = 100.0 if not state.get('eyes_on_road', True): score -= 40 gaze_deviation = state.get('gaze_deviation', 0) if gaze_deviation > 30: score -= 30 if state.get('secondary_task', False): score -= 50 return max(0, score) def _evaluate_posture(self, state: dict) -> float: """评估姿态""" score = 100.0 if not state.get('hands_near_wheel', True): score -= 30 if not state.get('seatbelt_fastened', True): score -= 40 seat_pos = state.get('seat_position', 'normal') if seat_pos == 'reclined': score -= 20 return max(0, score) def _evaluate_responsiveness(self, state: dict) -> float: """评估响应能力""" score = 100.0 fatigue = state.get('fatigue_level', 0) if fatigue > 0.7: score -= 50 elif fatigue > 0.4: score -= 30 reaction_time = state.get('reaction_time', 0.5) if reaction_time > 2.0: score -= 40 elif reaction_time > 1.0: score -= 20 return max(0, score)
class L3TakeoverController: """ L3 接管控制器 根据 DMS 准备度决定接管策略 """ def __init__(self): self.readiness_model = TakeoverReadinessModel() self.takeover_window = { 'ready': 5.0, 'warning': 15.0, 'emergency': 30.0 } def request_takeover( self, dms_state: dict, urgency: str = 'normal' ) -> dict: """ 请求接管 Args: dms_state: DMS 检测状态 urgency: 'normal' | 'urgent' | 'critical' Returns: { 'takeover_window': float, 'warnings': list, 'fallback_action': str } """ readiness = self.readiness_model.evaluate( dms_state['attention'], dms_state['posture'], dms_state['responsiveness'] ) base_window = self.takeover_window[readiness['level']] if urgency == 'urgent': base_window *= 0.5 elif urgency == 'critical': base_window *= 0.3 warnings = self._generate_warnings(readiness) if readiness['level'] == 'emergency': fallback = 'emergency_stop' elif readiness['level'] == 'warning': fallback = 'extend_window_and_warn' else: fallback = 'normal_takeover' return { 'takeover_window': base_window, 'warnings': warnings, 'fallback_action': fallback, 'readiness': readiness } def _generate_warnings(self, readiness: dict) -> list: """生成警告""" warnings = [] breakdown = readiness['breakdown'] if breakdown['attention'] < 70: warnings.append('请将注意力集中在道路') if breakdown['posture'] < 70: warnings.append('请调整坐姿,手放方向盘') if breakdown['responsiveness'] < 70: warnings.append('检测到疲劳,请休息') return warnings
|