Skip to content

Commit

Permalink
190 expand heap and task stack sizes, fix tasks getting from queues, …
Browse files Browse the repository at this point in the history
…make buttons more responsive, fix stack overflow and malloc fail, fix sending pedal data
  • Loading branch information
Sabramz committed Aug 7, 2024
1 parent 941ca77 commit 9d5b002
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
#define configTICK_RATE_HZ ((TickType_t)1000)
#define configMAX_PRIORITIES ( 56 )
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
#define configTOTAL_HEAP_SIZE ((size_t)15360)
#define configTOTAL_HEAP_SIZE ((size_t)20000)
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 1
#define configUSE_16_BIT_TICKS 0
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/cerberus_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// DEBUGGING: Start with a high debounce period, lower it if it is too slow
// NOTE: SteeringIOMonitor updates every 25 ms. Previous value of 25 ms doesn't
// give enough time to differentiate signal from noise.
#define STEERING_WHEEL_DEBOUNCE 100 /* ms */
#define STEERING_WHEEL_DEBOUNCE 70 /* ms */

/* Pin Assignments */
#define FAULT_MCU_Pin GPIO_PIN_3
Expand Down
22 changes: 11 additions & 11 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ void vCanDispatch(void *pv_params)
osThreadFlagsWait(CAN_NEW_MSG_FLAG, osFlagsWaitAny,
osWaitForever);
/* Send CAN message */
osMessageQueueGet(can_outbound_queue, &msg_from_queue, NULL,
osWaitForever);

msg_status = can_send_msg(can1, &msg_from_queue);

if (msg_status == HAL_ERROR) {
fault_data.diag = "Failed to send CAN message";
queue_fault(&fault_data);
} else if (msg_status == HAL_BUSY) {
fault_data.diag = "Outbound mailbox full!";
queue_fault(&fault_data);
while (osMessageQueueGet(can_outbound_queue, &msg_from_queue,
NULL, 0U) == osOK) {
msg_status = can_send_msg(can1, &msg_from_queue);

if (msg_status == HAL_ERROR) {
fault_data.diag = "Failed to send CAN message";
queue_fault(&fault_data);
} else if (msg_status == HAL_BUSY) {
fault_data.diag = "Outbound mailbox full!";
queue_fault(&fault_data);
}
}
}
}
2 changes: 1 addition & 1 deletion Core/Src/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
osThreadId brakelight_control_thread;
const osThreadAttr_t brakelight_monitor_attributes = {
.name = "BrakelightMonitor",
.stack_size = 32 * 8,
.stack_size = 32 * 16,
.priority = (osPriority_t)osPriorityHigh,
};

Expand Down
31 changes: 20 additions & 11 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#define PEDAL_DIFF_THRESH 30
#define PEDAL_FAULT_TIME 500 /* ms */

/* 25 ms */
#define WHEEL_BUTTON_SAMPLE_RATE 10

static bool brake_state = false;

enum { ACCELPIN_2, ACCELPIN_1, BRAKEPIN_1, BRAKEPIN_2 };
Expand Down Expand Up @@ -105,10 +108,17 @@ void pedal_fault_cb(void *arg)
queue_fault(&fault_data);
}

/**
* @brief Function to send raw pedal data over CAN.
*
* @param arg A pointer to an array of 4 unsigned 32 bit integers.
*/
void send_pedal_data(void *arg)
{
// TODO: validate that this works
uint32_t *adc_data = (uint32_t *)arg;
static const uint8_t adc_data_size = 4;
uint32_t adc_data[adc_data_size];
/* Copy contents of adc_data to new location in memory because we endian swap them */
memcpy(adc_data, (uint32_t *)arg, adc_data_size * sizeof(adc_data[0]));

can_msg_t accel_pedals_msg = { .id = CANID_PEDALS_ACCEL_MSG,
.len = 8,
Expand Down Expand Up @@ -142,7 +152,10 @@ void vPedalsMonitor(void *pv_params)
static pedals_t sensor_data;
uint32_t adc_data[4];
osTimerId_t send_pedal_data_timer =
osTimerNew(&send_pedal_data, osTimerOnce, adc_data, NULL);
osTimerNew(&send_pedal_data, osTimerPeriodic, adc_data, NULL);

/* Send CAN messages with raw pedal readings, we do not care if it fails*/
osTimerStart(send_pedal_data_timer, 100);

/* oc = Open Circuit */
osTimerId_t oc_fault_timer = osTimerNew(
Expand Down Expand Up @@ -211,10 +224,6 @@ void vPedalsMonitor(void *pv_params)
queue_fault(&fault_data);
}

/* Send CAN messages with raw pedal readings, we do not care if it fails*/
if (!osTimerIsRunning(send_pedal_data_timer))
osTimerStart(send_pedal_data_timer, 100);

osDelay(PEDALS_SAMPLE_DELAY);
}
}
Expand Down Expand Up @@ -301,7 +310,7 @@ void vIMUMonitor(void *pv_params)
osThreadId_t tsms_monitor_handle;
const osThreadAttr_t tsms_monitor_attributes = {
.name = "TsmsMonitor",
.stack_size = 32 * 8,
.stack_size = 32 * 16,
.priority = (osPriority_t)osPriorityHigh,
};

Expand All @@ -311,7 +320,7 @@ void vTsmsMonitor(void *pv_params)
.severity = DEFCON5 };
pdu_t *pdu = (pdu_t *)pv_params;
bool tsms_status = false;
/* ms */
/* 100 ms */
static const uint8_t TSMS_SENSE_SAMPLE_RATE = 100;

for (;;) {
Expand Down Expand Up @@ -439,7 +448,7 @@ void vShutdownMonitor(void *pv_params)
osThreadId steeringio_buttons_monitor_handle;
const osThreadAttr_t steeringio_buttons_monitor_attributes = {
.name = "SteeringIOButtonsMonitor",
.stack_size = 400,
.stack_size = 800,
.priority = (osPriority_t)osPriorityNormal,
};

Expand Down Expand Up @@ -475,7 +484,7 @@ void vSteeringIOButtonsMonitor(void *pv_params)
queue_fault(&fault_data);
}

osDelay(25);
osDelay(WHEEL_BUTTON_SAMPLE_RATE);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Core/Src/nero.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void set_mph(int8_t new_mph)
osThreadId_t nero_monitor_handle;
const osThreadAttr_t nero_monitor_attributes = {
.name = "NeroMonitor",
.stack_size = 32 * 8,
.stack_size = 32 * 32,
.priority = (osPriority_t)osPriorityNormal,
};

Expand Down
8 changes: 4 additions & 4 deletions Core/Src/serial_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ void vSerialMonitor(void *pv_params)
/* Get message to print */
status = osMessageQueueGet(printf_queue, &message, NULL,
osWaitForever);

if (status != osOK) {
// TODO: Trigger fault ?
} else {
while (status == osOK) {
printf(message);
free(message);
status = osMessageQueueGet(printf_queue, &message, NULL,
0U);
}

}
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Sun Aug 04 16:29:12 EDT 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Wed Aug 07 10:51:47 EDT 2024]
##########################################################################################################################

# ------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion cerberus.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ Dma.ADC3.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlign
Dma.Request0=ADC3
Dma.Request1=ADC1
Dma.RequestsNb=2
FREERTOS.IPParameters=Tasks01,configUSE_PREEMPTION,configUSE_MALLOC_FAILED_HOOK,configCHECK_FOR_STACK_OVERFLOW
FREERTOS.IPParameters=Tasks01,configUSE_PREEMPTION,configUSE_MALLOC_FAILED_HOOK,configCHECK_FOR_STACK_OVERFLOW,configTOTAL_HEAP_SIZE
FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL
FREERTOS.configCHECK_FOR_STACK_OVERFLOW=1
FREERTOS.configTOTAL_HEAP_SIZE=20000
FREERTOS.configUSE_MALLOC_FAILED_HOOK=1
FREERTOS.configUSE_PREEMPTION=0
File.Version=6
Expand Down

0 comments on commit 9d5b002

Please sign in to comment.