Skip to content

Commit 02a87cd

Browse files
committed
2 parents f437ef9 + d75937f commit 02a87cd

6 files changed

Lines changed: 75 additions & 58 deletions

File tree

DASH/Core/User/Inc/FEB_CAN_PCU.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
void FEB_CAN_PCU_Init(void);
1616
int16_t FEB_CAN_PCU_GetLastTorque(void);
1717
int8_t FEB_CAN_PCU_GetLastDirection(void);
18-
int8_t FEB_CAN_PCU_GetLastRMSEnabled(void);
19-
uint16_t FEB_CAN_PCU_GetLastBreakPosition(void);
18+
uint8_t FEB_CAN_PCU_GetLastRMSEnabled(void);
19+
uint16_t FEB_CAN_PCU_GetLastBrakePosition(void);
2020
bool FEB_CAN_PCU_IsRMSDataFresh(uint32_t timeout_ms);
21-
bool FEB_CAN_PCU_IsBreakDataFresh(uint32_t timeout_ms);
21+
bool FEB_CAN_PCU_IsBrakeDataFresh(uint32_t timeout_ms);
2222

2323
#endif /* FEB_CAN_PCU_H */

DASH/Core/User/Inc/FEB_RTD.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111

1212
#include <stdbool.h>
1313

14-
#define RTD_BUTTON_HOLD_DURATION 2000
14+
#define RTD_SAFETY_DURATION 2000
1515
#define BUZZER_DURATION_RTD_ENTER 2000
1616
#define BUZZER_DURATION_RTD_EXIT 500
1717

18-
typedef enum
19-
{
20-
NOT_BUZZED = 0,
21-
BUZZED_ENTER_RTD,
22-
BUZZED_EXIT_RTD
23-
} Buzzing_State_t;
24-
2518
void FEB_State_Update_RTD(void);
2619

2720
bool FEB_State_GetLastRTD(void);

DASH/Core/User/Src/FEB_CAN_PCU.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ static RMS_State_t rms_state = {.torque = 0x0000, .direction = 0xFF, .enabled =
3131

3232
typedef struct
3333
{
34-
volatile uint16_t break_position;
34+
volatile uint16_t brake_position;
3535
volatile uint32_t last_rx_tick;
36-
} Break_State_t;
36+
} Brake_State_t;
3737

38-
static Break_State_t break_state = {.break_position = 0, .last_rx_tick = 0};
38+
static Brake_State_t brake_state = {.brake_position = 0, .last_rx_tick = 0};
3939

4040
/* ============================================================================
4141
* RX Callback Handlers
@@ -59,17 +59,17 @@ static void rx_callback_torque(FEB_CAN_Instance_t instance, uint32_t can_id, FEB
5959
}
6060

6161
// FEB_CAN_RMS_COMMAND_FRAME_ID:
62-
// Byte 0-1: Break Position Centi-percent (int16_t)
62+
// Byte 0-1: Brake Position Centi-percent (int16_t)
6363
// ...
6464

65-
static void rx_callback_break_position(FEB_CAN_Instance_t instance, uint32_t can_id, FEB_CAN_ID_Type_t id_type,
65+
static void rx_callback_brake_position(FEB_CAN_Instance_t instance, uint32_t can_id, FEB_CAN_ID_Type_t id_type,
6666
const uint8_t *data, uint8_t length, void *user_data)
6767
{
6868
uint16_t position;
6969
memcpy(&position, &data[0], sizeof(uint16_t));
70-
break_state.break_position = position;
70+
brake_state.brake_position = position;
7171
__DMB();
72-
break_state.last_rx_tick = HAL_GetTick();
72+
brake_state.last_rx_tick = HAL_GetTick();
7373
}
7474

7575
/* ============================================================================
@@ -90,17 +90,17 @@ void FEB_CAN_PCU_Init(void)
9090
};
9191
FEB_CAN_RX_Register(&rx_params_torque);
9292

93-
// PCU Break Position
94-
FEB_CAN_RX_Params_t rx_params_break_position = {
93+
// PCU Brake Position
94+
FEB_CAN_RX_Params_t rx_params_brake_position = {
9595
.instance = FEB_CAN_INSTANCE_1,
9696
.can_id = FEB_CAN_BRAKE_FRAME_ID,
9797
.id_type = FEB_CAN_ID_STD,
9898
.filter_type = FEB_CAN_FILTER_EXACT,
9999
.mask = 0x7FF,
100100
.fifo = FEB_CAN_FIFO_0,
101-
.callback = rx_callback_break_position,
101+
.callback = rx_callback_brake_position,
102102
};
103-
FEB_CAN_RX_Register(&rx_params_break_position);
103+
FEB_CAN_RX_Register(&rx_params_brake_position);
104104
}
105105

106106
int16_t FEB_CAN_PCU_GetLastTorque(void)
@@ -113,14 +113,14 @@ int8_t FEB_CAN_PCU_GetLastDirection(void)
113113
return rms_state.direction;
114114
}
115115

116-
int8_t FEB_CAN_PCU_GetLastRMSEnabled(void)
116+
uint8_t FEB_CAN_PCU_GetLastRMSEnabled(void)
117117
{
118118
return rms_state.enabled;
119119
}
120120

121-
uint16_t FEB_CAN_PCU_GetLastBreakPosition(void)
121+
uint16_t FEB_CAN_PCU_GetLastBrakePosition(void)
122122
{
123-
return break_state.break_position;
123+
return brake_state.brake_position;
124124
}
125125

126126
bool FEB_CAN_PCU_IsRMSDataFresh(uint32_t timeout_ms)
@@ -131,9 +131,9 @@ bool FEB_CAN_PCU_IsRMSDataFresh(uint32_t timeout_ms)
131131
return (HAL_GetTick() - last) < timeout_ms;
132132
}
133133

134-
bool FEB_CAN_PCU_IsBreakDataFresh(uint32_t timeout_ms)
134+
bool FEB_CAN_PCU_IsBrakeDataFresh(uint32_t timeout_ms)
135135
{
136-
uint32_t last = break_state.last_rx_tick;
136+
uint32_t last = brake_state.last_rx_tick;
137137
if (last == 0)
138138
return false;
139139
return (HAL_GetTick() - last) < timeout_ms;

DASH/Core/User/Src/FEB_CAN_State.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ void FEB_CAN_State_Tick(void)
8585

8686
IO_States_t states = FEB_IO_GetLastIOStates();
8787

88-
struct feb_can_dash_state_t pack_states = {.buzzer = states.buzzer_enabled,
89-
.button1 = states.button_rtd,
90-
.switch1 = states.switch_accumulator_fans,
91-
.switch2 = states.switch_coolant_pump_radiator_fan,
92-
.switch3 = states.switch_logging};
93-
94-
if (feb_can_dash_state_pack(tx_data, &pack_states, 2) == 2)
88+
if (feb_can_dash_state_pack(tx_data,
89+
&((struct feb_can_dash_state_t){.buzzer = states.buzzer_enabled,
90+
.button1 = states.button_rtd,
91+
.switch1 = states.switch_accumulator_fans,
92+
.switch2 = states.switch_coolant_pump_radiator_fan,
93+
.switch3 = states.switch_logging,
94+
.ready_to_drive = FEB_State_GetLastRTD()}),
95+
2) == 2)
9596
{
9697
FEB_CAN_Status_t st = FEB_CAN_TX_Send(FEB_CAN_INSTANCE_1, FEB_CAN_DASH_STATE_FRAME_ID, FEB_CAN_ID_STD, tx_data,
9798
FEB_CAN_DASH_STATE_LENGTH);

DASH/Core/User/Src/FEB_Commands.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ static void cmd_pcu(int argc, char *argv[])
311311

312312
int16_t torque = FEB_CAN_PCU_GetLastTorque();
313313
int8_t direction = FEB_CAN_PCU_GetLastDirection();
314-
int8_t enabled = FEB_CAN_PCU_GetLastRMSEnabled();
315-
uint16_t brake = FEB_CAN_PCU_GetLastBreakPosition();
314+
uint8_t enabled = FEB_CAN_PCU_GetLastRMSEnabled();
315+
uint16_t brake = FEB_CAN_PCU_GetLastBrakePosition();
316316

317317
FEB_Console_Printf("PCU / RMS Status:\r\n");
318318
FEB_Console_Printf(" Torque: %d (raw)\r\n", (int)torque);
@@ -326,7 +326,7 @@ static void cmd_pcu_csv(int argc, char *argv[])
326326
(void)argc;
327327
(void)argv;
328328
FEB_Console_CsvEmit("pcu", "%d,%d,%d,%u", (int)FEB_CAN_PCU_GetLastTorque(), (int)FEB_CAN_PCU_GetLastDirection(),
329-
(int)FEB_CAN_PCU_GetLastRMSEnabled(), (unsigned int)FEB_CAN_PCU_GetLastBreakPosition());
329+
(int)FEB_CAN_PCU_GetLastRMSEnabled(), (unsigned int)FEB_CAN_PCU_GetLastBrakePosition());
330330
}
331331

332332
static const FEB_Console_Cmd_t dash_cmd_pcu = {

DASH/Core/User/Src/FEB_RTD.c

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,77 @@
88

99
static bool rtd = false;
1010

11-
static uint32_t rtd_button_press_start_tick = 0;
11+
static bool rtd_timer_armed = false;
12+
static uint32_t rtd_trying_to_toggle_start_tick = 0;
13+
static bool rtd_toggle_complete = false;
1214

1315
// Minimum brake pressure required for RTD activation (safety interlock)
14-
#define RTD_BRAKE_THRESHOLD 25
15-
static bool previous_rtd_button = false;
16+
#define RTD_BRAKE_THRESHOLD 5000
1617

17-
static Buzzing_State_t buzzing_state =
18-
NOT_BUZZED; // Here to ensure FEB_IO_Play_Buzzer() is not spammed and only called once
18+
// Any RTD input older than this is treated as missing — RTD will not arm on
19+
// stale CAN data. Matches BMS_STATE_TIMEOUT_MS.
20+
#define RTD_INPUT_FRESHNESS_MS 500
1921

2022
void FEB_State_Update_RTD(void)
2123
{
2224
// MARK: Start buzzer code
23-
// to react to BMS entering and exiting drive state
25+
// Buzz on any entry into / exit from BMS_STATE_DRIVE so faults that drop us
26+
// out of drive (DRIVE -> FAULT_*) still trigger the exit chime.
2427
static BMS_State_t previous_bms_state = BMS_STATE_BOOT;
2528
BMS_State_t bms_state = FEB_CAN_BMS_GetLastState();
26-
if (previous_bms_state == BMS_STATE_ENERGIZED && bms_state == BMS_STATE_DRIVE && buzzing_state != BUZZED_ENTER_RTD)
29+
if (previous_bms_state != BMS_STATE_DRIVE && bms_state == BMS_STATE_DRIVE)
2730
{
2831
FEB_IO_Play_Buzzer(BUZZER_DURATION_RTD_ENTER);
29-
buzzing_state = BUZZED_ENTER_RTD;
3032
}
31-
else if (previous_bms_state == BMS_STATE_DRIVE && bms_state == BMS_STATE_ENERGIZED &&
32-
buzzing_state != BUZZED_EXIT_RTD)
33+
else if (previous_bms_state == BMS_STATE_DRIVE && bms_state != BMS_STATE_DRIVE)
3334
{
3435
FEB_IO_Play_Buzzer(BUZZER_DURATION_RTD_EXIT);
35-
buzzing_state = BUZZED_EXIT_RTD;
3636
}
3737
previous_bms_state = bms_state;
3838

3939
// MARK: Signal enter rtd code
4040
// Send the ready to drive message over CAN when all the conditions are met
4141
IO_States_t states = FEB_IO_GetLastIOStates();
42-
uint16_t brake_pressure = FEB_CAN_PCU_GetLastBreakPosition();
43-
int8_t inv_enabled = FEB_CAN_PCU_GetLastRMSEnabled();
42+
uint16_t brake_pressure = FEB_CAN_PCU_GetLastBrakePosition();
43+
uint8_t inv_enabled = FEB_CAN_PCU_GetLastRMSEnabled();
4444

45-
if (previous_rtd_button == false && states.button_rtd)
45+
bool inputs_fresh = FEB_CAN_PCU_IsBrakeDataFresh(RTD_INPUT_FRESHNESS_MS) &&
46+
FEB_CAN_PCU_IsRMSDataFresh(RTD_INPUT_FRESHNESS_MS) &&
47+
FEB_CAN_BMS_IsDataFresh(RTD_INPUT_FRESHNESS_MS);
48+
49+
// RTD requires: fresh CAN inputs, button held, brake applied, inverter enabled, BMS energized
50+
// if (inputs_fresh && (brake_pressure >= RTD_BRAKE_THRESHOLD) && (inv_enabled == 1) &&
51+
// (bms_state == BMS_STATE_ENERGIZED) && states.button_rtd)
52+
if (states.button_rtd)
53+
{
54+
if (!rtd_timer_armed)
55+
{
56+
rtd_timer_armed = true;
57+
rtd_trying_to_toggle_start_tick = HAL_GetTick();
58+
}
59+
}
60+
else
4661
{
47-
rtd_button_press_start_tick = HAL_GetTick();
62+
rtd_timer_armed = false;
4863
}
4964

50-
// RTD requires: button held, brake applied, and inverter enabled
51-
if (states.button_rtd && rtd_button_press_start_tick + RTD_BUTTON_HOLD_DURATION < HAL_GetTick() &&
52-
brake_pressure >= RTD_BRAKE_THRESHOLD && inv_enabled == 1)
65+
if (rtd_timer_armed && HAL_GetTick() - rtd_trying_to_toggle_start_tick >= RTD_SAFETY_DURATION)
5366
{
54-
// FEB_IO_Play_Buzzer(BUZZER_DURATION_RTD_ENTER);
55-
rtd = true;
67+
if (!rtd_toggle_complete)
68+
{
69+
rtd = !rtd;
70+
rtd_toggle_complete = true;
71+
}
72+
}
73+
else
74+
{
75+
rtd_toggle_complete = false;
5676
}
5777

58-
previous_rtd_button = states.button_rtd;
78+
if (bms_state != BMS_STATE_DRIVE && bms_state != BMS_STATE_ENERGIZED)
79+
{
80+
rtd = false; // just in case
81+
}
5982
}
6083

6184
bool FEB_State_GetLastRTD(void)

0 commit comments

Comments
 (0)