Skip to content

Commit

Permalink
InverterOn State (#1081)
Browse files Browse the repository at this point in the history
### Summary
Created an InverterOn state in-between Init and Precharge in BMS. Should
wait 200ms then go into precharge.

### Changelist 
- Created InverterOn State
- Init goes to InverterOn instead of Precharge now
- Made small changes to App_Timer

### Testing Done
Created test check_inverter_on_state_is_broadcasted_over_can
Created test check_state_transition_from_init_to_inverter_to_precharge
Made small changes in other tests so they would take into account the
200ms InverterOn Stat

### Checklist
*Please change `[ ]` to `[x]` when you are ready.*
- [X] I have read and followed the code conventions detailed in
[README.md](../README.md) (*This will save time for both you and the
reviewer!*).
- [X] If this pull request is longer then **500** lines, I have provided
*explicit* justification in the summary above explaining why I *cannot*
break this up into multiple pull requests (*Small PR's are faster and
less painful for everyone involved!*).
  • Loading branch information
Jugveer-Sandher authored Nov 17, 2023
1 parent cb34abb commit b377f77
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 25 deletions.
3 changes: 2 additions & 1 deletion can_bus/json/BMS/BMS_enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"BMS_CHARGE_STATE": 2,
"BMS_DRIVE_STATE": 3,
"BMS_BALANCING_STATE": 4,
"BMS_FAULT_STATE": 5
"BMS_FAULT_STATE": 5,
"BMS_INVERTER_ON_STATE": 6
},
"ImdCondition": {
"IMD_CONDITION_SHORT_CIRCUIT": 0,
Expand Down
3 changes: 3 additions & 0 deletions firmware/shared/test_utils/Test_BaseStateMachineTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C"
{
#include "App_SharedMacros.h"
#include "App_SharedStateMachine.h"
#include "App_Timer.h"
}

class BaseStateMachineTest : public testing::Test
Expand Down Expand Up @@ -69,6 +70,8 @@ class BaseStateMachineTest : public testing::Test

UpdateSignals(state_machine, current_time_ms);
UpdateClock(state_machine, current_time_ms);
App_Timer_SetCurrentTimeMS(current_time_ms);

current_time_ms++;
}
}
Expand Down
14 changes: 14 additions & 0 deletions firmware/thruna/BMS/Inc/App/states/App_InverterOnState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "App_SharedStateMachine.h"

/**
* Get a pointer to the Inverter State.
* @return A pointer to the Inverter State.
*/
const struct State *App_GetInverterOnState(void);

/**
* Reset hasTimePassed bool
*/
void App_InverterOnState_Init(void);
6 changes: 2 additions & 4 deletions firmware/thruna/BMS/Src/App/states/App_InitState.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "states/App_AllStates.h"
#include "states/App_InitState.h"
#include "states/App_BalancingState.h"
#include "states/App_DriveState.h"
#include "states/App_PreChargeState.h"

#include "states/App_InverterOnState.h"
#include "App_SetPeriodicCanSignals.h"
#include "App_SharedMacros.h"

Expand Down Expand Up @@ -53,7 +51,7 @@ static void InitStateRunOnTick100Hz(struct StateMachine *const state_machine)
// if charger disconnected, proceed directly to precharge state
if (precharge_for_charging || (!is_charger_connected && !cell_balancing_enabled))
{
App_SharedStateMachine_SetNextState(state_machine, App_GetPreChargeState());
App_SharedStateMachine_SetNextState(state_machine, App_GetInverterOnState());
}
else if (cell_balancing_enabled)
{
Expand Down
59 changes: 59 additions & 0 deletions firmware/thruna/BMS/Src/App/states/App_InverterOnState.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "states/App_AllStates.h"
#include "states/App_PreChargeState.h"
#include "states/App_InverterOnState.h"
#include "App_SetPeriodicCanSignals.h"
#include "App_SharedMacros.h"
#include "App_Timer.h"

#define CHARGING_MILLISECONDS 200
TimerChannel timer;
static bool hasTimePassed;

static void InverterOnStateRunOnEntry(struct StateMachine *const state_machine)
{
App_CanTx_BMS_State_Set(BMS_INVERTER_ON_STATE);
App_Timer_InitTimer(&timer, CHARGING_MILLISECONDS);
App_Timer_Restart(&timer);
}

void App_InverterOnState_Init()
{
hasTimePassed = false;
}

static void InverterOnStateRunOnTick1Hz(struct StateMachine *const state_machine)
{
App_AllStatesRunOnTick1Hz(state_machine);
}

static void InverterOnStateRunOnTick100Hz(struct StateMachine *const state_machine)
{
if (App_AllStatesRunOnTick100Hz(state_machine))
{
TimerState timer_state = App_Timer_UpdateAndGetState(&timer);

if (timer_state == TIMER_STATE_EXPIRED || hasTimePassed)
{
App_SharedStateMachine_SetNextState(state_machine, App_GetPreChargeState());
hasTimePassed = true;
}
}
}

static void InverterOnStateRunOnExit(struct StateMachine *const state_machine)
{
UNUSED(state_machine);
}

const struct State *App_GetInverterOnState(void)
{
static struct State inverter_state = {
.name = "INVERTER_ON",
.run_on_entry = InverterOnStateRunOnEntry,
.run_on_tick_1Hz = InverterOnStateRunOnTick1Hz,
.run_on_tick_100Hz = InverterOnStateRunOnTick100Hz,
.run_on_exit = InverterOnStateRunOnExit,
};

return &inverter_state;
}
4 changes: 2 additions & 2 deletions firmware/thruna/BMS/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@
#include "states/App_InitState.h"
#include "configs/App_HeartbeatMonitorConfig.h"
#include "configs/App_ImdConfig.h"

#include "App_CommitInfo.h"

#include "App_Timer.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -1071,6 +1070,7 @@ void RunTask1kHz(void *argument)
const uint32_t task_start_ms = TICK_TO_MS(osKernelGetTickCount());

App_SharedClock_SetCurrentTimeInMilliseconds(clock, task_start_ms);
App_Timer_SetCurrentTimeMS(task_start_ms);
Io_CanTx_EnqueueOtherPeriodicMsgs(task_start_ms);

// Watchdog check-in must be the last function called before putting the
Expand Down
1 change: 1 addition & 0 deletions firmware/thruna/BMS/Test/Inc/Test_StateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ extern "C"
#include "states/App_ChargeState.h"
#include "configs/App_HeartbeatMonitorConfig.h"
#include "configs/App_ImdConfig.h"
#include "states/App_InverterOnState.h"
}
4 changes: 2 additions & 2 deletions firmware/thruna/BMS/Test/Src/Test_Faults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,14 +566,14 @@ TEST_F(BmsFaultTest, check_state_transition_fault_state_precharge_fault)
is_air_negative_closed_fake.return_val = true;
is_charger_connected_fake.return_val = false;
App_CanRx_Debug_StartCharging_Update(false);
LetTimePass(state_machine, 10U);
LetTimePass(state_machine, 210U);
ASSERT_EQ(App_GetPreChargeState(), App_SharedStateMachine_GetCurrentState(state_machine));
ASSERT_FALSE(App_CanAlerts_BMS_Fault_PrechargeFailure_Get());

// 3.8V nominal cell voltage * total # of cells to give estimate of nominal pack voltage
// trying to fool precahrge into thinking that ts_voltage is rising too quickly
get_ts_voltage_fake.return_val = 3.8f * ACCUMULATOR_NUM_SERIES_CELLS_TOTAL;
LetTimePass(state_machine, 10U);
LetTimePass(state_machine, 210U);

if (i < 3)
{
Expand Down
33 changes: 27 additions & 6 deletions firmware/thruna/BMS/Test/Src/Test_StateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class BmsStateMachineTest : public BaseStateMachineTest
// Default to starting the state machine in the `init` state
state_machine = App_SharedStateMachine_Create(world, App_GetInitState());
App_AllStates_Init();
App_InverterOnState_Init();

RESET_FAKE(get_pwm_frequency);
RESET_FAKE(get_pwm_duty_cycle);
Expand Down Expand Up @@ -302,6 +303,26 @@ TEST_F(BmsStateMachineTest, check_charge_state_is_broadcasted_over_can)
EXPECT_EQ(BMS_CHARGE_STATE, App_CanTx_BMS_State_Get());
}

TEST_F(BmsStateMachineTest, check_inverter_on_state_is_broadcasted_over_can)
{
SetInitialState(App_GetInverterOnState());
EXPECT_EQ(BMS_INVERTER_ON_STATE, App_CanTx_BMS_State_Get());
}

TEST_F(BmsStateMachineTest, check_state_transition_from_init_to_inverter_to_precharge)
{
is_charger_connected_fake.return_val = false;
get_ts_voltage_fake.return_val = 2.0f;
is_air_negative_closed_fake.return_val = true;
SetInitialState(App_GetInitState());

LetTimePass(state_machine, 200);
EXPECT_EQ(BMS_INVERTER_ON_STATE, App_CanTx_BMS_State_Get()) << "Expected state: BMS_INVERTER_STATE";

LetTimePass(state_machine, 10);
EXPECT_EQ(BMS_PRECHARGE_STATE, App_CanTx_BMS_State_Get()) << "Expected state: BMS_PRECHARGE_STATE";
}

TEST_F(BmsStateMachineTest, check_imd_frequency_is_broadcasted_over_can_in_all_states)
{
float fake_frequency = 0.0f;
Expand Down Expand Up @@ -637,7 +658,7 @@ TEST_F(BmsStateMachineTest, charger_connected_can_msg_init_state)

App_CanRx_Debug_StartCharging_Update(true);

LetTimePass(state_machine, 10);
LetTimePass(state_machine, 210U);

ASSERT_EQ(App_GetPreChargeState(), App_SharedStateMachine_GetCurrentState(state_machine));
}
Expand All @@ -657,11 +678,11 @@ TEST_F(BmsStateMachineTest, charger_connected_successful_precharge_stays)
App_CanRx_Debug_StartCharging_Update(true);

// Allow BMS time to go through Init state
LetTimePass(state_machine, 20);
LetTimePass(state_machine, 210U);
get_ts_voltage_fake.return_val = 400;

// Pause for slightly longer to allow pre-charge
LetTimePass(state_machine, 50);
LetTimePass(state_machine, 210U);

printf("%s", App_SharedStateMachine_GetCurrentState(state_machine)->name);
ASSERT_EQ(App_GetChargeState(), App_SharedStateMachine_GetCurrentState(state_machine));
Expand Down Expand Up @@ -874,19 +895,19 @@ TEST_F(BmsStateMachineTest, check_precharge_state_transitions_and_air_plus_statu
if (test_params[i].expect_precharge_starts)
{
// Precharge should start
LetTimePass(state_machine, 10);
LetTimePass(state_machine, 210U);
ASSERT_EQ(App_GetPreChargeState(), App_SharedStateMachine_GetCurrentState(state_machine));
ASSERT_EQ(close_air_positive_fake.call_count, 0);

// Let precharge duration elapse, confirm still in precharge state and AIR+ open
LetTimePass(state_machine, test_params[i].precharge_duration - 10);
LetTimePass(state_machine, test_params[i].precharge_duration);
ASSERT_EQ(App_GetPreChargeState(), App_SharedStateMachine_GetCurrentState(state_machine));
ASSERT_EQ(close_air_positive_fake.call_count, 0);

// Set voltage to pack voltage (i.e. voltage successfully rose within duration)
get_ts_voltage_fake.return_val = 3.8f * ACCUMULATOR_NUM_SEGMENTS * ACCUMULATOR_NUM_SERIES_CELLS_PER_SEGMENT;
LetTimePass(state_machine, 10);

LetTimePass(state_machine, 10);
if (test_params[i].expect_precharge_successful)
{
// Precharge successful, enter drive
Expand Down
6 changes: 1 addition & 5 deletions firmware/thruna/FSM/Test/Src/Test_Faults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ class FsmFaultTest : public BaseStateMachineTest
TearDownObject(wheels, App_Wheels_Destroy);
}

void UpdateClock(struct StateMachine *state_machine, uint32_t current_time_ms) override
{
UNUSED(state_machine);
App_Timer_SetCurrentTimeMS(current_time_ms);
}
void UpdateClock(struct StateMachine *state_machine, uint32_t current_time_ms) override { UNUSED(state_machine); }

void UpdateSignals(struct StateMachine *state_machine, uint32_t current_time_ms) override
{
Expand Down
6 changes: 1 addition & 5 deletions firmware/thruna/FSM/Test/Src/Test_StateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ class FsmStateMachineTest : public BaseStateMachineTest
TearDownObject(wheels, App_Wheels_Destroy);
}

void UpdateClock(struct StateMachine *state_machine, uint32_t current_time_ms) override
{
UNUSED(state_machine);
App_Timer_SetCurrentTimeMS(current_time_ms);
}
void UpdateClock(struct StateMachine *state_machine, uint32_t current_time_ms) override { UNUSED(state_machine); }

void UpdateSignals(struct StateMachine *state_machine, uint32_t current_time_ms) override
{
Expand Down

0 comments on commit b377f77

Please sign in to comment.