Skip to content

Commit

Permalink
190 brakelight control task, add task notifications to CAN I/O and ro…
Browse files Browse the repository at this point in the history
…uters
  • Loading branch information
Sabramz committed Aug 4, 2024
1 parent 37acc79 commit 322ad60
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 76 deletions.
1 change: 1 addition & 0 deletions Core/Inc/bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#define BMS_DCL_MSG 0x156 /*BMS MONITOR WATCHDOG*/
#define BMS_CURR_LIMIT_MSG 0x176
#define NEW_AMS_MSG_FLAG 1U

typedef struct {
osTimerId bms_monitor_timer;
Expand Down
24 changes: 24 additions & 0 deletions Core/Inc/control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file control.h
* @author Scott Abramson
* @brief Tasks for controlling the car.
* @version 0.1
* @date 2024-08-04
*
* @copyright Copyright (c) 2024
*
*/

#ifndef CONTROL_H
#define CONTROL_H

#define BRAKE_STATE_UPDATE_FLAG 0x00000001U

/**
* @brief Task for controlling the brakelight.
*/
void vBrakelightControl(void *pv_params);
extern osThreadId brakelight_control_thread;
extern const osThreadAttr_t brakelight_monitor_attributes;

#endif
2 changes: 2 additions & 0 deletions Core/Inc/dti.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
0x496 /* Throttle signal, Brake signal, IO, Drive enable */
#define DTI_QUEUE_SIZE 5

#define NEW_DTI_MSG_FLAG 1U

typedef struct {
int32_t rpm; /* SCALE: 1 UNITS: Rotations per Minute */
int16_t duty_cycle; /* SCALE: 10 UNITS: Percentage */
Expand Down
4 changes: 0 additions & 4 deletions Core/Inc/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,4 @@ void vSteeringIOButtonsMonitor(void *pv_params);
extern osThreadId_t steeringio_buttons_monitor_handle;
extern const osThreadAttr_t steeringio_buttons_monitor_attributes;

void vBrakelightMonitor(void *pv_params);
extern osThreadId brakelight_monitor_handle;
extern const osThreadAttr_t brakelight_monitor_attributes;

#endif // MONITOR_H
8 changes: 7 additions & 1 deletion Core/Inc/processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
#include "cmsis_os.h"
#include "stdbool.h"

#define TSMS_UPDATE_FLAG 0x00000001U

/**
* @brief Get the debounced TSMS reading.
*
* @return State of TSMS.
*/
bool get_tsms();

/**
Expand All @@ -23,6 +30,5 @@ bool get_tsms();
void vProcessTSMS(void *pv_params);
extern osThreadId_t process_tsms_thread_id;
extern const osThreadAttr_t process_tsms_attributes;
#define TSMS_UPDATE_FLAG 0x00000001U

#endif
2 changes: 0 additions & 2 deletions Core/Inc/queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#define IMU_QUEUE_SIZE 8
#define TSMS_QUEUE_SIZE 1

extern osMessageQueueId_t brakelight_signal;

typedef struct {
uint16_t accel_x;
uint16_t accel_y;
Expand Down
13 changes: 6 additions & 7 deletions Core/Src/bms.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ void vAMSCANMonitor(void *pv_params)

osTimerStart(bms->bms_monitor_timer, BMS_CAN_MONITOR_DELAY);
for (;;) {
if (osOK == osMessageQueueGet(bms_monitor_queue,
&msg_from_queue, NULL,
osWaitForever)) {
osTimerStart(bms->bms_monitor_timer,
BMS_CAN_MONITOR_DELAY);
}
osThreadFlagsWait(NEW_AMS_MSG_FLAG, osFlagsWaitAny,
osWaitForever);
osMessageQueueGet(bms_monitor_queue, &msg_from_queue, NULL,
osWaitForever);
osTimerStart(bms->bms_monitor_timer, BMS_CAN_MONITOR_DELAY);
}
}
}
49 changes: 27 additions & 22 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "dti.h"

#define CAN_MSG_QUEUE_SIZE 50 /* messages */
#define CAN_NEW_MSG_FLAG 1U

static osMessageQueueId_t can_outbound_queue;

can_t *can1;
Expand Down Expand Up @@ -82,16 +84,28 @@ void can1_callback(CAN_HandleTypeDef *hcan)
// case DTI_CANID_ID_IQ:
// case DTI_CANID_SIGNALS:
osMessageQueuePut(dti_router_queue, &new_msg, 0U, 0U);
osThreadFlagsSet(dti_router_handle, NEW_DTI_MSG_FLAG);
break;
case BMS_DCL_MSG:
//printf("Recieved dcl");
osMessageQueuePut(bms_monitor_queue, &new_msg, 0U, 0U);
osThreadFlagsSet(bms_monitor_handle, NEW_AMS_MSG_FLAG);
break;
default:
break;
}
}

int8_t queue_can_msg(can_msg_t msg)
{
if (!can_outbound_queue)
return -1;

osMessageQueuePut(can_outbound_queue, &msg, 0U, 0U);
osThreadFlagsSet(can_dispatch_handle, CAN_NEW_MSG_FLAG);
return 0;
}

osThreadId_t can_dispatch_handle;
const osThreadAttr_t can_dispatch_attributes = {
.name = "CanDispatch",
Expand All @@ -108,29 +122,20 @@ void vCanDispatch(void *pv_params)
HAL_StatusTypeDef msg_status;

for (;;) {
osThreadFlagsWait(CAN_NEW_MSG_FLAG, osFlagsWaitAny,
osWaitForever);
/* Send CAN message */
if (osOK == 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);
}
}
osMessageQueueGet(can_outbound_queue, &msg_from_queue, NULL,
osWaitForever);

osDelay(CAN_DISPATCH_DELAY);
}
}
msg_status = can_send_msg(can1, &msg_from_queue);

int8_t queue_can_msg(can_msg_t msg)
{
if (!can_outbound_queue)
return -1;

osMessageQueuePut(can_outbound_queue, &msg, 0U, 0U);
return 0;
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);
}
}
}
32 changes: 32 additions & 0 deletions Core/Src/control.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @file control.c
* @author Scott Abramson
* @brief Tasks for controlling the car.
* @version 0.1
* @date 2024-08-04
*
* @copyright Copyright (c) 2024
*
*/

#include "pdu.h"
#include "queues.h"
#include "control.h"

osThreadId brakelight_control_thread;
const osThreadAttr_t brakelight_monitor_attributes = {
.name = "BrakelightMonitor",
.stack_size = 32 * 8,
.priority = (osPriority_t)osPriorityHigh,
};

void vBrakelightControl(void *pv_params)
{
pdu_t *pdu = (pdu_t *)pv_params;

for (;;) {
osThreadFlagsWait(BRAKE_STATE_UPDATE_FLAG, osFlagsWaitAny,
osWaitForever);
write_brakelight(pdu, get_brake_state());
}
}
4 changes: 3 additions & 1 deletion Core/Src/dti.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ void vDTIRouter(void *pv_params)
dti_t *mc = (dti_t *)pv_params;

for (;;) {
/* Wait until new CAN message comes into queue */
osThreadFlagsWait(NEW_DTI_MSG_FLAG, osFlagsWaitAny,
osWaitForever);

status = osMessageQueueGet(dti_router_queue, &message, NULL,
osWaitForever);
if (status == osOK) {
Expand Down
8 changes: 3 additions & 5 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "dti.h"
#include "steeringio.h"
#include "processing.h"
#include "control.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -81,7 +82,6 @@ const osThreadAttr_t defaultTask_attributes = {
.priority = (osPriority_t) osPriorityNormal,
};
/* USER CODE BEGIN PV */
osMessageQueueId_t brakelight_signal;
osMessageQueueId_t pedal_data_queue;
osMessageQueueId_t imu_queue;
osMessageQueueId_t dti_router_queue;
Expand Down Expand Up @@ -202,8 +202,6 @@ int main(void)
/* USER CODE END RTOS_TIMERS */

/* USER CODE BEGIN RTOS_QUEUES */
brakelight_signal = osMessageQueueNew(BRAKELIGHT_QUEUE_SIZE, sizeof(bool), NULL);
assert(brakelight_signal);
imu_queue = osMessageQueueNew(IMU_QUEUE_SIZE, sizeof(imu_data_t), NULL);
assert(imu_queue);
pedal_data_queue = osMessageQueueNew(PEDAL_DATA_QUEUE_SIZE, sizeof(pedals_t), NULL);
Expand Down Expand Up @@ -261,8 +259,8 @@ int main(void)
assert(fault_handle);
sm_director_handle = osThreadNew(vStateMachineDirector, pdu, &sm_director_attributes);
assert(sm_director_handle);
brakelight_monitor_handle = osThreadNew(vBrakelightMonitor, pdu, &brakelight_monitor_attributes);;
assert(brakelight_monitor_handle);
brakelight_control_thread = osThreadNew(vBrakelightControl, pdu, &brakelight_monitor_attributes);;
assert(brakelight_control_thread);

/* USER CODE END RTOS_THREADS */

Expand Down
38 changes: 6 additions & 32 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "task.h"
#include "timer.h"
#include "processing.h"
#include "control.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -210,8 +211,8 @@ void vPedalsMonitor(void *pv_params)
is_braking = ((adc_data[BRAKEPIN_1] + adc_data[BRAKEPIN_2]) /
2) > PEDAL_BRAKE_THRESH;
brake_state = is_braking;

osMessageQueuePut(brakelight_signal, &is_braking, 0U, 0U);
osThreadFlagsSet(brakelight_control_thread,
BRAKE_STATE_UPDATE_FLAG);

/* Low Pass Filter */
sensor_data.accelerator_value =
Expand Down Expand Up @@ -347,7 +348,8 @@ void vTsmsMonitor(void *pv_params)
.severity = DEFCON5 };
pdu_t *pdu = (pdu_t *)pv_params;
bool tsms_status = false;
static const uint8_t TSMS_SENSE_DELAY = 100;
/* ms */
static const uint8_t TSMS_SENSE_SAMPLE_RATE = 100;

for (;;) {
/* If the TSMS reading throws an error, queue TSMS fault */
Expand All @@ -359,7 +361,7 @@ void vTsmsMonitor(void *pv_params)
osThreadFlagsSet(process_tsms_thread_id,
TSMS_UPDATE_FLAG);
}
osDelay(TSMS_SENSE_DELAY);
osDelay(TSMS_SENSE_SAMPLE_RATE);
}
}

Expand Down Expand Up @@ -411,11 +413,6 @@ void vFusingMonitor(void *pv_params)
queue_fault(&fault_data);
}

/* If the TSMS is off, the car should no longer be active. */
if (!get_tsms() && get_func_state() == ACTIVE) {
set_home_mode();
}

osDelay(FUSES_SAMPLE_DELAY);
}
}
Expand Down Expand Up @@ -529,29 +526,6 @@ void vSteeringIOButtonsMonitor(void *pv_params)
}
}

osThreadId brakelight_monitor_handle;
const osThreadAttr_t brakelight_monitor_attributes = {
.name = "BrakelightMonitor",
.stack_size = 32 * 8,
.priority = (osPriority_t)osPriorityHigh,
};

void vBrakelightMonitor(void *pv_params)
{
pdu_t *pdu = (pdu_t *)pv_params;

bool state;
osStatus_t status;

for (;;) {
status = osMessageQueueGet(brakelight_signal, &state, NULL,
osWaitForever);
if (!status) {
write_brakelight(pdu, state);
}
}
}

osThreadId_t temp_monitor_handle;
const osThreadAttr_t temp_monitor_attributes = {
.name = "TempMonitor",
Expand Down
2 changes: 1 addition & 1 deletion Core/Src/processing.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void vProcessTSMS(void *pv_params)
nertimer_t tsms_debounce_timer = { .active = false };

for (;;) {
/* Wait until this flag is set by TSMS data collection task */
osThreadFlagsWait(TSMS_UPDATE_FLAG, osFlagsWaitAny,
osWaitForever);

Expand Down Expand Up @@ -66,3 +65,4 @@ void vProcessTSMS(void *pv_params)
}
}
}

9 changes: 8 additions & 1 deletion Core/Src/serial_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#define PRINTF_QUEUE_SIZE 25 /* Strings */
#define PRINTF_BUFFER_LEN 128 /* Characters */
#define NEW_MESSAGE_FLAG 0x00000001U

osMessageQueueId_t printf_queue;
osThreadId_t serial_monitor_handle;
Expand Down Expand Up @@ -40,6 +41,8 @@ int serial_print(const char *format, ...)
return -3;
}

osThreadFlagsSet(serial_monitor_handle, NEW_MESSAGE_FLAG);

return 0;
}

Expand All @@ -52,9 +55,13 @@ void vSerialMonitor(void *pv_params)
osMessageQueueNew(PRINTF_QUEUE_SIZE, sizeof(char *), NULL);

for (;;) {
/* Wait until new printf message comes into queue */
osThreadFlagsWait(NEW_MESSAGE_FLAG, osFlagsWaitAny,
osWaitForever);

/* Get message to print */
status = osMessageQueueGet(printf_queue, &message, NULL,
osWaitForever);

if (status != osOK) {
// TODO: Trigger fault ?
} else {
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Core/Src/pdu.c \
Core/Src/mpu.c \
Core/Src/steeringio.c \
Core/Src/processing.c \
Core/Src/control.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
Expand Down

0 comments on commit 322ad60

Please sign in to comment.