diff --git a/board/drivers/fan.h b/board/drivers/fan.h index 7fcb5cc5d0b..6c265f2ea3a 100644 --- a/board/drivers/fan.h +++ b/board/drivers/fan.h @@ -11,8 +11,6 @@ struct fan_state_t { } fan_state_t; struct fan_state_t fan_state; -const float FAN_I = 0.001f; - const uint8_t FAN_TICK_FREQ = 8U; const uint8_t FAN_STALL_THRESHOLD_MIN = 3U; const uint8_t FAN_STALL_THRESHOLD_MAX = 8U; @@ -31,6 +29,8 @@ void fan_init(void) { // Call this at FAN_TICK_FREQ void fan_tick(void) { + const float FAN_I = 0.001f; + if (current_board->fan_max_rpm > 0U) { // Measure fan RPM uint16_t fan_rpm_fast = fan_state.tach_counter * (60U * FAN_TICK_FREQ / 4U); // 4 interrupts per rotation diff --git a/board/provision.h b/board/provision.h index 02768c93d09..80ee688e9b4 100644 --- a/board/provision.h +++ b/board/provision.h @@ -3,9 +3,9 @@ #define PROVISION_CHUNK_LEN 0x20 -const char unprovisioned_text[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; - void get_provision_chunk(uint8_t *resp) { + const char unprovisioned_text[] = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"; + (void)memcpy(resp, (uint8_t *)PROVISION_CHUNK_ADDRESS, PROVISION_CHUNK_LEN); if (memcmp(resp, unprovisioned_text, 0x20) == 0) { (void)memcpy(resp, "unprovisioned\x00\x00\x00testing123\x00\x00\xa3\xa6\x99\xec", 0x20); diff --git a/board/safety/safety_chrysler.h b/board/safety/safety_chrysler.h index be27832c9dd..a8ff16de0fc 100644 --- a/board/safety/safety_chrysler.h +++ b/board/safety/safety_chrysler.h @@ -1,33 +1,3 @@ -const SteeringLimits CHRYSLER_STEERING_LIMITS = { - .max_steer = 261, - .max_rt_delta = 112, - .max_rt_interval = 250000, - .max_rate_up = 3, - .max_rate_down = 3, - .max_torque_error = 80, - .type = TorqueMotorLimited, -}; - -const SteeringLimits CHRYSLER_RAM_DT_STEERING_LIMITS = { - .max_steer = 350, - .max_rt_delta = 112, - .max_rt_interval = 250000, - .max_rate_up = 6, - .max_rate_down = 6, - .max_torque_error = 80, - .type = TorqueMotorLimited, -}; - -const SteeringLimits CHRYSLER_RAM_HD_STEERING_LIMITS = { - .max_steer = 361, - .max_rt_delta = 182, - .max_rt_interval = 250000, - .max_rate_up = 14, - .max_rate_down = 14, - .max_torque_error = 80, - .type = TorqueMotorLimited, -}; - typedef struct { const int EPS_2; const int ESP_1; @@ -215,6 +185,36 @@ static void chrysler_rx_hook(const CANPacket_t *to_push) { } static bool chrysler_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits CHRYSLER_STEERING_LIMITS = { + .max_steer = 261, + .max_rt_delta = 112, + .max_rt_interval = 250000, + .max_rate_up = 3, + .max_rate_down = 3, + .max_torque_error = 80, + .type = TorqueMotorLimited, + }; + + const SteeringLimits CHRYSLER_RAM_DT_STEERING_LIMITS = { + .max_steer = 350, + .max_rt_delta = 112, + .max_rt_interval = 250000, + .max_rate_up = 6, + .max_rate_down = 6, + .max_torque_error = 80, + .type = TorqueMotorLimited, + }; + + const SteeringLimits CHRYSLER_RAM_HD_STEERING_LIMITS = { + .max_steer = 361, + .max_rt_delta = 182, + .max_rt_interval = 250000, + .max_rate_up = 14, + .max_rate_down = 14, + .max_torque_error = 80, + .type = TorqueMotorLimited, + }; + bool tx = true; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_gm.h b/board/safety/safety_gm.h index 09ac34ecbd0..e33886d8946 100644 --- a/board/safety/safety_gm.h +++ b/board/safety/safety_gm.h @@ -1,28 +1,3 @@ -const SteeringLimits GM_STEERING_LIMITS = { - .max_steer = 300, - .max_rate_up = 10, - .max_rate_down = 15, - .driver_torque_allowance = 65, - .driver_torque_factor = 4, - .max_rt_delta = 128, - .max_rt_interval = 250000, - .type = TorqueDriverLimited, -}; - -const LongitudinalLimits GM_ASCM_LONG_LIMITS = { - .max_gas = 3072, - .min_gas = 1404, - .inactive_gas = 1404, - .max_brake = 400, -}; - -const LongitudinalLimits GM_CAM_LONG_LIMITS = { - .max_gas = 3400, - .min_gas = 1514, - .inactive_gas = 1554, - .max_brake = 400, -}; - const LongitudinalLimits *gm_long_limits; const int GM_STANDSTILL_THRSLD = 10; // 0.311kph @@ -139,6 +114,17 @@ static void gm_rx_hook(const CANPacket_t *to_push) { } static bool gm_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits GM_STEERING_LIMITS = { + .max_steer = 300, + .max_rate_up = 10, + .max_rate_down = 15, + .driver_torque_allowance = 65, + .driver_torque_factor = 4, + .max_rt_delta = 128, + .max_rt_interval = 250000, + .type = TorqueDriverLimited, + }; + bool tx = true; int addr = GET_ADDR(to_send); @@ -218,6 +204,20 @@ static int gm_fwd_hook(int bus_num, int addr) { } static safety_config gm_init(uint16_t param) { + const LongitudinalLimits GM_ASCM_LONG_LIMITS = { + .max_gas = 3072, + .min_gas = 1404, + .inactive_gas = 1404, + .max_brake = 400, + }; + + const LongitudinalLimits GM_CAM_LONG_LIMITS = { + .max_gas = 3400, + .min_gas = 1514, + .inactive_gas = 1554, + .max_brake = 400, + }; + gm_hw = GET_FLAG(param, GM_PARAM_HW_CAM) ? GM_CAM : GM_ASCM; if (gm_hw == GM_ASCM) { diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index 5a324bff29f..2dc256557ca 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -17,9 +17,6 @@ .has_steer_req_tolerance = true, \ } -const SteeringLimits HYUNDAI_STEERING_LIMITS = HYUNDAI_LIMITS(384, 3, 7); -const SteeringLimits HYUNDAI_STEERING_LIMITS_ALT = HYUNDAI_LIMITS(270, 2, 3); - const LongitudinalLimits HYUNDAI_LONG_LIMITS = { .max_accel = 200, // 1/100 m/s2 .min_accel = -350, // 1/100 m/s2 @@ -215,6 +212,9 @@ static void hyundai_rx_hook(const CANPacket_t *to_push) { } static bool hyundai_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits HYUNDAI_STEERING_LIMITS = HYUNDAI_LIMITS(384, 3, 7); + const SteeringLimits HYUNDAI_STEERING_LIMITS_ALT = HYUNDAI_LIMITS(270, 2, 3); + bool tx = true; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index fb6ccf55a0d..220cf490700 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -1,23 +1,5 @@ #include "safety_hyundai_common.h" -const SteeringLimits HYUNDAI_CANFD_STEERING_LIMITS = { - .max_steer = 270, - .max_rt_delta = 112, - .max_rt_interval = 250000, - .max_rate_up = 2, - .max_rate_down = 3, - .driver_torque_allowance = 250, - .driver_torque_factor = 2, - .type = TorqueDriverLimited, - - // the EPS faults when the steering angle is above a certain threshold for too long. to prevent this, - // we allow setting torque actuation bit to 0 while maintaining the requested torque value for two consecutive frames - .min_valid_request_frames = 89, - .max_invalid_request_frames = 2, - .min_valid_request_rt_interval = 810000, // 810ms; a ~10% buffer on cutting every 90 frames - .has_steer_req_tolerance = true, -}; - const CanMsg HYUNDAI_CANFD_HDA2_TX_MSGS[] = { {0x50, 0, 16}, // LKAS {0x1CF, 1, 8}, // CRUISE_BUTTON @@ -228,6 +210,24 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) { } static bool hyundai_canfd_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits HYUNDAI_CANFD_STEERING_LIMITS = { + .max_steer = 270, + .max_rt_delta = 112, + .max_rt_interval = 250000, + .max_rate_up = 2, + .max_rate_down = 3, + .driver_torque_allowance = 250, + .driver_torque_factor = 2, + .type = TorqueDriverLimited, + + // the EPS faults when the steering angle is above a certain threshold for too long. to prevent this, + // we allow setting torque actuation bit to 0 while maintaining the requested torque value for two consecutive frames + .min_valid_request_frames = 89, + .max_invalid_request_frames = 2, + .min_valid_request_rt_interval = 810000, // 810ms; a ~10% buffer on cutting every 90 frames + .has_steer_req_tolerance = true, + }; + bool tx = true; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_mazda.h b/board/safety/safety_mazda.h index 7c6d8be9c78..dc17eb7f996 100644 --- a/board/safety/safety_mazda.h +++ b/board/safety/safety_mazda.h @@ -12,17 +12,6 @@ #define MAZDA_AUX 1 #define MAZDA_CAM 2 -const SteeringLimits MAZDA_STEERING_LIMITS = { - .max_steer = 800, - .max_rate_up = 10, - .max_rate_down = 25, - .max_rt_delta = 300, - .max_rt_interval = 250000, - .driver_torque_factor = 1, - .driver_torque_allowance = 15, - .type = TorqueDriverLimited, -}; - const CanMsg MAZDA_TX_MSGS[] = {{MAZDA_LKAS, 0, 8}, {MAZDA_CRZ_BTNS, 0, 8}, {MAZDA_LKAS_HUD, 0, 8}}; RxCheck mazda_rx_checks[] = { @@ -69,6 +58,17 @@ static void mazda_rx_hook(const CANPacket_t *to_push) { } static bool mazda_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits MAZDA_STEERING_LIMITS = { + .max_steer = 800, + .max_rate_up = 10, + .max_rate_down = 25, + .max_rt_delta = 300, + .max_rt_interval = 250000, + .driver_torque_factor = 1, + .driver_torque_allowance = 15, + .type = TorqueDriverLimited, + }; + bool tx = true; int addr = GET_ADDR(to_send); int bus = GET_BUS(to_send); diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 581dc50b7f1..5e8c73d3c42 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -16,11 +16,6 @@ .has_steer_req_tolerance = true, \ } - -const SteeringLimits SUBARU_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(2047, 50, 70); -const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(1000, 40, 40); - - const LongitudinalLimits SUBARU_LONG_LIMITS = { .min_gas = 808, // appears to be engine braking .max_gas = 3400, // approx 2 m/s^2 when maxing cruise_rpm and cruise_throttle @@ -180,6 +175,9 @@ static void subaru_rx_hook(const CANPacket_t *to_push) { } static bool subaru_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits SUBARU_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(2047, 50, 70); + const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(1000, 40, 40); + bool tx = true; int addr = GET_ADDR(to_send); bool violation = false; diff --git a/board/safety/safety_subaru_preglobal.h b/board/safety/safety_subaru_preglobal.h index 1047814ac35..7737ee75ed8 100644 --- a/board/safety/safety_subaru_preglobal.h +++ b/board/safety/safety_subaru_preglobal.h @@ -1,14 +1,3 @@ -const SteeringLimits SUBARU_PG_STEERING_LIMITS = { - .max_steer = 2047, - .max_rt_delta = 940, - .max_rt_interval = 250000, - .max_rate_up = 50, - .max_rate_down = 70, - .driver_torque_factor = 10, - .driver_torque_allowance = 75, - .type = TorqueDriverLimited, -}; - // Preglobal platform // 0x161 is ES_CruiseThrottle // 0x164 is ES_LKAS @@ -78,6 +67,17 @@ static void subaru_preglobal_rx_hook(const CANPacket_t *to_push) { } static bool subaru_preglobal_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits SUBARU_PG_STEERING_LIMITS = { + .max_steer = 2047, + .max_rt_delta = 940, + .max_rt_interval = 250000, + .max_rate_up = 50, + .max_rate_down = 70, + .driver_torque_factor = 10, + .driver_torque_allowance = 75, + .type = TorqueDriverLimited, + }; + bool tx = true; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_tesla.h b/board/safety/safety_tesla.h index 652161ff2c1..1a5e8ccdb8b 100644 --- a/board/safety/safety_tesla.h +++ b/board/safety/safety_tesla.h @@ -1,15 +1,3 @@ -const SteeringLimits TESLA_STEERING_LIMITS = { - .angle_deg_to_can = 10, - .angle_rate_up_lookup = { - {0., 5., 15.}, - {10., 1.6, .3} - }, - .angle_rate_down_lookup = { - {0., 5., 15.}, - {10., 7.0, .8} - }, -}; - const LongitudinalLimits TESLA_LONG_LIMITS = { .max_accel = 425, // 2. m/s^2 .min_accel = 287, // -3.52 m/s^2 // TODO: limit to -3.48 @@ -129,6 +117,18 @@ static void tesla_rx_hook(const CANPacket_t *to_push) { static bool tesla_tx_hook(const CANPacket_t *to_send) { + const SteeringLimits TESLA_STEERING_LIMITS = { + .angle_deg_to_can = 10, + .angle_rate_up_lookup = { + {0., 5., 15.}, + {10., 1.6, .3} + }, + .angle_rate_down_lookup = { + {0., 5., 15.}, + {10., 7.0, .8} + }, + }; + bool tx = true; int addr = GET_ADDR(to_send); bool violation = false; diff --git a/board/safety/safety_toyota.h b/board/safety/safety_toyota.h index 05f84e46049..d7a7ff31c28 100644 --- a/board/safety/safety_toyota.h +++ b/board/safety/safety_toyota.h @@ -28,8 +28,6 @@ const SteeringLimits TOYOTA_STEERING_LIMITS = { }; const int TOYOTA_LTA_MAX_ANGLE = 1657; // EPS only accepts up to 94.9461 -const int TOYOTA_LTA_MAX_MEAS_TORQUE = 1500; -const int TOYOTA_LTA_MAX_DRIVER_TORQUE = 150; // longitudinal limits const LongitudinalLimits TOYOTA_LONG_LIMITS = { @@ -75,7 +73,6 @@ RxCheck toyota_lta_rx_checks[] = { // safety param flags // first byte is for EPS factor, second is for flags const uint32_t TOYOTA_PARAM_OFFSET = 8U; -const uint32_t TOYOTA_EPS_FACTOR = (1UL << TOYOTA_PARAM_OFFSET) - 1U; const uint32_t TOYOTA_PARAM_ALT_BRAKE = 1UL << TOYOTA_PARAM_OFFSET; const uint32_t TOYOTA_PARAM_STOCK_LONGITUDINAL = 2UL << TOYOTA_PARAM_OFFSET; const uint32_t TOYOTA_PARAM_LTA = 4UL << TOYOTA_PARAM_OFFSET; @@ -184,6 +181,9 @@ static void toyota_rx_hook(const CANPacket_t *to_push) { } static bool toyota_tx_hook(const CANPacket_t *to_send) { + const int TOYOTA_LTA_MAX_DRIVER_TORQUE = 150; + const int TOYOTA_LTA_MAX_MEAS_TORQUE = 1500; + bool tx = true; int addr = GET_ADDR(to_send); int bus = GET_BUS(to_send); @@ -302,6 +302,8 @@ static bool toyota_tx_hook(const CANPacket_t *to_send) { } static safety_config toyota_init(uint16_t param) { + const uint32_t TOYOTA_EPS_FACTOR = (1UL << TOYOTA_PARAM_OFFSET) - 1U; + toyota_alt_brake = GET_FLAG(param, TOYOTA_PARAM_ALT_BRAKE); toyota_stock_longitudinal = GET_FLAG(param, TOYOTA_PARAM_STOCK_LONGITUDINAL); toyota_lta = GET_FLAG(param, TOYOTA_PARAM_LTA); diff --git a/board/safety/safety_volkswagen_mqb.h b/board/safety/safety_volkswagen_mqb.h index d880a69a6e5..a0eb55d6f76 100644 --- a/board/safety/safety_volkswagen_mqb.h +++ b/board/safety/safety_volkswagen_mqb.h @@ -1,25 +1,5 @@ #include "safety_volkswagen_common.h" -// lateral limits -const SteeringLimits VOLKSWAGEN_MQB_STEERING_LIMITS = { - .max_steer = 300, // 3.0 Nm (EPS side max of 3.0Nm with fault if violated) - .max_rt_delta = 75, // 4 max rate up * 50Hz send rate * 250000 RT interval / 1000000 = 50 ; 50 * 1.5 for safety pad = 75 - .max_rt_interval = 250000, // 250ms between real time checks - .max_rate_up = 4, // 2.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) - .max_rate_down = 10, // 5.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) - .driver_torque_allowance = 80, - .driver_torque_factor = 3, - .type = TorqueDriverLimited, -}; - -// longitudinal limits -// acceleration in m/s2 * 1000 to avoid floating point math -const LongitudinalLimits VOLKSWAGEN_MQB_LONG_LIMITS = { - .max_accel = 2000, - .min_accel = -3500, - .inactive_accel = 3010, // VW sends one increment above the max range when inactive -}; - #define MSG_ESP_19 0x0B2 // RX from ABS, for wheel speeds #define MSG_LH_EPS_03 0x09F // RX from EPS, for driver steering torque #define MSG_ESP_05 0x106 // RX from ABS, for brake switch state @@ -197,6 +177,26 @@ static void volkswagen_mqb_rx_hook(const CANPacket_t *to_push) { } static bool volkswagen_mqb_tx_hook(const CANPacket_t *to_send) { + // lateral limits + const SteeringLimits VOLKSWAGEN_MQB_STEERING_LIMITS = { + .max_steer = 300, // 3.0 Nm (EPS side max of 3.0Nm with fault if violated) + .max_rt_delta = 75, // 4 max rate up * 50Hz send rate * 250000 RT interval / 1000000 = 50 ; 50 * 1.5 for safety pad = 75 + .max_rt_interval = 250000, // 250ms between real time checks + .max_rate_up = 4, // 2.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) + .max_rate_down = 10, // 5.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) + .driver_torque_allowance = 80, + .driver_torque_factor = 3, + .type = TorqueDriverLimited, + }; + + // longitudinal limits + // acceleration in m/s2 * 1000 to avoid floating point math + const LongitudinalLimits VOLKSWAGEN_MQB_LONG_LIMITS = { + .max_accel = 2000, + .min_accel = -3500, + .inactive_accel = 3010, // VW sends one increment above the max range when inactive + }; + int addr = GET_ADDR(to_send); bool tx = true; diff --git a/board/safety/safety_volkswagen_pq.h b/board/safety/safety_volkswagen_pq.h index de147cb58ad..d95207023cd 100644 --- a/board/safety/safety_volkswagen_pq.h +++ b/board/safety/safety_volkswagen_pq.h @@ -1,25 +1,5 @@ #include "safety_volkswagen_common.h" -// lateral limits -const SteeringLimits VOLKSWAGEN_PQ_STEERING_LIMITS = { - .max_steer = 300, // 3.0 Nm (EPS side max of 3.0Nm with fault if violated) - .max_rt_delta = 113, // 6 max rate up * 50Hz send rate * 250000 RT interval / 1000000 = 75 ; 125 * 1.5 for safety pad = 113 - .max_rt_interval = 250000, // 250ms between real time checks - .max_rate_up = 6, // 3.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) - .max_rate_down = 10, // 5.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) - .driver_torque_factor = 3, - .driver_torque_allowance = 80, - .type = TorqueDriverLimited, -}; - -// longitudinal limits -// acceleration in m/s2 * 1000 to avoid floating point math -const LongitudinalLimits VOLKSWAGEN_PQ_LONG_LIMITS = { - .max_accel = 2000, - .min_accel = -3500, - .inactive_accel = 3010, // VW sends one increment above the max range when inactive -}; - #define MSG_LENKHILFE_3 0x0D0 // RX from EPS, for steering angle and driver steering torque #define MSG_HCA_1 0x0D2 // TX by OP, Heading Control Assist steering torque #define MSG_BREMSE_1 0x1A0 // RX from ABS, for ego speed @@ -170,6 +150,26 @@ static void volkswagen_pq_rx_hook(const CANPacket_t *to_push) { } static bool volkswagen_pq_tx_hook(const CANPacket_t *to_send) { + // lateral limits + const SteeringLimits VOLKSWAGEN_PQ_STEERING_LIMITS = { + .max_steer = 300, // 3.0 Nm (EPS side max of 3.0Nm with fault if violated) + .max_rt_delta = 113, // 6 max rate up * 50Hz send rate * 250000 RT interval / 1000000 = 75 ; 125 * 1.5 for safety pad = 113 + .max_rt_interval = 250000, // 250ms between real time checks + .max_rate_up = 6, // 3.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) + .max_rate_down = 10, // 5.0 Nm/s RoC limit (EPS rack has own soft-limit of 5.0 Nm/s) + .driver_torque_factor = 3, + .driver_torque_allowance = 80, + .type = TorqueDriverLimited, + }; + + // longitudinal limits + // acceleration in m/s2 * 1000 to avoid floating point math + const LongitudinalLimits VOLKSWAGEN_PQ_LONG_LIMITS = { + .max_accel = 2000, + .min_accel = -3500, + .inactive_accel = 3010, // VW sends one increment above the max range when inactive + }; + int addr = GET_ADDR(to_send); bool tx = true;