Skip to content

Commit

Permalink
190 consolidate TSMS logic, timers for data collection, fix can overf…
Browse files Browse the repository at this point in the history
…low error
  • Loading branch information
Sabramz committed Aug 10, 2024
1 parent 49439a7 commit 1f588e9
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 216 deletions.
13 changes: 13 additions & 0 deletions Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include "cmsis_os.h"
#include "dti.h"

/**
* @brief Callback to be called when a message is received on CAN line 1.
*
* @param hcan Pointer to struct representing CAN hardware.
*/
void can1_callback(CAN_HandleTypeDef *hcan);

/**
Expand All @@ -27,10 +32,18 @@ void can1_callback(CAN_HandleTypeDef *hcan);
* @return int8_t Error code.
*/
int8_t queue_can_msg(can_msg_t msg);

/**
* @brief Initialize CAN line 1.
*
* @param hcan Pointer to struct representing CAN hardware.
*/
void init_can1(CAN_HandleTypeDef *hcan);

/**
* @brief Task for sending CAN messages.
*
* @param pv_params CAN_HandleTypeDef for the CAN line that messages will be sent out on.
*/
void vCanDispatch(void *pv_params);
extern osThreadId_t can_dispatch_handle;
Expand Down
23 changes: 21 additions & 2 deletions Core/Inc/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,29 @@ typedef struct {
} pedals_t;

pedals_t get_pedal_data();

bool get_brake_state();

bool get_tsms_reading();
/**
* @brief Read the open cell voltage of the LV batteries and send a CAN message with the result.
*
* @param arg Pointer to mpu_t.
*/
void read_lv_sense(void *arg);

/**
* @brief Read data from the fuse monitor GPIO expander on the PDU.
*
* @param pdu Pointer to pdu_t.
*/
void read_fuse_data(void *arg);

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

/* Defining Temperature Monitor Task */
void vTempMonitor(void *pv_params);
Expand Down Expand Up @@ -45,7 +65,6 @@ extern const osThreadAttr_t shutdown_monitor_attributes;

/* Arguments for the data collection thread */
typedef struct {
mpu_t *mpu;
pdu_t *pdu;
steeringio_t *wheel;
} data_collection_args_t;
Expand Down
14 changes: 0 additions & 14 deletions Core/Inc/processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ void increase_torque_limit();
*/
void decrease_torque_limit();

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

/**
* @brief Task for processing data from the TSMS.
*/
void vProcessTSMS(void *pv_params);
extern osThreadId_t process_tsms_thread_id;
extern const osThreadAttr_t process_tsms_attributes;

typedef struct {
dti_t *mc;
pdu_t *pdu;
Expand Down
4 changes: 0 additions & 4 deletions Core/Inc/queues.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include "cmsis_os.h"

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

typedef struct {
uint16_t accel_x;
Expand All @@ -20,6 +18,4 @@ typedef struct {

extern osMessageQueueId_t imu_queue;

extern osMessageQueueId_t tsms_data_queue;

#endif // QUEUES_H
15 changes: 8 additions & 7 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ static osMessageQueueId_t can_inbound_queue;
can_t *can1;

/* Relevant Info for Initializing CAN 1 */
static uint32_t id_list[] = { DTI_CANID_ERPM, DTI_CANID_CURRENTS,
DTI_CANID_TEMPS_FAULT, DTI_CANID_ID_IQ,
DTI_CANID_SIGNALS, BMS_DCL_MSG };
static uint32_t id_list[] = { DTI_CANID_ERPM, DTI_CANID_CURRENTS, BMS_DCL_MSG };

void init_can1(CAN_HandleTypeDef *hcan)
{
Expand Down Expand Up @@ -107,12 +105,19 @@ void vCanDispatch(void *pv_params)
can_msg_t msg_from_queue;
HAL_StatusTypeDef msg_status;

CAN_HandleTypeDef *hcan = (CAN_HandleTypeDef *)pv_params;

for (;;) {
osThreadFlagsWait(CAN_DISPATCH_FLAG, osFlagsWaitAny,
osWaitForever);
/* Send CAN message */
while (osMessageQueueGet(can_outbound_queue, &msg_from_queue,
NULL, 0U) == osOK) {
/* Wait if CAN outbound queue is full */
while (HAL_CAN_GetTxMailboxesFreeLevel(hcan) == 0) {
osDelay(1);
}

msg_status = can_send_msg(can1, &msg_from_queue);

if (msg_status == HAL_ERROR) {
Expand Down Expand Up @@ -149,10 +154,6 @@ void vCanReceive(void *pv_params)
case DTI_CANID_ERPM:
dti_record_rpm(mc, msg);
break;
// case DTI_CANID_CURRENTS:
// case DTI_CANID_TEMPS_FAULT:
// case DTI_CANID_ID_IQ:
// case DTI_CANID_SIGNALS:
case BMS_DCL_MSG:
handle_dcl_msg();
break;
Expand Down
3 changes: 2 additions & 1 deletion Core/Src/cerb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void debounce(bool input, osTimerAttr_t *timer, uint32_t period)
osStatus_t queue_and_set_flag(osMessageQueueId_t queue, const void *msg_ptr,
osThreadId_t thread_id, uint32_t flags)
{
osStatus_t status = osMessageQueuePut(queue, msg_ptr, 0U, 0U);
osThreadFlagsSet(thread_id, flags);
return osMessageQueuePut(queue, msg_ptr, 0U, 0U);
return status;
}
41 changes: 19 additions & 22 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* One second with a tick rate of 1000 Hz */
#define ONE_SECOND 1000
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
Expand Down Expand Up @@ -81,9 +83,9 @@ const osThreadAttr_t defaultTask_attributes = {
};
/* USER CODE BEGIN PV */
osMessageQueueId_t imu_queue;
osMessageQueueId_t dti_router_queue;
osMessageQueueId_t tsms_data_queue;

osTimerId_t lv_sense_timer;
osTimerId_t fuse_timer;

/* USER CODE END PV */

Expand Down Expand Up @@ -201,12 +203,6 @@ int main(void)
/* USER CODE END RTOS_TIMERS */

/* USER CODE BEGIN RTOS_QUEUES */
imu_queue = osMessageQueueNew(IMU_QUEUE_SIZE, sizeof(imu_data_t), NULL);
assert(imu_queue);
dti_router_queue = osMessageQueueNew(DTI_QUEUE_SIZE, sizeof(can_msg_t), NULL);
assert(dti_router_queue);
tsms_data_queue = osMessageQueueNew(TSMS_QUEUE_SIZE, sizeof(bool), NULL);
assert(tsms_data_queue);
/* USER CODE END RTOS_QUEUES */

/* Create the thread(s) */
Expand All @@ -218,40 +214,41 @@ int main(void)
/* Monitors */
pedals_monitor_handle = osThreadNew(vPedalsMonitor, mpu, &pedals_monitor_attributes);
assert(pedals_monitor_handle);
// temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes);
// assert(temp_monitor_handle);
//imu_monitor_handle = osThreadNew(vIMUMonitor, mpu, &imu_monitor_attributes);
//assert(imu_monitor_handle);
// shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes);
// assert(shutdown_monitor_handle);

lv_sense_timer = osTimerNew(&read_lv_sense, osTimerPeriodic, mpu, NULL);
osTimerStart(lv_sense_timer, ONE_SECOND);
fuse_timer = osTimerNew(&read_fuse_data, osTimerPeriodic, pdu, NULL);
osTimerStart(fuse_timer, ONE_SECOND);

data_collection_args_t* data_args = malloc(sizeof(data_collection_args_t));
data_args->mpu = mpu;
data_args->pdu = pdu;
data_args->wheel = wheel;
data_collection_thread = osThreadNew(vDataCollection, data_args, &data_collection_attributes);
assert(data_collection_thread);

/* Data Processing */
process_tsms_thread_id = osThreadNew(vProcessTSMS, NULL, &process_tsms_attributes);
assert(process_tsms_thread_id);
// temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes);
// assert(temp_monitor_handle);
//imu_monitor_handle = osThreadNew(vIMUMonitor, mpu, &imu_monitor_attributes);
//assert(imu_monitor_handle);
// shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes);
// assert(shutdown_monitor_handle);

/* Messaging */
can_dispatch_handle = osThreadNew(vCanDispatch, NULL, &can_dispatch_attributes);
can_dispatch_handle = osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes);
assert(can_dispatch_handle);
can_receive_thread = osThreadNew(vCanReceive, mc, &can_receive_attributes);
assert(can_receive_thread);
serial_monitor_handle = osThreadNew(vSerialMonitor, NULL, &serial_monitor_attributes);
assert(serial_monitor_handle);

/* Control Logic */
fault_handle = osThreadNew(vFaultHandler, NULL, &fault_handle_attributes);
assert(fault_handle);

process_pedals_args_t *proc_pedal_args = malloc(sizeof(process_pedals_args_t));
proc_pedal_args->mc = mc;
proc_pedal_args->pdu = pdu;
process_pedals_thread = osThreadNew(vProcessPedals, proc_pedal_args, &torque_calc_attributes);
assert(process_pedals_thread);
fault_handle = osThreadNew(vFaultHandler, NULL, &fault_handle_attributes);
assert(fault_handle);

sm_director_args_t *sm_args = malloc(sizeof(sm_director_args_t));
sm_args->pdu = pdu;
Expand Down
Loading

0 comments on commit 1f588e9

Please sign in to comment.