Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finite state machine and can messages to control the Contactor #2

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 139 additions & 59 deletions firmware/src/can_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
uint8_t can_app_send_state_clk_div;
uint8_t can_app_send_motor_clk_div;
uint16_t can_app_checks_without_mic19_msg;
uint16_t can_app_checks_without_mac22_msg;
uint16_t can_app_checks_without_mswi19_msg;
uint8_t mswi19_connected;

Expand Down Expand Up @@ -58,6 +59,7 @@ inline void can_app_send_state(void)

msg.data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] = CAN_SIGNATURE_SELF;
msg.data[CAN_MSG_GENERIC_STATE_STATE_BYTE] = (uint8_t) state_machine;
// msg.data[CAN_MSG_GENERIC_STATE_CONTACTOR_BYTE] = (uint8_t) state_contactor; // TODO: adicionar?
msg.data[CAN_MSG_GENERIC_STATE_ERROR_BYTE] = error_flags.all;

can_send_message(&msg);
Expand All @@ -73,8 +75,44 @@ inline void can_app_send_motor(void)
msg.data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] = CAN_SIGNATURE_SELF;
msg.data[CAN_MSG_MAM19_MOTOR_D_BYTE] = control.D;
msg.data[CAN_MSG_MAM19_MOTOR_I_BYTE] = control.I;
// msg.data[CAN_MSG_MAM19_MOTOR_R_BYTE] = control.R; // TODO: adicionar?

can_send_message(&msg);
can_send_message(&msg);
}

inline void can_app_send_contactor_request(uint8_t request)
{
can_t msg;
msg.id = CAN_MSG_MAM19_CONTACTOR_ID;
msg.length = CAN_MSG_MAM19_CONTACTOR_LENGTH;
msg.flags.rtr = 0;

msg.data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] = CAN_SIGNATURE_SELF;
msg.data[CAN_MSG_MAM19_CONTACTOR_REQUEST_BYTE] = request;

can_send_message(&msg);

contactor.message_received = CONTACTOR_REQUEST_UNKNOWN;
contactor.message_sent = request;
contactor.timeout_clk_div = 0;
}

/**
* @brief extracts the specific MIC19 MOTOR message
*
* The msg is AAAAAAAABBBBBBBB
* A is the Signature of module
* B is the Response
*
* @param *msg pointer to the message to be extracted
*/
inline void can_app_extractor_mac22_contactor_response(can_t *msg)
{
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MAC22){
can_app_checks_without_mac22_msg = 0;

contactor.message_received = msg->data[CAN_MSG_MAC22_CONTACTOR_RESPONSE_BYTE];
}
}

/**
Expand All @@ -84,7 +122,7 @@ inline void can_app_send_motor(void)
inline void can_app_extractor_mic19_state(can_t *msg)
{
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MIC19){
// zerar contador
can_app_checks_without_mic19_msg = 0;
if(msg->data[CAN_MSG_GENERIC_STATE_ERROR_BYTE]){
//ERROR!!!
}
Expand All @@ -96,10 +134,25 @@ inline void can_app_extractor_mic19_state(can_t *msg)
}
}

inline void can_app_extractor_mac22_state(can_t *msg)
{
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MAC22){
can_app_checks_without_mac22_msg = 0;
if(msg->data[CAN_MSG_GENERIC_STATE_ERROR_BYTE]){
//ERROR!!!
}
/*if(contador == maximo)*/{
//ERROR!!!
}


}
}

inline void can_app_extractor_mswi19_state(can_t *msg)
{
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MSWI19){
// zerar contador
can_app_checks_without_mswi19_msg = 0;
if(msg->data[CAN_MSG_GENERIC_STATE_ERROR_BYTE]){
//ERROR!!!
}
Expand Down Expand Up @@ -135,15 +188,15 @@ inline void can_app_extractor_mic19_motor(can_t *msg)
CAN_MSG_MIC19_MOTOR_MOTOR_BYTE],
CAN_MSG_MIC19_MOTOR_MOTOR_DMS_ON_BIT);

system_flags.reverse = bit_is_set(msg->data[
CAN_MSG_MIC19_MOTOR_MOTOR_BYTE],
CAN_MSG_MIC19_MOTOR_MOTOR_REVERSE_BIT);

if(!mswi19_connected){
control.D_raw_target = msg->data[CAN_MSG_MIC19_MOTOR_D_BYTE];
}

control.I_raw_target = msg->data[CAN_MSG_MIC19_MOTOR_I_BYTE];

}
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MSWI19){
control.D_raw_target = msg->data[CAN_MSG_MIC19_MOTOR_D_BYTE];
}
}

Expand All @@ -152,25 +205,67 @@ inline void can_app_extractor_mswi19_motor(can_t *msg)
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MSWI19){
can_app_checks_without_mswi19_msg = 0;
mswi19_connected = 1;
/*
system_flags.motor_on = bit_is_set(msg->data[
CAN_MSG_MSWI19_MOTOR_MOTOR_BYTE],
CAN_MSG_MSWI19_MOTOR_MOTOR_MOTOR_ON_BIT);

system_flags.dms = bit_is_set(msg->data[
CAN_MSG_MSWI19_MOTOR_MOTOR_BYTE],
CAN_MSG_MSWI19_MOTOR_MOTOR_DMS_ON_BIT);
*/

control.D_raw_target = msg->data[CAN_MSG_MSWI19_MOTOR_D_BYTE];
control.D_raw_target = msg->data[CAN_MSG_MSWI19_MOTOR_D_BYTE];
}
}

/*
control.I_raw_target = msg->data[CAN_MSG_MSWI19_MOTOR_I_BYTE];
*/
inline void can_app_msg_mic19_extractors_switch(can_t *msg)
{
switch(msg->id){
case CAN_MSG_MIC19_MOTOR_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a motor msg from mic19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mic19_motor(msg);
break;
case CAN_MSG_MIC19_STATE_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a state msg from mic19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mic19_state(msg);
break;
default:
VERBOSE_MSG_CAN_APP(usart_send_string("got a unknown msg from mic19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
break;
}
}

inline void can_app_msg_mswi19_extractors_switch(can_t *msg)
{
switch(msg->id){
case CAN_MSG_MSWI19_MOTOR_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a motor msg from mswi19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mswi19_motor(msg);
break;
case CAN_MSG_MSWI19_STATE_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a state msg from mswi19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mswi19_state(msg);
break;
default:
VERBOSE_MSG_CAN_APP(usart_send_string("got a unknown msg from mswi19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
break;
}
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MSWI19){
control.D_raw_target = msg->data[CAN_MSG_MSWI19_MOTOR_D_BYTE];
}

inline void can_app_msg_mac22_extractors_switch(can_t *msg)
{
switch(msg->id){
case CAN_MSG_MAC22_CONTACTOR_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a motor msg from mac22: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mac22_contactor_response(msg);
break;
case CAN_MSG_MAC22_STATE_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a state msg from mac22: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mac22_state(msg);
break;
default:
VERBOSE_MSG_CAN_APP(usart_send_string("got a unknown msg from mac22: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
break;
}
}

Expand All @@ -180,42 +275,18 @@ inline void can_app_extractor_mswi19_motor(can_t *msg)
*/
inline void can_app_msg_extractors_switch(can_t *msg)
{
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MIC19){
switch(msg->id){
case CAN_MSG_MIC19_MOTOR_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a motor msg from mic19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mic19_motor(msg);
break;
case CAN_MSG_MIC19_STATE_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a state msg from mic19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mic19_state(msg);
break;
default:
VERBOSE_MSG_CAN_APP(usart_send_string("got a unknown msg from mic19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
break;
}
switch(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE]){
case CAN_SIGNATURE_MIC19:
can_app_msg_mic19_extractors_switch(msg);
break;
case CAN_SIGNATURE_MSWI19:
can_app_msg_mswi19_extractors_switch(msg);
break;
case CAN_SIGNATURE_MAC22:
can_app_msg_mac22_extractors_switch(msg);
break;
default: break;
}
if(msg->data[CAN_MSG_GENERIC_STATE_SIGNATURE_BYTE] == CAN_SIGNATURE_MSWI19){
switch(msg->id){
case CAN_MSG_MSWI19_MOTOR_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a motor msg from mswi19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mswi19_motor(msg);
break;
case CAN_MSG_MSWI19_STATE_ID:
VERBOSE_MSG_CAN_APP(usart_send_string("got a state msg from mswi19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
can_app_extractor_mswi19_state(msg);
break;
default:
VERBOSE_MSG_CAN_APP(usart_send_string("got a unknown msg from mswi19: "));
VERBOSE_MSG_CAN_APP(can_app_print_msg(msg));
break;
}
}
}

/**
Expand All @@ -224,7 +295,7 @@ inline void can_app_msg_extractors_switch(can_t *msg)
inline void check_can(void)
{
// If no messages is received from mic19 for
// CAN_APP_CHECKS_WITHOUT_MIC19_MSG cycles, than it go to a specific error state.
// CAN_APP_CHECKS_WITHOUT_MIC19_MSG cycles, than it go to a specific error state.
//VERBOSE_MSG_CAN_APP(usart_send_string("checks: "));
//VERBOSE_MSG_CAN_APP(usart_send_uint16(can_app_checks_without_mic19_msg));
if(can_app_checks_without_mic19_msg++ >= CAN_APP_CHECKS_WITHOUT_MIC19_MSG){
Expand All @@ -240,6 +311,15 @@ inline void check_can(void)
mswi19_connected = 0;
}

if(can_app_checks_without_mac22_msg++ >= CAN_APP_CHECKS_WITHOUT_MAC22_MSG){
VERBOSE_MSG_CAN_APP(usart_send_string("Error: too many cycles without mac22 messages.\n"));
can_app_checks_without_mac22_msg = 0;
#ifdef SET_ERROR_WHEN_NO_STATE_MESSAGES_FROM_MAC22
error_flags.no_contactor = 1;
set_state_error();
#endif
}

if(can_check_message()){
can_t msg;
if(can_get_message(&msg)){
Expand Down
9 changes: 9 additions & 0 deletions firmware/src/can_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,27 @@ void can_app_print_msg(can_t *msg);
void can_app_task(void);
void can_app_send_state(void);
void can_app_send_motor(void);
void can_app_send_contactor_request(uint8_t request);
void can_app_extractor_mic19_state(can_t *msg);
void can_app_extractor_mic19_motor(can_t *msg);
void can_app_extractor_mswi19_state(can_t *msg);
void can_app_extractor_mswi19_motor(can_t *msg);
void can_app_extractor_mac22_state(can_t *msg);
void can_app_extractor_mac22_contactor_response(can_t *msg);
void can_app_msg_extractors_switch(can_t *msg);
void can_app_msg_mic19_extractors_switch(can_t *msg);
void can_app_msg_mswi19_extractors_switch(can_t *msg);
void can_app_msg_mac22_extractors_switch(can_t *msg);
void check_can(void);

#define CAN_APP_SEND_STATE_CLK_DIV 100
#define CAN_APP_SEND_MOTOR_CLK_DIV 50

extern uint8_t can_app_send_state_clk_div;
extern uint8_t can_app_send_motor_clk_div;
// #define SET_ERROR_WHEN_NO_STATE_MESSAGES_FROM_MAC22 //<!" Consider the lack of MAC22's communication as an ERROR!
extern uint16_t can_app_checks_without_mac22_msg;
#define CAN_APP_CHECKS_WITHOUT_MAC22_MSG 200
extern uint16_t can_app_checks_without_mic19_msg;
#define CAN_APP_CHECKS_WITHOUT_MIC19_MSG 100
extern uint16_t can_app_checks_without_mswi19_msg;
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/can_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ const uint8_t can_filter[] PROGMEM =
// Group 1
MCP2515_FILTER(CAN_MSG_MIC19_STATE_ID), // Filter 2
MCP2515_FILTER(CAN_MSG_MIC19_MOTOR_ID), // Filter 3
MCP2515_FILTER(CAN_MSG_MIC19_STATE_ID), // Filter 4
MCP2515_FILTER(CAN_MSG_MIC19_MOTOR_ID), // Filter 5
MCP2515_FILTER(CAN_MSG_MAC22_STATE_ID), // Filter 4
MCP2515_FILTER(CAN_MSG_MAC22_CONTACTOR_ID), // Filter 5

MCP2515_FILTER(CAN_MASK_MIC19), // Mask 0 (for group 0)
MCP2515_FILTER(CAN_MASK_MIC19), // Mask 1 (for group 1)
Expand Down
Loading