From 9f109ee9cf71e180c09d5e05bd63d46f01dd2c2b Mon Sep 17 00:00:00 2001 From: Hugo Pezziardi Date: Wed, 1 Mar 2023 11:12:58 +0100 Subject: [PATCH] :white_check_mark: (Tests): Update & create new tests --- libs/IMUKit/tests/IMUKit_test.cpp | 2 +- libs/MotionKit/tests/MotionKit_test.cpp | 23 ++-- libs/MotionKit/tests/PID_test.cpp | 125 ------------------ libs/MotionKit/tests/RotationControl_test.cpp | 67 ++++++++++ .../tests/StabilizationControl_test.cpp | 81 ++++++++++++ .../tests/ReinforcerKit_test.cpp | 8 +- .../mocks/mocks/leka/{IMUKit.h => IMUKit.hpp} | 7 +- 7 files changed, 164 insertions(+), 149 deletions(-) delete mode 100644 libs/MotionKit/tests/PID_test.cpp create mode 100644 libs/MotionKit/tests/RotationControl_test.cpp create mode 100644 libs/MotionKit/tests/StabilizationControl_test.cpp rename tests/unit/mocks/mocks/leka/{IMUKit.h => IMUKit.hpp} (67%) diff --git a/libs/IMUKit/tests/IMUKit_test.cpp b/libs/IMUKit/tests/IMUKit_test.cpp index f56d1daacf..242bc8a86c 100644 --- a/libs/IMUKit/tests/IMUKit_test.cpp +++ b/libs/IMUKit/tests/IMUKit_test.cpp @@ -66,7 +66,7 @@ TEST_F(IMUKitTest, setOrigin) TEST_F(IMUKitTest, onDataReady) { - testing::MockFunction mock_callback {}; + testing::MockFunction mock_callback {}; imukit.onEulerAnglesReady(mock_callback.AsStdFunction()); diff --git a/libs/MotionKit/tests/MotionKit_test.cpp b/libs/MotionKit/tests/MotionKit_test.cpp index f16defd47d..248e8b08fe 100644 --- a/libs/MotionKit/tests/MotionKit_test.cpp +++ b/libs/MotionKit/tests/MotionKit_test.cpp @@ -7,14 +7,13 @@ #include "IMUKit.hpp" #include "gtest/gtest.h" #include "mocks/leka/CoreMotor.h" -#include "mocks/leka/IMUKit.h" +#include "mocks/leka/IMUKit.hpp" #include "mocks/leka/Timeout.h" using namespace leka; using ::testing::_; using ::testing::AtLeast; -using ::testing::MockFunction; // TODO(@leka/dev-embedded): temporary fix, changes are needed when updating fusion algorithm @@ -36,9 +35,7 @@ class MotionKitTest : public ::testing::Test mock::Timeout mock_timeout {}; - const interface::EulerAngles angles { - 0.0F, 0.0F, 0.F - }; + const EulerAngles angles {0.0F, 0.0F, 0.F}; mock::IMUKit mock_imukit {}; @@ -90,6 +87,8 @@ TEST_F(MotionKitTest, rotateCounterClockwise) TEST_F(MotionKitTest, startStabilisation) { + const EulerAngles angles_quarter_left {0.0F, 0.0F, 90.F}; + EXPECT_CALL(mock_timeout, stop).Times(1); EXPECT_CALL(mock_motor_left, stop).Times(1); EXPECT_CALL(mock_motor_right, stop).Times(1); @@ -97,8 +96,7 @@ TEST_F(MotionKitTest, startStabilisation) EXPECT_CALL(mock_imukit, getEulerAngles).Times(1); motion.startStabilisation(); - - mock_imukit.call_angles_ready_callback(angles); + mock_imukit.call_angles_ready_callback(angles_quarter_left); } TEST_F(MotionKitTest, rotateAndStop) @@ -116,13 +114,12 @@ TEST_F(MotionKitTest, rotateAndStop) EXPECT_CALL(mock_motor_right, spin(Rotation::clockwise, _)).Times(AtLeast(1)); motion.rotate(1, Rotation::clockwise); + mock_imukit.call_angles_ready_callback(angles); EXPECT_CALL(mock_timeout, stop).Times(1); EXPECT_CALL(mock_motor_left, stop).Times(1); EXPECT_CALL(mock_motor_right, stop).Times(1); - mock_imukit.call_angles_ready_callback(angles); - motion.stop(); } @@ -143,18 +140,19 @@ TEST_F(MotionKitTest, rotateAndTimeOutOver) EXPECT_CALL(mock_motor_right, spin(Rotation::clockwise, _)).Times(AtLeast(1)); motion.rotate(1, Rotation::clockwise); + mock_imukit.call_angles_ready_callback(angles); EXPECT_CALL(mock_timeout, stop).Times(1); EXPECT_CALL(mock_motor_left, stop).Times(1); EXPECT_CALL(mock_motor_right, stop).Times(1); - mock_imukit.call_angles_ready_callback(angles); - on_timeout_callback(); } TEST_F(MotionKitTest, startStabilisationAndStop) { + const EulerAngles angles_quarter_left {0.0F, 0.0F, 90.F}; + EXPECT_CALL(mock_timeout, stop).Times(1); EXPECT_CALL(mock_motor_left, stop).Times(1); EXPECT_CALL(mock_motor_right, stop).Times(1); @@ -162,12 +160,11 @@ TEST_F(MotionKitTest, startStabilisationAndStop) EXPECT_CALL(mock_imukit, getEulerAngles).Times(1); motion.startStabilisation(); + mock_imukit.call_angles_ready_callback(angles_quarter_left); EXPECT_CALL(mock_timeout, stop).Times(1); EXPECT_CALL(mock_motor_left, stop).Times(1); EXPECT_CALL(mock_motor_right, stop).Times(1); - mock_imukit.call_angles_ready_callback(angles); - motion.stop(); } diff --git a/libs/MotionKit/tests/PID_test.cpp b/libs/MotionKit/tests/PID_test.cpp deleted file mode 100644 index ee4139bae1..0000000000 --- a/libs/MotionKit/tests/PID_test.cpp +++ /dev/null @@ -1,125 +0,0 @@ -// Leka - LekaOS -// Copyright 2022 APF France handicap -// SPDX-License-Identifier: Apache-2.0 - -#include "PID.hpp" - -#include "gtest/gtest.h" - -using namespace leka; - -class PIDTest : public ::testing::Test -{ - protected: - PIDTest() = default; - - void SetUp() override { pid.setTargetYaw(kTargetYaw); } - // void TearDown() override {} - - PID pid {}; - - float kTargetYaw = 0; - - float halfturn_error_speed = 6.3F; //? ((360-180)*Kp + (360-180)*Kd)/KDeltaT -}; - -TEST_F(PIDTest, initialization) -{ - ASSERT_NE(&pid, nullptr); -} - -TEST_F(PIDTest, processPIDDefaultPosition) -{ - auto pitch = 0.F; - auto roll = 0.F; - auto yaw = 0.F; - - auto [speed, direction] = pid.processPID(pitch, roll, yaw); - - EXPECT_EQ(speed, 0.F); - EXPECT_EQ(direction, Rotation::clockwise); -} - -TEST_F(PIDTest, processPIDRolledOverAHalfRight) -{ - auto pitch = 0.F; - auto roll = 0.F; - auto yaw = 180.F; - - auto [speed, direction] = pid.processPID(pitch, roll, yaw); - - EXPECT_EQ(speed, halfturn_error_speed); - EXPECT_EQ(direction, Rotation::counterClockwise); -} - -TEST_F(PIDTest, processPIDRolledOverAQuarterRight) -{ - auto pitch = 0.F; - auto roll = 0.F; - auto yaw = 90.F; - - auto [speed, direction] = pid.processPID(pitch, roll, yaw); - - EXPECT_EQ(speed, halfturn_error_speed / 2.F); - EXPECT_EQ(direction, Rotation::counterClockwise); -} - -TEST_F(PIDTest, processPIDRolledOverAQuarterLeft) -{ - auto pitch = 0.F; - auto roll = 0.F; - auto yaw = -90.F; - - auto [speed, direction] = pid.processPID(pitch, roll, yaw); - - EXPECT_EQ(speed, halfturn_error_speed / 2.F); - EXPECT_EQ(direction, Rotation::clockwise); -} - -TEST_F(PIDTest, processPIDRolledOverAHalfLeft) -{ - auto pitch = 0.F; - auto roll = 0.F; - auto yaw = -180.F; - - auto [speed, direction] = pid.processPID(pitch, roll, yaw); - - EXPECT_EQ(speed, halfturn_error_speed); - EXPECT_EQ(direction, Rotation::clockwise); -} - -TEST_F(PIDTest, processPIDByErrorDefaultPosition) -{ - auto error = 0.F; - - auto speed = pid.processPIDByError(error); - - EXPECT_EQ(speed, 0.F); -} - -TEST_F(PIDTest, processPIDByErrorHalfTurn) -{ - auto error = 180.F; - - auto speed = pid.processPIDByError(error); - - EXPECT_EQ(speed, halfturn_error_speed); -} - -TEST_F(PIDTest, processPIDByErrorOneTurn) -{ - auto error = 360.F; - - auto speed = pid.processPIDByError(error); - - EXPECT_EQ(speed, halfturn_error_speed * 2); -} - -TEST_F(PIDTest, processPIDByError2Turn) -{ - auto error = 720.F; - - auto speed = pid.processPIDByError(error); - - EXPECT_EQ(speed, halfturn_error_speed * 4); -} diff --git a/libs/MotionKit/tests/RotationControl_test.cpp b/libs/MotionKit/tests/RotationControl_test.cpp new file mode 100644 index 0000000000..0a3b8dcd6a --- /dev/null +++ b/libs/MotionKit/tests/RotationControl_test.cpp @@ -0,0 +1,67 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "RotationControl.hpp" + +#include "gtest/gtest.h" + +using namespace leka; + +class RotationControlTest : public ::testing::Test +{ + protected: + RotationControlTest() = default; + + // void SetUp() override {} + // void TearDown() override {} + + RotationControl rotation_control {}; + + float yaw = 0.F; + + float halfturn_error_speed = 1.85F; //? mapSpeed(180 * (Kp + Kd)/KDeltaT) + float one_turn_error_speed = 3.55000019F; //? mapSpeed(360 * (Kp + Kd)/KDeltaT) + float two_turn_error_speed = 6.95000029F; //? mapSpeed(720 * (Kp + Kd)/KDeltaT) +}; + +TEST_F(RotationControlTest, initialization) +{ + ASSERT_NE(&rotation_control, nullptr); +} + +TEST_F(RotationControlTest, processRotationAngleDefaultPosition) +{ + auto target = 0.F; + + auto speed = rotation_control.processRotationAngle(target, yaw); + + EXPECT_EQ(speed, 0.F); +} + +TEST_F(RotationControlTest, processRotationAngleHalfTurn) +{ + auto target = 180.F; + + auto speed = rotation_control.processRotationAngle(target, yaw); + + EXPECT_EQ(speed, halfturn_error_speed); +} + +TEST_F(RotationControlTest, processRotationAngleOneTurn) +{ + auto target = 360.F; + + auto speed = rotation_control.processRotationAngle(target, yaw); + + EXPECT_EQ(speed, one_turn_error_speed); +} + +TEST_F(RotationControlTest, processRotationAngle2Turn) +{ + auto target = 720.F; + + auto speed = rotation_control.processRotationAngle(target, yaw); + + EXPECT_EQ(speed, two_turn_error_speed); +} diff --git a/libs/MotionKit/tests/StabilizationControl_test.cpp b/libs/MotionKit/tests/StabilizationControl_test.cpp new file mode 100644 index 0000000000..4a29f8e3ac --- /dev/null +++ b/libs/MotionKit/tests/StabilizationControl_test.cpp @@ -0,0 +1,81 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include "StabilizationControl.hpp" + +#include "gtest/gtest.h" + +using namespace leka; + +class StabilizationControlTest : public ::testing::Test +{ + protected: + StabilizationControlTest() = default; + + // void SetUp() override {} + // void TearDown() override {} + + StabilizationControl stabilization_control {}; + + EulerAngles target {.pitch = 0.F, .roll = 0.F, .yaw = 0.F}; + EulerAngles current {}; + + float quarterturn_error_speed = 1.F; //? mapSpeed(90 * (Kp + Kd)/ KDeltaT) + float halfturn_error_speed = 1.85F; //? mapSpeed(180 * (Kp + Kd)/ KDeltaT) +}; + +TEST_F(StabilizationControlTest, initialization) +{ + ASSERT_NE(&stabilization_control, nullptr); +} + +TEST_F(StabilizationControlTest, processStabilizationAngleDefaultPosition) +{ + current = {.pitch = 0.F, .roll = 0.F, .yaw = 0.F}; + + auto [speed, direction] = stabilization_control.processStabilizationAngle(target, current); + + EXPECT_EQ(speed, 0.F); + EXPECT_EQ(direction, Rotation::clockwise); +} + +TEST_F(StabilizationControlTest, processStabilizationAngleRolledOverAHalfRight) +{ + current = {.pitch = 0.F, .roll = 0.F, .yaw = 180.F}; + + auto [speed, direction] = stabilization_control.processStabilizationAngle(target, current); + + EXPECT_EQ(speed, halfturn_error_speed); + EXPECT_EQ(direction, Rotation::counterClockwise); +} + +TEST_F(StabilizationControlTest, processStabilizationAngleRolledOverAQuarterRight) +{ + current = {.pitch = 0.F, .roll = 0.F, .yaw = 90.F}; + + auto [speed, direction] = stabilization_control.processStabilizationAngle(target, current); + + EXPECT_EQ(speed, quarterturn_error_speed); + EXPECT_EQ(direction, Rotation::counterClockwise); +} + +TEST_F(StabilizationControlTest, processStabilizationAngleRolledOverAQuarterLeft) +{ + current = {.pitch = 0.F, .roll = 0.F, .yaw = -90.F}; + + auto [speed, direction] = stabilization_control.processStabilizationAngle(target, current); + + EXPECT_EQ(speed, quarterturn_error_speed); + EXPECT_EQ(direction, Rotation::clockwise); +} + +TEST_F(StabilizationControlTest, processStabilizationAngleRolledOverAHalfLeft) +{ + current = {.pitch = 0.F, .roll = 0.F, .yaw = -180.F}; + + auto [speed, direction] = stabilization_control.processStabilizationAngle(target, current); + + EXPECT_EQ(speed, halfturn_error_speed); + EXPECT_EQ(direction, Rotation::clockwise); +} diff --git a/libs/ReinforcerKit/tests/ReinforcerKit_test.cpp b/libs/ReinforcerKit/tests/ReinforcerKit_test.cpp index 6ba2a8f58a..c2dfc86500 100644 --- a/libs/ReinforcerKit/tests/ReinforcerKit_test.cpp +++ b/libs/ReinforcerKit/tests/ReinforcerKit_test.cpp @@ -9,7 +9,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" #include "mocks/leka/CoreMotor.h" -#include "mocks/leka/IMUKit.h" +#include "mocks/leka/IMUKit.hpp" #include "mocks/leka/LedKit.h" #include "mocks/leka/Timeout.h" #include "mocks/leka/VideoKit.h" @@ -17,10 +17,8 @@ using namespace leka; -using ::testing::AnyNumber; using ::testing::AtLeast; using ::testing::AtMost; -using ::testing::Sequence; MATCHER_P(isSameAnimation, expected_animation, "") { @@ -51,9 +49,7 @@ class ReinforcerkitTest : public ::testing::Test ReinforcerKit reinforcerkit; - const interface::EulerAngles angles { - 0.0F, 0.0F, 0.F - }; + const EulerAngles angles {0.0F, 0.0F, 0.F}; void expectedCallsMovingReinforcer(interface::LEDAnimation *animation) { diff --git a/tests/unit/mocks/mocks/leka/IMUKit.h b/tests/unit/mocks/mocks/leka/IMUKit.hpp similarity index 67% rename from tests/unit/mocks/mocks/leka/IMUKit.h rename to tests/unit/mocks/mocks/leka/IMUKit.hpp index 48e5a9fde1..cb93b1b90f 100644 --- a/tests/unit/mocks/mocks/leka/IMUKit.h +++ b/tests/unit/mocks/mocks/leka/IMUKit.hpp @@ -5,22 +5,21 @@ #pragma once #include "gmock/gmock.h" -#include "interface/libs/IMUKit.h" +#include "interface/libs/IMUKit.hpp" namespace leka::mock { class IMUKit : public interface::IMUKit { public: - MOCK_METHOD(void, init, (), (override)); MOCK_METHOD(void, start, (), (override)); MOCK_METHOD(void, stop, (), (override)); MOCK_METHOD(void, setOrigin, (), (override)); - MOCK_METHOD(interface::EulerAngles, getEulerAngles, (), (const, override)); + MOCK_METHOD(EulerAngles, getEulerAngles, (), (const, override)); void onEulerAnglesReady(angles_ready_callback_t const &cb) override { angles_ready_callback = cb; } - void call_angles_ready_callback(const interface::EulerAngles &data) { angles_ready_callback(data); } + void call_angles_ready_callback(const EulerAngles &data) { angles_ready_callback(data); } private: angles_ready_callback_t angles_ready_callback {};