Fix Palisade HDA II steering handoff

This commit is contained in:
firestar5683
2026-11-24 11:57:55 -06:00
parent d0a212be98
commit 7e9e2b43b3
42 changed files with 109 additions and 5 deletions
@@ -19,6 +19,8 @@ static bool blended_aol_token = false;
static bool blended_aol_claimed = false;
static bool blended_aol_session = false;
static bool blended_aol_request_seen = false;
static bool blended_aol_brake_continuation = false;
static bool blended_aol_idle_owned[2] = {false, false};
static bool blended_aol_neutral[3] = {false, false, false};
static bool blended_aol_pressed[3] = {false, false, false};
static uint32_t blended_aol_source_ts[3] = {0U, 0U, 0U};
@@ -44,6 +46,9 @@ static void blended_aol_clear(void) {
blended_aol_claimed = false;
blended_aol_session = false;
blended_aol_request_seen = false;
blended_aol_brake_continuation = false;
blended_aol_idle_owned[0] = false;
blended_aol_idle_owned[1] = false;
blended_aol_token_ts = 0U;
blended_aol_token_source = 3U;
aol_host_axis_mask = 0U;
@@ -71,6 +76,9 @@ static void blended_aol_reset(void) {
static void blended_aol_host_request(uint8_t mask) {
blended_aol_request_seen |= mask != 0U;
if ((mask & 1U) == 0U) {
blended_aol_brake_continuation = false;
}
}
static uint8_t blended_aol_request_mask(void) {
@@ -107,7 +115,7 @@ static uint8_t blended_aol_permission_mask(void) {
(safety_get_ts_elapsed(microsecond_timer_get(), blended_aol_source_ts[blended_aol_token_source]) <= 300000U);
if (!aol_rx_healthy() || (blended_aol_token && !source_current)) {
blended_aol_clear();
} else if (((blended_aol_request_mask() & 1U) != 0U) && (controls_allowed || blended_aol_token)) {
} else if (((blended_aol_request_mask() & 1U) != 0U) && (controls_allowed || blended_aol_token || blended_aol_brake_continuation)) {
result = 1U;
} else {
}
@@ -145,6 +153,14 @@ static void blended_aol_rx(const CANPacket_t *msg) {
if (blended_aol_enabled) {
const unsigned int pt = blended_aol_hda2 ? 1U : 0U;
const uint32_t now = microsecond_timer_get();
if (blended_aol_hda2 && !blended_aol_long && msg_matches(msg, 0x394U, pt, 8U)) {
const bool brake = ((msg->data[5] >> 5U) & 3U) == 2U;
if (brake && !brake_pressed && controls_allowed && blended_aol_owned[0] &&
(safety_get_ts_elapsed(now, blended_aol_tx_ts[0]) <= AOL_HOST_REQUEST_TIMEOUT_US) &&
((blended_aol_request_mask() & 1U) != 0U) && aol_rx_healthy()) {
blended_aol_brake_continuation = true;
}
}
if (blended_aol_long && msg_matches(msg, 0x394U, pt, 8U)) {
blended_aol_main_seen = true;
blended_aol_main = ((msg->data[5] >> 3U) & 3U) == 0U;
@@ -323,6 +339,19 @@ static void blended_aol_tx(const CANPacket_t *msg, bool accepted) {
}
}
accepted_current &= active;
if (blended_aol_hda2 && !blended_aol_long) {
const bool zero_torque = (((msg->data[6] & 0xFU) << 7U) | (msg->data[5] >> 1U)) == 1024U;
blended_aol_idle_owned[0] = accepted && !active && zero_torque && aol_rx_healthy() &&
!safety_rx_checks_invalid && !relay_malfunction;
if (!blended_aol_idle_owned[0]) {
blended_aol_idle_owned[1] = false;
}
}
}
if (blended_aol_hda2 && !blended_aol_long && (msg->addr == 0x2A4U) && (msg->bus == 0U) && !retain_camera_ownership) {
blended_aol_idle_owned[1] = accepted && blended_aol_idle_owned[0] && aol_rx_healthy() &&
!safety_rx_checks_invalid && !relay_malfunction &&
(safety_get_ts_elapsed(microsecond_timer_get(), blended_aol_tx_ts[0]) <= AOL_HOST_REQUEST_TIMEOUT_US);
}
for (unsigned int i = 0U; i < count; i++) {
const unsigned int bus = blended_aol_hda2 && (i >= 2U) ? 1U : 0U;
@@ -339,6 +368,16 @@ static bool blended_aol_fwd(int bus, int addr) {
bool blocked = blended_aol_long && !blended_aol_enabled && (bus == 2) &&
((addr == (blended_aol_hda2 ? 0x50 : 0x340)) ||
(addr == (blended_aol_hda2 ? 0x2A4 : 0x364)) || (!blended_aol_hda2 && (addr == 0x485)));
if (blended_aol_enabled && blended_aol_hda2 && !blended_aol_long && (bus == 2) &&
aol_rx_healthy() && !safety_rx_checks_invalid && !relay_malfunction) {
const uint32_t now = microsecond_timer_get();
const bool primary_current = blended_aol_idle_owned[0] &&
(safety_get_ts_elapsed(now, blended_aol_tx_ts[0]) <= AOL_HOST_REQUEST_TIMEOUT_US);
if (primary_current) {
blocked |= (addr == 0x50) || ((addr == 0x2A4) && blended_aol_idle_owned[1] &&
(safety_get_ts_elapsed(now, blended_aol_tx_ts[1]) <= AOL_HOST_REQUEST_TIMEOUT_US));
}
}
if (blended_aol_enabled && (bus == 2) && aol_rx_healthy() && (controls_allowed || (blended_aol_permission_mask() != 0U))) {
const uint32_t addresses[4] = {blended_aol_hda2 ? 0x50U : 0x340U, blended_aol_hda2 ? 0x2A4U : 0x364U, 0x485U, 0x340U};
const unsigned int count = blended_aol_hda2 ? 2U : 3U;
@@ -221,3 +221,47 @@ class TestHyundaiBlendedStock(unittest.TestCase):
self.safety.safety_rx_hook(common.make_msg(0, 0x50, 16))
self.assertTrue(self.safety.get_relay_malfunction())
self.safety.set_alternative_experience(0)
def _start_hda2_aol(self):
self.safety.init_tests()
self.safety.set_alternative_experience(32)
self.assertEqual(self.safety.set_safety_hooks(CarParams.SafetyModel.hyundai, 0x2010), 0)
self.safety.set_aol_test_heartbeat(True)
self.steer_counter = 0
self.bus = 1
self._refresh_hda2_aol_rx(1_000_000)
self.safety.aol_set_host_request(1)
def _refresh_hda2_aol_rx(self, now):
self.safety.set_timer(now)
for status in self.safety.rx_status:
status.msg_seen = status.valid_checksum = status.valid_quality_flag = True
status.last_timestamp = now
def test_hda2_brake_retains_aol_only_after_accepted_steering(self):
for steering_before_brake in (False, True):
self._start_hda2_aol()
self.assertTrue(self.safety.safety_rx_hook(common.make_msg(1, 0x4F1, dat=bytes([1, 0, 0, 0]))))
self.assertTrue(self.safety.safety_rx_hook(self.scc(0, True)))
self.assertTrue(self.safety.get_controls_allowed())
if steering_before_brake:
self.assertTrue(self.safety.safety_tx_hook(self.steer(3)))
self._refresh_hda2_aol_rx(1_010_000)
brake = common.make_msg(1, 0x394, dat=bytes.fromhex('036c000003449798'))
self.assertTrue(self.safety.safety_rx_hook(brake))
self.assertFalse(self.safety.get_controls_allowed())
self.assertEqual(bool(self.safety.safety_tx_hook(self.steer(3))), steering_before_brake)
self.safety.aol_set_host_request(0)
self.safety.aol_set_host_request(1)
self.assertFalse(self.safety.safety_tx_hook(self.steer(3)))
def test_hda2_idle_blocks_duplicate_only_while_host_steering_is_fresh(self):
self._start_hda2_aol()
self.assertEqual(self.safety.safety_fwd_hook(2, 0x50), 0)
self.assertTrue(self.safety.safety_tx_hook(self.steer(0, False)))
self.assertEqual(self.safety.safety_fwd_hook(2, 0x50), -1)
self.assertEqual(self.safety.safety_fwd_hook(2, 0x421), 0)
self._refresh_hda2_aol_rx(1_500_000)
self.assertEqual(self.safety.safety_fwd_hook(2, 0x50), 0)
self.safety.set_relay_malfunction(True)
self.assertEqual(self.safety.safety_fwd_hook(2, 0x50), -1)
Binary file not shown.
@@ -26,8 +26,12 @@ class PalisadeTorquePolicy(HKGShapedTorquePolicy):
def friction(self, setpoint, jerk, speed):
taper = shaping.get_palisade_center_taper_scale(setpoint, speed)
return (shaping.get_palisade_friction_threshold(speed, setpoint, jerk),
1.0 + (shaping.get_palisade_friction_scale(speed, setpoint, jerk) - 1.0) * taper)
threshold = shaping.get_palisade_friction_threshold(speed, setpoint, jerk)
if self.hda2 and speed > 24. and abs(setpoint) < 1.0 and not self.previous_pressed:
highway = float(np.interp(speed, (24., 29.), (0., 1.)))
shallow = float(np.interp(abs(setpoint), (.25, 1.0), (1., 0.)))
threshold += .20 * highway * shallow
return threshold, 1.0 + (shaping.get_palisade_friction_scale(speed, setpoint, jerk) - 1.0) * taper
def output(self, value, setpoint, speed):
return value * shaping.get_palisade_center_output_scale(setpoint, speed, taper_max=self.center_output_taper)
@@ -111,3 +111,20 @@ def test_hda2_softens_rapid_highway_corrections_across_longitudinal_modes(alpha,
assert policy.output_context(value, demand, .2, 0., 31.) == baseline
assert policy.output_context(value, direction, jerk, 0., 31.) == policy.output(value, direction, 31.)
assert policy.output_context(value, demand, jerk, 0., 20.) == policy.output(value, demand, 20.)
@pytest.mark.parametrize('alpha', (False, True))
def test_hda2_softens_shallow_highway_friction_in_both_longitudinal_modes(alpha):
cp = topology_params('hdaii')
cp.openpilotLongitudinalControl, cp.pcmCruise = alpha, not alpha
policy = LatControlTorque(cp.as_reader(), interfaces[cp.carFingerprint](cp), .01).starpilot_extension.policy
baseline = shaping.get_palisade_friction_threshold(30., .4, 1.1)
assert policy.friction(.4, 1.1, 30.)[0] == pytest.approx(baseline + .16)
for speed, setpoint in ((4., .4), (30., 1.0), (24., -.4)):
assert policy.friction(setpoint, 1.1, speed)[0] == shaping.get_palisade_friction_threshold(speed, setpoint, 1.1)
policy.previous_pressed = True
assert policy.friction(.4, 1.1, 30.)[0] == baseline
hda1_cp = topology_params('hdai')
hda1 = LatControlTorque(hda1_cp.as_reader(), interfaces[hda1_cp.carFingerprint](hda1_cp), .01).starpilot_extension.policy
assert hda1.friction(.4, 1.1, 30.)[0] == baseline
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
DEV-7950d25a-Pf38f504f-DEBUG
DEV-004f3491-Pf38f504f-DEBUG
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "03a78a446bdde904d147eaee39cc0e173675ea2686d2c8f459defafcf3b76b80", "schema_version": 1}
{"protected_inventory_sha256": "0bd5326d74f76e28deb02a52aad961f1f03dce20df977361092dc4c55bcda7f7", "schema_version": 1}