diff --git a/Tests/LowLevel/Application/controller.cpp b/Tests/LowLevel/Application/controller.cpp index 733b1db..fc23749 100644 --- a/Tests/LowLevel/Application/controller.cpp +++ b/Tests/LowLevel/Application/controller.cpp @@ -5,6 +5,9 @@ extern "C" { } TEST(TEST_NAME, no_error) { + /* + * Setpoint equal current attitude + */ imu_data_t imuData{}; imuData.roll_mul_16 = 37 * 16; imuData.pitch_mul_16 = -49 * 16; @@ -14,37 +17,141 @@ TEST(TEST_NAME, no_error) { } TEST(TEST_NAME, need_roll_right) { + /* + * Plane is banked left (89°), should be banked right (89°). + * Left elevon should be down, right up. + */ imu_data_t imuData{}; - imuData.roll_mul_16 = -90 * 16; + imuData.roll_mul_16 = -89 * 16; imuData.pitch_mul_16 = -49 * 16; - auto controllerCmd = controller_update(&imuData, 90, -49); + auto controllerCmd = controller_update(&imuData, 89, -49); EXPECT_EQ(controllerCmd.elevon_right, 500); EXPECT_EQ(controllerCmd.elevon_left, -500); } TEST(TEST_NAME, need_roll_left) { + /* + * Plane is banked right (89°), should be banked left (89°). + * Left elevon should be up, right down. + */ imu_data_t imuData{}; - imuData.roll_mul_16 = 90 * 16; + imuData.roll_mul_16 = 89 * 16; imuData.pitch_mul_16 = -49 * 16; - auto controllerCmd = controller_update(&imuData, -90, -49); + auto controllerCmd = controller_update(&imuData, -89, -49); EXPECT_EQ(controllerCmd.elevon_right, -500); EXPECT_EQ(controllerCmd.elevon_left, 500); } TEST(TEST_NAME, need_pitch_up) { + /* + * Plane is pitched down (89°), should be pitched up (89°). + * Left and right elevon should be up. + */ imu_data_t imuData{}; imuData.roll_mul_16 = 37 * 16; - imuData.pitch_mul_16 = 90 * 16; - auto controllerCmd = controller_update(&imuData, 37, -90); + imuData.pitch_mul_16 = 89 * 16; + auto controllerCmd = controller_update(&imuData, 37, -89); EXPECT_EQ(controllerCmd.elevon_right, 500); EXPECT_EQ(controllerCmd.elevon_left, 500); } TEST(TEST_NAME, need_pitch_down) { + /* + * Plane is pitched up (89°), should be pitched down (89°). + * Left and right elevon should be down. + */ imu_data_t imuData{}; imuData.roll_mul_16 = 37 * 16; - imuData.pitch_mul_16 = -90 * 16; - auto controllerCmd = controller_update(&imuData, 37, 90); + imuData.pitch_mul_16 = -89 * 16; + auto controllerCmd = controller_update(&imuData, 37, 89); EXPECT_EQ(controllerCmd.elevon_right, -500); EXPECT_EQ(controllerCmd.elevon_left, -500); } + +TEST(TEST_NAME, need_roll_left_inverted) { + /* + * Plane is banked left (91°), should be banked right (91°), the shortest path is via inverted roll. + * Left elevon should be up, right down. + */ + imu_data_t imuData{}; + imuData.roll_mul_16 = -91 * 16; + imuData.pitch_mul_16 = -49 * 16; + auto controllerCmd = controller_update(&imuData, 91, -49); + EXPECT_EQ(controllerCmd.elevon_right, -500); + EXPECT_EQ(controllerCmd.elevon_left, 500); +} + +TEST(TEST_NAME, need_roll_right_inverted) { + /* + * Plane is banked right (91°), should be banked left (91°), the shortest path is via inverted roll. + * Left elevon should be down, right up. + */ + imu_data_t imuData{}; + imuData.roll_mul_16 = 91 * 16; + imuData.pitch_mul_16 = -49 * 16; + auto controllerCmd = controller_update(&imuData, -91, -49); + EXPECT_EQ(controllerCmd.elevon_right, 500); + EXPECT_EQ(controllerCmd.elevon_left, -500); +} + +TEST(TEST_NAME, need_pitch_down_inverted) { + /* + * Plane is pitched down (91°), should be pitched up (91°), the shortest path is via half-looping. + * Left and right elevon should be down. + */ + imu_data_t imuData{}; + imuData.roll_mul_16 = 37 * 16; + imuData.pitch_mul_16 = 91 * 16; + auto controllerCmd = controller_update(&imuData, 37, -91); + EXPECT_EQ(controllerCmd.elevon_right, -500); + EXPECT_EQ(controllerCmd.elevon_left, -500); +} + +TEST(TEST_NAME, need_pitch_up_inverted) { + /* + * Plane is pitched up (91°), should be pitched down (91°), the shortest path is via half-looping. + * Left and right elevon should be up. + */ + imu_data_t imuData{}; + imuData.roll_mul_16 = 37 * 16; + imuData.pitch_mul_16 = -91 * 16; + auto controllerCmd = controller_update(&imuData, 37, 91); + EXPECT_EQ(controllerCmd.elevon_right, 500); + EXPECT_EQ(controllerCmd.elevon_left, 500); +} + +TEST(TEST_NAME, no_error_roll_wrap_pos) { + imu_data_t imuData{}; + imuData.roll_mul_16 = (37 + 4 * 360) * 16; + imuData.pitch_mul_16 = -49 * 16; + auto controllerCmd = controller_update(&imuData, 37, -49); + EXPECT_EQ(controllerCmd.elevon_right, 0); + EXPECT_EQ(controllerCmd.elevon_left, 0); +} + +TEST(TEST_NAME, no_error_pitch_wrap_pos) { + imu_data_t imuData{}; + imuData.roll_mul_16 = 37 * 16; + imuData.pitch_mul_16 = (-49 + 4 * 360) * 16; + auto controllerCmd = controller_update(&imuData, 37, -49); + EXPECT_EQ(controllerCmd.elevon_right, 0); + EXPECT_EQ(controllerCmd.elevon_left, 0); +} + +TEST(TEST_NAME, no_error_roll_wrap_neg) { + imu_data_t imuData{}; + imuData.roll_mul_16 = (37 - 4 * 360) * 16; + imuData.pitch_mul_16 = -49 * 16; + auto controllerCmd = controller_update(&imuData, 37, -49); + EXPECT_EQ(controllerCmd.elevon_right, 0); + EXPECT_EQ(controllerCmd.elevon_left, 0); +} + +TEST(TEST_NAME, no_error_pitch_wrap_neg) { + imu_data_t imuData{}; + imuData.roll_mul_16 = 37 * 16; + imuData.pitch_mul_16 = (-49 - 4 * 360) * 16; + auto controllerCmd = controller_update(&imuData, 37, -49); + EXPECT_EQ(controllerCmd.elevon_right, 0); + EXPECT_EQ(controllerCmd.elevon_left, 0); +} diff --git a/Tests/LowLevel/Application/error_handler.cpp b/Tests/LowLevel/Application/error_handler.cpp index f5ad3c5..78819e9 100644 --- a/Tests/LowLevel/Application/error_handler.cpp +++ b/Tests/LowLevel/Application/error_handler.cpp @@ -1,4 +1,5 @@ #include +#include #include extern "C" { @@ -15,3 +16,16 @@ TEST(TEST_NAME, handle_warning) { error_handler_handle_warning(static_cast(6), 11); EXPECT_EQ(PORTL, 16 * 11 + 6); } + +TEST(TEST_NAME, handle_error) { + auto wdtHandle = mock::wdt.getHandle(); + std::size_t count = 0; + wdtHandle.overrideFunc([&count]() { + EXPECT_EQ(PORTL, 16 * 11 + 6); + count += 1; + if (count >= 10) { // This solves the halting problem... + throw std::runtime_error{"EXCEPTION TO BREAK LOOP FOR TESTS"}; + } + }); + EXPECT_THROW(error_handler_handle_error(static_cast(6), 11), std::runtime_error); +} diff --git a/Tests/LowLevel/Drivers/sbus.cpp b/Tests/LowLevel/Drivers/sbus.cpp index 7f96414..471c063 100644 --- a/Tests/LowLevel/Drivers/sbus.cpp +++ b/Tests/LowLevel/Drivers/sbus.cpp @@ -10,8 +10,6 @@ TEST(TEST_NAME, init) { auto uartHandle = mock::uart.getHandle(); auto ringBufferHandle = mock::ring_buffer.getHandle(); ringBufferHandle.overrideFunc([]() { return ring_buffer_data_t{}; }); - ringBufferHandle.overrideFunc( - [](ring_buffer_data_t * /*ring_buffer_data*/, uint8_t /*data*/) { return true; }); sbus_init(); /* * https://github.com/bolderflight/sbus/blob/main/README.md: @@ -46,22 +44,6 @@ TEST(TEST_NAME, rx_fill_buffer) { EXPECT_TRUE(ringBufferHandle.functionGotCalled(std::ignore, 54)); } -TEST(TEST_NAME, available_read_buffer) { - auto uartHandle = mock::uart.getHandle(); - auto ringBufferHandle = mock::ring_buffer.getHandle(); - ringBufferHandle.overrideFunc([]() { return ring_buffer_data_t{}; }); - sbus_init(); - EXPECT_TRUE(uartHandle.functionGotCalled()); - - std::size_t count = 0; - ringBufferHandle.overrideFunc([&count](ring_buffer_data_t *ringBufferData, uint8_t *out) { - EXPECT_NE(ringBufferData, nullptr); - count += 1; - *out = count; - return count <= 4; - }); -} - TEST(TEST_NAME, decode) { auto uartHandle = mock::uart.getHandle(); auto ringBufferHandle = mock::ring_buffer.getHandle();