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

Bms relay fsm (4/4 fault checks done) #261

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
8477565
Created basic structure for relays_fsm and validated
Jan 23, 2024
e32e669
fixed after merging new current_sense
Jan 30, 2024
29ac315
Added open/close relay functions
Jan 31, 2024
ad74298
added current sense fault handler with relays_fsm
Feb 4, 2024
c215df4
reorganized bms_fault checks
Feb 4, 2024
1fe4392
formatting + lint
Feb 4, 2024
18cf623
rebase + formatting
Feb 4, 2024
004260e
added afe reading + fault check
Feb 5, 2024
b297a68
formatting and error fix
Feb 5, 2024
1ed4f61
reorganized cycle running and added all checks
Feb 5, 2024
deb770c
moved all of afe into relays fsm
Feb 5, 2024
04e4ece
Made revisions
Feb 8, 2024
6d61284
making regen braking rely on BMS
Feb 9, 2024
cd95481
started bms smoke
Bafran Feb 10, 2024
fe9c3ec
changd regen braking to be dependent on batt voltage
Feb 12, 2024
d905e4c
merged with new BMS changes
Feb 12, 2024
8824115
Formatting
Feb 12, 2024
fe8469c
added delay reminder for relays switching on/off
Feb 12, 2024
4abb719
altered calculation to work with motor controller easily
Feb 13, 2024
de8a93b
fan smoke test
Feb 15, 2024
9652796
validation changes (not done validating)
Feb 16, 2024
b391ade
afe daisy chaining fix
Feb 29, 2024
948f775
updated afe daisy chaining. MAIN ERROR: Timing delays of cell_conv ca…
Feb 29, 2024
f79ab68
Add retry mechanism to ltc cell conversion
Bafran Mar 2, 2024
730591d
added bms fan module
Akashem06 Mar 4, 2024
2710b9e
fan module
Akashem06 Mar 6, 2024
957ba69
added+validated fan module and new current_sense module. Next step: a…
Akashem06 Mar 9, 2024
d3190de
small changes: add fault check and verify fan sense
Akashem06 Mar 9, 2024
55b588d
fan testing + added fault msgs
Akashem06 Mar 10, 2024
24b2e06
bruh
Akashem06 Mar 10, 2024
f4f3a00
fault messages
Akashem06 Mar 10, 2024
3ec8abc
added most fault checks following electrical standards
Mar 12, 2024
55a623e
small changes in syntax
Akashem06 Mar 12, 2024
c217405
cell sense module
Akashem06 Mar 14, 2024
469efc4
fault_bps file complete
Akashem06 Mar 14, 2024
5be4e05
afe/currentsense comms loss fault added
Akashem06 Mar 14, 2024
83f386b
Removed fault_check function frmo ltc_afe_impl since not used anymore
Akashem06 Mar 14, 2024
2fa2711
aux_sense added
Akashem06 Mar 15, 2024
1ea2527
validation changes
Akashem06 Mar 15, 2024
4782044
major validation changes
Akashem06 Mar 19, 2024
c9521b5
validation changes
Akashem06 Mar 20, 2024
8e89c94
small change
Akashem06 Mar 20, 2024
8598c1c
test push
Akashem06 Mar 20, 2024
32e1f25
library changes
Akashem06 Mar 21, 2024
edeb57e
testing
Akashem06 Mar 21, 2024
966b08c
testing
Akashem06 Mar 21, 2024
36e248d
updated dashboard so neutral off when car off, but on when car on
Akashem06 Mar 22, 2024
6d20afa
build fix
Akashem06 Mar 22, 2024
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
48 changes: 8 additions & 40 deletions libraries/codegen/boards/bms_carrier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
watchdog: 3
critical: true
signals:
batt_perc:
fault:
length: 16
status:
fan1:
length: 8
fault:
fan2:
length: 8
aux_batt_v:
length: 16
max_cell_v:
length: 16

battery_vt:
id: 30
target:
centre_console:
watchdog: 0
telemetry:
watchdog: 0
critical: false
signals:
voltage:
Expand All @@ -43,41 +45,7 @@
length: 16
batt_perc:
length: 16

battery_aggregate_vc:
id: 33
target:
telemetry:
watchdog: 0
signals:
voltage:
length: 32
current:
length: 32

battery_fan_state:
id: 57
target:
bms_carrier:
watchdog: 0
signals:
fan_1:
length: 8
fan_2:
length: 8
fan_3:
length: 8
fan_4:
length: 8
fan_5:
length: 8
fan_6:
length: 8
fan_7:
length: 8
fan_8:
length: 8


battery_relay_info:
id: 58
target:
Expand Down
1 change: 1 addition & 0 deletions libraries/ms-common/inc/arm/pwm_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ typedef enum {
PWM_TIMER_1 = 0,
// PWM_TIMER_2, // Requisitioned to back the soft_timer module.
PWM_TIMER_3,
PWM_TIMER_4, // Usually used by i2c
PWM_TIMER_14,
PWM_TIMER_15,
PWM_TIMER_16,
Expand Down
1 change: 1 addition & 0 deletions libraries/ms-common/inc/exported_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
typedef enum {
EE_RELAY_STATE_OPEN = 0,
EE_RELAY_STATE_CLOSE,
EE_RELAY_STATE_FAULT,
NUM_EE_RELAY_STATES,
} EERelayState;

Expand Down
1 change: 1 addition & 0 deletions libraries/ms-common/inc/x86/pwm_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef enum {
PWM_TIMER_1 = 0,
// PWM_TIMER_2, // Requisitioned to back the soft_timer module.
PWM_TIMER_3,
PWM_TIMER_4, // Usually used by i2c
PWM_TIMER_14,
PWM_TIMER_15,
PWM_TIMER_16,
Expand Down
5 changes: 5 additions & 0 deletions libraries/ms-common/src/arm/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef enum APBClk {
static uint16_t s_period_us[NUM_PWM_TIMERS] = {
[PWM_TIMER_1] = 0, //
[PWM_TIMER_3] = 0, //
[PWM_TIMER_4] = 0, // This timer is typically used by I2C
[PWM_TIMER_14] = 0, //
[PWM_TIMER_15] = 0, //
[PWM_TIMER_16] = 0, //
Expand All @@ -25,6 +26,7 @@ static uint16_t s_period_us[NUM_PWM_TIMERS] = {
static TIM_TypeDef *s_timer_def[NUM_PWM_TIMERS] = {
[PWM_TIMER_1] = TIM1, //
[PWM_TIMER_3] = TIM3, //
[PWM_TIMER_4] = TIM4, // Typically used for I2C
[PWM_TIMER_14] = TIM14, //
[PWM_TIMER_15] = TIM15, //
[PWM_TIMER_16] = TIM16, //
Expand All @@ -39,6 +41,9 @@ static APBClk prv_enable_periph_clock(PwmTimer timer) {
case PWM_TIMER_3:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
return APB1;
case PWM_TIMER_4:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
return APB1;
case PWM_TIMER_14:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
return APB1;
Expand Down
27 changes: 17 additions & 10 deletions libraries/ms-drivers/inc/ltc6811.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ static_assert(sizeof(LtcAfeConfigRegisterData) == 6, "LtcAfeConfigRegisterData m

// COMM Register, refer to LTC6803 datasheet page 31, Table 15
typedef struct {
uint16_t icom0 : 4;
uint16_t d0 : 8;
uint16_t fcom0 : 4;
uint8_t icom0 : 4;
uint8_t d0 : 8;
uint8_t fcom0 : 4;

uint16_t icom1 : 4;
uint16_t d1 : 8;
uint16_t fcom1 : 4;
uint8_t icom1 : 4;
uint8_t d1 : 8;
uint8_t fcom1 : 4;

uint16_t icom2 : 4;
uint16_t d2 : 8;
uint16_t fcom2 : 4;
uint8_t icom2 : 4;
uint8_t d2 : 8;
uint8_t fcom2 : 4;
} _PACKED LtcAfeCommRegisterData;
static_assert(sizeof(LtcAfeCommRegisterData) == 6, "LtcAfeCommRegisterData must be 6 bytes");

Expand Down Expand Up @@ -116,7 +116,7 @@ typedef struct {
LtcAfeWriteDeviceConfigPacket devices[LTC_AFE_MAX_CELLS_PER_DEVICE];
} _PACKED LtcAfeWriteConfigPacket;
#define SIZEOF_LTC_AFE_WRITE_CONFIG_PACKET(devices) \
(LTC6811_CMD_SIZE + (devices) * sizeof(LtcAfeWriteConfigPacket))
(LTC6811_CMD_SIZE + (devices) * sizeof(LtcAfeWriteDeviceConfigPacket))

typedef union {
uint16_t voltages[3];
Expand Down Expand Up @@ -187,6 +187,10 @@ static_assert(sizeof(LtcAfeAuxRegisterGroupPacket) == 8,

#define LTC6811_STCOMM_RESERVED (1 << 0) | (1 << 1) | (1 << 5) | (1 << 8) | (1 << 9) | (1 << 10)

#define LTC6811_WRPWM_RESERVED (1 << 5)

#define LTC6811_RDPWM_RESERVED (1 << 5) | (1 << 2)

// command bits
// see Table 40 (p. 62)
#define LTC6811_GPIO1_PD_ON (0 << 3)
Expand Down Expand Up @@ -224,3 +228,6 @@ static_assert(sizeof(LtcAfeAuxRegisterGroupPacket) == 8,

#define LTC6811_FCOM_CSBM_LOW (0 << 0)
#define LTC6811_FCOM_CSBM_HIGH (1 << 3) | (1 << 0)

// see Table 17 (p. 38)
#define LTC6811_PWMC_DC_100 (0xF)
2 changes: 1 addition & 1 deletion libraries/ms-drivers/inc/ltc_afe.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ typedef struct LtcAfeStorage {
uint16_t aux_index;
uint16_t retry_count;
uint16_t device_cell;
uint16_t time_elapsed;
uint8_t timer_start;

uint16_t cell_voltages[LTC_AFE_MAX_CELLS];
uint16_t aux_voltages[LTC_AFE_MAX_THERMISTORS];
Expand Down
6 changes: 6 additions & 0 deletions libraries/ms-drivers/inc/ltc_afe_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// represent whether we should monitor the cell input for the given device.
StatusCode ltc_afe_impl_init(LtcAfeStorage *afe, const LtcAfeSettings *settings);

// Write an LTC config based on the given storage settings
StatusCode ltc_afe_impl_write_config(LtcAfeStorage *afe);

// Triggers a conversion. Note that we need to wait for the conversions to complete before the
// readback will be valid.
StatusCode ltc_afe_impl_trigger_cell_conv(LtcAfeStorage *afe);
Expand All @@ -29,3 +32,6 @@ StatusCode ltc_afe_impl_read_aux(LtcAfeStorage *afe, uint8_t device_cell);
// Mark cell for discharging (takes effect after config is re-written)
// |cell| should be [0, LTC_AFE_MAX_CELLS)
StatusCode ltc_afe_impl_toggle_cell_discharge(LtcAfeStorage *afe, uint16_t cell, bool discharge);

// Sets the duty cycle to the same value for all cells on all afes
StatusCode ltc_afe_impl_set_discharge_pwm_cycle(LtcAfeStorage *afe, uint8_t duty_cycle);
4 changes: 2 additions & 2 deletions libraries/ms-drivers/inc/max17261_fuel_gauge.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct {
uint16_t charge_term_current; // LSB = 1.5625 (micro Volts / R Sense)

uint16_t i_thresh_max;
uint16_t i_thresh_min;
int16_t i_thresh_min;
uint16_t temp_thresh_max;

float r_sense_mohms; // Rsense in micro ohms
Expand Down Expand Up @@ -77,7 +77,7 @@ StatusCode max17261_time_to_full(Max17261Storage *storage, uint16_t *ttf_ms);
* @param soc_pct - current in amps returned in this var
* @return STATUS_CODE_OK on success
*/
StatusCode max17261_current(Max17261Storage *storage, uint16_t *current_a);
StatusCode max17261_current(Max17261Storage *storage, int16_t *current_a);

/* @brief Gets a single cell's voltage in mV
* @param storage - a pointer to an already initialized Max17261Storage struct
Expand Down
41 changes: 38 additions & 3 deletions libraries/ms-drivers/src/ltc_afe_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static uint16_t s_read_reg_cmd[NUM_LTC_AFE_REGISTERS] = {
[LTC_AFE_REGISTER_STATUS_A] = LTC6811_RDSTATA_RESERVED,
[LTC_AFE_REGISTER_STATUS_B] = LTC6811_RDSTATB_RESERVED,
[LTC_AFE_REGISTER_READ_COMM] = LTC6811_RDCOMM_RESERVED,
[LTC_AFE_REGISTER_START_COMM] = LTC6811_STCOMM_RESERVED
[LTC_AFE_REGISTER_START_COMM] = LTC6811_STCOMM_RESERVED,
};

static uint8_t s_voltage_reg[NUM_LTC_AFE_VOLTAGE_REGISTERS] = {
Expand All @@ -39,8 +39,8 @@ static void prv_wakeup_idle(LtcAfeStorage *afe) {
for (size_t i = 0; i < settings->num_devices; i++) {
gpio_set_state(&settings->cs, GPIO_STATE_LOW);
gpio_set_state(&settings->cs, GPIO_STATE_HIGH);
// Wait for 300us - greater than tWAKE, less than tIDLE
delay_ms(0.3);
// Wait for 1ms (should be 300us) - greater than tWAKE, less than tIDLE
delay_ms(1);
}
}

Expand Down Expand Up @@ -234,6 +234,7 @@ StatusCode ltc_afe_impl_init(LtcAfeStorage *afe, const LtcAfeSettings *settings)

prv_calc_offsets(afe);
crc15_init_table();
afe->timer_start = 0;

SpiSettings spi_config = {
.baudrate = settings->spi_baudrate, //
Expand All @@ -248,6 +249,9 @@ StatusCode ltc_afe_impl_init(LtcAfeStorage *afe, const LtcAfeSettings *settings)
// Use GPIO1 as analog input, GPIO 3-5 for SPI
uint8_t gpio_bits =
LTC6811_GPIO1_PD_OFF | LTC6811_GPIO3_PD_OFF | LTC6811_GPIO4_PD_OFF | LTC6811_GPIO5_PD_OFF;

status_ok_or_return(ltc_afe_impl_set_discharge_pwm_cycle(afe, LTC6811_PWMC_DC_100));

return prv_write_config(afe, gpio_bits);
}

Expand Down Expand Up @@ -289,6 +293,11 @@ StatusCode ltc_afe_impl_read_cells(LtcAfeStorage *afe) {
uint16_t data_pec = crc15_calculate((uint8_t *)&voltage_register[device], 6);
if (received_pec != data_pec) {
// return early on failure
LOG_DEBUG("RECEIVED_PEC: %d\n\r", received_pec);
LOG_DEBUG("DATA_PEC: %d\n\r", data_pec);
LOG_DEBUG("Voltage: %d %d %d\n\r", voltage_register[device].reg.voltages[0],
voltage_register[device].reg.voltages[1],
voltage_register[device].reg.voltages[2]);
return status_code(STATUS_CODE_INTERNAL_ERROR);
}
}
Expand Down Expand Up @@ -342,3 +351,29 @@ StatusCode ltc_afe_impl_toggle_cell_discharge(LtcAfeStorage *afe, uint16_t cell,

return STATUS_CODE_OK;
}

// Just a wrapper setting the correct gpio bits
StatusCode ltc_afe_impl_write_config(LtcAfeStorage *afe) {
uint8_t gpio_bits =
LTC6811_GPIO1_PD_OFF | LTC6811_GPIO3_PD_OFF | LTC6811_GPIO4_PD_OFF | LTC6811_GPIO5_PD_OFF;
return prv_write_config(afe, gpio_bits);
}

// Sets the duty cycle to the same value for all cells on all afes
StatusCode ltc_afe_impl_set_discharge_pwm_cycle(LtcAfeStorage *afe, uint8_t duty_cycle) {
LtcAfeSettings *settings = &afe->settings;

uint8_t cmd[4 + (6 * 3)] = { 0 };
prv_build_cmd(LTC6811_WRPWM_RESERVED, cmd, 4);

// For every device, set all 6 PWM bytes to the same config
for (uint8_t curr_device = 0; curr_device < settings->num_devices; curr_device++) {
for (int cell_pwm = 0; cell_pwm < 6; cell_pwm++) {
cmd[(curr_device * 6) + cell_pwm] = duty_cycle;
}
}

size_t len = 4 + (6 * settings->num_devices);
prv_wakeup_idle(afe);
return spi_exchange(settings->spi_port, cmd, len, NULL, 0);
}
17 changes: 14 additions & 3 deletions libraries/ms-drivers/src/max17261_fuel_gauge.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "max17261_fuel_gauge.h"

#include "log.h"

// See Table 3 on pg.18 of the datasheet
#define PCT_LSB (1.0f / 256) // LSBit is 1/256%
#define CAP_LSB (5.0f / storage->settings->r_sense_mohms) // LSBit is 5 micro Volt hrs / Rsense
Expand Down Expand Up @@ -67,10 +69,14 @@ StatusCode max17261_time_to_full(Max17261Storage *storage, uint16_t *ttf_ms) {
return STATUS_CODE_OK;
}

StatusCode max17261_current(Max17261Storage *storage, uint16_t *current_ua) {
StatusCode max17261_current(Max17261Storage *storage, int16_t *current_ua) {
uint16_t current_reg_val = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_CURRENT, &current_reg_val));
*current_ua = current_reg_val * CUR_LSB;
if (~current_reg_val & (1 << 15)) {
*current_ua = -(int16_t)((~current_reg_val) + 1);
} else {
*current_ua = current_reg_val * CUR_LSB;
}
return STATUS_CODE_OK;
}

Expand All @@ -84,7 +90,8 @@ StatusCode max17261_voltage(Max17261Storage *storage, uint16_t *vcell_mv) {
StatusCode max17261_temp(Max17261Storage *storage, uint16_t *temp_c) {
uint16_t temp_reg_val = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_TEMP, &temp_reg_val));
*temp_c = temp_reg_val * temp_reg_val;
LOG_DEBUG("%d register val of temp\n", temp_reg_val);
*temp_c = temp_reg_val * TEMP_LSB;
return STATUS_CODE_OK;
}

Expand All @@ -100,6 +107,10 @@ StatusCode max17261_init(Max17261Storage *storage, Max17261Settings *settings) {
uint16_t config = 0;
status_ok_or_return(max17261_get_reg(storage, MAX17261_CONFIG, &config));
config |= (1 << 2);
// Enable external temp readings
// config |= (1 << 15);
// config |= (1 << 8);
// config |= (1 << 4);
status_ok_or_return(max17261_set_reg(storage, MAX17261_CONFIG, config));
// Upper byte is IMAX and lower byte is IMIN
uint16_t current_th = (settings->i_thresh_max << 8) & (settings->i_thresh_min & 0x00FF);
Expand Down
2 changes: 1 addition & 1 deletion projects/bms_carrier/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"libs": [
"FreeRTOS",
"ms-common",
"master",
"master",
"ms-drivers"
],
"can": true
Expand Down
18 changes: 18 additions & 0 deletions projects/bms_carrier/inc/aux_sense.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "adc.h"
#include "bms_carrier_getters.h"
#include "bms_carrier_setters.h"
#include "gpio.h"
#include "log.h"

#define R2_OHMS 10000
#define R1_OHMS 47000

typedef struct AuxStorage {
uint16_t aux_battery_voltage;
} AuxStorage;

StatusCode aux_sense_init(AuxStorage *aux_storage);

StatusCode aux_sense_run(void);
Loading
Loading