Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include "safety_declarations.h" | ||
4 | |||
5 | // Preglobal platform | ||
6 | // 0x161 is ES_CruiseThrottle | ||
7 | // 0x164 is ES_LKAS | ||
8 | |||
9 | #define MSG_SUBARU_PG_CruiseControl 0x144 | ||
10 | #define MSG_SUBARU_PG_Throttle 0x140 | ||
11 | #define MSG_SUBARU_PG_Wheel_Speeds 0xD4 | ||
12 | #define MSG_SUBARU_PG_Brake_Pedal 0xD1 | ||
13 | #define MSG_SUBARU_PG_ES_LKAS 0x164 | ||
14 | #define MSG_SUBARU_PG_ES_Distance 0x161 | ||
15 | #define MSG_SUBARU_PG_Steering_Torque 0x371 | ||
16 | |||
17 | #define SUBARU_PG_MAIN_BUS 0 | ||
18 | #define SUBARU_PG_CAM_BUS 2 | ||
19 | |||
20 | static bool subaru_pg_reversed_driver_torque = false; | ||
21 | |||
22 | 20736 | static void subaru_preglobal_rx_hook(const CANPacket_t *to_push) { | |
23 | 20736 | const int bus = GET_BUS(to_push); | |
24 | |||
25 |
2/2✓ Branch 0 taken 9472 times.
✓ Branch 1 taken 11264 times.
|
20736 | if (bus == SUBARU_PG_MAIN_BUS) { |
26 | 9472 | int addr = GET_ADDR(to_push); | |
27 |
2/2✓ Branch 0 taken 3770 times.
✓ Branch 1 taken 5702 times.
|
9472 | if (addr == MSG_SUBARU_PG_Steering_Torque) { |
28 | int torque_driver_new; | ||
29 | 3770 | torque_driver_new = (GET_BYTE(to_push, 3) >> 5) + (GET_BYTE(to_push, 4) << 3); | |
30 | 3770 | torque_driver_new = to_signed(torque_driver_new, 11); | |
31 |
2/2✓ Branch 0 taken 1885 times.
✓ Branch 1 taken 1885 times.
|
3770 | torque_driver_new = subaru_pg_reversed_driver_torque ? -torque_driver_new : torque_driver_new; |
32 | 3770 | update_sample(&torque_driver, torque_driver_new); | |
33 | } | ||
34 | |||
35 | // enter controls on rising edge of ACC, exit controls on ACC off | ||
36 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 9456 times.
|
9472 | if (addr == MSG_SUBARU_PG_CruiseControl) { |
37 | 16 | bool cruise_engaged = GET_BIT(to_push, 49U); | |
38 | 16 | pcm_cruise_check(cruise_engaged); | |
39 | } | ||
40 | |||
41 | // update vehicle moving with any non-zero wheel speed | ||
42 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 9456 times.
|
9472 | if (addr == MSG_SUBARU_PG_Wheel_Speeds) { |
43 |
3/4✓ Branch 1 taken 12 times.
✓ Branch 2 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
|
16 | vehicle_moving = ((GET_BYTES(to_push, 0, 4) >> 12) != 0U) || (GET_BYTES(to_push, 4, 4) != 0U); |
44 | } | ||
45 | |||
46 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 9446 times.
|
9472 | if (addr == MSG_SUBARU_PG_Brake_Pedal) { |
47 | 26 | brake_pressed = ((GET_BYTES(to_push, 0, 4) >> 16) & 0xFFU) > 0U; | |
48 | } | ||
49 | |||
50 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 9450 times.
|
9472 | if (addr == MSG_SUBARU_PG_Throttle) { |
51 | 22 | gas_pressed = GET_BYTE(to_push, 0) != 0U; | |
52 | } | ||
53 | |||
54 | 9472 | generic_rx_checks((addr == MSG_SUBARU_PG_ES_LKAS)); | |
55 | } | ||
56 | 20736 | } | |
57 | |||
58 | 32766 | static bool subaru_preglobal_tx_hook(const CANPacket_t *to_send) { | |
59 | const SteeringLimits SUBARU_PG_STEERING_LIMITS = { | ||
60 | .max_steer = 2047, | ||
61 | .max_rt_delta = 940, | ||
62 | .max_rt_interval = 250000, | ||
63 | .max_rate_up = 50, | ||
64 | .max_rate_down = 70, | ||
65 | .driver_torque_multiplier = 10, | ||
66 | .driver_torque_allowance = 75, | ||
67 | .type = TorqueDriverLimited, | ||
68 | }; | ||
69 | |||
70 | 32766 | bool tx = true; | |
71 | 32766 | int addr = GET_ADDR(to_send); | |
72 | |||
73 | // steer cmd checks | ||
74 |
2/2✓ Branch 0 taken 32764 times.
✓ Branch 1 taken 2 times.
|
32766 | if (addr == MSG_SUBARU_PG_ES_LKAS) { |
75 | 32764 | int desired_torque = ((GET_BYTES(to_send, 0, 4) >> 8) & 0x1FFFU); | |
76 | 32764 | desired_torque = -1 * to_signed(desired_torque, 13); | |
77 | |||
78 | 32764 | bool steer_req = GET_BIT(to_send, 24U); | |
79 | |||
80 |
2/2✓ Branch 1 taken 16702 times.
✓ Branch 2 taken 16062 times.
|
32764 | if (steer_torque_cmd_checks(desired_torque, steer_req, SUBARU_PG_STEERING_LIMITS)) { |
81 | 16702 | tx = false; | |
82 | } | ||
83 | |||
84 | } | ||
85 | 32766 | return tx; | |
86 | } | ||
87 | |||
88 | 16896 | static int subaru_preglobal_fwd_hook(int bus_num, int addr) { | |
89 | 16896 | int bus_fwd = -1; | |
90 | |||
91 |
2/2✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 11264 times.
|
16896 | if (bus_num == SUBARU_PG_MAIN_BUS) { |
92 | 5632 | bus_fwd = SUBARU_PG_CAM_BUS; // Camera CAN | |
93 | } | ||
94 | |||
95 |
2/2✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 11264 times.
|
16896 | if (bus_num == SUBARU_PG_CAM_BUS) { |
96 |
4/4✓ Branch 0 taken 5630 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5628 times.
|
5632 | bool block_msg = ((addr == MSG_SUBARU_PG_ES_Distance) || (addr == MSG_SUBARU_PG_ES_LKAS)); |
97 |
2/2✓ Branch 0 taken 5628 times.
✓ Branch 1 taken 4 times.
|
5632 | if (!block_msg) { |
98 | 5628 | bus_fwd = SUBARU_PG_MAIN_BUS; // Main CAN | |
99 | } | ||
100 | } | ||
101 | |||
102 | 16896 | return bus_fwd; | |
103 | } | ||
104 | |||
105 | 58 | static safety_config subaru_preglobal_init(uint16_t param) { | |
106 | static const CanMsg SUBARU_PG_TX_MSGS[] = { | ||
107 | {MSG_SUBARU_PG_ES_Distance, SUBARU_PG_MAIN_BUS, 8}, | ||
108 | {MSG_SUBARU_PG_ES_LKAS, SUBARU_PG_MAIN_BUS, 8} | ||
109 | }; | ||
110 | |||
111 | // TODO: do checksum and counter checks after adding the signals to the outback dbc file | ||
112 | static RxCheck subaru_preglobal_rx_checks[] = { | ||
113 | {.msg = {{MSG_SUBARU_PG_Throttle, SUBARU_PG_MAIN_BUS, 8, .frequency = 100U}, { 0 }, { 0 }}}, | ||
114 | {.msg = {{MSG_SUBARU_PG_Steering_Torque, SUBARU_PG_MAIN_BUS, 8, .frequency = 50U}, { 0 }, { 0 }}}, | ||
115 | {.msg = {{MSG_SUBARU_PG_CruiseControl, SUBARU_PG_MAIN_BUS, 8, .frequency = 20U}, { 0 }, { 0 }}}, | ||
116 | }; | ||
117 | |||
118 | 58 | const int SUBARU_PG_PARAM_REVERSED_DRIVER_TORQUE = 4; | |
119 | |||
120 | 58 | subaru_pg_reversed_driver_torque = GET_FLAG(param, SUBARU_PG_PARAM_REVERSED_DRIVER_TORQUE); | |
121 | 58 | return BUILD_SAFETY_CFG(subaru_preglobal_rx_checks, SUBARU_PG_TX_MSGS); | |
122 | } | ||
123 | |||
124 | const safety_hooks subaru_preglobal_hooks = { | ||
125 | .init = subaru_preglobal_init, | ||
126 | .rx = subaru_preglobal_rx_hook, | ||
127 | .tx = subaru_preglobal_tx_hook, | ||
128 | .fwd = subaru_preglobal_fwd_hook, | ||
129 | }; | ||
130 |