|
| 1 | + |
| 2 | +// CAN msgs we care about |
| 3 | +#define MAZDA_LKAS 0x243 |
| 4 | +#define MAZDA_LANEINFO 0x440 |
| 5 | +#define MAZDA_CRZ_CTRL 0x21c |
| 6 | +#define MAZDA_WHEEL_SPEED 0x215 |
| 7 | +#define MAZDA_STEER_TORQUE 0x240 |
| 8 | + |
| 9 | +// CAN bus numbers |
| 10 | +#define MAZDA_MAIN 0 |
| 11 | +#define MAZDA_AUX 1 |
| 12 | +#define MAZDA_CAM 2 |
| 13 | + |
| 14 | +#define MAZDA_MAX_STEER 2048 |
| 15 | + |
| 16 | +// max delta torque allowed for real time checks |
| 17 | +#define MAZDA_MAX_RT_DELTA 940 |
| 18 | +// 250ms between real time checks |
| 19 | +#define MAZDA_RT_INTERVAL 250000 |
| 20 | +#define MAZDA_MAX_RATE_UP 10 |
| 21 | +#define MAZDA_MAX_RATE_DOWN 25 |
| 22 | +#define MAZDA_DRIVER_TORQUE_ALLOWANCE 15 |
| 23 | +#define MAZDA_DRIVER_TORQUE_FACTOR 1 |
| 24 | + |
| 25 | + |
| 26 | +int mazda_cruise_engaged_last = 0; |
| 27 | +int mazda_rt_torque_last = 0; |
| 28 | +int mazda_desired_torque_last = 0; |
| 29 | +uint32_t mazda_ts_last = 0; |
| 30 | +struct sample_t mazda_torque_driver; // last few driver torques measured |
| 31 | + |
| 32 | +// track msgs coming from OP so that we know what CAM msgs to drop and what to forward |
| 33 | +int mazda_op_lkas_detected = 0; |
| 34 | +int mazda_op_laneinfo_detected = 0; |
| 35 | + |
| 36 | +int mazda_forward_cam = 0; |
| 37 | +int mazda_giraffe_switch_2_on = 0; |
| 38 | + |
| 39 | +void mazda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { |
| 40 | + int bus = GET_BUS(to_push); |
| 41 | + int addr = GET_ADDR(to_push); |
| 42 | + |
| 43 | + if ((addr == MAZDA_STEER_TORQUE) && (bus == MAZDA_MAIN)) { |
| 44 | + int torque_driver_new = GET_BYTE(to_push, 0) - 127; |
| 45 | + // update array of samples |
| 46 | + update_sample(&mazda_torque_driver, torque_driver_new); |
| 47 | + } |
| 48 | + |
| 49 | + // enter controls on rising edge of ACC, exit controls on ACC off |
| 50 | + if ((addr == MAZDA_CRZ_CTRL) && (bus == MAZDA_MAIN)) { |
| 51 | + int cruise_engaged = GET_BYTE(to_push, 0) & 8; |
| 52 | + if (cruise_engaged != 0) { |
| 53 | + if (!mazda_cruise_engaged_last) { |
| 54 | + controls_allowed = 1; |
| 55 | + } |
| 56 | + } |
| 57 | + else { |
| 58 | + controls_allowed = 0; |
| 59 | + } |
| 60 | + mazda_cruise_engaged_last = cruise_engaged; |
| 61 | + } |
| 62 | + |
| 63 | + // we have msgs on bus MAZDA_CAM |
| 64 | + if (bus == MAZDA_CAM) { |
| 65 | + // the stock CAM is connected |
| 66 | + if (addr == MAZDA_LKAS) { |
| 67 | + mazda_forward_cam = 1; |
| 68 | + } |
| 69 | + // if we see wheel speed msgs on MAZDA_CAM bus then giraffe switch 2 is high |
| 70 | + // (hardware passthru) |
| 71 | + if (addr == MAZDA_WHEEL_SPEED) { |
| 72 | + mazda_giraffe_switch_2_on = 1; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +static int mazda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { |
| 78 | + int tx = 1; |
| 79 | + int addr = GET_ADDR(to_send); |
| 80 | + int bus = GET_BUS(to_send); |
| 81 | + |
| 82 | + // Check if msg is sent on the main BUS |
| 83 | + if (bus == MAZDA_MAIN) { |
| 84 | + if ((addr == MAZDA_LKAS) && !mazda_op_lkas_detected){ |
| 85 | + mazda_op_lkas_detected = 1; |
| 86 | + } |
| 87 | + if ((addr == MAZDA_LANEINFO) && !mazda_op_laneinfo_detected){ |
| 88 | + mazda_op_laneinfo_detected = 1; |
| 89 | + } |
| 90 | + |
| 91 | + // steer cmd checks |
| 92 | + if (addr == MAZDA_LKAS) { |
| 93 | + int desired_torque = (((GET_BYTE(to_send, 0) & 0x0f) << 8) | GET_BYTE(to_send, 1)) - MAZDA_MAX_STEER; |
| 94 | + bool violation = 0; |
| 95 | + uint32_t ts = TIM2->CNT; |
| 96 | + |
| 97 | + if (controls_allowed) { |
| 98 | + |
| 99 | + // *** global torque limit check *** |
| 100 | + violation |= max_limit_check(desired_torque, MAZDA_MAX_STEER, -MAZDA_MAX_STEER); |
| 101 | + |
| 102 | + // *** torque rate limit check *** |
| 103 | + int desired_torque_last = mazda_desired_torque_last; |
| 104 | + violation |= driver_limit_check(desired_torque, desired_torque_last, &mazda_torque_driver, |
| 105 | + MAZDA_MAX_STEER, MAZDA_MAX_RATE_UP, MAZDA_MAX_RATE_DOWN, |
| 106 | + MAZDA_DRIVER_TORQUE_ALLOWANCE, MAZDA_DRIVER_TORQUE_FACTOR); |
| 107 | + // used next time |
| 108 | + mazda_desired_torque_last = desired_torque; |
| 109 | + |
| 110 | + // *** torque real time rate limit check *** |
| 111 | + violation |= rt_rate_limit_check(desired_torque, mazda_rt_torque_last, MAZDA_MAX_RT_DELTA); |
| 112 | + |
| 113 | + // every RT_INTERVAL set the new limits |
| 114 | + uint32_t ts_elapsed = get_ts_elapsed(ts, mazda_ts_last); |
| 115 | + if (ts_elapsed > ((uint32_t) MAZDA_RT_INTERVAL)) { |
| 116 | + mazda_rt_torque_last = desired_torque; |
| 117 | + mazda_ts_last = ts; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // no torque if controls is not allowed |
| 122 | + if (!controls_allowed && (desired_torque != 0)) { |
| 123 | + violation = 1; |
| 124 | + } |
| 125 | + |
| 126 | + // reset to 0 if either controls is not allowed or there's a violation |
| 127 | + if (violation || !controls_allowed) { |
| 128 | + mazda_desired_torque_last = 0; |
| 129 | + mazda_rt_torque_last = 0; |
| 130 | + mazda_ts_last = ts; |
| 131 | + } |
| 132 | + |
| 133 | + if (violation) { |
| 134 | + tx = 0; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return tx; |
| 139 | +} |
| 140 | + |
| 141 | +static int mazda_fwd_hook(int bus, CAN_FIFOMailBox_TypeDef *to_fwd) { |
| 142 | + int bus_fwd = -1; |
| 143 | + if (mazda_forward_cam && !mazda_giraffe_switch_2_on) { |
| 144 | + int addr = GET_ADDR(to_fwd); |
| 145 | + if (bus == MAZDA_MAIN) { |
| 146 | + bus_fwd = MAZDA_CAM; |
| 147 | + } |
| 148 | + else if (bus == MAZDA_CAM) { |
| 149 | + // drop stock CAM_LKAS and CAM_LANEINFI if OP is sending them |
| 150 | + if (!((addr == MAZDA_LKAS) && mazda_op_lkas_detected) && |
| 151 | + !((addr == MAZDA_LANEINFO) && mazda_op_laneinfo_detected)) { |
| 152 | + bus_fwd = MAZDA_MAIN; |
| 153 | + } |
| 154 | + } |
| 155 | + else { |
| 156 | + bus_fwd = -1; |
| 157 | + } |
| 158 | + } |
| 159 | + return bus_fwd; |
| 160 | +} |
| 161 | + |
| 162 | +const safety_hooks mazda_hooks = { |
| 163 | + .init = nooutput_init, |
| 164 | + .rx = mazda_rx_hook, |
| 165 | + .tx = mazda_tx_hook, |
| 166 | + .tx_lin = nooutput_tx_lin_hook, |
| 167 | + .ignition = default_ign_hook, |
| 168 | + .fwd = mazda_fwd_hook, |
| 169 | +}; |
0 commit comments