Skip to content

Commit

Permalink
Add BMSManager library
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon-Knight committed Oct 20, 2024
1 parent 9189313 commit d46c64b
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 126 deletions.
176 changes: 57 additions & 119 deletions include/BMSLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,180 +4,118 @@

#include <bms_ant_data.h>

extern UART_HandleTypeDef hBms1Uart;
extern UART_HandleTypeDef hBms2Uart;


// -----------------------------------------------------------------------------------------------------------------
#include <BMSManager.h>
#include <drivers/BMSAnt.h>
#include <drivers/BMSOther.h>



namespace BMSLogic
{
//*********************************************************************
// BMS firmware settings
//*********************************************************************

/// @brief BMS request period
static constexpr uint32_t CFG_BMSRequestPeriod = 250;

/// @brief BMS request timeout
static constexpr uint32_t CFG_BMSRequestTimeout = 250;

// -----------------------------------------------------------------------------------------------------------------

//*********************************************************************
// BMS firmware constants
//*********************************************************************

/// @brief BMS get info request


extern UART_HandleTypeDef hBms1Uart;
extern UART_HandleTypeDef hBms2Uart;





namespace BMSLogic
{

void UART_TX(uint8_t idx, const uint8_t *data, const uint16_t length);


BMSAnt ant1;
BMSAnt ant2;
BMSOther other;
BMSManager bms(UART_TX, [](uint8_t idx, int8_t code)
{
DEBUG_LOG_TOPIC("BMS-ERR", "idx: %d, code: %d\n", idx, code);
});






static constexpr uint8_t PACKET_TX_REQUEST[] = {0x5A, 0x5A, 0x00, 0x00, 0x00, 0x00};

static constexpr uint8_t PACKET_RX_SIZE = 140;
static constexpr uint8_t PACKET_RX_HEADER[] = {0xAA, 0x55, 0xAA, 0xFF};

struct data_t
{
UART_HandleTypeDef *hal; // Указатель на объект HAL USART
uint8_t hot[PACKET_RX_SIZE + 60]; // Горячий массив данных (Работа в прерывании)
uint8_t cold[PACKET_RX_SIZE]; // Холодный массив данных (Работа в программе)
bool ready; // Флаг того, что массив данные приняты и готовы к анализу
bool busy; // Флаг того, что разбор данных не окончен и новые копировать нельзя.
} data[2];
UART_HandleTypeDef *hal; // Указатель на объект HAL USART
uint8_t hot[200]; // Горячий массив данных (Работа в прерывании)
} uart_data[2];

enum bms_num_t : uint8_t { BMS_1 = 0, BMS_2 = 1 };



int8_t error = 0;




void ReverseArray(uint8_t *array, uint8_t length)
{
uint8_t i = 0;
uint8_t j = length - 1;
uint8_t temp;
while(i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;

i++;
j--;
}

return;
}








void RxPacket(bms_num_t idx, uint16_t data_length)
inline void UART_RX(uint8_t idx, const uint16_t length)
{
data_t *obj = &data[idx];

if(obj->busy == true) return;
if(data_length != BMS_BOARD_PACKET_SIZE) return;
if(memcmp(PACKET_RX_HEADER, obj->hot, sizeof(PACKET_RX_HEADER)) != 0) return;

memcpy(obj->cold, obj->hot, sizeof(obj->cold));
ReverseArray(obj->cold, sizeof(obj->cold));

obj->ready = true;
obj->busy = true;
bms.DataRx(idx, uart_data[idx].hot, length);

return;
}

bool CheckCRC(bms_num_t idx)

void UART_TX(uint8_t idx, const uint8_t *data, const uint16_t length)
{
data_t *obj = &data[idx];
BMSANTLib::packet_raw_reverse_t *data = (BMSANTLib::packet_raw_reverse_t *)obj->cold;

uint16_t crc = 0x0000;
for(uint8_t i = 4; i < 138; ++i)
{
crc += obj->cold[i];
}
if(data->crc != crc)
{
//DEBUG_LOG("ERROR: BMS CRC error! Expected: 0x%04X, presented: 0x%04X", bms_raw_data_crc(bms_raw_packet_data), get_bms_raw_data_crc(bms_raw_packet_data));
DEBUG_LOG_TOPIC("BMS", "CRC error!");

return false;
}
HAL_UART_Transmit(uart_data[idx].hal, (uint8_t *)data, length, 64U);

return true;
return;
}




inline void Setup()
{
memset(data, 0x00, sizeof(data));
memset(uart_data, 0x00, sizeof(uart_data));

data[BMS_1].hal = &hBms1Uart;
data[BMS_2].hal = &hBms2Uart;
uart_data[BMS_1].hal = &hBms1Uart;
uart_data[BMS_2].hal = &hBms2Uart;


HAL_UARTEx_ReceiveToIdle_IT(&hBms1Uart, data[BMS_1].hot, sizeof(data[BMS_1].hot));
//HAL_UARTEx_ReceiveToIdle_IT(&hBms2Uart, data[BMS_2].hot, sizeof(data[BMS_2].hot));

bms.SetModel(BMS_1, ant1);
bms.SetModel(BMS_2, ant2);


HAL_UARTEx_ReceiveToIdle_IT(uart_data[BMS_1].hal, uart_data[BMS_1].hot, sizeof(uart_data[BMS_1].hot));
HAL_UARTEx_ReceiveToIdle_IT(uart_data[BMS_2].hal, uart_data[BMS_2].hot, sizeof(uart_data[BMS_2].hot));

return;
}

inline void Loop(uint32_t &current_time)
{
static uint32_t last_tick = 0;
if(current_time - last_tick > 0)
{
last_tick = current_time;

data_t *obj;
for(uint8_t i = 0; i < 2; ++i)
{
obj = &data[i];

if(obj->ready == true)
{
obj->ready = false;

CheckCRC((bms_num_t)i);
DEBUG_LOG_ARRAY_HEX("BMS", obj->cold, sizeof(obj->cold));
CANLib::UpdateCANObjects_BMS(obj->cold);

obj->busy = false;
}
}
}

static uint32_t iter = 0;
if(current_time - iter > CFG_BMSRequestPeriod)
bms.Tick(current_time);


static uint32_t tick500 = 0;
if(current_time - tick500 > 500)
{
iter = current_time;

HAL_UART_Transmit(data[BMS_1].hal, (uint8_t *)PACKET_TX_REQUEST, sizeof(PACKET_TX_REQUEST), 64U);
HAL_UART_Transmit(data[BMS_2].hal, (uint8_t *)PACKET_TX_REQUEST, sizeof(PACKET_TX_REQUEST), 64U);
tick500 = current_time;

DEBUG_LOG_TOPIC("BMS1", "vol: %d, cur: %d, pow: %d, err: %d\n", bms.data[0].voltage, bms.data[0].current, bms.data[0].power, error);
DEBUG_LOG_TOPIC("BMS2", "vol: %d, cur: %d, pow: %d, err: %d\n", bms.data[1].voltage, bms.data[1].current, bms.data[1].power, error);
DEBUG_LOG_ARRAY_HEX("BMS1-hex", (uint8_t *)ant1.data, 140);
DEBUG_LOG_NEW_LINE();
}

//CANLib::UpdateCANObjects_BMS(obj->cold);


//HAL_UART_Transmit(data[BMS_1].hal, (uint8_t *)PACKET_TX_REQUEST, sizeof(PACKET_TX_REQUEST), 64U);
//HAL_UART_Transmit(data[BMS_2].hal, (uint8_t *)PACKET_TX_REQUEST, sizeof(PACKET_TX_REQUEST), 64U);

current_time = HAL_GetTick();

return;
Expand Down
9 changes: 9 additions & 0 deletions lib/PixelBMSLibrary/src/BMSData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <inttypes.h>

struct bms_common_data_t
{
uint32_t voltage;
uint32_t current;
int32_t power;
};
54 changes: 54 additions & 0 deletions lib/PixelBMSLibrary/src/BMSDeviceInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once
#include <inttypes.h>

class BMSManager;

class BMSDeviceInterface
{
public:

BMSDeviceInterface() : _initiated(false), _manager(nullptr), _idx(0) {}

virtual void Init() = 0;
virtual void Tick(uint32_t &time) = 0;
virtual int8_t DataRx(const uint8_t *raw, const uint8_t length) = 0;

bool IsInitiated()
{
return _initiated;
}

void PrepareInit(BMSManager *obj, uint8_t idx)
{
_initiated = true;
_manager = obj;
_idx = idx;
}

protected:

void _ReverseArray(uint8_t *array, uint8_t length)
{
uint8_t i = 0;
uint8_t j = length - 1;
uint8_t temp;
while(i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;

i++;
j--;
}

return;
}

bool _initiated;
BMSManager *_manager;
uint8_t _idx;

private:

};
51 changes: 51 additions & 0 deletions lib/PixelBMSLibrary/src/BMSManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <BMSManager.h>

void BMSManager::SetModel(uint8_t idx, BMSDeviceInterface &device)
{
if(idx >= _max_dev) return;
if(device.IsInitiated() == true) return;

_device[idx] = &device;
device.PrepareInit(this, idx);
device.Init();

return;
}

void BMSManager::Tick(uint32_t &time)
{
for(uint8_t idx = 0; idx < _max_dev; ++idx)
{
if(_error[idx].is_error == true)
{
_error[idx].is_error = false;

_callback_error(idx, _error[idx].code);
}

_device[idx]->Tick(time);
}

return;
}

void BMSManager::DataRx(uint8_t idx, const uint8_t *data, const uint8_t length)
{
if(idx >= _max_dev) return;
if(_device[idx] == nullptr) return;

_error[idx].code = _device[idx]->DataRx(data, length);
if(_error[idx].code != 0)
{
_error[idx].is_error = true;
}

return;
}

void BMSManager::DataTx(uint8_t idx, const uint8_t *data, const uint8_t length)
{
_callback_tx(idx, data, length);

return;
}
Loading

0 comments on commit d46c64b

Please sign in to comment.