Skip to content

Commit

Permalink
190 fix stack size, fix state machine init, BSPD prefault back in
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Aug 7, 2024
1 parent 9d5b002 commit e823f31
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 101 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#define configUSE_16_BIT_TICKS 0
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 1
#define configCHECK_FOR_STACK_OVERFLOW 2
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_MALLOC_FAILED_HOOK 1
#define configUSE_COUNTING_SEMAPHORES 1
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#define BRAKELIGHT_QUEUE_SIZE 8
#define ONBOARD_TEMP_QUEUE_SIZE 8
#define PEDAL_DATA_QUEUE_SIZE 8
#define PEDAL_DATA_QUEUE_SIZE 1
#define IMU_QUEUE_SIZE 8
#define TSMS_QUEUE_SIZE 1

Expand Down
67 changes: 34 additions & 33 deletions Core/Src/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int queue_fault(fault_data_t *fault_data)
osThreadId_t fault_handle;
const osThreadAttr_t fault_handle_attributes = {
.name = "FaultHandler",
.stack_size = 52 * 8,
.stack_size = 32 * 16,
.priority = (osPriority_t)osPriorityRealtime7,
};

Expand All @@ -41,39 +41,40 @@ void vFaultHandler(void *pv_params)
osThreadFlagsWait(NEW_FAULT_FLAG, osFlagsWaitAny,
osWaitForever);

osMessageQueueGet(fault_handle_queue, &fault_data, NULL,
osWaitForever);

uint32_t fault_id = (uint32_t)fault_data.id;
endian_swap(&fault_id, sizeof(fault_id));
uint8_t defcon = (uint8_t)fault_data.severity;
while (osMessageQueueGet(fault_handle_queue, &fault_data, NULL,
osWaitForever) == osOK) {
uint32_t fault_id = (uint32_t)fault_data.id;
endian_swap(&fault_id, sizeof(fault_id));
uint8_t defcon = (uint8_t)fault_data.severity;

can_msg_t msg;
msg.id = CANID_FAULT_MSG;
msg.len = 8;
uint8_t msg_data[8];
memcpy(msg_data, &fault_id, sizeof(fault_id));
memcpy(msg_data + sizeof(fault_id), &defcon, sizeof(defcon));
memcpy(msg.data, msg_data, msg.len);
queue_can_msg(msg);
printf("\r\nFault Handler! Diagnostic Info:\t%s\r\n\r\n",
fault_data.diag);
switch (fault_data.severity) {
case DEFCON1: /* Highest(1st) Priority */
fault();
break;
case DEFCON2:
fault();
break;
case DEFCON3:
fault();
break;
case DEFCON4:
break;
case DEFCON5: /* Lowest Priority */
break;
default:
break;
can_msg_t msg;
msg.id = CANID_FAULT_MSG;
msg.len = 8;
uint8_t msg_data[8];
memcpy(msg_data, &fault_id, sizeof(fault_id));
memcpy(msg_data + sizeof(fault_id), &defcon,
sizeof(defcon));
memcpy(msg.data, msg_data, msg.len);
queue_can_msg(msg);
printf("\r\nFault Handler! Diagnostic Info:\t%s\r\n\r\n",
fault_data.diag);
switch (fault_data.severity) {
case DEFCON1: /* Highest(1st) Priority */
fault();
break;
case DEFCON2:
fault();
break;
case DEFCON3:
fault();
break;
case DEFCON4:
break;
case DEFCON5: /* Lowest Priority */
break;
default:
break;
}
}
}
}
1 change: 1 addition & 0 deletions Core/Src/freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ void vApplicationMallocFailedHook(void)
/* USER CODE BEGIN Application */

/* USER CODE END Application */

8 changes: 5 additions & 3 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,12 @@ int main(void)
assert(process_pedals_thread);
fault_handle = osThreadNew(vFaultHandler, NULL, &fault_handle_attributes);
assert(fault_handle);
sm_director_handle = osThreadNew(vStateMachineDirector, mc, &sm_director_attributes);
uint32_t *state_machine_args = malloc(sizeof(uint32_t) * 2);
state_machine_args[0] = (uint32_t) pdu;
state_machine_args[1] = (uint32_t) mc;
sm_director_handle = osThreadNew(vStateMachineDirector, state_machine_args, &sm_director_attributes);
assert(sm_director_handle);
free(state_machine_args);
brakelight_control_thread = osThreadNew(vBrakelightControl, pdu, &brakelight_monitor_attributes);;
assert(brakelight_control_thread);

Expand All @@ -274,8 +278,6 @@ int main(void)
/* USER CODE BEGIN WHILE */
while (1)
{
// write_pump(pdu, false);
// sound_rtds(pdu);
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
Expand Down
19 changes: 9 additions & 10 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#define PEDAL_FAULT_TIME 500 /* ms */

/* 25 ms */
#define WHEEL_BUTTON_SAMPLE_RATE 10
#define WHEEL_BUTTON_SAMPLE_RATE 10

static bool brake_state = false;

Expand Down Expand Up @@ -147,8 +147,8 @@ const osThreadAttr_t pedals_monitor_attributes = {

void vPedalsMonitor(void *pv_params)
{
fault_data_t fault_data = { .id = ONBOARD_PEDAL_FAULT,
.severity = DEFCON1 };
// fault_data_t fault_data = { .id = ONBOARD_PEDAL_FAULT,
// .severity = DEFCON1 };
static pedals_t sensor_data;
uint32_t adc_data[4];
osTimerId_t send_pedal_data_timer =
Expand Down Expand Up @@ -215,14 +215,13 @@ void vPedalsMonitor(void *pv_params)
sensor_data.brake_value = brake_val;

/* Publish to Onboard Pedals Queue */
osStatus_t check = osMessageQueuePut(pedal_data_queue,
&sensor_data, 0U, 0U);
osMessageQueuePut(pedal_data_queue, &sensor_data, 0U, 0U);
osThreadFlagsSet(process_pedals_thread, PEDAL_DATA_FLAG);

if (check != 0) {
fault_data.diag = "Failed to push pedal data to queue";
queue_fault(&fault_data);
}
// if (check != 0) {
// fault_data.diag = "Failed to push pedal data to queue";
// queue_fault(&fault_data);
// }

osDelay(PEDALS_SAMPLE_DELAY);
}
Expand Down Expand Up @@ -340,7 +339,7 @@ void vTsmsMonitor(void *pv_params)
osThreadId_t fusing_monitor_handle;
const osThreadAttr_t fusing_monitor_attributes = {
.name = "FusingMonitor",
.stack_size = 64 * 8,
.stack_size = 64 * 16,
.priority = (osPriority_t)osPriorityNormal,
};

Expand Down
41 changes: 21 additions & 20 deletions Core/Src/processing.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void vProcessTSMS(void *pv_params)
debounce(!tsms_reading, tsms_debounce_timer,
TSMS_DEBOUNCE_PERIOD);

if (get_func_state() == ACTIVE && tsms == false) {
if (get_func_state() == ACTIVE && get_tsms() == false) {
//TODO: make task notification to check if car should shut off if TSMS goes off, however this works for now
set_home_mode();
}
Expand Down Expand Up @@ -225,7 +225,7 @@ void vProcessPedals(void *pv_params)
assert(delay_time < MAX_COMMAND_DELAY);
pedals_t pedal_data;
float mph = 0;
// bool motor_disabled = false;
bool motor_disabled = false;

dti_t *mc = (dti_t *)pv_params;

Expand All @@ -250,25 +250,26 @@ void vProcessPedals(void *pv_params)

/* EV.4.7: If brakes are engaged and APPS signals more than 25% pedal travel, disable power
to the motor(s). Re-enable when accelerator has less than 5% pedal travel. */
//fault_data_t fault_data = { .id = BSPD_PREFAULT, .severity = DEFCON5 };
fault_data_t fault_data = { .id = BSPD_PREFAULT,
.severity = DEFCON5,
.diag = "BSPD prefault triggered" };
/* 600 is an arbitrary threshold to consider the brakes mechanically activated */
// if (brake_value > 600 && (accelerator_value) > 0.25)
// {
// printf("\n\n\n\rENTER MOTOR DISABLED\r\n\n\n");
// motor_disabled = true;
// dti_set_torque(0);
// //queue_fault(&fault_data);
// }

// if (motor_disabled)
// {
// printf("\nMotor disabled\n");
// if (accelerator_value < 0.05)
// {
// motor_disabled = false;
// printf("\n\nMotor reenabled\n\n");
// }
// }
if (brake_value > 700 && (accelerator_value) > 0.25) {
printf("\n\n\n\rENTER MOTOR DISABLED\r\n\n\n");
motor_disabled = true;
queue_fault(&fault_data);
}

if (motor_disabled) {
printf("\nMotor disabled\n");
if (accelerator_value < 0.05) {
motor_disabled = false;
printf("\n\nMotor reenabled\n\n");
} else {
dti_set_torque(0);
continue;
}
}

drive_state_t drive_state = get_drive_state();

Expand Down
3 changes: 1 addition & 2 deletions Core/Src/serial_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ void vSerialMonitor(void *pv_params)
printf(message);
free(message);
status = osMessageQueueGet(printf_queue, &message, NULL,
0U);
0U);
}

}
}
Loading

0 comments on commit e823f31

Please sign in to comment.