Skip to content

Commit 08d4d84

Browse files
committed
tested
2 parents 06a827c + 2faea99 commit 08d4d84

File tree

9 files changed

+147
-30
lines changed

9 files changed

+147
-30
lines changed

Core/Inc/cerberus_conf.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
#define FAULT_HANDLE_DELAY
2222

2323
/* Pedal tuning */
24-
#define PEDALS_SAMPLE_DELAY 10 /* ms */
25-
#define MAX_APPS1_VOLTS 3.02 /* volts */
26-
#define MIN_APPS1_VOLTS 1.75 /* volts */
27-
#define MAX_APPS2_VOLTS 2.28 /* volts */
28-
#define MIN_APPS2_VOLTS 1.01 /* volts */
29-
#define PEDAL_BRAKE_THRESH 0.12 /* percentage */
24+
#define PEDALS_SAMPLE_DELAY 10 /* ms */
25+
#define MAX_APPS1_VOLTS 3.03 /* volts */
26+
#define MIN_APPS1_VOLTS 1.81 /* volts */
27+
#define MAX_APPS2_VOLTS 2.28 /* volts */
28+
#define MIN_APPS2_VOLTS 1.07 /* volts */
29+
#define PEDAL_BRAKE_THRESH 0.20 /* percentage */
30+
#define PEDAL_HARD_BRAKE_THRESH 0.50 /* percentage */
31+
32+
/* brake sensor thresholds */
33+
#define BRAKE_SENSOR_IRREGULAR_HIGH 4.5
34+
#define BRAKE_SENSOR_IRREGULAR_LOW 0.5
3035

3136
/* Torque Tuning */
3237
#define MAX_TORQUE 220 /* Nm */
@@ -36,7 +41,7 @@
3641
#define ACCELERATION_THRESHOLD 0.25
3742

3843
/* Maximum AC braking current */
39-
#define MAX_REGEN_CURRENT 300
44+
#define MAX_REGEN_CURRENT 250
4045

4146
#define STEERING_WHEEL_DEBOUNCE 10 /* ms */
4247

Core/Inc/dti.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,14 @@ osStatus_t dti_get_motor_temp(dti_t *mc, uint16_t *motorTemp);
154154
*/
155155
osStatus_t dti_get_controller_temp(dti_t *mc, uint16_t *controllerTemp);
156156

157+
/**
158+
* @brief Record the currents from DTI
159+
*
160+
* @param mc Pointer to DTI struct
161+
* @param msg CAN message containing currents data
162+
*/
163+
osStatus_t dti_record_currents(dti_t *mc, can_msg_t msg);
164+
165+
osStatus_t dti_get_dc_current(dti_t *mc, int16_t *dc_current);
166+
157167
#endif

Core/Src/can_handler.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static uint16_t id_list_1[4] = {
3333
static uint16_t id_list_2[4] = { DIAL_CANID_IO, CONTROL_CANID_FANBATTBOX,
3434
CONTROL_CANID_PUMP, CONTROL_CANID_RADFAN };
3535

36-
static uint16_t id_list_3[4] = { BMS_CANID_CELL_TEMPS };
36+
static uint16_t id_list_3[4] = { BMS_CANID_CELL_TEMPS, DTI_CANID_CURRENTS };
3737

3838
void init_can1(CAN_HandleTypeDef *hcan)
3939
{
@@ -187,6 +187,8 @@ void vCanReceive(void *pv_params)
187187
break;
188188
case CONTROL_CANID_RADFAN:
189189
control_radfan_record(msg);
190+
case DTI_CANID_CURRENTS:
191+
dti_record_currents(mc, msg);
190192
default:
191193
break;
192194
}

Core/Src/dti.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,29 @@ osStatus_t dti_get_controller_temp(dti_t *mc, uint16_t *controllerTemp)
335335

336336
return osMutexRelease(mc->mutex);
337337
}
338+
339+
osStatus_t dti_record_currents(dti_t *mc, can_msg_t msg)
340+
{
341+
osStatus_t stat = osMutexAcquire(mc->mutex, osWaitForever);
342+
if (stat)
343+
return stat;
344+
345+
int16_t ac_current = (msg.data[0] << 8) + (msg.data[1]) / 10;
346+
int16_t dc_current = (msg.data[2] << 8) + (msg.data[3]) / 10;
347+
348+
mc->ac_current = ac_current;
349+
mc->dc_current = dc_current;
350+
351+
return osMutexRelease(mc->mutex);
352+
}
353+
354+
osStatus_t dti_get_dc_current(dti_t *mc, int16_t *dc_current)
355+
{
356+
osStatus_t stat = osMutexAcquire(mc->mutex, osWaitForever);
357+
if (stat)
358+
return stat;
359+
360+
memcpy(dc_current, &mc->dc_current, sizeof(mc->dc_current));
361+
362+
return osMutexRelease(mc->mutex);
363+
}

Core/Src/fault.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ static void clear_fault(void *args)
4242
*fault_id < MAX_CRITICAL_FAULT) {
4343
set_ready_mode();
4444
}
45-
46-
free(fault_id);
4745
}
4846

4947
/**

Core/Src/pdu.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ pdu_t *init_pdu(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *pump_sensors_adc)
188188
/* Initialize Pumps Current Sensor */
189189
pdu->pumps_current_sensor = malloc(sizeof(ina226_t));
190190
assert(pdu->pumps_current_sensor);
191-
if (init_ina(pdu, pdu->pumps_current_sensor, PUMPS_CURRENT_SENSOR_ADDR,
192-
0.01f, 2.0f)) {
193-
return NULL;
194-
}
191+
// if (init_ina(pdu, pdu->pumps_current_sensor, PUMPS_CURRENT_SENSOR_ADDR,
192+
// 0.01f, 2.0f)) {
193+
// return NULL;
194+
// }
195195

196196
/* Initialize LV Boards Current Sensor */
197197
pdu->lv_boards_current_sensor = malloc(sizeof(ina226_t));
@@ -546,12 +546,11 @@ int8_t read_all_current(pdu_t *pdu, float *motor_controller_current,
546546
{
547547
// setting to 0 since current sensor is nonfunctional
548548
*battbox_fans_current = 0;
549+
*pumps_current = 0;
549550

550551
if (read_current(pdu, pdu->motor_controller_current_sensor,
551552
motor_controller_current))
552553
return -1;
553-
if (read_current(pdu, pdu->pumps_current_sensor, pumps_current))
554-
return -1;
555554
if (read_current(pdu, pdu->lv_boards_current_sensor, lv_boards_current))
556555
return -1;
557556
return 0;

Core/Src/pedals.c

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ static bool launch_control_enabled = false;
4343
#define MAX_VOLTS_UNSCALED 5.0
4444

4545
#define PEDAL_DIFF_THRESH 0.20 /* percentage */
46-
#define PEDAL_FAULT_TIME 90 /* ms */
46+
#define PEDAL_FAULT_TIME 95 /* ms */
47+
#define BRAKE_FAULT_TIME 300 /* ms */
4748

48-
#define APPS_THRESHOLD_BUF 0.45
49+
#define APPS_THRESHOLD_BUF 0.45
50+
#define BRAKE_THRESHOLD_BUF 0.25
4951
enum { ACCELPIN_1, ACCELPIN_2, BRAKEPIN_1, BRAKEPIN_2 };
5052

5153
/* Factor for converting MPH to KMH */
@@ -254,6 +256,44 @@ bool calc_pedal_faults(float accel1, float accel2, float accel1_norm,
254256
return false;
255257
}
256258

259+
/**
260+
* @brief Determine if there has been a brake sensor fault
261+
*
262+
* @param brake1 sensor 1 voltage reading (out of 5V)
263+
* @param accel2 sensor 2 voltage reading (out of 5V)
264+
*/
265+
bool calc_brake_faults(float brake1, float brake2)
266+
{
267+
/* oc = Open Circuit */
268+
static nertimer_t oc_fault_timer;
269+
270+
/* sc = Short Circuit */
271+
static nertimer_t sc_fault_timer;
272+
273+
/* EV3.5.4: For analog acceleration control signals, this error checking must detect open circuit, short to
274+
ground and short to sensor power. */
275+
276+
/* Pedal open circuit fault */
277+
bool open_circuit =
278+
brake1 > BRAKE_SENSOR_IRREGULAR_HIGH + BRAKE_THRESHOLD_BUF ||
279+
brake2 > BRAKE_SENSOR_IRREGULAR_HIGH + BRAKE_THRESHOLD_BUF;
280+
debounce(open_circuit, &oc_fault_timer, BRAKE_FAULT_TIME,
281+
&pedal_fault_cb, "Brake open circuit fault - max brake value");
282+
283+
/* Pedal short circuit to gnd */
284+
bool short_circuit =
285+
brake1 < BRAKE_SENSOR_IRREGULAR_LOW - BRAKE_THRESHOLD_BUF ||
286+
brake2 < BRAKE_SENSOR_IRREGULAR_LOW - BRAKE_THRESHOLD_BUF;
287+
debounce(short_circuit, &sc_fault_timer, BRAKE_FAULT_TIME,
288+
&pedal_fault_cb,
289+
"Brake grounded circuit fault - 0 brake value");
290+
291+
if (open_circuit || short_circuit) {
292+
return true;
293+
}
294+
return false;
295+
}
296+
257297
/**
258298
* @brief Function to send raw pedal data over CAN.
259299
*
@@ -295,20 +335,29 @@ void send_pedal_data(void *arg)
295335
/**
296336
* @brief Determine if power to the motor controller should be disabled based on brake and accelerator pedal travel.
297337
*
338+
* Will queue fault
339+
*
298340
* @param accel_val Percent travel of the accelerator pedal from 0-1
299-
* @param brake_val Brake pressure sensor reading
341+
* @param brake_val Brake pressure sensor reading, 0-1
300342
* @return bool True for prefault conditions met, false for no prefault
301343
*/
302-
bool calc_bspd_prefault(float accel_val, float brake_val)
344+
bool calc_bspd_prefault(float accel_val, float brake_val, float dc_current)
303345
{
304-
static fault_data_t fault_data = { .fault_id = BSPD_PREFAULT,
305-
.diag = "BSPD prefault triggered" };
346+
static const fault_data_t fault_data = {
347+
.fault_id = BSPD_PREFAULT, .diag = "BSPD prefault triggered"
348+
};
306349
static bool motor_disabled = false;
307350

308351
/* EV.4.7: If brakes are engaged and APPS signals more than 25% pedal travel, disable power
309352
to the motor(s). Re-enable when accelerator has less than 5% pedal travel. */
310353

311-
if (brake_val > PEDAL_BRAKE_THRESH && accel_val > 0.25) {
354+
if (brake_val > PEDAL_HARD_BRAKE_THRESH && accel_val > 0.25) {
355+
motor_disabled = true;
356+
queue_fault(&fault_data);
357+
}
358+
359+
// prevent a fault
360+
if (brake_val > PEDAL_HARD_BRAKE_THRESH && dc_current > 10) {
312361
motor_disabled = true;
313362
queue_fault(&fault_data);
314363
}
@@ -596,6 +645,8 @@ void vProcessPedals(void *pv_params)
596645

597646
float accel1_volts = adc_to_volts(adc_data[ACCELPIN_1]);
598647
float accel2_volts = adc_to_volts(adc_data[ACCELPIN_2]);
648+
float brake1_volts = adc_to_volts(adc_data[BRAKEPIN_1]);
649+
float brake2_volts = adc_to_volts(adc_data[BRAKEPIN_2]);
599650

600651
// printf("accel1 volts %f\n", accel1_volts);
601652
// printf("accel2 volts %f\n", accel2_volts);
@@ -606,8 +657,10 @@ void vProcessPedals(void *pv_params)
606657
float accel2_norm = pedal_percent_pressed(
607658
accel2_volts, MIN_APPS2_VOLTS, MAX_APPS2_VOLTS);
608659

609-
bool possible_faults = calc_pedal_faults(
660+
bool possible_pedal_faults = calc_pedal_faults(
610661
accel1_volts, accel2_volts, accel1_norm, accel2_norm);
662+
bool possible_brake_faults =
663+
calc_brake_faults(brake1_volts, brake2_volts);
611664

612665
/* same for brake values */
613666
float brake_avg =
@@ -630,10 +683,13 @@ void vProcessPedals(void *pv_params)
630683
osMutexRelease(brake_state_mut);
631684
write_brakelight(pdu, brake_pressed);
632685

633-
if (calc_bspd_prefault(accel_value, brake_value)) {
686+
int16_t dc_current;
687+
dti_get_dc_current(mc, &dc_current);
688+
689+
if (calc_bspd_prefault(accel_value, brake_value, dc_current)) {
634690
/* Prefault triggered */
635-
// osDelay(delay_time);
636691
// dti_set_torque(0);
692+
// osDelay(delay_time);
637693
// continue;
638694
}
639695

Core/Src/state_machine.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/* Internal State of Vehicle */
2121
static state_t cerberus_state;
22+
static osTimerId_t reverse_sound_timer;
2223

2324
typedef struct {
2425
enum { FUNCTIONAL, NERO } id;
@@ -82,6 +83,14 @@ nero_state_t get_nero_state()
8283
return cerberus_state.nero;
8384
}
8485

86+
void sound_reverse_callback(void *pdu)
87+
{
88+
static bool sound = false;
89+
90+
write_rtds(pdu, sound);
91+
sound = !sound;
92+
}
93+
8594
static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
8695
dti_t *mc, mpu_t *mpu)
8796
{
@@ -97,6 +106,8 @@ static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
97106

98107
/* Make sure wheels are not spinning before changing modes */
99108
bool brake_state;
109+
osTimerStop(reverse_sound_timer);
110+
write_rtds(pdu, false);
100111

101112
/* Catching state transitions */
102113
switch (new_state) {
@@ -110,6 +121,7 @@ static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
110121
printf("Reverse is disabled.");
111122
return 4;
112123
#endif
124+
osTimerStart(reverse_sound_timer, pdMS_TO_TICKS(500));
113125
case F_PIT:
114126
case F_PERFORMANCE:
115127
case F_EFFICIENCY:
@@ -331,6 +343,8 @@ void vStateMachineDirector(void *pv_params)
331343

332344
/* Write to GPIO expander to set initial state */
333345
write_fault(mpu, false);
346+
reverse_sound_timer =
347+
osTimerNew(sound_reverse_callback, osTimerPeriodic, pdu, NULL);
334348

335349
for (;;) {
336350
if (osMessageQueueGet(state_trans_queue, &new_state_req, NULL,

Core/Src/steeringio.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,32 @@ void buttons_update(can_msg_t msg, dti_t *mc)
3535
break;
3636
case BUTTON_ESC:
3737
printf("Esc button pressed \n");
38-
set_home_mode();
38+
if (fabs(dti_get_mph(mc)) < 1) {
39+
set_home_mode();
40+
}
3941
break;
4042
case BUTTON_UP:
4143
printf("Up button pressed \n");
42-
if (get_func_state() == F_EFFICIENCY || (dti_get_mph(mc) <= 0 && get_func_state() == F_PERFORMANCE)) {
44+
if (get_func_state() == F_EFFICIENCY ||
45+
(dti_get_mph(mc) <= 0 && get_brake_state() &&
46+
get_func_state() == F_PERFORMANCE)) {
4347
increase_regen_limit();
4448
}
4549
decrement_nero_index();
4650
break;
4751
case BUTTON_DOWN:
4852
printf("Down button pressed \n");
49-
if (get_func_state() == F_EFFICIENCY || (dti_get_mph(mc) <= 0 && get_func_state() == F_PERFORMANCE)) {
53+
if (get_func_state() == F_EFFICIENCY ||
54+
(dti_get_mph(mc) <= 0 && get_brake_state() &&
55+
get_func_state() == F_PERFORMANCE)) {
5056
decrease_regen_limit();
5157
}
5258
increment_nero_index();
5359
break;
5460
case BUTTON_ENTER:
5561
printf("Enter button pressed \n");
56-
if (get_func_state() == F_PERFORMANCE) {
62+
if (get_func_state() == F_PERFORMANCE &&
63+
fabs(dti_get_mph(mc)) < 1) {
5764
toggle_launch_control();
5865
}
5966
select_nero_index();

0 commit comments

Comments
 (0)