Skip to content

Commit

Permalink
Quadruna CAN Charging (#1252)
Browse files Browse the repository at this point in the history
### Changelist 
<!-- Give a list of the changes covered in this PR. This will help both
you and the reviewer keep this PR within scope. -->

- Added dedicated BRUSA "board" for sending and receiving can msgs from
the charger.
- Added logic to have BMS control starting and stopping charging over
can.
- Added default values for charging currents, voltages and mains
currents
- Added translation functions from big to little endian (and vice versa)
and a function that uses it to convert floats for can msgs so that we
can set values directly in the code without having to do a manual
conversion to have the charger interpret the value we want.
- Added threshold of cycles we run after manual charging stop signal is
received and telling the charger to stop charging to stop the BMS from
immediately exiting charge state like we had seen testing with thruna
- Changed all (I think) io functions to do with charger to can
functions.
-Initialized all hbs from other boards to false on startup and all missing hb
faults to true - they will be cleared once other boards send a hb.


### Testing Done
<!-- Outline the testing that was done to demonstrate the changes are
solid. This could be unit tests, integration tests, testing on the car,
etc. Include relevant code snippets, screenshots, etc as needed. -->
Tested charging with 120 and 240V at low and higher currents as well as tested CAN signals being sent and received from the BRUSA charger. Also added a unit test to ensure missing hb would not let the bms go into drive state when not connected to the other boards.

### Resolved Tickets
<!-- Link any tickets that this PR resolves. -->

---------

Co-authored-by: will-chaba <[email protected]>
  • Loading branch information
KelmLelm and will-chaba authored May 26, 2024
1 parent 2b42a87 commit 6de086e
Show file tree
Hide file tree
Showing 32 changed files with 363 additions and 214 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
"name": "Debug Tests: RSM (Quadruna)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build_fw_test/firmware/quadruna/RSM/RSM_test",
"program": "${workspaceRoot}/build_fw_test/firmware/quadruna/RSM/quadruna_RSM_test",
"cwd": "${workspaceRoot}",
"preLaunchTask": "Build Tests: RSM (Quadruna)",
"linux": {
Expand All @@ -267,7 +267,7 @@
"name": "Debug Tests: CRIT (Quadruna)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build_fw_test/firmware/quadruna/CRIT/CRIT_test",
"program": "${workspaceRoot}/build_fw_test/firmware/quadruna/CRIT/quadruna_CRIT_test",
"cwd": "${workspaceRoot}",
"preLaunchTask": "Build Tests: CRIT (Quadruna)",
"linux": {
Expand Down
1 change: 1 addition & 0 deletions can_bus/quadruna/BMS/BMS_rx.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"messages": [
"RSM_Vitals",
"VC_Vitals",
"BRUSA_Vitals",
"Debug_Charging",
"Debug_CanMode",
"Debug_CellBalancing",
Expand Down
43 changes: 38 additions & 5 deletions can_bus/quadruna/BMS/BMS_tx.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,46 @@
}
}
},
"Charger": {
"msg_id": 110,
"cycle_time": 1000,
"description": "Flags relaying BMS Charger Status",
"Charger":{
"msg_id": 107,
"cycle_time": 100,
"description": "Charger infomation for the BMS to broadcast to other boards",
"signals":{
"ChargerConnected":{
"bits": 1
}
}
},
"BrusaControls": {
"msg_id": 1560,
"cycle_time": 100,
"description": "Flags and parameters for controlling the BRUSA charger",
"signals": {
"ChargerConnected": {
"ChargerEnable": {
"start_bit": 7,
"bits": 1
},
"ClearLatch": {
"start_bit": 6,
"bits": 1
},
"MaxMainsCurrent": {
"start_bit": 8,
"min": 0,
"max": 6000,
"resolution": 0.1
},
"ChargingVoltage": {
"start_bit": 24,
"min": 0,
"max": 6000,
"resolution": 0.1
},
"ChargingCurrent": {
"start_bit": 40,
"min": 0,
"max": 6000,
"resolution": 0.1
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions can_bus/quadruna/BRUSA/BRUSA_alerts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
3 changes: 3 additions & 0 deletions can_bus/quadruna/BRUSA/BRUSA_enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
5 changes: 5 additions & 0 deletions can_bus/quadruna/BRUSA/BRUSA_rx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"messages": [
"BMS_BrusaControls"
]
}
21 changes: 21 additions & 0 deletions can_bus/quadruna/BRUSA/BRUSA_tx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Vitals": {
"msg_id": 1552,
"cycle_time": 100,
"description": "Flags relaying BMS Charger Faults",
"signals": {
"IsConnected": {
"start_bit": 7,
"bits": 1
},
"Error": {
"start_bit": 6,
"bits": 1
},
"PowerLimitedWarning": {
"start_bit": 5,
"bits": 1
}
}
}
}
24 changes: 24 additions & 0 deletions can_bus/quadruna/Debug/Debug_tx.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@
"signals": {
"StartCharging": {
"bits": 1
},
"ClearChargerLatchedFault":{
"bits": 1
},
"FaultEncounteredOverride": {
"bits": 1
},
"ChargingCurrentOverride": {
"bits": 1
},
"ChargingVoltageOverride": {
"bits": 1
},
"ChargingCurrentTargetValue": {
"resolution": 0.1,
"min": 0,
"max": 12.5,
"unit": "A"
},
"ChargingVoltageTargetValue": {
"resolution": 0.1,
"min": 0,
"max": 350.0,
"unit": "V"
}
}
},
Expand Down
1 change: 0 additions & 1 deletion firmware/quadruna/BMS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ elseif("${TARGET}" STREQUAL "test")
"${CMAKE_CURRENT_SOURCE_DIR}/src/io/ltc6813/io_ltc6813CellVoltages.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/io/ltc6813/io_ltc6813Shared.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/io/io_airs.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/io/io_charger.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/io/io_faultLatch.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/io/io_imd.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/io/io_thermistors.h"
Expand Down
4 changes: 3 additions & 1 deletion firmware/quadruna/BMS/src/app/app_globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ void app_globals_init(const GlobalsConfig *config)

app_timer_init(&globals->precharge_lower_bound_timer, PRECHARGE_COMPLETION_LOWER_BOUND);
app_timer_init(&globals->precharge_upper_bound_timer, PRECHARGE_COMPLETION_UPPER_BOUND);

globals->charger_connected_counter = 0;
globals->ignore_charger_fault_counter = 0;
globals->charger_exit_counter = 0;
globals->precharge_limit_exceeded = false;
globals->fault_encountered = false;
globals->broadcast_charger_connected = true;
}
4 changes: 3 additions & 1 deletion firmware/quadruna/BMS/src/app/app_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <stdint.h>
#include "io_faultLatch.h"
#include "io_charger.h"
#include "io_thermistors.h"
#include "app_heartbeatMonitor.h"
#include "app_timer.h"
Expand All @@ -23,10 +22,13 @@ typedef struct
uint32_t cell_monitor_settle_count;
uint32_t num_precharge_failures;
bool precharge_limit_exceeded;
bool fault_encountered;
bool broadcast_charger_connected;
TimerChannel precharge_lower_bound_timer;
TimerChannel precharge_upper_bound_timer;
uint32_t ignore_charger_fault_counter;
uint32_t charger_exit_counter;
uint32_t charger_connected_counter;
} Globals;

extern Globals *const globals;
Expand Down
55 changes: 28 additions & 27 deletions firmware/quadruna/BMS/src/app/app_soc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ typedef struct
static SocStats stats;
extern bool sd_inited;

#ifndef TARGET_EMBEDDED

// ONLY FOR USE IN OFF-TARGET TESTING
void app_soc_setPrevCurrent(float current)
{
stats.prev_current_A = current;
}

#endif
#ifdef TARGET_EMBEDDED

static void convert_float_to_bytes(uint8_t *byte_array, float float_to_convert)
{
Expand All @@ -75,9 +67,18 @@ static float convert_bytes_to_float(uint8_t *byte_array)
return converted_float;
}

static bool sdCardReady()
#else

// ONLY FOR USE IN OFF-TARGET TESTING
void app_soc_setPrevCurrent(float current)
{
stats.prev_current_A = current;
}

#endif

static bool sdCardReady(void)
{
// return sd_inited && io_sdGpio_checkSdPresent();
return io_sdGpio_checkSdPresent();
}

Expand Down Expand Up @@ -120,14 +121,13 @@ void app_soc_init(void)
{
stats.prev_current_A = 0.0f;

// Asoc assumed corrupt until proven otherwise
// SOC assumed corrupt until proven otherwise
stats.is_corrupt = true;
stats.charge_c = -1;

// A negative soc value will indicate to app_soc_Create that saved SOC value is corrupted
float saved_soc_c = -1.0f;

#ifdef TARGET_EMBEDDED
// A negative SOC value will indicate to app_soc_Create that saved SOC value is corrupted
float saved_soc_c = -1.0f;

if (app_soc_readSocFromSd(&saved_soc_c))
{
Expand Down Expand Up @@ -206,22 +206,23 @@ void app_soc_resetSocCustomValue(float soc_percent)

bool app_soc_readSocFromSd(float *saved_soc_c)
{
uint8_t sd_read_data[SD_SECTOR_SIZE];
uint8_t sd_read_soc_bytes[NUM_SOC_BYTES];
uint8_t sd_read_crc_bytes[NUM_SOC_CRC_BYTES];

uint32_t sd_read_soc;
uint32_t sd_read_crc;
float soc;
uint32_t calculated_crc;
*saved_soc_c = -1.0f;

#ifdef TARGET_EMBEDDED
uint32_t sd_read_crc = 0;
float soc = 0.0f;
uint32_t calculated_crc = 0;
*saved_soc_c = -1.0f;

if (!sdCardReady())
{
return false;
}

#ifdef TARGET_EMBEDDED
uint8_t sd_read_crc_bytes[NUM_SOC_CRC_BYTES];
uint8_t sd_read_soc_bytes[NUM_SOC_BYTES];
uint8_t sd_read_data[SD_SECTOR_SIZE];

uint32_t sd_read_soc;

if (hw_sd_read(sd_read_data, DEFAULT_SOC_ADDR, 1) == SD_CARD_OK)
{
memcpy(sd_read_soc_bytes, sd_read_data, sizeof(uint32_t));
Expand Down Expand Up @@ -263,12 +264,12 @@ bool app_soc_readSocFromSd(float *saved_soc_c)

bool app_soc_writeSocToSd(float soc)
{
#ifdef TARGET_EMBEDDED
if (!sdCardReady())
{
return false;
}

#ifdef TARGET_EMBEDDED
uint8_t sd_write_data[SD_SECTOR_SIZE];
convert_float_to_bytes(sd_write_data, soc);
uint32_t soc_value = ARRAY_TO_UINT32(sd_write_data);
Expand Down
10 changes: 6 additions & 4 deletions firmware/quadruna/BMS/src/app/app_tractiveSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "app_canTx.h"
#include "io_tractiveSystem.h"

#define W_TO_KW 1.0e-3f

float app_tractiveSystem_getVoltage()
{
return io_tractiveSystem_getVoltage();
Expand All @@ -25,13 +27,13 @@ float app_tractiveSystem_getCurrent(void)

void app_tractiveSystem_broadcast()
{
const float ts_voltage = app_tractiveSystem_getVoltage();
const float ts_current = app_tractiveSystem_getCurrent();
const float ts_power = ts_voltage * ts_current;
const float ts_voltage = app_tractiveSystem_getVoltage();
const float ts_current = app_tractiveSystem_getCurrent();
const float ts_power_kw = ts_voltage * ts_current * W_TO_KW;

app_canTx_BMS_TractiveSystemVoltage_set(ts_voltage);
app_canTx_BMS_TractiveSystemCurrent_set(ts_current);
app_canTx_BMS_TractiveSystemPower_set(ts_power);
app_canTx_BMS_TractiveSystemPower_set(ts_power_kw);
}

bool app_tractveSystem_checkFaults()
Expand Down
13 changes: 10 additions & 3 deletions firmware/quadruna/BMS/src/app/states/app_allStates.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ uint32_t iso_spi_state_counter = 0;

void app_allStates_runOnTick1Hz(void)
{
bool charger_is_connected = io_charger_isConnected();
app_canTx_BMS_ChargerConnected_set(charger_is_connected);
// If charge state has not placed a lock on broadcasting
// if the charger is charger is connected
if (globals->broadcast_charger_connected)
{
// Broadcast the can msg from the BRUSA charger to the entire car
bool charger_is_connected = app_canRx_BRUSA_IsConnected_get();
app_canTx_BMS_ChargerConnected_set(charger_is_connected);
}

const float min_soc = app_soc_getMinSocCoulombs();

Expand Down Expand Up @@ -155,7 +161,8 @@ bool app_allStates_runOnTick100Hz(void)
}
else if (acc_fault || ts_fault)
{
status = false;
status = false;
globals->fault_encountered = true;
app_stateMachine_setNextState(app_faultState_get());
}

Expand Down
Loading

0 comments on commit 6de086e

Please sign in to comment.