Skip to content

Commit

Permalink
bps led fault changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Akashem06 committed Apr 23, 2024
1 parent 812c8b6 commit 0fd7652
Show file tree
Hide file tree
Showing 25 changed files with 253 additions and 130 deletions.
8 changes: 0 additions & 8 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@ if [ "$GIT_AUTHOR_NAME" = "vagrant" ] || [ "$GIT_AUTHOR_EMAIL" = "vagrant@midsun
echo "https://uwmidsun.atlassian.net/wiki/spaces/ELEC/pages/60260353/Module+1+Setup"
exit 1
fi

echo "Automatically formatting and linting"
scons format lint && git add .

if [ $? -ne 0 ]; then
echo "Code must be properly formatted before commiting!"
exit 1
fi
2 changes: 2 additions & 0 deletions libraries/codegen/boards/motor_controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
target:
centre_console:
watchdog: 0
power_distribution:
watchdog: 0
signals:
velocity_l:
length: 16
Expand Down
2 changes: 2 additions & 0 deletions libraries/codegen/boards/power_distribution.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@
length: 8
fault_bitset:
length: 8
bps_persist:
length: 8
8 changes: 4 additions & 4 deletions libraries/ms-drivers/src/seg_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ StatusCode seg_displays_init(SegDisplay *display) {
gpio_init_pin(&display->A2, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->B2, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->C2, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->D2, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_HIGH);
// gpio_init_pin(&display->D2, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_HIGH);
gpio_init_pin(&display->A3, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->B3, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->C3, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
Expand All @@ -38,7 +38,7 @@ void seg_displays_reset(SegDisplay *display) {
gpio_set_state(&display->A2, GPIO_STATE_LOW);
gpio_set_state(&display->B2, GPIO_STATE_LOW);
gpio_set_state(&display->C2, GPIO_STATE_LOW);
gpio_set_state(&display->D2, GPIO_STATE_LOW);
// gpio_set_state(&display->D2, GPIO_STATE_LOW);
gpio_set_state(&display->A3, GPIO_STATE_LOW);
gpio_set_state(&display->B3, GPIO_STATE_LOW);
gpio_set_state(&display->C3, GPIO_STATE_LOW);
Expand All @@ -65,8 +65,8 @@ static void prv_set_seg_displays(SegDisplay *display, uint16_t val1, uint16_t va
state = ((val1 & 1) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
gpio_set_state(&display->A1, state);

state = ((val2 & 8) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
gpio_set_state(&display->D2, state);
// state = ((val2 & 8) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
// gpio_set_state(&display->D2, state);

state = ((val2 & 4) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
gpio_set_state(&display->C2, state);
Expand Down
2 changes: 1 addition & 1 deletion libraries/ms-freertos/inc/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,4 @@ extern uint32_t SystemCoreClock;
// Section where parameter definitions can be added (for instance, to override
// default ones in FreeRTOS.h)

#define PortCPUClockFreqHz 48000000
#define PortCPUClockFreqHz configCPU_CLOCK_HZ
24 changes: 19 additions & 5 deletions projects/bms_carrier/inc/current_sense.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,26 @@
#define MAX17261_I2C_PORT (I2C_PORT_2)
#define MAX17261_I2C_ADDR (0x36)

// TODO (Adel C): Change these values to their actual values
#define CURRENT_SENSE_R_SENSE_MILLI_OHMS (0.5)
#define CURRENT_SENSE_R_SENSE_MOHMS (0.5)
#define PACK_CAPACITY_MAH 3000

// Capacity in units of 5.0uVh/Rsense
#define MAIN_PACK_DESIGN_CAPACITY \
(1.0f / CURRENT_SENSE_R_SENSE_MILLI_OHMS) // LSB = 5.0 (micro Volt Hours / R Sense)
#define MAIN_PACK_EMPTY_VOLTAGE (1.0f / 78.125) // Only a 9-bit field, LSB = 78.125 (micro Volts)
#define CHARGE_TERMINATION_CURRENT (1.0f / (1.5625f / CURRENT_SENSE_R_SENSE_MILLI_OHMS))
(PACK_CAPACITY_MAH * CURRENT_SENSE_R_SENSE_MOHMS / 5)

// LSB = 78.125 (micro Volts)
#define MAIN_PACK_EMPTY_VOLTAGE_MV 2500
#define MAIN_PACK_EMPTY_VOLTAGE (MAIN_PACK_EMPTY_VOLTAGE_MV / 78.125)


#define CHARGE_TERMINATION_CURRENT 0

// // TODO (Adel C): Change these values to their actual values
// #define CURRENT_SENSE_R_SENSE_MILLI_OHMS (0.5)
// #define MAIN_PACK_DESIGN_CAPACITY
// (1.0f / CURRENT_SENSE_R_SENSE_MILLI_OHMS) // LSB = 5.0 (micro Volt Hours / R Sense)
// #define MAIN_PACK_EMPTY_VOLTAGE (1.0f / 78.125) // Only a 9-bit field, LSB = 78.125 (micro Volts)
// #define CHARGE_TERMINATION_CURRENT (1.0f / (1.5625f / CURRENT_SENSE_R_SENSE_MILLI_OHMS))

// Thresholds for ALRT Pin
#define CURRENT_SENSE_MAX_CURRENT_A (58.2f) // 58.2 Amps
Expand Down
2 changes: 1 addition & 1 deletion projects/bms_carrier/inc/relays.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "status.h"
#include "task.h"

#define BMS_CLOSE_RELAYS_DELAY 200
#define BMS_CLOSE_RELAYS_DELAY 300
typedef enum RelayType { NO_RELAYS = 0, POS_RELAY, NEG_RELAY, SOLAR_RELAY } RelayType;

void bms_relay_fault(void);
Expand Down
19 changes: 10 additions & 9 deletions projects/bms_carrier/src/cell_sense.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ static const LtcAfeSettings s_afe_settings = {
.cell_bitset = { 0xFFF, 0xFFF, 0xFFF },
.aux_bitset = { 0xFF, 0xFF, 0xFF },

.num_devices = 1,
.num_devices = 3,
.num_cells = 12,
.num_thermistors = 6,
.num_thermistors = 0,
};

static inline StatusCode prv_cell_sense_conversions() {
Expand Down Expand Up @@ -144,8 +144,9 @@ StatusCode cell_sense_run() {
uint16_t min_voltage = 0xffff;

for (size_t cell = 0; cell < (s_afe_settings.num_devices * s_afe_settings.num_cells); cell++) {
// LOG_DEBUG("CELL %d: %d\n\r", cell,
// ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]]);
LOG_DEBUG("CELL %d: %d\n\r", cell,
ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]]);
delay_ms(3);
max_voltage =
ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]] > max_voltage
? ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]]
Expand All @@ -155,8 +156,8 @@ StatusCode cell_sense_run() {
? ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]]
: min_voltage;
}
LOG_DEBUG("MAX VOLTAGE: %d\n", max_voltage);
LOG_DEBUG("MIN VOLTAGE: %d\n", min_voltage);
// LOG_DEBUG("MAX VOLTAGE: %d\n", max_voltage);
// LOG_DEBUG("MIN VOLTAGE: %d\n", min_voltage);
set_battery_info_max_cell_v(max_voltage);

if (max_voltage >= CELL_OVERVOLTAGE) {
Expand Down Expand Up @@ -192,13 +193,13 @@ StatusCode cell_sense_run() {
}
}

LOG_DEBUG("Config discharge bitset %d\n", ltc_afe_storage->discharge_bitset[0]);
// LOG_DEBUG("Config discharge bitset %d\n", ltc_afe_storage->discharge_bitset[0]);

for (size_t thermistor = 0;
thermistor < s_afe_settings.num_thermistors * s_afe_settings.num_devices; thermistor += 2) {
// Log thermistor result
LOG_DEBUG("Thermistor reading: %d\n",
ltc_afe_storage->aux_voltages[ltc_afe_storage->aux_result_lookup[thermistor]]);
// LOG_DEBUG("Thermistor reading: %d\n",
// ltc_afe_storage->aux_voltages[ltc_afe_storage->aux_result_lookup[thermistor]]);

if (ltc_afe_storage->aux_result_lookup[thermistor] >= CELL_MAX_TEMPERATURE) {
LOG_DEBUG("CELL OVERTEMP\n");
Expand Down
6 changes: 3 additions & 3 deletions projects/bms_carrier/src/current_sense.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ StatusCode current_sense_init(BmsStorage *bms_storage, I2CSettings *i2c_settings

// Expected MAX current / (uV / uOhmsSense) resolution
s_fuel_gauge_settings.i_thresh_max =
((CURRENT_SENSE_MAX_CURRENT_A) / (ALRT_PIN_V_RES_MICRO_V / CURRENT_SENSE_R_SENSE_MILLI_OHMS));
((CURRENT_SENSE_MAX_CURRENT_A) / (ALRT_PIN_V_RES_MICRO_V / CURRENT_SENSE_R_SENSE_MOHMS));
// Expected MIN current / (uV / uOhmsSense) resolution
s_fuel_gauge_settings.i_thresh_min =
((CURRENT_SENSE_MIN_CURRENT_A) / (ALRT_PIN_V_RES_MICRO_V / CURRENT_SENSE_R_SENSE_MILLI_OHMS));
((CURRENT_SENSE_MIN_CURRENT_A) / (ALRT_PIN_V_RES_MICRO_V / CURRENT_SENSE_R_SENSE_MOHMS));
// Interrupt threshold limits are stored in 2s-complement format with 1C resolution
s_fuel_gauge_settings.temp_thresh_max = CURRENT_SENSE_MAX_TEMP;

s_fuel_gauge_settings.r_sense_mohms = CURRENT_SENSE_R_SENSE_MILLI_OHMS;
s_fuel_gauge_settings.r_sense_mohms = CURRENT_SENSE_R_SENSE_MOHMS;

// Soft timer period for soc & chargin check
s_current_storage->fuel_guage_cycle_ms = fuel_guage_cycle_ms;
Expand Down
10 changes: 5 additions & 5 deletions projects/bms_carrier/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void pre_loop_init() {
LOG_DEBUG("Welcome to BMS \n");
fault_bps_init(&bms_storage.bps_storage);
current_sense_init(&bms_storage, &i2c_settings, FUEL_GAUGE_CYCLE_TIME_MS);
// cell_sense_init(&bms_storage.ltc_afe_storage);
cell_sense_init(&bms_storage.ltc_afe_storage);
aux_sense_init(&bms_storage.aux_storage);
init_bms_relays();
bms_fan_init(&bms_storage);
Expand All @@ -51,12 +51,12 @@ void run_medium_cycle() {
run_can_rx_cycle();
wait_tasks(1);

// cell_conversions();
// wait_tasks(1);
cell_conversions();
wait_tasks(1);
current_sense_run();
wait_tasks(1);

// cell_sense_run();
cell_sense_run();
aux_sense_run();
bms_run_fan();

Expand All @@ -65,7 +65,7 @@ void run_medium_cycle() {
}

void run_slow_cycle() {
// cell_discharge(&bms_storage.ltc_afe_storage);
cell_discharge(&bms_storage.ltc_afe_storage);
}

int main() {
Expand Down
3 changes: 2 additions & 1 deletion projects/centre_console/inc/cc_hw_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
{ .i2c_address = CC_IO_EXP_ADDR, .pin = PCA9555_PIN_IO1_7 }
#define POWER_LED_ADDR \
{ .i2c_address = CC_IO_EXP_ADDR, .pin = PCA9555_PIN_IO0_2 }

#define BPS_LED_ADDR \
{ .i2c_address = CC_IO_EXP_ADDR, .pin = PCA9555_PIN_IO0_2 }
{ .port = GPIO_PORT_A, .pin = 10 }

// Seven Segment display GPIO Addresses
// Aux Batt
Expand Down
1 change: 0 additions & 1 deletion projects/centre_console/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ void run_slow_cycle() {}
int main() {
tasks_init();
log_init();
gpio_init();
gpio_it_init();
I2CSettings i2c_settings = {
.speed = I2C_SPEED_STANDARD,
Expand Down
1 change: 1 addition & 0 deletions projects/centre_console/src/pedal.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static void prv_read_throttle_data(uint32_t *reading) {
} else if (calculated_reading > 1) {
calculated_reading = 1;
}
LOG_DEBUG("READING %d\n", (int)(calculated_reading * 100));
memcpy(reading, &calculated_reading, sizeof(calculated_reading));
}

Expand Down
12 changes: 10 additions & 2 deletions projects/centre_console/src/update_dashboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define CONVERT_VELOCITY_TO_KPH 0.036

SegDisplay all_displays = ALL_DISPLAYS;
GpioAddress bps_led = BPS_LED_ADDR;

// Centre Console State Variables
static bool s_cc_enabled;
Expand Down Expand Up @@ -100,6 +101,12 @@ void update_indicators(uint32_t notif) {
} else {
pca9555_gpio_set_state(&s_output_leds[AUX_WARNING_LED], PCA9555_GPIO_STATE_LOW);
}

if (get_battery_status_fault() || get_pd_status_power_state() == EE_POWER_FAULT_STATE || get_pd_status_bps_persist()) {
gpio_set_state(&bps_led, GPIO_STATE_LOW);
} else {
gpio_set_state(&bps_led, GPIO_STATE_HIGH);
}
}

void monitor_cruise_control() {
Expand Down Expand Up @@ -149,6 +156,7 @@ void update_drive_output() {
}

TASK(update_displays, TASK_MIN_STACK_SIZE) {
seg_displays_init(&all_displays);
while (true) {
float avg_speed = (get_motor_velocity_velocity_l() + get_motor_velocity_velocity_r()) / 2;
float speed_kph = avg_speed * CONVERT_VELOCITY_TO_KPH;
Expand All @@ -170,11 +178,11 @@ StatusCode dashboard_init(void) {
for (int i = 0; i < NUM_DRIVE_LED; i++) {
status_ok_or_return(pca9555_gpio_init_pin(&s_output_leds[i], &settings));
}
// seg_displays_init(&all_displays);
gpio_init_pin(&bps_led, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_HIGH);

return STATUS_CODE_OK;
}

StatusCode display_init(void) {
return tasks_init_task(update_displays, TASK_PRIORITY(1), NULL);
return tasks_init_task(update_displays, TASK_PRIORITY(2), NULL);
}
7 changes: 4 additions & 3 deletions projects/motor_controller/src/precharge.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@

StatusCode precharge_init(const PrechargeSettings *settings, Event event, const Task *task) {
gpio_init_pin(&settings->motor_sw, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&settings->precharge_monitor, GPIO_INPUT_FLOATING, GPIO_STATE_LOW);
gpio_init_pin(&settings->precharge_monitor, GPIO_INPUT_FLOATING, GPIO_STATE_HIGH);

GpioState state;
gpio_get_state(&settings->precharge_monitor, &state);
if (state) {
if (state == GPIO_STATE_HIGH) {
InterruptSettings monitor_it_settings = {
.type = INTERRUPT_TYPE_INTERRUPT,
.priority = INTERRUPT_PRIORITY_NORMAL,
.edge = INTERRUPT_EDGE_FALLING,
};
set_mc_status_precharge_status(false);
return gpio_it_register_interrupt(&settings->precharge_monitor, &monitor_it_settings, event,
task);
} else {
gpio_set_state(&settings->motor_sw, GPIO_STATE_HIGH);
set_mc_status_precharge_status(true);
gpio_set_state(&settings->motor_sw, GPIO_STATE_HIGH);
return STATUS_CODE_OK;
}
}
26 changes: 14 additions & 12 deletions projects/motor_controller/test/test_motor_can_rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void setup_test(void) {
log_init();
mcp2515_init(&s_mcp2515_storage, &s_mcp2515_settings);
init_motor_controller_can();
g_tx_struct.mc_status_precharge_status = 1;
}

void teardown_test(void) {}
Expand All @@ -66,8 +67,8 @@ void run_motor_controller_cycle() {
void push_mc_message(uint32_t id, float data_1, float data_2) {
CanMessage message = {
.id.raw = id,
.data_u32[0] = prv_get_uint32(data_1),
.data_u32[1] = prv_get_uint32(data_2),
.data_u32[1] = prv_get_uint32(data_1),
.data_u32[0] = prv_get_uint32(data_2),
.dlc = 8,
};
TEST_ASSERT_OK(can_queue_push(&s_mcp2515_storage.rx_queue, &message));
Expand All @@ -77,26 +78,27 @@ TEST_IN_TASK
void test_status(void) {
CanMessage status_l = {
.id.raw = MOTOR_CONTROLLER_BASE_L + 0x01,
.data_u8[0] = 1, // Receive error count
.data_u8[1] = 2, // Transmit error count
.data_u16[1] = 3, // Active motor
.data_u16[2] = 0b011001101, // Error Flags
.data_u16[3] = 0b110011010, // Limit Flags
.data_u8[4] = 1, // Receive error count
.data_u8[3] = 2, // Transmit error count
.data_u16[2] = 3, // Active motor
.data_u16[1] = 0b011001101, // Error Flags
.data_u16[0] = 0b110011010, // Limit Flags
.dlc = 8,
};
can_queue_push(&s_mcp2515_storage.rx_queue, &status_l);
CanMessage status_r = {
.id.raw = MOTOR_CONTROLLER_BASE_R + 0x01,
.data_u8[0] = 1, // Receive error count
.data_u8[1] = 2, // Transmit error count
.data_u16[1] = 3, // Active motor
.data_u16[2] = 0b100110010, // Error Flags
.data_u16[3] = 0b001100101, // Limit Flags
.data_u8[4] = 1, // Receive error count
.data_u8[3] = 2, // Transmit error count
.data_u16[2] = 3, // Active motor
.data_u16[1] = 0b100110010, // Error Flags
.data_u16[0] = 0b001100101, // Limit Flags
.dlc = 8,
};
can_queue_push(&s_mcp2515_storage.rx_queue, &status_r);

run_motor_controller_cycle();
LOG_DEBUG("error bitset: %d\n", g_tx_struct.mc_status_error_bitset_l);

TEST_ASSERT_EQUAL(0b01100110, g_tx_struct.mc_status_error_bitset_l);
TEST_ASSERT_EQUAL(0b10011010, g_tx_struct.mc_status_limit_bitset_l);
Expand Down
Loading

0 comments on commit 0fd7652

Please sign in to comment.