Skip to content

Commit

Permalink
MADS: state check cleanup (#15)
Browse files Browse the repository at this point in the history
* MADS: cruise main button check cleanup

* exit and resume controls cleanup

* shorten and simplier

* Update var names

* simplify LKAS button press

* more cleanup

* Hyundai longitudinal: revert back for now

* Add todo for future PR
  • Loading branch information
sunnyhaibin authored Aug 20, 2023
1 parent 59f157d commit 446ad94
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 188 deletions.
40 changes: 6 additions & 34 deletions board/safety.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "safety_declarations.h"
#include "can_definitions.h"

#include "safety_sunnypilot_common.h"

// include the safety policies.
#include "safety/safety_defaults.h"
#include "safety/safety_honda.h"
Expand Down Expand Up @@ -269,47 +271,17 @@ void generic_rx_checks(bool stock_ecu_detected) {

// exit controls on rising edge of brake press
if (brake_pressed && (!brake_pressed_prev || vehicle_moving)) {
if (alternative_experience & ALT_EXP_MADS_DISABLE_DISENGAGE_LATERAL_ON_BRAKE) {
disengageFromBrakes = true;
controls_allowed_long = 0;
} else if (alternative_experience & ALT_EXP_ENABLE_MADS) {
if (controls_allowed == 1) {
disengageFromBrakes = true;
}
controls_allowed = 0;
controls_allowed_long = 0;
} else {
controls_allowed = 0;
controls_allowed_long = 0;
}
mads_exit_controls_check();
} else if (!brake_pressed && disengageFromBrakes) {
disengageFromBrakes = false;
if (alternative_experience & ALT_EXP_ENABLE_MADS) {
controls_allowed = 1;
}
mads_resume_controls_check();
}
brake_pressed_prev = brake_pressed;

// exit controls on rising edge of regen paddle
if (regen_braking && (!regen_braking_prev || vehicle_moving)) {
if (alternative_experience & ALT_EXP_MADS_DISABLE_DISENGAGE_LATERAL_ON_BRAKE) {
disengageFromBrakes = true;
controls_allowed_long = 0;
} else if (alternative_experience & ALT_EXP_ENABLE_MADS) {
if (controls_allowed == 1) {
disengageFromBrakes = true;
}
controls_allowed = 0;
controls_allowed_long = 0;
} else {
controls_allowed = 0;
controls_allowed_long = 0;
}
mads_exit_controls_check();
} else if (!regen_braking && disengageFromBrakes) {
disengageFromBrakes = false;
if (alternative_experience & ALT_EXP_ENABLE_MADS) {
controls_allowed = 1;
}
mads_resume_controls_check();
}
regen_braking_prev = regen_braking;

Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,7 @@ static int chrysler_rx_hook(CANPacket_t *to_push) {
pcm_cruise_check(cruise_engaged);

acc_main_on = GET_BIT(to_push, 20U) != 0U;
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

// TODO: use the same message for both
Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,7 @@ static int ford_rx_hook(CANPacket_t *to_push) {
pcm_cruise_check(cruise_engaged);

acc_main_on = (cruise_state == 3U) || (cruise_state == 4U) || (cruise_state == 5U);
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

// If steering controls messages are received on the destination bus, it's an indication
Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,7 @@ static int gm_rx_hook(CANPacket_t *to_push) {

if (addr == 0xC9) {
acc_main_on = GET_BIT(to_push, 29U) != 0U;
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

bool stock_ecu_detected = (addr == 384); // ASCMLKASteeringCmd
Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,7 @@ static int honda_rx_hook(CANPacket_t *to_push) {
// 0x326 for all Bosch and some Nidec, 0x1A6 for some Nidec
if ((addr == 0x326) || (addr == 0x1A6)) {
acc_main_on = GET_BIT(to_push, ((addr == 0x326) ? 28U : 47U));
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

// state machine to enter and exit controls for button enabling
Expand Down
39 changes: 6 additions & 33 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,7 @@ static int hyundai_rx_hook(CANPacket_t *to_push) {
// ACC main state
if (!hyundai_longitudinal && (addr == 1056)) {
acc_main_on = GET_BIT(to_push, 0U);
// ACC main state
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}
if (addr == 1057) {
// 2 bits: 13-14
Expand All @@ -232,12 +223,9 @@ static int hyundai_rx_hook(CANPacket_t *to_push) {
update_sample(&torque_driver, torque_driver_new);
}

if (addr == 913) {
if ((addr == 913) && hyundai_lfa_button && mads_enabled) {
bool lfa_pressed = GET_BIT(to_push, 4U); // LFA_PRESSED signal
if (lfa_pressed && !lfa_pressed_prev && hyundai_lfa_button && mads_enabled) {
controls_allowed = 1;
}
lfa_pressed_prev = lfa_pressed;
mads_lkas_button_check(lfa_pressed);
}

// ACC steering wheel buttons
Expand All @@ -249,6 +237,7 @@ static int hyundai_rx_hook(CANPacket_t *to_push) {
bool main_enabled_prev = false;
hyundai_common_cruise_buttons_check(cruise_button, main_button);

// TODO: Refactor this method at a later time
if (hyundai_longitudinal) {
// enter controls on rising edge of main
if (main_button && !main_button_prev) {
Expand All @@ -270,28 +259,12 @@ static int hyundai_rx_hook(CANPacket_t *to_push) {
if (hyundai_non_scc) {
if (addr == 871) {
bool cruise_engaged = GET_BYTE(to_push, 0) != 0U; // CF_Lvr_CruiseSet signal
if (cruise_engaged && !cruise_engaged_prev) {
controls_allowed = 1;
controls_allowed_long = 1;
}

if (!cruise_engaged) {
controls_allowed_long = 0;
}
cruise_engaged_prev = cruise_engaged;
hyundai_common_cruise_state_check(cruise_engaged);
}

if (addr == 608) {
acc_main_on = GET_BIT(to_push, 25U); // CRUISE_LAMP_M signal
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}
}

Expand Down
5 changes: 2 additions & 3 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,9 @@ static int hyundai_canfd_rx_hook(CANPacket_t *to_push) {
}
hyundai_common_cruise_buttons_check(cruise_button, main_button);

if (lfa_pressed && !lfa_pressed_prev && mads_enabled) {
controls_allowed = 1;
if (mads_enabled) {
mads_lkas_button_check(lfa_pressed);
}
lfa_pressed_prev = lfa_pressed;
}

// gas press, different for EV, hybrid, and ICE models
Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_mazda.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,7 @@ static int mazda_rx_hook(CANPacket_t *to_push) {
pcm_cruise_check(cruise_engaged);

acc_main_on = GET_BIT(to_push, 17U) != 0U;
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

if (addr == MAZDA_ENGINE_DATA) {
Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_nissan.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,7 @@ static int nissan_rx_hook(CANPacket_t *to_push) {
if ((addr == 0x239) && (bus == 0)) {
acc_main_on = GET_BIT(to_push, 17U) != 0U;
}
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

generic_rx_checks((addr == 0x169) && (bus == 0));
Expand Down
17 changes: 3 additions & 14 deletions board/safety/safety_subaru.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,9 @@ static int subaru_rx_hook(CANPacket_t *to_push) {
update_sample(&torque_driver, torque_driver_new);
}

if ((addr == MSG_SUBARU_ES_LKAS_State) && (bus == SUBARU_CAM_BUS)) {
if ((addr == MSG_SUBARU_ES_LKAS_State) && (bus == SUBARU_CAM_BUS) && mads_enabled) {
bool lkas_pressed = (GET_BYTE(to_push, 2) & 0x0C) > 0; // LKAS_Dash_State signal
if (lkas_pressed && !lkas_pressed_prev && mads_enabled) {
controls_allowed = 1;
}
lkas_pressed_prev = lkas_pressed;
mads_lkas_button_check(lkas_pressed);
}

// enter controls on rising edge of ACC, exit controls on ACC off
Expand All @@ -126,15 +123,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) {
pcm_cruise_check(cruise_engaged);

acc_main_on = GET_BIT(to_push, 40U) != 0U;
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

// update vehicle moving with any non-zero wheel speed
Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_subaru_preglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ static int subaru_preglobal_rx_hook(CANPacket_t *to_push) {
pcm_cruise_check(cruise_engaged);

acc_main_on = GET_BIT(to_push, 48U) != 0U;
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

// update vehicle moving with any non-zero wheel speed
Expand Down
26 changes: 6 additions & 20 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,16 @@ static int toyota_rx_hook(CANPacket_t *to_push) {
bool valid = addr_safety_check(to_push, &toyota_rx_checks,
toyota_get_checksum, toyota_compute_checksum, NULL, NULL);

if (valid && (GET_BUS(to_push) == 2U)) {
if (valid && (GET_BUS(to_push) == 2U) && mads_enabled) {
int addr = GET_ADDR(to_push);
if ((addr == 0x412) && !toyota_mads_lta_msg) {
bool set_me = (GET_BYTE(to_push, 0) & 0xC0) > 0; // LKAS_HUD
if (set_me && !set_me_prev && mads_enabled) {
controls_allowed = 1;
}
set_me_prev = set_me;
bool lkas_pressed = (GET_BYTE(to_push, 0) & 0xC0) > 0; // LKAS_HUD
mads_lkas_button_check(lkas_pressed);
}

if ((addr == 0x412) && toyota_mads_lta_msg) {
bool set_me = (GET_BYTE(to_push, 3) & 0x40) > 0; // LDA_ON_MESSAGE
if (set_me && !set_me_prev && mads_enabled) {
controls_allowed = 1;
}
set_me_prev = set_me;
bool lkas_pressed = (GET_BYTE(to_push, 3) & 0x40) > 0; // LDA_ON_MESSAGE
mads_lkas_button_check(lkas_pressed);
}
} else if (valid && (GET_BUS(to_push) == 0U)) {
int addr = GET_ADDR(to_push);
Expand Down Expand Up @@ -131,15 +125,7 @@ static int toyota_rx_hook(CANPacket_t *to_push) {

if (addr == 0x1D3) {
acc_main_on = GET_BIT(to_push, 15U) != 0U;
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

if (addr == 0xaa) {
Expand Down
11 changes: 1 addition & 10 deletions board/safety/safety_volkswagen_mqb.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,7 @@ static int volkswagen_mqb_rx_hook(CANPacket_t *to_push) {
pcm_cruise_check(cruise_engaged);
}

if (acc_main_on && mads_enabled) {
controls_allowed = true;
}

if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = false;
controls_allowed_long = false;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

if (addr == MSG_GRA_ACC_01) {
Expand Down
10 changes: 1 addition & 9 deletions board/safety/safety_volkswagen_pq.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,7 @@ static int volkswagen_pq_rx_hook(CANPacket_t *to_push) {
// ACC main switch on is a prerequisite to enter controls, exit controls immediately on main switch off
// Signal: Motor_5.GRA_Hauptschalter
acc_main_on = GET_BIT(to_push, 50U);
if (acc_main_on && mads_enabled) {
controls_allowed = 1;
}
if (!acc_main_on && acc_main_on_prev) {
disengageFromBrakes = false;
controls_allowed = 0;
controls_allowed_long = 0;
}
acc_main_on_prev = acc_main_on;
mads_acc_main_check(acc_main_on);
}

if (addr == MSG_GRA_NEU) {
Expand Down
2 changes: 0 additions & 2 deletions board/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ bool regen_braking = false;
bool regen_braking_prev = false;
bool cruise_engaged_prev = false;
bool acc_main_on_prev = false;
bool lfa_pressed_prev = false;
bool set_me_prev = false;
bool lkas_pressed_prev = false;
struct sample_t vehicle_speed;
bool vehicle_moving = false;
Expand Down
Loading

0 comments on commit 446ad94

Please sign in to comment.