allow disabling heartbeat while in non-car safety mode (#1035)

* allow disabling heartbeat while in non-car safety mode

* simple test

* clean that up

* remove that
This commit is contained in:
Adeeb Shihadeh 2022-08-17 22:42:18 -07:00 committed by GitHub
parent ba8772123f
commit 7e843b370d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 13 deletions

View File

@ -131,6 +131,7 @@ void set_safety_mode(uint16_t mode, uint16_t param) {
bool is_car_safety_mode(uint16_t mode) {
return (mode != SAFETY_SILENT) &&
(mode != SAFETY_NOOUTPUT) &&
(mode != SAFETY_ALLOUTPUT) &&
(mode != SAFETY_ELM327);
}
@ -192,6 +193,11 @@ void tick_handler(void) {
heartbeat_counter += 1U;
}
// disabling heartbeat not allowed while in safety mode
if (is_car_safety_mode(current_safety_mode)) {
heartbeat_disabled = false;
}
if (siren_countdown > 0U) {
siren_countdown -= 1U;
}

View File

@ -531,12 +531,12 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
case 0xf7:
green_led_enabled = (req->param1 != 0U);
break;
#ifdef ALLOW_DEBUG
// **** 0xf8: disable heartbeat checks
case 0xf8:
heartbeat_disabled = true;
if (!is_car_safety_mode(current_safety_mode)) {
heartbeat_disabled = true;
}
break;
#endif
// **** 0xde: set CAN FD data bitrate
case 0xf9:
if (req->param1 < CAN_CNT) {

View File

@ -34,7 +34,6 @@ if __name__ == "__main__":
panda = Panda()
panda.set_safety_mode(Panda.SAFETY_ELM327)
panda.set_heartbeat_disabled()
print("querying addresses ...")
with tqdm(addrs) as t:
for addr in t:

View File

@ -209,7 +209,7 @@ class Panda:
FLAG_GM_HW_CAM = 1
def __init__(self, serial: Optional[str] = None, claim: bool = True):
def __init__(self, serial: Optional[str] = None, claim: bool = True, disable_checks: bool = True):
self._serial = serial
self._handle = None
self._bcd_device = None
@ -217,6 +217,11 @@ class Panda:
# connect and set mcu type
self.connect(claim)
# disable openpilot's heartbeat checks
if disable_checks:
self.set_heartbeat_disabled()
self.set_power_save(0)
def close(self):
self._handle.close()
self._handle = None
@ -552,11 +557,8 @@ class Panda:
self._handle.controlWrite(Panda.REQUEST_OUT, 0xda, int(bootmode), 0, b'')
time.sleep(0.2)
def set_safety_mode(self, mode=SAFETY_SILENT, param=0, disable_checks=True):
def set_safety_mode(self, mode=SAFETY_SILENT, param=0):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xdc, mode, param, b'')
if disable_checks:
self.set_heartbeat_disabled()
self.set_power_save(0)
def set_gmlan(self, bus=2):
# TODO: check panda type

View File

@ -61,3 +61,24 @@ def test_hw_type(p):
assert pp.get_type() == hw_type, "Bootstub and app hw type mismatch"
assert pp.get_mcu_type() == mcu_type, "Bootstub and app MCU type mismatch"
pp.close()
@test_all_pandas
@panda_connect_and_init
def test_heartbeat(p):
# TODO: add more cases here once the tests aren't super slow
p.set_safety_mode(mode=Panda.SAFETY_HYUNDAI, param=Panda.FLAG_HYUNDAI_LONG)
p.send_heartbeat()
assert p.health()['safety_mode'] == Panda.SAFETY_HYUNDAI
assert p.health()['safety_param'] == Panda.FLAG_HYUNDAI_LONG
# shouldn't do anything once we're in a car safety mode
p.set_heartbeat_disabled()
time.sleep(6)
h = p.health()
assert h['heartbeat_lost']
assert h['safety_mode'] == Panda.SAFETY_SILENT
assert h['safety_param'] == 0
assert h['controls_allowed'] == 0

View File

@ -197,7 +197,6 @@ def panda_connect_and_init(fn=None, full_reset=True):
panda.set_can_speed_kbps(bus, speed)
clear_can_buffers(panda)
panda.set_power_save(False)
panda.set_heartbeat_disabled()
try:
fn(*pandas, *kwargs)

View File

@ -33,7 +33,6 @@ if __name__ == "__main__":
receiver.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
sender.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
sender.set_heartbeat_disabled()
# Start transmisson
threading.Thread(target=flood_tx, args=(sender,)).start()

View File

@ -9,7 +9,6 @@ from panda import Panda # noqa: E402
power = 0
if __name__ == "__main__":
p = Panda()
p.set_heartbeat_disabled()
while True:
p.set_fan_power(power)
time.sleep(5)

View File

@ -46,7 +46,6 @@ if __name__ == "__main__":
receiver.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
sender.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
sender.set_heartbeat_disabled()
# Start transmisson
threading.Thread(target=flood_tx, args=(sender,)).start()