Skip to content

Commit 83bcaa3

Browse files
authored
small logic cleanup (#118)
1 parent 9d92bf2 commit 83bcaa3

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

board/safety/safety_toyota.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ int cruise_engaged_last = 0; // cruise state
3232
static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
3333
// get eps motor torque (0.66 factor in dbc)
3434
if ((to_push->RIR>>21) == 0x260) {
35-
int16_t torque_meas_new_16 = (((to_push->RDHR) & 0xFF00) | ((to_push->RDHR >> 16) & 0xFF));
35+
int torque_meas_new = (((to_push->RDHR) & 0xFF00) | ((to_push->RDHR >> 16) & 0xFF));
36+
torque_meas_new = to_signed(torque_meas_new, 16);
37+
38+
// scale by dbc_factor
39+
torque_meas_new *= dbc_eps_torque_factor / 100;
3640

3741
// increase torque_meas by 1 to be conservative on rounding
38-
int torque_meas_new = ((int)(torque_meas_new_16) * dbc_eps_torque_factor / 100) + (torque_meas_new_16 > 0 ? 1 : -1);
42+
torque_meas_new += (torque_meas_new > 0 ? 1 : -1);
3943

4044
// update array of sample
4145
update_sample(&torque_meas, torque_meas_new);
@@ -88,7 +92,6 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
8892
if (desired_torque < -MAX_TORQUE) violation = 1;
8993
if (desired_torque > MAX_TORQUE) violation = 1;
9094

91-
9295
// *** torque rate limit check ***
9396
int16_t highest_allowed_torque = max(desired_torque_last, 0) + MAX_RATE_UP;
9497
int16_t lowest_allowed_torque = min(desired_torque_last, 0) - MAX_RATE_UP;

0 commit comments

Comments
 (0)