Skip to content

Commit

Permalink
adding more tests for the state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
RCMast3r committed Jan 28, 2024
1 parent a15d427 commit c99f7ee
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 45 deletions.
11 changes: 11 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
testing strategy:
- using GTest for mocking systems
- custom re-definitions of interfaces (for now)
- cant use GTest mocking easily for this I dont think

- the mock interfaces will just be empty classes p much to have something that the systems classes can have pointers to if they own a pointer to an interface

- the mock systems will use the gmock lib for better mocking of systems for system to system interaction in the test env



Empty file added TODO.md
Empty file.
4 changes: 2 additions & 2 deletions lib/mock_interfaces/DashboardInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class DashboardInterface
{
private:
public:
bool start_button;
bool start_button_pressed() { return start_button; };
bool start_button_status_;
bool start_button_pressed() { return start_button_status_; };
};

#endif /* DASHBOARDINTERFACE */
4 changes: 3 additions & 1 deletion lib/state_machine/include/MCUStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __MCU_STATE_MACHINE__

#include "Logger.h"

#include "PedalsSystem.h"
#include "DrivetrainSystem.h"
#include "Buzzer.h"
Expand All @@ -27,11 +28,12 @@ template <typename DrivetrainSysType>
class MCUStateMachine
{
public:
MCUStateMachine(BuzzerController *buzzer, DrivetrainSysType *drivetrain, DashboardInterface *dashboard)
MCUStateMachine(BuzzerController *buzzer, DrivetrainSysType *drivetrain, DashboardInterface *dashboard, PedalsSystem *pedals)
{
buzzer_ = buzzer;
drivetrain_ = drivetrain;
dashboard_ = dashboard;
pedals_ = pedals;
}

/// @brief our components can use this time to tell when to do things. We can set this ourselves for testing purposes instead of using metro timers
Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[env:test_env]
platform = native

test_framework = googletest
build_src_filter =
-<**/*.c>
-<**/*.cpp>
Expand All @@ -22,6 +22,7 @@ upload_protocol = teensy-cli
extra_scripts = pre:extra_script.py
lib_ignore =
mock_interfaces
mock_systems
lib_deps =
SPI
https://github.com/tonton81/FlexCAN_T4
Expand Down
5 changes: 0 additions & 5 deletions test/DashboardInterface.h

This file was deleted.

27 changes: 10 additions & 17 deletions test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#include <gtest/gtest.h>

#include "state_machine_test.h"
#include "pedals_system_test.h"
void setUp(void)
{
// declare a component obj
// possibly make temp class that is a freind class of PedalsSystem to access private members
// possibly modify the
}
// #include "pedals_system_test.h"

void tearDown(void)
{
}

int main()
{

UNITY_BEGIN();
int main(int argc, char **argv)
{

RUN_TEST(test_pedal_is_active);
RUN_TEST(test_state_machine_init_tick);


UNITY_END(); // stop unit testing
testing::InitGoogleMock(&argc, argv);
if (RUN_ALL_TESTS())
;
// Always return zero-code and allow PlatformIO to parse results
return 0;
}
1 change: 0 additions & 1 deletion test/pedals_system_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ author: Lucas Plant

#ifndef PEDALS_SYSTEM_TEST
#define PEDALS_SYSTEM_TEST
#include <unity.h>
#include <string>
#include "PedalsSystem.h"
#include <array>
Expand Down
97 changes: 79 additions & 18 deletions test/state_machine_test.h
Original file line number Diff line number Diff line change
@@ -1,55 +1,116 @@
#ifndef STATE_MACHINE_TEST
#define STATE_MACHINE_TEST
#include <unity.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "MCUStateMachine.h"
#include "PedalsSystem.h"




class DrivetrainMock
{
public:


bool drivetrain_ready_;
bool drivetrain_ready() {};
bool hv_thresh_;
bool drivetrain_ready(){};
/// @param curr_time current system tick time (millis()) that sets the init phase start time
void enable_drivetrain_hv(unsigned long curr_time){};

// startup phase 2
bool check_drivetrain_quit_dc_on() {};
bool check_drivetrain_quit_dc_on(){};

// on entry logic
void request_enable() {};
void command_drivetrain_no_torque() {};
void request_enable(){};
void command_drivetrain_no_torque(){};

// final check for drivetrain initialization to check if quit inverter on
bool drivetrain_enabled(){};

// check to see if init time limit has passed
bool inverter_init_timeout(unsigned long curr_time) {};
bool inverter_init_timeout(unsigned long curr_time){};

bool hv_over_threshold_on_drivetrain() {};
void disable() {};
bool drivetrain_error_occured() {};
bool hv_over_threshold_on_drivetrain() { return hv_thresh_; };
void disable(){};
bool drivetrain_error_occured(){};

void command_drivetrain(const DrivetrainCommand &data) {};
void command_drivetrain(const DrivetrainCommand &data){};
};


BuzzerController buzzer(500);

DrivetrainMock drivetrain;


PedalsParams params_for_test = {1, 1, 10, 10, 1, 1, 9, 9};
PedalsSystem pedals(params_for_test, params_for_test);

DashboardInterface dash_interface;

MCUStateMachine<DrivetrainMock> state_machine(&buzzer, &drivetrain, &dash_interface);
MCUStateMachine<DrivetrainMock> state_machine(&buzzer, &drivetrain, &dash_interface, &pedals);

void test_state_machine_init_tick(void)
TEST(MCUStateMachineTesting, test_state_machine_init_tick)
{
unsigned long sys_time = 1000;
TEST_ASSERT_TRUE(state_machine.get_state() == CAR_STATE::STARTUP);
EXPECT_EQ(state_machine.get_state(), CAR_STATE::STARTUP);
state_machine.tick_state_machine(sys_time);
TEST_ASSERT_TRUE(state_machine.get_state() == CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE);
EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE);
}



TEST(MCUStateMachineTesting, test_state_machine_tractive_system_activation)
{
unsigned long sys_time = 1000;

// ticking without hv over threshold testing and ensuring the tractive system not active still
state_machine.tick_state_machine(sys_time);
sys_time += 1;
drivetrain.hv_thresh_ = false;
state_machine.tick_state_machine(sys_time);
sys_time += 1;
EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE);

// hv going over threshold -> tractive system
drivetrain.hv_thresh_ = true;
sys_time +=1;
state_machine.tick_state_machine(sys_time);
EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_ACTIVE);


// hv going under thresh -> tractive system not active
drivetrain.hv_thresh_ =false;
sys_time +=1;
state_machine.tick_state_machine(sys_time);
EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE);
}

TEST(MCUStateMachineTesting, test_state_machine_tractive_system_enabling)
{
unsigned long sys_time = 1000;

// ticking without hv over threshold testing and ensuring the tractive system not active still
state_machine.tick_state_machine(sys_time);
sys_time += 1;
drivetrain.hv_thresh_ = false;
state_machine.tick_state_machine(sys_time);
sys_time += 1;
EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_NOT_ACTIVE);

// hv going over threshold -> tractive system
drivetrain.hv_thresh_ = true;
sys_time +=1;
state_machine.tick_state_machine(sys_time);
EXPECT_EQ(state_machine.get_state(), CAR_STATE::TRACTIVE_SYSTEM_ACTIVE);


sys_time +=1;
dash_interface.start_button_status_ = true;
auto pedals_brake_active_res = pedals.evaluate_pedals({0,0,3,3}, sys_time);
state_machine.tick_state_machine(sys_time);
EXPECT_EQ(state_machine.get_state(), CAR_STATE::ENABLING_INVERTERS);


}


#endif /* STATE_MACHINE_TEST */

0 comments on commit c99f7ee

Please sign in to comment.