Skip to content

Commit

Permalink
#45: Mode handler mode selection test
Browse files Browse the repository at this point in the history
  • Loading branch information
aul12 committed Dec 29, 2022
1 parent 48e22ec commit 2917ce6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 29 deletions.
8 changes: 5 additions & 3 deletions Src/Application/mode_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ mode_handler_mode_t mode_handler_handle(imu_data_t *imu_data, remote_data_t *rem
/*
* Setpoint selection
*/
if (imu_active && remote_active && flightcomputer_active && !remote_data->override_active &&
remote_data->is_armed) {
return MODE_FLIGHTCOMPUTER;
if (imu_active && remote_active && flightcomputer_active && !remote_data->override_active) {
if (remote_data->is_armed) {
return MODE_FLIGHTCOMPUTER;
}
return MODE_STABILISED_FAILSAVE;
}

if (remote_active) {
Expand Down
5 changes: 3 additions & 2 deletions Src/Application/mode_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ void mode_handler_init(void);
* | IMU | Remote | FCP | Override | Armed | Mode |
* | --- | ------ | --- | -------- | ----- | -------------------- |
* | Y | Y | Y | N | Y | FCP |
* | | Y | | | | Remote |
* | Y | Y | Y | N | N | Stabilisied Failsave |
* | Y | N | | | | Stabilisied Failsave |
* | N | N | | | | Failsave |
* | | Y | | | | Remote |
* | | | | | | Failsave |
*
*
* @param imu_data out-parameter, contains the last valid imu measurement
Expand Down
107 changes: 84 additions & 23 deletions Tests/LowLevel/Application/mode_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@ extern "C" {
#include <Application/mode_handler.h>
}

TEST(TEST_NAME, all_ok) {
auto imuHandle = mock::imu.getHandle();
auto flightcomputerHandle = mock::flightcomputer.getHandle();
auto remoteHandle = mock::remote.getHandle();
auto errorHandlerHandle = mock::error_handler.getHandle();

imuHandle.overrideFunc<imu_data_available>([]() { return true; });
imuHandle.overrideFunc<imu_get_latest_data>([]() { return imu_data_t{.imu_ok = true}; });
remoteHandle.overrideFunc<remote_data_available>([]() { return true; });
remoteHandle.overrideFunc<remote_get_data>(
[]() { return remote_data_t{.is_armed = true, .override_active = false, .remote_ok = true}; });
flightcomputerHandle.overrideFunc<flightcomputer_setpoint_available>([]() { return true; });
flightcomputerHandle.overrideFunc<flightcomputer_get_setpoint>([]() { return flightcomputer_setpoint_t{}; });

mode_handler_init();

imu_data_t imuData;
remote_data_t remoteData;
flightcomputer_setpoint_t flightcomputerSetpoint;

EXPECT_EQ(mode_handler_handle(&imuData, &remoteData, &flightcomputerSetpoint), MODE_FLIGHTCOMPUTER);
}

TEST(TEST_NAME, imu_initial_timeout) {
auto imuHandle = mock::imu.getHandle();
auto flightcomputerHandle = mock::flightcomputer.getHandle();
Expand Down Expand Up @@ -380,3 +357,87 @@ TEST(TEST_NAME, fcp_timeout) {
EXPECT_FALSE(errorHandlerHandle.functionGotCalled<error_handler_handle_warning>(MODE_HANDLER,
MODE_HANDLER_ERROR_NO_FCP_DATA));
}

TEST(TEST_NAME, modeselection) {
auto imuHandle = mock::imu.getHandle();
auto flightcomputerHandle = mock::flightcomputer.getHandle();
auto remoteHandle = mock::remote.getHandle();
auto errorHandlerHandle = mock::error_handler.getHandle();

imuHandle.overrideFunc<imu_data_available>([]() { return true; });
remoteHandle.overrideFunc<remote_data_available>([]() { return true; });
flightcomputerHandle.overrideFunc<flightcomputer_get_setpoint>([]() { return flightcomputer_setpoint_t{}; });

struct availability_values {
bool imu, remote, fcp, override, arm;
};

const std::vector<std::pair<availability_values, mode_handler_mode_t>> decisionTable{
{{false, false, false, false, false}, MODE_FAILSAVE},
{{false, false, false, false, true}, MODE_FAILSAVE},
{{false, false, false, true, false}, MODE_FAILSAVE},
{{false, false, false, true, true}, MODE_FAILSAVE},
{{false, false, true, false, false}, MODE_FAILSAVE},
{{false, false, true, false, true}, MODE_FAILSAVE},
{{false, false, true, true, false}, MODE_FAILSAVE},
{{false, false, true, true, true}, MODE_FAILSAVE},
{{false, true, false, false, false}, MODE_REMOTE},
{{false, true, false, false, true}, MODE_REMOTE},
{{false, true, false, true, false}, MODE_REMOTE},
{{false, true, false, true, true}, MODE_REMOTE},
{{false, true, true, false, false}, MODE_REMOTE},
{{false, true, true, false, true}, MODE_REMOTE},
{{false, true, true, true, false}, MODE_REMOTE},
{{false, true, true, true, true}, MODE_REMOTE},
{{true, false, false, false, false}, MODE_STABILISED_FAILSAVE},
{{true, false, false, false, true}, MODE_STABILISED_FAILSAVE},
{{true, false, false, true, false}, MODE_STABILISED_FAILSAVE},
{{true, false, false, true, true}, MODE_STABILISED_FAILSAVE},
{{true, false, true, false, false}, MODE_STABILISED_FAILSAVE},
{{true, false, true, false, true}, MODE_STABILISED_FAILSAVE},
{{true, false, true, true, false}, MODE_STABILISED_FAILSAVE},
{{true, false, true, true, true}, MODE_STABILISED_FAILSAVE},
{{true, true, false, false, false}, MODE_REMOTE},
{{true, true, false, false, true}, MODE_REMOTE},
{{true, true, false, true, false}, MODE_REMOTE},
{{true, true, false, true, true}, MODE_REMOTE},
{{true, true, true, false, false}, MODE_STABILISED_FAILSAVE},
{{true, true, true, false, true}, MODE_FLIGHTCOMPUTER},
{{true, true, true, true, false}, MODE_REMOTE},
{{true, true, true, true, true}, MODE_REMOTE},
};

for (auto [availability_value, expected_mode] : decisionTable) {
mode_handler_init();
imuHandle.overrideFunc<imu_get_latest_data>(
[availability_value]() { return imu_data_t{.imu_ok = availability_value.imu}; });
remoteHandle.overrideFunc<remote_get_data>([availability_value]() {
return remote_data_t{.is_armed = availability_value.arm,
.override_active = availability_value.override,
.remote_ok = availability_value.remote};
});
flightcomputerHandle.overrideFunc<flightcomputer_setpoint_available>(
[availability_value]() { return availability_value.fcp; });

imu_data_t imuData;
remote_data_t remoteData;
flightcomputer_setpoint_t flightcomputerSetpoint;
EXPECT_EQ(mode_handler_handle(&imuData, &remoteData, &flightcomputerSetpoint), expected_mode);
if (!availability_value.remote) {
EXPECT_TRUE(errorHandlerHandle.functionGotCalled<error_handler_handle_warning>(
MODE_HANDLER, MODE_HANDLER_ERROR_NO_REMOTE_DATA));
}
if (!availability_value.imu) {
EXPECT_TRUE(errorHandlerHandle.functionGotCalled<error_handler_handle_warning>(
MODE_HANDLER, MODE_HANDLER_ERROR_NO_IMU_DATA));
}
if (!availability_value.fcp) {
EXPECT_TRUE(errorHandlerHandle.functionGotCalled<error_handler_handle_warning>(
MODE_HANDLER, MODE_HANDLER_ERROR_NO_FCP_DATA));
}
}
}

TEST(TEST_NAME, fill_out_vars) {
EXPECT_FALSE(true);
}
2 changes: 1 addition & 1 deletion Tests/LowLevel/Mock/Lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ project(MockLib)

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE ..)
target_link_libraries(${PROJECT_NAME} PUBLIC gtest gmock pthread)
target_link_libraries(${PROJECT_NAME} INTERFACE gtest gmock pthread)

0 comments on commit 2917ce6

Please sign in to comment.