Skip to content

Commit

Permalink
190 CAN consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Aug 10, 2024
1 parent f3f5415 commit cb30f94
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 125 deletions.
9 changes: 2 additions & 7 deletions Core/Inc/bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@

typedef struct {
osTimerId bms_monitor_timer;
uint16_t dcl;
uint16_t ccl;
} bms_t;

extern bms_t *bms;

void bms_init();

/**
* @brief Task for handling CAN messages received from the AMS.
* @brief Callback for when a DCL message is received from the AMS.
*/
void vAMSCANMonitor(void *pv_params);
extern osThreadId_t bms_monitor_handle;
extern const osThreadAttr_t bms_monitor_attributes;
extern osMessageQueueId_t bms_monitor_queue;
void handle_dcl_msg();

#endif /*BMS_H*/
20 changes: 15 additions & 5 deletions Core/Inc/can_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@

#include "can.h"
#include "cmsis_os.h"
#include "dti.h"

void can1_callback(CAN_HandleTypeDef *hcan);

/**
* @brief Place a CAN message in a queue.
*
* @param msg CAN message to be sent.
* @return int8_t Error code.
*/
int8_t queue_can_msg(can_msg_t msg);
void init_can1(CAN_HandleTypeDef *hcan);

/**
* @brief Task for sending CAN messages.
*/
Expand All @@ -27,12 +37,12 @@ extern osThreadId_t can_dispatch_handle;
extern const osThreadAttr_t can_dispatch_attributes;

/**
* @brief Place a CAN message in a queue.
* @brief Task for processing received can messages.
*
* @param msg CAN message to be sent.
* @return int8_t Error code.
* @param pv_params A can_receive_args_t*.
*/
int8_t queue_can_msg(can_msg_t msg);
void init_can1(CAN_HandleTypeDef *hcan);
void vCanReceive(void *pv_params);
extern osThreadId_t can_receive_thread;
extern const osThreadAttr_t can_receive_attributes;

#endif
14 changes: 8 additions & 6 deletions Core/Inc/dti.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ typedef struct {
// TODO: Expand GET interface
int32_t dti_get_rpm(dti_t *dti);

/**
* @brief Process DTI ERPM CAN message.
*
* @param mc Pointer to struct representing motor controller.
* @param msg CAN message to process.
*/
void dti_record_rpm(dti_t *mc, can_msg_t msg);

float dti_get_mph(dti_t *dti);

uint16_t dti_get_input_voltage(dti_t *dti);

/* Utilities for Decoding CAN message */
extern osThreadId_t dti_router_handle;
extern const osThreadAttr_t dti_router_attributes;
extern osMessageQueueId_t dti_router_queue;
void vDTIRouter(void *pv_params);

dti_t *dti_init();

/*
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "cmsis_os.h"
#include "fault.h"
#include "nero.h"
#include "dti.h"
#include "pdu.h"
#include "dti.h"

/*
* This is a hierarchical state machine, with "drive modes"
Expand Down
21 changes: 2 additions & 19 deletions Core/Src/bms.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
#include <stdlib.h>
#include "stdio.h"

#define BMS_QUEUE_SIZE 5

osMessageQueueId_t bms_monitor_queue;

bms_t *bms;

void bms_fault_callback();
Expand Down Expand Up @@ -41,22 +37,9 @@ void bms_init()
/*TO-DO: specify timer attributes*/
bms->bms_monitor_timer =
osTimerNew(&bms_fault_callback, osTimerOnce, NULL, NULL);

bms_monitor_queue =
osMessageQueueNew(BMS_QUEUE_SIZE, sizeof(can_msg_t), NULL);
assert(bms_monitor_queue);
}

void vAMSCANMonitor(void *pv_params)
void handle_dcl_msg()
{
can_msg_t msg_from_queue;

osTimerStart(bms->bms_monitor_timer, BMS_CAN_MONITOR_DELAY);
for (;;) {
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);
}
}
}
73 changes: 48 additions & 25 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
*/

#include "can_handler.h"
#include "can.h"
#include "cerberus_conf.h"
#include "dti.h"
#include "fault.h"
#include "steeringio.h"
#include "serial_monitor.h"
Expand All @@ -21,12 +19,15 @@
#include <stdlib.h>
#include "stdio.h"
#include <string.h>
#include "dti.h"

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

#define CAN_DISPATCH_FLAG 1U

#define NEW_CAN_MSG_FLAG 1U

static osMessageQueueId_t can_outbound_queue;
static osMessageQueueId_t can_inbound_queue;

can_t *can1;

Expand All @@ -51,6 +52,8 @@ void init_can1(CAN_HandleTypeDef *hcan)

can_outbound_queue =
osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL);
can_inbound_queue =
osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL);
}

/* Callback to be called when we get a CAN message */
Expand All @@ -75,25 +78,8 @@ void can1_callback(CAN_HandleTypeDef *hcan)
new_msg.len = rx_header.DLC;
new_msg.id = rx_header.StdId;

// TODO: Switch to hash map
switch (new_msg.id) {
/* Messages Relevant to Motor Controller */
case DTI_CANID_ERPM:
// case DTI_CANID_CURRENTS:
// case DTI_CANID_TEMPS_FAULT:
// 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;
}
osMessageQueuePut(can_inbound_queue, &new_msg, 0U, 0U);
osThreadFlagsSet(can_receive_thread, NEW_CAN_MSG_FLAG);
}

int8_t queue_can_msg(can_msg_t msg)
Expand All @@ -102,7 +88,7 @@ int8_t queue_can_msg(can_msg_t msg)
return -1;

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

Expand All @@ -122,7 +108,7 @@ void vCanDispatch(void *pv_params)
HAL_StatusTypeDef msg_status;

for (;;) {
osThreadFlagsWait(CAN_NEW_MSG_FLAG, osFlagsWaitAny,
osThreadFlagsWait(CAN_DISPATCH_FLAG, osFlagsWaitAny,
osWaitForever);
/* Send CAN message */
while (osMessageQueueGet(can_outbound_queue, &msg_from_queue,
Expand All @@ -139,3 +125,40 @@ void vCanDispatch(void *pv_params)
}
}
}

osThreadId_t can_receive_thread;
const osThreadAttr_t can_receive_attributes = {
.name = "CanProcessing",
.stack_size = 128 * 8,
.priority = (osPriority_t)osPriorityRealtime,
};

void vCanReceive(void *pv_params)
{
dti_t *mc = (dti_t *)pv_params;

can_msg_t msg;

for (;;) {
osThreadFlagsWait(NEW_CAN_MSG_FLAG, osFlagsWaitAny,
osWaitForever);
while (osOK ==
osMessageQueueGet(can_inbound_queue, &msg, 0U, 0U)) {
switch (msg.id) {
/* Messages Relevant to Motor Controller */
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;
default:
break;
}
}
}
}
44 changes: 1 addition & 43 deletions Core/Src/dti.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,7 @@ float dti_get_mph(dti_t *mc)
(TIRE_DIAMETER / 63360.0) * M_PI;
}

/* Inbound Task-specific Info */
osThreadId_t dti_router_handle;
const osThreadAttr_t dti_router_attributes = {
.name = "DTIRouter",
.stack_size = 64 * 8,
.priority = (osPriority_t)osPriorityHigh
};

static void dti_record_rpm(dti_t *mc, can_msg_t msg)
void dti_record_rpm(dti_t *mc, can_msg_t msg)
{
/* ERPM is first four bytes of can message in big endian format */
int32_t erpm = (msg.data[0] << 24) + (msg.data[1] << 16) +
Expand All @@ -287,37 +279,3 @@ static void dti_record_rpm(dti_t *mc, can_msg_t msg)
osMutexRelease(*mc->mutex);
set_mph(dti_get_mph(mc));
}

void vDTIRouter(void *pv_params)
{
can_msg_t message;
osStatus_t status;
// fault_data_t fault_data = { .id = DTI_ROUTING_FAULT, .severity = DEFCON2 };

dti_t *mc = (dti_t *)pv_params;

for (;;) {
osThreadFlagsWait(NEW_DTI_MSG_FLAG, osFlagsWaitAny,
osWaitForever);

status = osMessageQueueGet(dti_router_queue, &message, NULL,
osWaitForever);
if (status == osOK) {
switch (message.id) {
case DTI_CANID_ERPM:
dti_record_rpm(mc, message);
break;
case DTI_CANID_CURRENTS:
break;
case DTI_CANID_TEMPS_FAULT:
break;
case DTI_CANID_ID_IQ:
break;
case DTI_CANID_SIGNALS:
break;
default:
break;
}
}
}
}
6 changes: 2 additions & 4 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,10 @@ int main(void)
assert(process_tsms_thread_id);

/* Messaging */
dti_router_handle = osThreadNew(vDTIRouter, mc, &dti_router_attributes);
assert(dti_router_handle);
can_dispatch_handle = osThreadNew(vCanDispatch, NULL, &can_dispatch_attributes);
assert(can_dispatch_handle);
bms_monitor_handle = osThreadNew(vAMSCANMonitor, NULL, &bms_monitor_attributes);
assert(bms_monitor_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);
nero_monitor_handle = osThreadNew(vNeroMonitor, NULL, &nero_monitor_attributes);
Expand Down
17 changes: 2 additions & 15 deletions Core/Src/state_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ typedef struct {
} state;
} state_req_t;

/* State Transition Map */
// static const bool valid_trans_to_from[MAX_FUNC_STATES][MAX_FUNC_STATES] = {
// /*READY DRIVING FAULTED*/
// { true, true, true }, /* READY */
// { true, true, true }, /* PERFORMANCE */
// { true, false, true } /* FAULTED */
// };

osThreadId_t sm_director_handle;
const osThreadAttr_t sm_director_attributes = {
.name = "State Machine Director",
Expand Down Expand Up @@ -67,11 +59,6 @@ nero_state_t get_nero_state()
static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
dti_t *mc)
{
// if (!valid_trans_to_from[cerberus_state.functional][new_state]) {
// printf("Invalid State transition");
// return -1;
// }

/* Catching state transitions */
switch (new_state) {
case READY:
Expand Down Expand Up @@ -120,7 +107,7 @@ static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
osTimerStart(fault_timer, 5000);
HAL_IWDG_Refresh(&hiwdg);
cerberus_state.nero =
(nero_state_t){ .nero_index = OFF, .home_mode = true };
(nero_state_t){ .nero_index = OFF, .home_mode = false };
serial_print("FAULTED\r\n");
break;
default:
Expand Down Expand Up @@ -200,7 +187,7 @@ static int queue_state_transition(state_req_t new_state)
int increment_nero_index()
{
/* Wrap around if end of menu reached */
if (get_nero_state().nero_index + 1 == MAX_NERO_STATES) {
if (get_nero_state().nero_index + 1 >= MAX_NERO_STATES) {
return queue_state_transition((state_req_t){
.id = NERO,
.state.nero = (nero_state_t){
Expand Down

0 comments on commit cb30f94

Please sign in to comment.