Skip to content

Commit

Permalink
feat(balancing): remove eeprom blocking functions from can interrupt …
Browse files Browse the repository at this point in the history
…callback
  • Loading branch information
Tonidotpy committed Feb 27, 2024
1 parent 6c6893b commit f8ffbfc
Show file tree
Hide file tree
Showing 10 changed files with 3,728 additions and 3,927 deletions.
4 changes: 4 additions & 0 deletions cellboard/Core/Inc/bal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <inttypes.h>
#include <stdbool.h>

#define BAL_THRESHOLD_DEFAULT 300 // mV * 10
#define BAL_THRESHOLD_MIN 50 // mV * 10
#define BAL_THRESHOLD_MAX 2000 // mV * 10

#define BAL_NULL_INDEX UINT8_MAX

/**
Expand Down
2 changes: 1 addition & 1 deletion cellboard/Core/Src/bal_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bal_fsm_params bal_params = {
.discharge_cells = 0,
.cycle_length = DCTO_30S,
.target = CELL_MAX_VOLTAGE,
.threshold = BAL_MAX_VOLTAGE_THRESHOLD
.threshold = BAL_THRESHOLD_DEFAULT
};
bal_fsm_transition_request set_bal_request = {
.is_new = false,
Expand Down
5 changes: 0 additions & 5 deletions fenice_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ static const uint8_t TEMP_SENSOR_ADDRESS_CODING[TEMP_SENSORS_PER_STRIP] = {000,
#define AIRP_CHECK_TIMEOUT 1000
#define CELLBOARD_COMM_TIMEOUT 500

// Default voltage delta between cells (mV * 10)
#define BAL_DEFAULT_VOLTAGE_THRESHOLD 300
// Maximum voltage delta between cells (mV * 10)
#define BAL_MAX_VOLTAGE_THRESHOLD 2000

// How much does a balancing cycle last (ms)
#define BAL_CYCLE_LENGTH 30000
#define BAL_TIME_ON 1000
Expand Down
75 changes: 49 additions & 26 deletions mainboard/Inc/bal.h
Original file line number Diff line number Diff line change
@@ -1,58 +1,81 @@
/**
* @file bal_fsm.h
* @brief This file contains the balancing functions
* @file bal.h
* @brief This file contains the balancing functions
*
* @date May 09, 2021
* @date May 09, 2021
*
* @author Matteo Bonora [matteo.bonora@studenti.unitn.it]
* @author Matteo Bonora [matteo.bonora@studenti.unitn.it]
* @author Antonio Gelain [antonio.gelain@studenti.unitn.it]
*/

#ifndef BAL_FSM_H
#define BAL_FSM_H
#ifndef BAL_H
#define BAL_H

#include <inttypes.h>
#include <stdbool.h>

#include "../../fenice_config.h"

#define BAL_THRESHOLD_DEFAULT 300 // mV * 10
#define BAL_THRESHOLD_MIN 50 // mV * 10
#define BAL_THRESHOLD_MAX 2000 // mV * 10

/** @brief Request a change of state in the balancing status */
typedef struct {
bool status;
voltage_t threshold;
bool is_new;
} BalRequest;


/** @brief Initialize balancing */
void bal_init(void);

/**
* @brief Get balancing threshold voltage value
* @brief Request a change to the current balancing status
*
* @return voltage_t The threshold voltage
* @param status The new status to change into
* @param threshold The minimum allowed voltage difference between all cells (in mV * 10)
*/
voltage_t bal_get_threshold();
void bal_change_status_request(bool status, voltage_t threshold);

/**
* @brief Set balancing threshold voltage value
* @brief Get balancing threshold voltage value
*
* @param thresh The new threshold voltage to set
* @return voltage_t The threshold voltage
*/
void bal_set_threshold(uint16_t thresh);
voltage_t bal_get_threshold(void);

/**
* @brief Check if the cellboards are balancing or not
*
* @return true If any cellboards is balancing
* @return false If no cellboards are balancing
*/
bool bal_is_balancing();
bool bal_is_balancing(void);

/**
* @brief Set if the cellboards are balancing or not
* @brief Workaround for 'can_bms_send'
*
* @param is_bal True if any cellboard is balancing false
* @return BalRequest The request data
*/
void bal_set_is_balancing(uint8_t cellboard_id, bool is_bal);
BalRequest bal_get_request(void);

/**
* @brief Workaround for can_bms_send
* @brief Update the balancing status of a cellboard
*
* @return true during bal_start
* @return false otherwise
* @param cellboard The cellboard ID
* @param status The balancing status of the cellboard
*/
bool bal_need_balancing();
void bal_update_status(uint8_t cellboard, bool status);

/** @brief Initialize balancing */
void bal_init();
/** @brief Start balancing */
void bal_start();
/** @brief Stop balancing */
void bal_stop();
void bal_stop(void);

/**
* @brief Routine that keep the current balancing status updated
* @details This function handles the start and stop mechanism of the balancing
*/
void bal_routine(void);

#endif
#endif // BAL_H
98 changes: 42 additions & 56 deletions mainboard/Src/bal.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,62 @@

#include <string.h>

#include "config.h"
#include "can_comm.h"
#include "cli_bms.h"
#include "fans_buzzer.h"
#include "temperature.h"
#include "measures.h"

// TODO: Start and stop balancing per cellboard

#define CONF_VER 0x01
#define CONF_ADDR 0x01

#define BAL_FANS_SPEED 0.2f

typedef struct {
/** @brief Current balancing status */
struct {
uint8_t status: CELLBOARD_COUNT; // Each bit represent a celloboard status
voltage_t threshold;
} bal_params;
bal_params bal_params_default = { BAL_DEFAULT_VOLTAGE_THRESHOLD };
} bal_status;

config_t config;
uint8_t is_balancing;
bool set_balancing;
BalRequest bal_request;

voltage_t bal_get_threshold() {
return ((bal_params *)config_get(&config))->threshold;
}
void bal_set_threshold(uint16_t thresh) {
thresh = MIN(thresh, BAL_MAX_VOLTAGE_THRESHOLD);

bal_params params = *(bal_params *)config_get(&config);
params.threshold = thresh;
void bal_init(void) {
bal_status.status = 0;
bal_status.threshold = BAL_THRESHOLD_DEFAULT;

config_set(&config, &params);
config_write(&config);
bal_request.status = false;
bal_request.threshold = BAL_THRESHOLD_DEFAULT;
bal_request.is_new = false;
}
bool bal_is_balancing() {
return is_balancing != 0;
void bal_change_status_request(bool status, voltage_t threshold) {
bal_request.status = status;
bal_request.threshold = threshold <= BAL_THRESHOLD_MAX && threshold >= BAL_THRESHOLD_MIN ? threshold : BAL_THRESHOLD_DEFAULT;
bal_request.is_new = true;
}
void bal_set_is_balancing(uint8_t cellboard_id, bool is_bal) {
// Set the bit of the cellboard who's balancing
is_balancing &= ~(1 << cellboard_id);
is_balancing |= (1 << cellboard_id) & is_bal;
voltage_t bal_get_threshold(void) {
return bal_status.threshold;
}
bool bal_need_balancing() {
return set_balancing;
bool bal_is_balancing(void) {
return (bool)bal_status.status;
}

void bal_init() {
is_balancing = false;
set_balancing = false;
config_init(&config, CONF_ADDR, CONF_VER, &bal_params_default, sizeof(bal_params));
BalRequest bal_get_request(void) {
return bal_request;
}

void bal_start() {
// Override fans speed
// if (!fans_is_overrided())
// fans_toggle_override();
// fans_set_speed(0.6f);

cli_bms_debug("Starting balancing...", 21);
set_balancing = true;
can_bms_send(BMS_SET_BALANCING_STATUS_FRAME_ID);
set_balancing = false;
void bal_update_status(uint8_t cellboard, bool status) {
if (cellboard >= CELLBOARD_COUNT)
return;

bal_status.status &= ~(1 << cellboard);
bal_status.status |= (1 << cellboard) & status;
}
void bal_stop(void) {
bal_change_status_request(false, BAL_THRESHOLD_DEFAULT);
bal_routine();
}
void bal_routine(void) {
if (bal_request.is_new) {
// Update the balancing status if needed
if (bal_is_balancing() != bal_request.status) {
bal_status.threshold = bal_request.threshold;
can_bms_send(BMS_SET_BALANCING_STATUS_FRAME_ID);
}
// Reset request
bal_request.is_new = false;
}
}
void bal_stop() {
set_balancing = false;
can_bms_send(BMS_SET_BALANCING_STATUS_FRAME_ID);

// Set fans speed to auto
// if (fans_is_overrided())
// fans_toggle_override();
// fans_set_speed(0);
}
3 changes: 3 additions & 0 deletions mainboard/Src/bms_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ bms_state_t do_idle(state_data_t *data) {

/* Your Code Here */

// Update balancing
bal_routine();

// Check for fatal errors
if (error_get_fatal() > 0)
next_state = STATE_FATAL_ERROR;
Expand Down
13 changes: 7 additions & 6 deletions mainboard/Src/cli_bms.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,25 +422,26 @@ void _cli_status(uint16_t argc, char **argv, char *out) {
}

// TODO: Balancing actions
uint16_t bal_threshold = BAL_THRESHOLD_DEFAULT;
void _cli_balance(uint16_t argc, char **argv, char *out) {
strcpy(out, "Work in progress...\n\0");

if (strcmp(argv[1], "on") == 0) {
// if (argc > 2)
// bal.target = atoi(argv[2]);
bal_start();
bal_change_status_request(true, bal_threshold);
sprintf(out, "enabling balancing\r\n");
} else if (strcmp(argv[1], "off") == 0) {
bal_stop();
bal_change_status_request(false, bal_threshold);
sprintf(out, "disabling balancing\r\n");
} else if (strcmp(argv[1], "thr") == 0) {
if (argv[2] != NULL) {
voltage_t thresh = atoff(argv[2]) * 10;
if (thresh <= BAL_MAX_VOLTAGE_THRESHOLD) {
bal_set_threshold(thresh);
bal_threshold = (voltage_t)atol(argv[2]) * 10;
if (bal_threshold < BAL_THRESHOLD_MIN || bal_threshold > BAL_THRESHOLD_MAX) {
bal_threshold = BAL_THRESHOLD_DEFAULT;
}
}
sprintf(out, "balancing threshold is %.2f mV\r\n", bal_get_threshold() / 10.0);
sprintf(out, "balancing threshold is %.2f mV\r\n", bal_threshold / 10.f);
} else if (strcmp(argv[1], "test") == 0) {
/*
CAN_TxHeaderTypeDef tx_header;
Expand Down
52 changes: 16 additions & 36 deletions mainboard/Src/peripherals/can_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,32 +573,23 @@ HAL_StatusTypeDef can_bms_send(uint16_t id) {
uint8_t buffer[CAN_MAX_PAYLOAD_LENGTH] = { 0 };

if (id == BMS_SET_BALANCING_STATUS_FRAME_ID) {
BalRequest request = bal_get_request();

bms_set_balancing_status_t raw_bal = { 0 };
bms_set_balancing_status_converted_t conv_bal = {
.balancing_status = request.status,
.target = MAX(CELL_MIN_VOLTAGE, cell_voltage_get_min()),
.threshold = request.threshold
};
bms_set_balancing_status_conversion_to_raw_struct(&raw_bal, &conv_bal);
if ((tx_header.DLC = bms_set_balancing_status_pack(buffer, &raw_bal, BMS_SET_BALANCING_STATUS_BYTE_SIZE)) < 0)
return HAL_ERROR;

size_t errors = 0;

bool bal_status = bal_need_balancing();
uint16_t target = MAX(CELL_MIN_VOLTAGE, cell_voltage_get_min());
uint16_t threshold = MIN(BAL_MAX_VOLTAGE_THRESHOLD, bal_get_threshold());

for (size_t i = 0; i < CELLBOARD_COUNT; ++i) {
bms_set_balancing_status_t raw_bal = { 0 };
bms_set_balancing_status_converted_t conv_bal = { 0 };

conv_bal.balancing_status = bal_status;
conv_bal.target = target;
conv_bal.threshold = threshold;

bms_set_balancing_status_conversion_to_raw_struct(&raw_bal, &conv_bal);

int data_len = bms_set_balancing_status_pack(buffer, &raw_bal, BMS_SET_BALANCING_STATUS_BYTE_SIZE);
if (data_len < 0)
if (can_send(&BMS_CAN, buffer, &tx_header) != HAL_OK)
++errors;
else {
tx_header.DLC = data_len;
if (can_send(&BMS_CAN, buffer, &tx_header) != HAL_OK)
++errors;
}
}

return errors == 0 ? HAL_OK : HAL_ERROR;
}
else if (id == BMS_JMP_TO_BLT_FRAME_ID) {
Expand Down Expand Up @@ -834,7 +825,7 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef * hcan) {
// Reset time since last communication
time_since_last_comm[conv_status.cellboard_id] = HAL_GetTick();

bal_set_is_balancing(conv_status.cellboard_id, conv_status.balancing_status);
bal_update_status(conv_status.cellboard_id, conv_status.balancing_status);

// Check cellboard errors
uint32_t error_status = conv_status.errors_can_comm |
Expand Down Expand Up @@ -1006,19 +997,8 @@ void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) {
}
primary_set_cell_balancing_status_raw_to_conversion_struct(&conv_bal_status, &raw_bal_status);

// Set threshold
uint16_t thr = conv_bal_status.balancing_threshold; // Received threshold is in mV
bal_set_threshold(thr * 10); // Expected threshold is in mV * 10

// Request for balancing start or stop
switch(conv_bal_status.set_balancing_status) {
case PRIMARY_SET_CELL_BALANCING_STATUS_SET_BALANCING_STATUS_ON_CHOICE:
bal_start();
break;
case PRIMARY_SET_CELL_BALANCING_STATUS_SET_BALANCING_STATUS_OFF_CHOICE:
bal_stop();
break;
}
// Send balancing request
bal_change_status_request(conv_bal_status.set_balancing_status, conv_bal_status.balancing_threshold * 10);
}
else if (rx_header.StdId == PRIMARY_HANDCART_STATUS_FRAME_ID) {
primary_handcart_status_t raw_handcart_status = { 0 };
Expand Down

0 comments on commit f8ffbfc

Please sign in to comment.