diff --git a/docs/CARS.md b/docs/CARS.md index 8d80b957cdb..e3ca7630f2a 100644 --- a/docs/CARS.md +++ b/docs/CARS.md @@ -1,6 +1,6 @@ -# Support Information for 391 Known Cars +# Support Information for 394 Known Cars |Make|Model|Package|Support Level| |---|---|---|:---:| @@ -254,6 +254,7 @@ |Nissan|Rogue 2018-20|ProPILOT Assist|[Upstream](#upstream)| |Nissan|X-Trail 2017|ProPILOT Assist|[Upstream](#upstream)| |Peugeot|208 2019-25|Adaptive Cruise Control (ACC) & Lane Assist|[Dashcam mode](#dashcam)| +|Polestar|2 2020+|Pilot Assist & Adaptive Cruise Control|[Upstream](#upstream)| |Porsche|Macan 2017-24|Adaptive Cruise Control (ACC) & Lane Assist|[Dashcam mode](#dashcam)| |Ram|1500 2019-24|Adaptive Cruise Control (ACC)|[Upstream](#upstream)| |Ram|2500 2020-24|Adaptive Cruise Control (ACC)|[Dashcam mode](#dashcam)| @@ -395,6 +396,8 @@ |Volkswagen|Tiguan 2018-24|Adaptive Cruise Control (ACC) & Lane Assist|[Upstream](#upstream)| |Volkswagen|Tiguan eHybrid 2021-23|Adaptive Cruise Control (ACC) & Lane Assist|[Upstream](#upstream)| |Volkswagen|Touran 2016-23|Adaptive Cruise Control (ACC) & Lane Assist|[Upstream](#upstream)| +|Volvo|S60 Recharge 2024|Pilot Assist & Adaptive Cruise Control|[Upstream](#upstream)| +|Volvo|XC40 Recharge 2021-2023|Pilot Assist & Adaptive Cruise Control|[Upstream](#upstream)| # Types of Support diff --git a/opendbc/car/volvo/carcontroller.py b/opendbc/car/volvo/carcontroller.py index 26b95f6f85e..466acf3b1cd 100644 --- a/opendbc/car/volvo/carcontroller.py +++ b/opendbc/car/volvo/carcontroller.py @@ -3,7 +3,6 @@ from opendbc.can.packer import CANPacker from opendbc.car import Bus from opendbc.car.interfaces import CarControllerBase -from opendbc.car.volvo.helpers import LCA3CounterSync from opendbc.car.volvo.live_testing import LiveTestingManager from opendbc.car.volvo.volvocan import create_lca_message, create_pscm_message, create_lca_3_message, create_lca_2_message, create_lca_4_message, create_lca_5_message, create_lca_6_message, create_lca_7_message, create_speed_message, create_speed_2_message, create_speed_3_message, create_0x1a_message, create_gear_position_message, create_egsm_message, create_pscm_related_message from opendbc.car.volvo.values import CarControllerParams @@ -29,8 +28,8 @@ def __init__(self, dbc_names, CP): # Counter management for PSCM_RELATED self.pscm_related_counter = None # Will grab initial value from CarState - # Counter management for LCA_3 (pattern-based) - self.lca_3_counter_sync = LCA3CounterSync() + # Counter management for LCA_3 + self.lca_3_counter = None # Will grab initial value from CarState # Counter management for LCA_5 (formerly SPEED_1) self.lca_5_counter = None # Will grab initial value from CarState @@ -210,9 +209,10 @@ def update(self, CC, CS, now_nanos): # Check if PA hands-on-wheel spoof toggle is enabled (bit 7 of alternativeExperience) spoof_pa_hands_enabled = bool(self.CP.alternativeExperience & 128) - spoof_pa_hands = CS.pilot_assist_engaged and spoof_pa_hands_enabled + spoof_hands_on_wheel = lat_active or (CS.pilot_assist_engaged and spoof_pa_hands_enabled) + # PSCM (bus 2 -> 0) - 0x16 - 100 Hz - can_sends.append(create_pscm_message(self.packer, lat_active, CS.msg_pscm, self.frame, spoof_pa_hands)) + can_sends.append(create_pscm_message(self.packer, CS.msg_pscm, spoof_hands_on_wheel)) # EGSM - 0x45 - 100 Hz #can_sends.append(create_egsm_message(self.packer, CS.msg_egsm)) @@ -232,11 +232,10 @@ def update(self, CC, CS, now_nanos): # 0x57 at ~66.67 Hz: send on 2 out of every 3 frames # Pattern: send on frame % 3 == 0 or 2, skip when frame % 3 == 1 if self.frame % 3 != 1: # → 2/3 * 100 Hz = 66.67 Hz - # Update counter with observed value, get counter to send - counter, is_synced = self.lca_3_counter_sync.update(CS.msg_lca_3['COUNTER_1']) - can_sends.append(create_lca_3_message(self.packer, lat_active, apply_angle, CS.msg_lca_3, counter)) - #can_sends.append(create_0x1a_message(self.packer, CS.msg_0x1a)) - pass + if self.lca_3_counter is None: + self.lca_3_counter = CS.msg_lca_3['COUNTER_1'] + self.lca_3_counter = (self.lca_3_counter + 1) % 4 + can_sends.append(create_lca_3_message(self.packer, lat_active, apply_angle, CS.msg_lca_3, self.lca_3_counter)) # SPEED messages - 0x60, 0x68 - 50 Hz if self.frame % 2 == 0: # 50 Hz diff --git a/opendbc/car/volvo/helpers.py b/opendbc/car/volvo/helpers.py index ccdb7655d9c..03a3ee96650 100644 --- a/opendbc/car/volvo/helpers.py +++ b/opendbc/car/volvo/helpers.py @@ -176,93 +176,6 @@ def checksum_lca_4_message(*args) -> int: return 0 -class LCA3CounterSync: - """ - Best-effort pattern synchronization for LCA_3 COUNTER_1. - - The counter follows a 20-element pattern that cycles based on transmission count. - We track recent observed counter values and match them against the pattern to - determine the current index. While not synchronized, we pass through stock values. - Once synchronized, we permanently use the pattern. - """ - - PATTERN = [2, 2, 1, 2, 2, 2, 1, 2, 2, 3, 0, 2, 3, 2, 0, 2, 3, 2, 0, 3] - PATTERN_LEN = 20 - WINDOW_SIZE = 5 # Track last 5 values for matching - MIN_CONFIDENCE = 4 # Need 4 consecutive matches to sync - - def __init__(self): - self.pattern_index = None # Current index in pattern (None = not synced) - self.observed_window = [] # Circular buffer of last N observed values - self.confidence = 0 # Number of consecutive successful matches - - def update(self, observed_counter: int) -> tuple: - """ - Update with newly observed counter value from stock message. - - Args: - observed_counter: Counter value from CS.msg_lca_3['COUNTER_1'] - - Returns: - Tuple of (counter_to_send, is_synchronized) - """ - # If already synchronized, ignore stock and use our pattern permanently - if self.pattern_index is not None: - counter_to_send = self.PATTERN[self.pattern_index] - self.pattern_index = (self.pattern_index + 1) % self.PATTERN_LEN - return counter_to_send, True - - # Not synchronized yet - try to find pattern index - self.observed_window.append(observed_counter) - if len(self.observed_window) > self.WINDOW_SIZE: - self.observed_window.pop(0) - - # Attempt to sync if we have enough samples - if len(self.observed_window) >= 3: - self._attempt_sync() - - # While not synced, pass through stock counter - return observed_counter, False - - def _attempt_sync(self): - """Try to find current pattern index based on observed window.""" - # Try to match observation window against all positions in pattern - best_match_idx = None - best_match_len = 0 - - for start_idx in range(self.PATTERN_LEN): - match_len = self._count_match(start_idx) - if match_len > best_match_len: - best_match_len = match_len - best_match_idx = start_idx - - # Require MIN_CONFIDENCE matching values to declare sync - if best_match_len >= self.MIN_CONFIDENCE: - # The match tells us where we WERE in the pattern - # We need to set index to NEXT position for next transmission - self.pattern_index = (best_match_idx + len(self.observed_window)) % self.PATTERN_LEN - self.confidence = best_match_len - - def _count_match(self, pattern_start_idx: int) -> int: - """ - Count how many values in observed_window match pattern starting at pattern_start_idx. - - Returns: - Number of consecutive matching values from start - """ - match_count = 0 - for i, observed in enumerate(self.observed_window): - pattern_idx = (pattern_start_idx + i) % self.PATTERN_LEN - if observed == self.PATTERN[pattern_idx]: - match_count += 1 - else: - break # Stop at first mismatch - return match_count - - def is_synchronized(self) -> bool: - """Returns True if we have synchronized to the pattern.""" - return self.pattern_index is not None - def checksum_lca_5_message(byte0: int, byte1: int, byte3: int, byte4: int, byte5: int) -> int: """ Calculate checksum for LCA_5 message 0x67 (byte 2) diff --git a/opendbc/car/volvo/volvocan.py b/opendbc/car/volvo/volvocan.py index 387b1da645c..92ae3bfd370 100644 --- a/opendbc/car/volvo/volvocan.py +++ b/opendbc/car/volvo/volvocan.py @@ -50,26 +50,25 @@ def create_lca_message(packer, lat_active: bool, apply_angle: float, msg_lca: di return packer.make_can_msg('LCA', 2, values) -def create_pscm_message(packer, lat_active: bool, msg_pscm: dict, frame: int, spoof_pa_hands_on_wheel: bool): +def create_pscm_message(packer, msg_pscm: dict, spoof_hands_on_wheel: bool): values = { 'PSCM_ANGLE_SENSOR': msg_pscm['PSCM_ANGLE_SENSOR'], 'BIT_0': msg_pscm['BIT_0'], - 'HANDS_ON_STEERING_WHEEL_A': msg_pscm['HANDS_ON_STEERING_WHEEL_A'], - 'HANDS_ON_STEERING_WHEEL_B': msg_pscm['HANDS_ON_STEERING_WHEEL_B'], + 'HANDS_ON_WHEEL_ALERT_TIMER': msg_pscm['HANDS_ON_WHEEL_ALERT_TIMER'], + 'HANDS_ON_WHEEL_ALERT_FLAGS': msg_pscm['HANDS_ON_WHEEL_ALERT_FLAGS'], + 'PADDING': msg_pscm['PADDING'], + 'FAST_TIMER': msg_pscm['FAST_TIMER'], + 'NEW_SIGNAL_3': msg_pscm['NEW_SIGNAL_3'], + 'NEW_SIGNAL_4': msg_pscm['NEW_SIGNAL_4'], + 'NEW_SIGNAL_5': msg_pscm['NEW_SIGNAL_5'], 'BYTE_4': msg_pscm['BYTE_4'], 'DRIVER_INPUT_DEVIATION': msg_pscm['DRIVER_INPUT_DEVIATION'], - 'BYTE_6': msg_pscm['BYTE_6'], 'BYTE_7': msg_pscm['BYTE_7'], } - # Spoof hands on wheel when: - # - lat_active (openpilot is steering), OR - # - spoof_pa_hands_on_wheel (Pilot Assist is engaged AND toggle enabled) - if lat_active or spoof_pa_hands_on_wheel: - #values['DRIVER_INPUT_DEVIATION'] = -1 # Spoof hands on steering wheel - #values['DRIVER_INPUT_DEVIATION'] = 1 if frame % 2 == 0 else 0 - values['HANDS_ON_STEERING_WHEEL_B'] = 186 if frame % 2 == 0 else 154 # msg_pscm['HANDS_ON_STEERING_WHEEL_B'] - values['HANDS_ON_STEERING_WHEEL_A'] = 195 if frame % 2 == 0 else 249 # msg_pscm['HANDS_ON_STEERING_WHEEL_A'] + if spoof_hands_on_wheel: + values['HANDS_ON_WHEEL_ALERT_FLAGS'] = 3 # follow timer + values['HANDS_ON_WHEEL_ALERT_TIMER'] = 0 # seconds since last hands on wheel return packer.make_can_msg('PSCM', 0, values) diff --git a/opendbc/dbc/volvo_mid_1.dbc b/opendbc/dbc/volvo_mid_1.dbc index 1fb5c01162e..b685b43c854 100644 --- a/opendbc/dbc/volvo_mid_1.dbc +++ b/opendbc/dbc/volvo_mid_1.dbc @@ -11,11 +11,15 @@ BO_ 21 DRIVER_INPUT: 8 XXX BO_ 22 PSCM: 8 XXX SG_ PSCM_ANGLE_SENSOR : 6|15@0- (0.05596,0) [-916|916] "º" XXX SG_ BIT_0 : 7|1@0+ (1,0) [0|1] "" XXX - SG_ HANDS_ON_STEERING_WHEEL_A : 23|8@0+ (1,0) [0|255] "" XXX - SG_ HANDS_ON_STEERING_WHEEL_B : 31|8@0+ (1,0) [0|255] "" XXX + SG_ HANDS_ON_WHEEL_ALERT_FLAGS : 17|2@0+ (1,0) [0|3] "" XXX + SG_ HANDS_ON_WHEEL_ALERT_TIMER : 21|4@0+ (1,0) [0|15] "" XXX + SG_ PADDING : 23|2@0+ (1,0) [0|3] "" XXX + SG_ FAST_TIMER : 27|4@0+ (1,0) [0|15] "" XXX + SG_ NEW_SIGNAL_5 : 28|1@0+ (1,0) [0|1] "" XXX + SG_ NEW_SIGNAL_3 : 29|1@0+ (1,0) [0|1] "" XXX + SG_ NEW_SIGNAL_4 : 31|2@0+ (1,0) [0|3] "" XXX SG_ BYTE_4 : 39|8@0+ (1,0) [0|255] "" XXX - SG_ DRIVER_INPUT_DEVIATION : 47|8@0- (1,0) [-128|127] "" XXX - SG_ BYTE_6 : 55|8@0+ (1,0) [0|255] "" XXX + SG_ DRIVER_INPUT_DEVIATION : 47|16@0- (1,0) [0|65535] "" XXX SG_ BYTE_7 : 63|8@0+ (1,0) [0|255] "" XXX BO_ 23 PSCM_RELATED: 8 XXX @@ -177,6 +181,8 @@ BO_ 1336 NEW_MSG_538: 8 XXX SG_ NEW_SIGNAL_3 : 24|1@0+ (1,0) [0|1] "" XXX SG_ NEW_SIGNAL_2 : 29|1@0+ (1,0) [0|1] "" XXX +CM_ SG_ 22 HANDS_ON_WHEEL_ALERT_TIMER "0: hands on wheel; 1-13: counts up 1 per second; 14: hands on wheel warning displayed; 15: hands on wheel alert displayed"; +CM_ SG_ 22 FAST_TIMER "unknown purpose or what it's doing"; CM_ BO_ 23 "Might be related to PSCM 0x16"; CM_ SG_ 87 LCA_ACCEPT_COMMANDS_RELATED "Goes to 15 when actively operating LCA"; CM_ SG_ 87 LCA_ACCEPT_COMMANDS_INV "Goes to 0 when PSCM allowed to receive LCA commands"; @@ -185,4 +191,5 @@ CM_ SG_ 103 WHEEL_SPEED_1 "Possible Front Left (FL)"; CM_ SG_ 103 WHEEL_SPEED_2 "Possible Front Right (FR)"; CM_ SG_ 105 ESC_ACTUATING "Goes to 1 in combination with ESC_ELIGIBLE when ESC is actively actuating"; CM_ SG_ 105 ESC_ELIGIBLE "Goes to 1 on e.g. speed bump, but doesn't trigger any intervention"; +VAL_ 22 HANDS_ON_WHEEL_ALERT_FLAGS 0 "LCA not active" 1 "Alert Active" 3 "Follow Timer"; VAL_ 128 GEAR_POSITION 0 "Park" 1 "Reverse" 2 "Neutral" 3 "Drive" 4 "B-Mode";