Skip to content

Commit

Permalink
✅ (Tests): Update & create new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Mar 2, 2023
1 parent 62069f0 commit 9f109ee
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 149 deletions.
2 changes: 1 addition & 1 deletion libs/IMUKit/tests/IMUKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ TEST_F(IMUKitTest, setOrigin)

TEST_F(IMUKitTest, onDataReady)
{
testing::MockFunction<void(interface::EulerAngles angles)> mock_callback {};
testing::MockFunction<void(EulerAngles angles)> mock_callback {};

imukit.onEulerAnglesReady(mock_callback.AsStdFunction());

Expand Down
23 changes: 10 additions & 13 deletions libs/MotionKit/tests/MotionKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {};

Expand Down Expand Up @@ -90,15 +87,16 @@ 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);

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)
Expand All @@ -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();
}

Expand All @@ -143,31 +140,31 @@ 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);

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();
}
125 changes: 0 additions & 125 deletions libs/MotionKit/tests/PID_test.cpp

This file was deleted.

67 changes: 67 additions & 0 deletions libs/MotionKit/tests/RotationControl_test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
81 changes: 81 additions & 0 deletions libs/MotionKit/tests/StabilizationControl_test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit 9f109ee

Please sign in to comment.