Skip to content

Commit

Permalink
Bms relay fsm working (#271)
Browse files Browse the repository at this point in the history
* Created basic structure for relays_fsm and validated

* fixed after merging new current_sense

* Added open/close relay functions

* added current sense fault handler with relays_fsm

* reorganized bms_fault checks

* formatting + lint

* rebase + formatting

* added afe reading + fault check

* formatting and error fix

* reorganized cycle running and added all checks

* moved all of afe into relays fsm

* Made revisions

* making regen braking rely on BMS

* started bms smoke

* changd regen braking to be dependent on batt voltage

* Formatting

* added delay reminder for relays switching on/off

* altered calculation to work with motor controller easily

* fan smoke test

* validation changes (not done validating)

* afe daisy chaining fix

* updated afe daisy chaining. MAIN ERROR: Timing delays of cell_conv cause master task to fall out of sync + return spi status timeout

* Add retry mechanism to ltc cell conversion

* added bms fan module

* fan module

* added+validated fan module and new current_sense module. Next step: add in fault handling

* small changes: add fault check and verify fan sense

* fan testing + added fault msgs

* bruh

* fault messages

* added most fault checks following electrical standards

* small changes in syntax

* cell sense module

* fault_bps file complete

* afe/currentsense comms loss fault added

* Removed fault_check function frmo ltc_afe_impl since not used anymore

* aux_sense added

* validation changes

* major validation changes

* AFEs and I think Current Sense Working

* Commented out Current Sense Faults

* centre_console chnages + formatting

* validation changes

* relay delay validated

* build fix workflows annoying

* soft timer for relays works

* current sense added + working with kill switch

* fan.c smoke updated

* pd validation with mci precharge

* jarvis fixes

* jarvis fixes 2

---------

Co-authored-by: vagrant <vagrant@midsunbox>
Co-authored-by: Bafran <[email protected]>
Co-authored-by: Akashem <akashem06>
  • Loading branch information
3 people authored Apr 6, 2024
1 parent 0346df0 commit 2704dab
Show file tree
Hide file tree
Showing 36 changed files with 940 additions and 266 deletions.
57 changes: 16 additions & 41 deletions libraries/codegen/boards/bms_carrier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,29 @@
watchdog: 3
critical: true
signals:
batt_perc:
fault:
length: 16
status:
aux_batt_v:
length: 32

battery_info:
id: 33
target:
centre_console:
watchdog: 0
signals:
fan1:
length: 8
fault:
fan2:
length: 8

max_cell_v:
length: 32

battery_vt:
id: 30
target:
centre_console:
watchdog: 0
telemetry:
watchdog: 0
critical: false
signals:
voltage:
Expand All @@ -43,41 +52,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
3 changes: 2 additions & 1 deletion libraries/ms-common/inc/exported_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
typedef enum {
EE_RELAY_STATE_OPEN = 0,
EE_RELAY_STATE_CLOSE,
EE_RELAY_STATE_FAULT,
NUM_EE_RELAY_STATES,
} EERelayState;

// STEERING SIGNALS
// Signals for analog inputs received at the steering board
typedef enum SteeringInfoAnalog {
EE_STEERING_LIGHTS_OFF_STATE = 0, // Turn lights off
EE_STEERING_LIGHTS_RIGHT_STATE, // Right Turn Signal
EE_STEERING_LIGHTS_LEFT_STATE, // Left Turn Signal
EE_STEERING_LIGHTS_RIGHT_STATE, // Right Turn Signal
NUM_EE_STEERING_LIGHTS,
} SteeringInfoAnalog;

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
4 changes: 1 addition & 3 deletions libraries/ms-drivers/inc/ltc_afe.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ typedef struct LtcAfeSettings {
} LtcAfeSettings;

typedef struct LtcAfeStorage {
Fsm fsm;

// Only used for storage in the FSM so we store data for the correct cells
uint16_t aux_index;
uint16_t retry_count;
uint16_t device_cell;
uint16_t time_elapsed;
uint32_t timer_start;

uint16_t cell_voltages[LTC_AFE_MAX_CELLS];
uint16_t aux_voltages[LTC_AFE_MAX_THERMISTORS];
Expand Down
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
8 changes: 4 additions & 4 deletions libraries/ms-drivers/src/ltc_afe_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ static StatusCode prv_read_register(LtcAfeStorage *afe, LtcAfeRegister reg, uint
}

// read from a voltage register
static StatusCode prv_read_voltage(LtcAfeStorage *afe, LtcAfeVoltageRegister reg,
LtcAfeVoltageRegisterGroup *data) {
static inline StatusCode prv_read_voltage(LtcAfeStorage *afe, LtcAfeVoltageRegister reg,
LtcAfeVoltageRegisterGroup *data) {
if (reg > NUM_LTC_AFE_VOLTAGE_REGISTERS) {
return status_code(STATUS_CODE_INVALID_ARGS);
}
Expand All @@ -86,7 +86,7 @@ static StatusCode prv_read_voltage(LtcAfeStorage *afe, LtcAfeVoltageRegister reg
}

// start cell voltage conversion
static StatusCode prv_trigger_adc_conversion(LtcAfeStorage *afe) {
static inline StatusCode prv_trigger_adc_conversion(LtcAfeStorage *afe) {
LtcAfeSettings *settings = &afe->settings;
uint8_t mode = (uint8_t)((settings->adc_mode + 1) % 3);
// ADCV command
Expand Down Expand Up @@ -264,7 +264,7 @@ StatusCode ltc_afe_impl_trigger_aux_conv(LtcAfeStorage *afe, uint8_t device_cell
return prv_trigger_aux_adc_conversion(afe);
}

StatusCode ltc_afe_impl_read_cells(LtcAfeStorage *afe) {
inline StatusCode ltc_afe_impl_read_cells(LtcAfeStorage *afe) {
// Read all voltage A, then B, ...
LtcAfeSettings *settings = &afe->settings;
for (uint8_t cell_reg = 0; cell_reg < NUM_LTC_AFE_VOLTAGE_REGISTERS; ++cell_reg) {
Expand Down
12 changes: 9 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,10 @@ 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;
*current_ua = (int16_t)(current_reg_val)*CUR_LSB;
return STATUS_CODE_OK;
}

Expand All @@ -84,7 +86,7 @@ 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;
*temp_c = temp_reg_val * TEMP_LSB;
return STATUS_CODE_OK;
}

Expand All @@ -100,6 +102,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
17 changes: 17 additions & 0 deletions projects/bms_carrier/inc/aux_sense.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "adc.h"
#include "bms_carrier_getters.h"
#include "bms_carrier_setters.h"
#include "gpio.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);
48 changes: 29 additions & 19 deletions projects/bms_carrier/inc/bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,44 @@

#include <stdint.h>

#include "cell_sense.h"
#include "current_sense.h"
#include "aux_sense.h"
#include "fault_bps.h"
#include "i2c.h"
#include "ltc_afe.h"
#include "max17261_fuel_gauge.h"
#include "status.h"

#define BMS_PERIPH_I2C_PORT I2C_PORT_2
#define BMS_PERIPH_I2C_SDA_PIN \
{ GPIO_PORT_B, 11 }
{ .port = GPIO_PORT_B, .pin = 11 }
#define BMS_PERIPH_I2C_SCL_PIN \
{ GPIO_PORT_B, 10 }
#define BMS_FAN_ALERT_PIN \
{ GPIO_PORT_A, 9 }
{ .port = GPIO_PORT_B, .pin = 10 }

#define BMS_IO_EXPANDER_I2C_ADDR 0x40
typedef struct CurrentStorage {
uint16_t soc;
int16_t current;
uint16_t voltage;
uint16_t temperature;
uint32_t fuel_guage_cycle_ms; // Time in ms between conversions (soft timer kicks)
} CurrentStorage;

#define BMS_FAN_CTRL_1_I2C_ADDR 0x5E
#define BMS_FAN_CTRL_2_I2C_ADDR 0x5F
#define NUM_BMS_FAN_CTRLS 2

// Not dealing with debouncer here
typedef struct BmsStorage {
// RelayStorage relay_storage;
AuxStorage aux_storage;
CurrentStorage current_storage;
AfeReadings afe_readings;
LtcAfeStorage ltc_afe_storage;
CellSenseStorage cell_storage;
// FanStorage fan_storage_1;
// FanStorage fan_storage_2;
// DebouncerStorage killswitch_storage;
// BpsStorage bps_storage;
BpsStorage bps_storage;
Max17261Settings fuel_guage_settings;
Max17261Storage fuel_guage_storage;
} BmsStorage;

typedef enum {
BMS_FAULT_OVERVOLTAGE,
BMS_FAULT_UNBALANCE,
BMS_FAULT_OVERTEMP_AMBIENT,
BMS_FAULT_COMMS_LOSS_AFE,
BMS_FAULT_COMMS_LOSS_CURR_SENSE,
BMS_FAULT_OVERTEMP_CELL,
BMS_FAULT_OVERCURRENT,
BMS_FAULT_UNDERVOLTAGE,
BMS_FAULT_KILLSWITCH
} BmsFault;
Loading

0 comments on commit 2704dab

Please sign in to comment.