Files
panda-meb/tests/libpanda/safety_helpers.py
Jason Wen 3ae376ecfe Modular Assistive Driving System (MADS) (#57)
* improvements on the state machine for exiting controls and improvements on replay drive, and optimizations

* New tests on hyundai for now to see how they behave with lkas  and main button pressed interchangeably

* cleaning up hyundai tests and ensuring we always cleanup mads states at the end of the tests

* Adjusting tests

* Adjusting the tests a bit more to ensure clean states

* Cleaning up and simplifying logic

* ensuring all tests always cleanup

* improving the state

* make static happy

* Refactor safety replay script for better debugging and update Honda safety code

The commit performs a comprehensive revision of the safety replay script, specifically focusing on introducing debug variables and enhancing the logging capabilities for improved debugging. Furthermore, changes were made to the Honda safety code. The test helpers within libpanda were also expanded for inclusion of additional test conditions.

* Introduce 'ACC_MAIN_OFF' as a new disengagement reason in Sunnypilot's 'safety_mads.h'

The Sunnypilot's 'safety_mads.h' file has been updated to include 'ACC_MAIN_OFF' as a new cause for disconnection in the 'DisengageReason' enumeration. If an 'acc_main_off' signal is received, the 'mads_exit_controls' function halts all requests for lateral control engagement. Additionally, the status of 'controls_requested_lat' now mirrors 'controls_allowed_lat' after a button press.

* simpler logic cleaner

* reorder code for readability

* tmp

* Refactor state transitions and add event handlers

Renamed StateTransition to EdgeTransition for clarity and updated related logic. Introduced event handlers for button presses and ACC state changes, reducing duplicated control flow code. Improved encapsulation and maintainability by restructuring state update functions.

* reorder

* Refactor MADS state handling logic

Removed redundant event handler functions and unnecessary timestamp fields to streamline the code. Simplified button and binary state updates by integrating logic directly into transition checks. Commented out unused fields

* adding some more tests

* split init

* format

* update naming

* Refine lateral control request logic in safety_mads.h

The logic for setting the `controls_requested_lat` variable in safety_mads.h has been refined. Previously, it switched state based on the current value of `controls_allowed_lat`. Now, it also takes into account the current state of `acc_main`, ensuring a more nuanced control request mechanism that accounts for different operational scenarios.

* Fix button state handling in mads_exit_controls logic.

Refactor button state transitions to better handle lateral control requests when ACC is active. Ensure controls are correctly disengaged under specific conditions, by setting `controls_requested_lat` more reliably during state transitions. This change improves safety by preventing inadvertent disengagement when ACC is not active.

* Add test for LKAS button press with ACC main on

This commit introduces a new test to ensure that controls remain enabled when the LKAS/LFA button is pressed while ACC main is on. It checks that LKAS button operations don't interfere with control permissions in this specific configuration, improving test coverage and preventing potential safety issues.

* Add mismatch detection and change mads_acc_main to bool

Enhanced mismatch detection logic by tracking cases where 'controls_allowed' is true while 'controls_allowed_lat' is false, updating the script to print relevant debug information. Additionally, changed the data type of 'mads_acc_main' and 'mads_acc_main_prev' from int to bool for improved type accuracy and consistency.

* update controls_allowed_lat_pkt on health pkt to actually follow is_lat_active() which has the final word on whether we can allow lat or not.

* Can't perform this test on toyota as we never really process a button disengagement for toyota

* wow, we forgot about pcm hyundai can-fd

* nuke nuke nuke

* Revert "nuke nuke nuke"

This reverts commit 9bf0de640a3439ac43c27bcbc6568853966d370b.

* update name

* event driven update states

* add get_pcm_main_cruise_available

* split PCM and non-PCM main cruise tets

* fix some

* pcm main cruise availability mutation

* toyota pass fake lkas btn pressed

* more

* make pcm acc main rising edge on init

* only falling edge when actually 0 (need test for mutation)

* misra

* remove state flags, main button related

* skip lkas related tests with toyota and subaru fake button

* need for honda

* static

* mutation

* misra

* skip nidec pcm alt

* engage mads if controls allowed rising

* static

* remove non pcm properties

* fixup! engage mads if controls allowed rising

* move back

* fix static

* move around

* Hyundai openpilot longitudinal main cruise button state handling

* main button unit test

* acc_main_on mismatch unit tests

* clean up old main cruise button unit tests

* add more reasons

* cleanup

* rename

* rearrange

* Revert "rearrange"

This reverts commit f07caaa5b98b74c23667b387430ac48ba95bf21c.

* more rearrange

* rename

* more

* too slow

* Revert "too slow"

This reverts commit 31a249aebfa9c985e37be050e525b6924ca9e83d.

* too slow v2

* cleanup

* rename

* more cleanup

* Parse more flags from alt exp, more tests, hyundai main cruise allowed

* missed

* mutation for controls allowed rising edge

* ford mutation

* Update tests/safety/test.sh

Co-authored-by: DevTekVE <devtekve@gmail.com>

* license

* unused

* remove

* comment

* Apply suggestions from code review

Co-authored-by: DevTekVE <devtekve@gmail.com>

* comment

* refactor alternative experience handling with helper function

* use always allowed mads button alt exp

* rename

* parenthesis

* use alternative experience for unit tests inits

* cleanup

* rename

* mutation tests for alternative experience flags

* bump timer

* test for disengage and no disengage lateral on brake

* test allow MADS engage with brake pressed

* rename

* move around

* button combo test

* use acc_main_on directly from global

* fix caught failures from last commit's fix

* Revert "use acc_main_on directly from global"

This reverts commit 346964f55d020a287a1a679e22691ad8873e2a64.

* Properly fix lmao

* Add support for LKAS button handling across Chrysler platforms

Introduced LKAS button message parsing for multiple Chrysler platforms, including specific handling for center stack button messages. Updated tests and safety configurations to reflect these changes, ensuring compatibility with different vehicle variants. This enhances modularity and improves safety feature integration.

* Dockerfile: point to sunnypilot/opendbc

* Happy days :)

* clean

* testx

* Revert "Happy days :)"

This reverts commit 7ea27b53c8f3b1e37677c1ce9498229fceac9de6.

* symlink prior building

* comment

* only parse mads lateral, not stock op's lateral

* do not allow controls allowed if acc_main_on is off

* expose system_enabled, do not allow controls allowed to steer if system_mads is off

* fix hyundai tests with acc_main_on requirement

* fix test with new controls allowed with system_mads off

* fix replay drive

* Change 'DISABLE_DISENGAGE_LATERAL_ON_BRAKE' to 'DISENGAGE_LATERAL_ON_BRAKE'

The commit modifies the usage of the 'DISABLE_DISENGAGE_LATERAL_ON_BRAKE' variable globally and replaces it with 'DISENGAGE_LATERAL_ON_BRAKE'. This change promotes correct and clear semantics, since the variable now indicates a state rather than the negation of a state.

* Adding some more debug printouts on replay drive

* remove unified engagement mode in panda

* treat MADS button as user entry

* controls allow should be allowed at all times

* squash! treat MADS button as user entry

* heartbeat for mads

* heartbeat mismatch exit control

* remove always allow mads button from alt

* move to safety_mads

* check heartbeat directly in main

* remove main cruise allowed from alt

* uint

* squash! check heartbeat directly in main

* update tests

* not needed

* fix mads_exit_controls sometimes not assigning disengage reason

* more disengage lateral on brake tests

* extern

* missesd

* honda mutation test

* again

* rename

* more dlob test

* update name

* fix tests

* fix panda tests

* Refactor MADS state management to simplify pointer usage.

This change replaces many pointer-based state variables with direct ones, improving code readability and reducing complexity. It also standardizes the use of `const` for parameters and updates function implementations accordingly. These improvements enhance maintainability and reduce potential for pointer-related errors.

* Simplify braking logic in m_mads_check_braking function

Removed redundant conditions to streamline braking logic. This change maintains functionality while improving code readability and maintainability. Only necessary checks are now performed to determine disengagement.

* Prevent lateral control engagement during braking

Added a condition to disable lateral control engagement when braking with disengage-on-brake enabled. This change is marked as a demonstration and is not final for merging. Moved the disengage_reason to be set only when an actual disengagement occurred.

* Refactor MADS state handling and fix type consistency

Remove redundant `get_mads_state` inline definition and migrate it to a static function. Fix return type syntax in `get_mads_pending_disengage_reason`. Minor formatting adjustments improve readability and code clarity.

Refactor disengagement logic with enhanced reason tracking

Added distinction between active and pending disengagement reasons to improve system state tracking. Updated related enums, structs, and logic to ensure proper handling during control transitions. Added new safety tests to verify behavior under braking and ACC conditions.

Refactoring lateral control permissions and brake checks in MADS

This revision refactors the MADS safety code. The aim is to simplify and improve readability. Operations and checks for brake states and lateral control permissions have been consolidated into fewer methods. In addition, unused 'previous_disengage' state tracking has been removed from MADSState structure to avoid unnecessary state tracking. Moreover, the 'can_allow_controls_lat' function has been removed entirely and its functionality has been incorporated into other functions, reducing the function count and complexity of the code. The braking status is now tracked with BinaryStateTracking for consistency. These changes maintain the system's functionality while optimizing the code and improving maintainability.

* Why MISRA, why!? WHY!???? I DIDNT EVEN TOUCH THIS FILE OR NOWHERE NEAR!

* Some format

* no more messing with misra

* const

* more generic names

* revert to validate

* are you srs

* make gpio.h stock again and add to supression lists the check on gpio.h since we are not even touching it and we don't plan on ever doing so

* hard code to skip heartbeat check

* update comment

* cleanup

* Update tests/safety/test_honda.py

---------

Co-authored-by: DevTekVE <devtekve@gmail.com>
2024-12-15 18:05:31 -05:00

144 lines
5.4 KiB
Python

# panda safety helpers, from safety_helpers.c
from typing import Protocol
def setup_safety_helpers(ffi):
ffi.cdef("""
void set_controls_allowed(bool c);
bool get_controls_allowed(void);
bool get_lat_active(void);
bool get_controls_allowed_lat(void);
bool get_controls_requested_lat(void);
bool get_longitudinal_allowed(void);
void set_alternative_experience(int mode);
int get_alternative_experience(void);
void set_relay_malfunction(bool c);
bool get_relay_malfunction(void);
bool get_gas_pressed_prev(void);
void set_gas_pressed_prev(bool);
bool get_brake_pressed_prev(void);
bool get_regen_braking_prev(void);
bool get_acc_main_on(void);
int get_vehicle_speed_min(void);
int get_vehicle_speed_max(void);
int get_vehicle_speed_last(void);
int get_current_safety_mode(void);
int get_current_safety_param(void);
void set_torque_meas(int min, int max);
int get_torque_meas_min(void);
int get_torque_meas_max(void);
void set_torque_driver(int min, int max);
int get_torque_driver_min(void);
int get_torque_driver_max(void);
void set_desired_torque_last(int t);
void set_rt_torque_last(int t);
void set_desired_angle_last(int t);
int get_desired_angle_last();
void set_angle_meas(int min, int max);
int get_angle_meas_min(void);
int get_angle_meas_max(void);
bool get_cruise_engaged_prev(void);
void set_cruise_engaged_prev(bool engaged);
bool get_vehicle_moving(void);
int get_hw_type(void);
void set_timer(uint32_t t);
void safety_tick_current_safety_config();
bool safety_config_valid();
void init_tests(void);
void set_honda_fwd_brake(bool c);
bool get_honda_fwd_brake(void);
void set_honda_alt_brake_msg(bool c);
void set_honda_bosch_long(bool c);
int get_honda_hw(void);
bool get_enable_mads(void);
bool get_disengage_lateral_on_brake(void);
void set_mads_button_press(int mads_button_press);
void set_controls_allowed_lat(bool c);
void set_controls_requested_lat(bool c);
bool get_mads_acc_main(void);
void set_acc_main_on(bool c);
int get_mads_button_press(void);
void mads_set_current_disengage_reason(int reason);
int mads_get_current_disengage_reason(void);
int get_temp_debug(void);
uint32_t get_acc_main_on_mismatches(void);
void set_mads_params(bool enable_mads, bool disengage_lat_on_brake);
void set_heartbeat_engaged_mads(bool c);
""")
class PandaSafety(Protocol):
def set_controls_allowed(self, c: bool) -> None: ...
def get_controls_allowed(self) -> bool: ...
def set_controls_allowed_lat(self, c: bool) -> None: ...
def set_controls_requested_lat(self, c: bool) -> None: ...
def get_lat_active(self) -> bool: ...
def get_controls_allowed_lat(self) -> bool: ...
def get_controls_requested_lat(self) -> bool: ...
def get_mads_acc_main(self) -> bool: ...
def get_longitudinal_allowed(self) -> bool: ...
def set_alternative_experience(self, mode: int) -> None: ...
def get_alternative_experience(self) -> int: ...
def set_relay_malfunction(self, c: bool) -> None: ...
def get_relay_malfunction(self) -> bool: ...
def get_gas_pressed_prev(self) -> bool: ...
def set_gas_pressed_prev(self, c: bool) -> None: ...
def get_brake_pressed_prev(self) -> bool: ...
def get_regen_braking_prev(self) -> bool: ...
def get_acc_main_on(self) -> bool: ...
def set_acc_main_on(self, c: bool) -> None: ...
def get_vehicle_speed_min(self) -> int: ...
def get_vehicle_speed_max(self) -> int: ...
def get_vehicle_speed_last(self) -> int: ...
def get_current_safety_mode(self) -> int: ...
def get_current_safety_param(self) -> int: ...
def set_torque_meas(self, min: int, max: int) -> None: ... # noqa: A002
def get_torque_meas_min(self) -> int: ...
def get_torque_meas_max(self) -> int: ...
def set_torque_driver(self, min: int, max: int) -> None: ... # noqa: A002
def get_torque_driver_min(self) -> int: ...
def get_torque_driver_max(self) -> int: ...
def set_desired_torque_last(self, t: int) -> None: ...
def set_rt_torque_last(self, t: int) -> None: ...
def set_desired_angle_last(self, t: int) -> None: ...
def get_desired_angle_last(self) -> int: ...
def set_angle_meas(self, min: int, max: int) -> None: ... # noqa: A002
def get_angle_meas_min(self) -> int: ...
def get_angle_meas_max(self) -> int: ...
def get_cruise_engaged_prev(self) -> bool: ...
def set_cruise_engaged_prev(self, enabled: bool) -> None: ...
def get_vehicle_moving(self) -> bool: ...
def get_hw_type(self) -> int: ...
def set_timer(self, t: int) -> None: ...
def safety_tick_current_safety_config(self) -> None: ...
def safety_config_valid(self) -> bool: ...
def init_tests(self) -> None: ...
def set_honda_fwd_brake(self, c: bool) -> None: ...
def get_honda_fwd_brake(self) -> bool: ...
def set_honda_alt_brake_msg(self, c: bool) -> None: ...
def set_honda_bosch_long(self, c: bool) -> None: ...
def get_honda_hw(self) -> int: ...
def set_mads_button_press(self, mads_button_press: int) -> None: ...
def get_enable_mads(self) -> bool: ...
def get_disengage_lateral_on_brake(self) -> bool: ...
def get_mads_button_press(self) -> int: ...
def mads_set_current_disengage_reason(self, reason: int) -> None: ...
def mads_get_current_disengage_reason(self) -> int: ...
def get_acc_main_on_mismatches(self) -> int: ...
def set_mads_params(self, enable_mads: bool, disengage_lat_on_brake: bool) -> None: ...
def set_heartbeat_engaged_mads(self, c: bool) -> None: ...
# def get_temp_debug(self) -> int: ...