Skip to content

Commit

Permalink
Modular Assistive Driving System (MADS) (#57)
Browse files Browse the repository at this point in the history
* 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 9bf0de6.

* 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 f07caaa.

* more rearrange

* rename

* more

* too slow

* Revert "too slow"

This reverts commit 31a249a.

* 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 <[email protected]>

* license

* unused

* remove

* comment

* Apply suggestions from code review

Co-authored-by: DevTekVE <[email protected]>

* 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 346964f.

* 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 7ea27b5.

* 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 <[email protected]>
  • Loading branch information
sunnyhaibin and devtekve authored Dec 15, 2024
1 parent 463bcee commit 3ae376e
Show file tree
Hide file tree
Showing 35 changed files with 1,407 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ jobs:
- name: Build Docker image
run: eval "$BUILD"
- name: Mutation tests
timeout-minutes: 5
timeout-minutes: 15
run: ${{ env.RUN }} "GIT_REF=${{ github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/master-new') && github.event.before || 'origin/master' }} cd tests/safety && ./mutation.sh"

static_analysis:
Expand Down
1 change: 1 addition & 0 deletions board/health.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct __attribute__((packed)) health_t {
uint8_t ignition_line_pkt;
uint8_t ignition_can_pkt;
uint8_t controls_allowed_pkt;
uint8_t controls_allowed_lat_pkt;
uint8_t car_harness_status_pkt;
uint8_t safety_mode_pkt;
uint16_t safety_param_pkt;
Expand Down
2 changes: 2 additions & 0 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ static void tick_handler(void) {
heartbeat_engaged_mismatches = 0U;
}

mads_heartbeat_engaged_check();

if (!heartbeat_disabled) {
// if the heartbeat has been gone for a while, go to SILENT safety mode and enter power save
if (heartbeat_counter >= (check_started() ? HEARTBEAT_IGNITION_CNT_ON : HEARTBEAT_IGNITION_CNT_OFF)) {
Expand Down
3 changes: 3 additions & 0 deletions board/main_comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static int get_health_pkt(void *dat) {
health->ignition_can_pkt = ignition_can;

health->controls_allowed_pkt = controls_allowed;
health->controls_allowed_lat_pkt = mads_is_lateral_control_allowed_by_mads();
health->safety_tx_blocked_pkt = safety_tx_blocked;
health->safety_rx_invalid_pkt = safety_rx_invalid;
health->tx_buffer_overflow_pkt = tx_buffer_overflow;
Expand Down Expand Up @@ -247,6 +248,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
// you can only set this if you are in a non car safety mode
if (!is_car_safety_mode(current_safety_mode)) {
alternative_experience = req->param1;
mads_set_alternative_experience(&alternative_experience);
}
break;
// **** 0xe0: uart read
Expand Down Expand Up @@ -350,6 +352,7 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
heartbeat_lost = false;
heartbeat_disabled = false;
heartbeat_engaged = (req->param1 == 1U);
heartbeat_engaged_mads = true; // FIXME-SP: Implement proper heartbeat check from sunnypilot
break;
}
// **** 0xf6: set siren enabled
Expand Down
18 changes: 13 additions & 5 deletions board/safety.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "sunnypilot/safety_mads.h"

#include "safety_declarations.h"
#include "can.h"

Expand Down Expand Up @@ -111,6 +113,10 @@ uint16_t current_safety_param = 0;
static const safety_hooks *current_hooks = &nooutput_hooks;
safety_config current_safety_config;

static bool is_lat_active(void) {
return controls_allowed || mads_is_lateral_control_allowed_by_mads();
}

static bool is_msg_valid(RxCheck addr_list[], int index) {
bool valid = true;
if (index != -1) {
Expand Down Expand Up @@ -301,6 +307,7 @@ void safety_tick(const safety_config *cfg) {
cfg->rx_checks[i].status.lagging = lagging;
if (lagging) {
controls_allowed = false;
mads_exit_controls(MADS_DISENGAGE_REASON_LAG);
}

if (lagging || !is_msg_valid(cfg->rx_checks, i)) {
Expand Down Expand Up @@ -343,6 +350,7 @@ void generic_rx_checks(bool stock_ecu_detected) {
if ((safety_mode_cnt > RELAY_TRNS_TIMEOUT) && stock_ecu_detected) {
relay_malfunction_set();
}
mads_state_update(vehicle_moving, acc_main_on, controls_allowed, brake_pressed || regen_braking);
}

static void relay_malfunction_reset(void) {
Expand Down Expand Up @@ -601,7 +609,7 @@ bool steer_torque_cmd_checks(int desired_torque, int steer_req, const SteeringLi
bool violation = false;
uint32_t ts = microsecond_timer_get();

if (controls_allowed) {
if (is_lat_active()) {
// *** global torque limit check ***
violation |= max_limit_check(desired_torque, limits.max_steer, -limits.max_steer);

Expand All @@ -628,7 +636,7 @@ bool steer_torque_cmd_checks(int desired_torque, int steer_req, const SteeringLi
}

// no torque if controls is not allowed
if (!controls_allowed && (desired_torque != 0)) {
if (!is_lat_active() && (desired_torque != 0)) {
violation = true;
}

Expand Down Expand Up @@ -670,7 +678,7 @@ bool steer_torque_cmd_checks(int desired_torque, int steer_req, const SteeringLi
}

// reset to 0 if either controls is not allowed or there's a violation
if (violation || !controls_allowed) {
if (violation || !is_lat_active()) {
valid_steer_req_count = 0;
invalid_steer_req_count = 0;
desired_torque_last = 0;
Expand All @@ -686,7 +694,7 @@ bool steer_torque_cmd_checks(int desired_torque, int steer_req, const SteeringLi
bool steer_angle_cmd_checks(int desired_angle, bool steer_control_enabled, const SteeringLimits limits) {
bool violation = false;

if (controls_allowed && steer_control_enabled) {
if (is_lat_active() && steer_control_enabled) {
// convert floating point angle rate limits to integers in the scale of the desired angle on CAN,
// add 1 to not false trigger the violation. also fudge the speed by 1 m/s so rate limits are
// always slightly above openpilot's in case we read an updated speed in between angle commands
Expand Down Expand Up @@ -729,7 +737,7 @@ bool steer_angle_cmd_checks(int desired_angle, bool steer_control_enabled, const
}

// No angle control allowed when controls are not allowed
violation |= !controls_allowed && steer_control_enabled;
violation |= !is_lat_active() && steer_control_enabled;

return violation;
}
Expand Down
28 changes: 27 additions & 1 deletion board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ typedef struct {
const int DAS_6;
const int LKAS_COMMAND;
const int CRUISE_BUTTONS;
const int LKAS_HEARTBIT;
const int Center_Stack_1;
const int Center_Stack_2;
const int TRACTION_BUTTON;
} ChryslerAddrs;

typedef enum {
Expand Down Expand Up @@ -78,6 +82,7 @@ static void chrysler_rx_hook(const CANPacket_t *to_push) {
if ((bus == das_3_bus) && (addr == chrysler_addrs->DAS_3)) {
bool cruise_engaged = GET_BIT(to_push, 21U);
pcm_cruise_check(cruise_engaged);
acc_main_on = GET_BIT(to_push, 20U);
}

// TODO: use the same message for both
Expand All @@ -101,6 +106,19 @@ static void chrysler_rx_hook(const CANPacket_t *to_push) {
brake_pressed = ((GET_BYTE(to_push, 0U) & 0xFU) >> 2U) == 1U;
}

if ((chrysler_platform == CHRYSLER_PACIFICA) && (bus == 0) && (addr == chrysler_addrs->TRACTION_BUTTON)) {
mads_button_press = GET_BIT(to_push, 53U) ? MADS_BUTTON_PRESSED : MADS_BUTTON_NOT_PRESSED;
}

if ((chrysler_platform != CHRYSLER_PACIFICA) && (bus == 0)) {
if (addr == chrysler_addrs->Center_Stack_1) {
mads_button_press = GET_BIT(to_push, 53U) ? MADS_BUTTON_PRESSED : MADS_BUTTON_NOT_PRESSED;
}
if (addr == chrysler_addrs->Center_Stack_2) {
mads_button_press = GET_BIT(to_push, 57U) ? MADS_BUTTON_PRESSED : MADS_BUTTON_NOT_PRESSED;
}
}

generic_rx_checks((bus == 0) && (addr == chrysler_addrs->LKAS_COMMAND));
}

Expand Down Expand Up @@ -175,7 +193,8 @@ static int chrysler_fwd_hook(int bus_num, int addr) {
}

// forward all messages from camera except LKAS messages
const bool is_lkas = ((addr == chrysler_addrs->LKAS_COMMAND) || (addr == chrysler_addrs->DAS_6));
const bool is_lkas_heartbit = (addr == chrysler_addrs->LKAS_HEARTBIT) && (chrysler_platform == CHRYSLER_PACIFICA);
const bool is_lkas = ((addr == chrysler_addrs->LKAS_COMMAND) || (addr == chrysler_addrs->DAS_6) || is_lkas_heartbit);
if ((bus_num == 2) && !is_lkas){
bus_fwd = 0;
}
Expand All @@ -197,6 +216,8 @@ static safety_config chrysler_init(uint16_t param) {
.DAS_6 = 0x2A6, // LKAS HUD and auto headlight control from DASM
.LKAS_COMMAND = 0x292, // LKAS controls from DASM
.CRUISE_BUTTONS = 0x23B, // Cruise control buttons
.LKAS_HEARTBIT = 0x2D9, // LKAS HEARTBIT from DASM
.TRACTION_BUTTON = 0x330, // Traction control button
};

// CAN messages for the 5th gen RAM DT platform
Expand All @@ -209,6 +230,8 @@ static safety_config chrysler_init(uint16_t param) {
.DAS_6 = 0xFA, // LKAS HUD and auto headlight control from DASM
.LKAS_COMMAND = 0xA6, // LKAS controls from DASM
.CRUISE_BUTTONS = 0xB1, // Cruise control buttons
.Center_Stack_1 = 0xDD, // Center stack buttons1
.Center_Stack_2 = 0x28A, // Center stack buttons2
};

static RxCheck chrysler_ram_dt_rx_checks[] = {
Expand All @@ -232,6 +255,7 @@ static safety_config chrysler_init(uint16_t param) {
{CHRYSLER_ADDRS.CRUISE_BUTTONS, 0, 3},
{CHRYSLER_ADDRS.LKAS_COMMAND, 0, 6},
{CHRYSLER_ADDRS.DAS_6, 0, 8},
{CHRYSLER_ADDRS.LKAS_HEARTBIT, 0, 5},
};

static const CanMsg CHRYSLER_RAM_DT_TX_MSGS[] = {
Expand All @@ -251,6 +275,8 @@ static safety_config chrysler_init(uint16_t param) {
.DAS_6 = 0x275, // LKAS HUD and auto headlight control from DASM
.LKAS_COMMAND = 0x276, // LKAS controls from DASM
.CRUISE_BUTTONS = 0x23A, // Cruise control buttons
.Center_Stack_1 = 0x330, // Center stack buttons1
.Center_Stack_2 = 0x28A, // Center stack buttons2
};

static RxCheck chrysler_ram_hd_rx_checks[] = {
Expand Down
5 changes: 5 additions & 0 deletions board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ static void ford_rx_hook(const CANPacket_t *to_push) {
unsigned int cruise_state = GET_BYTE(to_push, 1) & 0x07U;
bool cruise_engaged = (cruise_state == 4U) || (cruise_state == 5U);
pcm_cruise_check(cruise_engaged);
acc_main_on = (cruise_state == 3U) || cruise_engaged;
}

if (addr == FORD_Steering_Data_FD1) {
mads_button_press = GET_BIT(to_push, 40U) ? MADS_BUTTON_PRESSED : MADS_BUTTON_NOT_PRESSED;
}

// If steering controls messages are received on the destination bus, it's an indication
Expand Down
4 changes: 4 additions & 0 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ static void gm_rx_hook(const CANPacket_t *to_push) {
regen_braking = (GET_BYTE(to_push, 0) >> 4) != 0U;
}

if (addr == 0xC9) {
acc_main_on = GET_BIT(to_push, 29U);
}

bool stock_ecu_detected = (addr == 0x180); // ASCMLKASteeringCmd

// Check ASCMGasRegenCmd only if we're blocking it
Expand Down
8 changes: 8 additions & 0 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ static void honda_rx_hook(const CANPacket_t *to_push) {
if (((addr == 0x1A6) || (addr == 0x296)) && (bus == pt_bus)) {
int button = (GET_BYTE(to_push, 0) & 0xE0U) >> 5;

int cruise_setting = (GET_BYTE(to_push, (addr == 0x296) ? 0U : 5U) & 0x0CU) >> 2U;
if (cruise_setting == 1) {
mads_button_press = MADS_BUTTON_PRESSED;
} else if (cruise_setting == 0) {
mads_button_press = MADS_BUTTON_NOT_PRESSED;
} else {
}

// enter controls on the falling edge of set or resume
bool set = (button != HONDA_BTN_SET) && (cruise_button_prev == HONDA_BTN_SET);
bool res = (button != HONDA_BTN_RESUME) && (cruise_button_prev == HONDA_BTN_RESUME);
Expand Down
17 changes: 17 additions & 0 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,23 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) {
hyundai_common_cruise_state_check(cruise_engaged);
}

if ((addr == 0x420) && (((bus == 0) && !hyundai_camera_scc) || ((bus == 2) && hyundai_camera_scc))) {
if (!hyundai_longitudinal) {
acc_main_on = GET_BIT(to_push, 0U);
}
}

if (bus == 0) {
if (addr == 0x251) {
int torque_driver_new = (GET_BYTES(to_push, 0, 2) & 0x7ffU) - 1024U;
// update array of samples
update_sample(&torque_driver, torque_driver_new);
}

if (addr == 0x391) {
mads_button_press = GET_BIT(to_push, 4U) ? MADS_BUTTON_PRESSED : MADS_BUTTON_NOT_PRESSED;
}

// ACC steering wheel buttons
if (addr == 0x4F1) {
int cruise_button = GET_BYTE(to_push, 0) & 0x7U;
Expand Down Expand Up @@ -170,6 +180,8 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) {
}
generic_rx_checks(stock_ecu_detected);
}

hyundai_common_reset_acc_main_on_mismatches();
}

static bool hyundai_tx_hook(const CANPacket_t *to_send) {
Expand All @@ -190,6 +202,11 @@ static bool hyundai_tx_hook(const CANPacket_t *to_send) {
}
}

if (addr == 0x420) {
acc_main_on_tx = GET_BIT(to_send, 0U);
hyundai_common_acc_main_on_sync();
}

// ACCEL: safety check
if (addr == 0x421) {
int desired_accel_raw = (((GET_BYTE(to_send, 4) & 0x7U) << 8) | GET_BYTE(to_send, 3)) - 1023U;
Expand Down
8 changes: 8 additions & 0 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
if (addr == 0x1cf) {
cruise_button = GET_BYTE(to_push, 2) & 0x7U;
main_button = GET_BIT(to_push, 19U);
mads_button_press = GET_BIT(to_push, 23U) ? MADS_BUTTON_PRESSED : MADS_BUTTON_NOT_PRESSED;
} else {
cruise_button = (GET_BYTE(to_push, 4) >> 4) & 0x7U;
main_button = GET_BIT(to_push, 34U);
mads_button_press = GET_BIT(to_push, 39U) ? MADS_BUTTON_PRESSED : MADS_BUTTON_NOT_PRESSED;
}
hyundai_common_cruise_buttons_check(cruise_button, main_button);
}
Expand Down Expand Up @@ -105,6 +107,7 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
int cruise_status = ((GET_BYTE(to_push, 8) >> 4) & 0x7U);
bool cruise_engaged = (cruise_status == 1) || (cruise_status == 2);
hyundai_common_cruise_state_check(cruise_engaged);
acc_main_on = GET_BIT(to_push, 66U);
}
}

Expand All @@ -118,6 +121,8 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
}
generic_rx_checks(stock_ecu_detected);

hyundai_common_reset_acc_main_on_mismatches();

}

static bool hyundai_canfd_tx_hook(const CANPacket_t *to_send) {
Expand Down Expand Up @@ -192,6 +197,9 @@ static bool hyundai_canfd_tx_hook(const CANPacket_t *to_send) {
if (violation) {
tx = false;
}

acc_main_on_tx = GET_BIT(to_send, 66U);
hyundai_common_acc_main_on_sync();
}

return tx;
Expand Down
Loading

0 comments on commit 3ae376e

Please sign in to comment.