Skip to content

Commit

Permalink
charger ifndef, one msg no packed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Apr 27, 2024
1 parent 6f399e3 commit 58781ae
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 30 deletions.
90 changes: 66 additions & 24 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define MAX_CAN1_STORAGE 10
#define MAX_CAN2_STORAGE 10
// #define CHARGING_ENABLED

uint8_t fan_speed;
bool is_charging_enabled;
Expand Down Expand Up @@ -37,6 +38,8 @@ uint32_t adc_values[2] = {0};
uint8_t calc_charger_led_state();
float read_ref_voltage();
float read_vout();
uint8_t get_high_byte(uint16_t val);
uint8_t get_low_byte(uint16_t val);

uint8_t compute_init()
{
Expand Down Expand Up @@ -116,18 +119,21 @@ int compute_send_charging_message(uint16_t voltage_to_set, uint16_t current_to_s
charger_msg.data[2] = charger_msg.data[3];
charger_msg.data[3] = temp;

#ifdef CHARGING_ENABLED
HAL_StatusTypeDef res = can_send_extended_msg(&can2, &charger_msg);
if(res != HAL_OK) {
printf("CAN ERROR CODE %X", res);
return -1;
}
#endif

return 0;
}

bool compute_charger_connected()
{
//TODO need to set up CAN msg that actually toggles this bool
return bmsdata->is_charger_connected;
return false; //bmsdata->is_charger_connected;
}

//TODO add this back
Expand Down Expand Up @@ -263,27 +269,29 @@ void compute_send_mc_charge_message(acc_data_t* bmsdata)
void compute_send_acc_status_message(acc_data_t* bmsdata)
{

struct __attribute__((packed)) {
uint16_t packVolt;
uint16_t pack_current;
uint16_t pack_ah;
uint8_t pack_soc;
uint8_t pack_health;
} acc_status_msg_data;

acc_status_msg_data.packVolt = bmsdata->pack_voltage;
acc_status_msg_data.pack_current = (uint16_t) (bmsdata->pack_current); // convert with 2s complement
acc_status_msg_data.pack_ah = 0;
acc_status_msg_data.pack_soc = bmsdata->soc;
acc_status_msg_data.pack_health = 0;
uint16_t packVolt = bmsdata->pack_voltage;
uint16_t pack_current = (uint16_t) (bmsdata->pack_current); // convert with 2s complement
uint16_t pack_ah = 0;
uint8_t pack_soc = bmsdata->soc;
uint8_t pack_health = 0;

uint8_t volt_high = get_high_byte(packVolt);
uint8_t volt_low = get_low_byte(packVolt);
uint8_t curr_high = get_high_byte(pack_current);
uint8_t curr_low = get_low_byte(pack_current);
uint8_t ah_high = get_high_byte(pack_ah);
uint8_t ah_low = get_low_byte(pack_ah);

uint8_t acc_status_msg_data[8] = {volt_high, volt_low, curr_high, curr_low, ah_high, ah_low, pack_soc, pack_health};
can_msg_t acc_msg;
acc_msg.id = 0x80; // TODO replace with correct ID;
acc_msg.len = sizeof(acc_status_msg_data);
memcpy(acc_msg.data, &acc_status_msg_data, sizeof(acc_status_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;

#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand All @@ -308,7 +316,11 @@ void compute_send_bms_status_message(acc_data_t* bmsdata, int bms_state, bool ba
acc_msg.len = sizeof(bms_status_msg_data);
memcpy(acc_msg.data, &bms_status_msg_data, sizeof(bms_status_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;
#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand All @@ -325,7 +337,11 @@ void compute_send_shutdown_ctrl_message(uint8_t mpe_state)
acc_msg.len = sizeof(shutdown_control_msg_data);
memcpy(acc_msg.data, &shutdown_control_msg_data, sizeof(shutdown_control_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;
#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand All @@ -350,7 +366,11 @@ void compute_send_cell_data_message(acc_data_t* bmsdata)
acc_msg.len = sizeof(cell_data_msg_data);
memcpy(acc_msg.data, &cell_data_msg_data, sizeof(cell_data_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;
#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand All @@ -377,7 +397,11 @@ void compute_send_cell_voltage_message(uint8_t cell_id, uint16_t instant_voltage
acc_msg.len = sizeof(cell_voltage_msg_data);
memcpy(acc_msg.data, &cell_voltage_msg_data, sizeof(cell_voltage_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;
#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand All @@ -398,7 +422,11 @@ void compute_send_current_message(acc_data_t* bmsdata)
acc_msg.len = sizeof(current_status_msg_data);
memcpy(acc_msg.data, &current_status_msg_data, sizeof(current_status_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;
#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand Down Expand Up @@ -429,7 +457,11 @@ void compute_send_cell_temp_message(acc_data_t* bmsdata)
acc_msg.len = sizeof(cell_temp_msg_data);
memcpy(acc_msg.data, &cell_temp_msg_data, sizeof(cell_temp_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;
#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand All @@ -455,7 +487,11 @@ void compute_send_segment_temp_message(acc_data_t* bmsdata)
acc_msg.len = sizeof(segment_temp_msg_data);
memcpy(acc_msg.data, &segment_temp_msg_data, sizeof(segment_temp_msg_data));

can_t* can_line = (bmsdata->is_charger_connected) ? &can2 : &can1;
#ifdef CHARGING_ENABLED
can_t* can_line = &can2;
#else
can_t* can_line = &can1;
#endif
can_send_msg(can_line, &acc_msg);
}

Expand Down Expand Up @@ -514,3 +550,9 @@ float read_vout()
return vout;
}

uint8_t get_high_byte(uint16_t val) {
return (uint8_t) ((packVolt & 0xFF00) >> 8);
}
uint8_t get_low_byte(uint16_t val) {
return (uint8_t) (packVolt & 0x00FF);
}
12 changes: 6 additions & 6 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ int main(void)
//TODO add ISR/timer based debug LED toggle

acc_data_t *acc_data = malloc(sizeof(acc_data_t));

acc_data->is_charger_connected = false;
//acc_data->faultCode = FAULTS_CLEAR;

/* check for inbound CAN */
// get_can1_msg();
get_can2_msg();

/*
* Collect all the segment data needed to perform analysis
* Not state specific
Expand All @@ -287,11 +291,7 @@ int main(void)
/* Perform calculations on the data in the frame */
analyzer_push(acc_data);
sm_handle_state(acc_data);

/* check for inbound CAN */
// get_can1_msg();
get_can2_msg();


#ifdef DEBUG_STATS
print_bms_stats(acc_data);
#endif
Expand Down

0 comments on commit 58781ae

Please sign in to comment.