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 1, 2023
1 parent ada7c26 commit 970fdb2
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 143 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
11 changes: 4 additions & 7 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 @@ -94,7 +91,7 @@ TEST_F(MotionKitTest, startStabilisation)
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

EXPECT_CALL(mock_imukit, getEulerAngles).Times(1);
EXPECT_CALL(mock_imukit, getEulerAngles).Times(2);

motion.startStabilisation();

Expand Down Expand Up @@ -159,7 +156,7 @@ TEST_F(MotionKitTest, startStabilisationAndStop)
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

EXPECT_CALL(mock_imukit, getEulerAngles).Times(1);
EXPECT_CALL(mock_imukit, getEulerAngles).Times(2);

motion.startStabilisation();

Expand Down
67 changes: 67 additions & 0 deletions libs/MotionKit/tests/PIDRotation_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 "PIDRotation.hpp"

#include "gtest/gtest.h"

using namespace leka;

class PIDRotationTest : public ::testing::Test
{
protected:
PIDRotationTest() = default;

// void SetUp() override {}
// void TearDown() override {}

PIDRotation pid {};

float yaw = 0.F;

float halfturn_error_speed = 1.23181832F; //? mapSpeed((180*Kp + 180*Kd)/KDeltaT)
float one_turn_error_speed = 2.31363678F; //? mapSpeed((360*Kp + 360*Kd)/KDeltaT)
float two_turn_error_speed = 4.47727346F; //? mapSpeed((720*Kp + 720*Kd)/KDeltaT)
};

TEST_F(PIDRotationTest, initialization)
{
ASSERT_NE(&pid, nullptr);
}

TEST_F(PIDRotationTest, processRotationAngleDefaultPosition)
{
auto target = 0.F;

auto speed = pid.processRotationAngle(target, yaw);

EXPECT_EQ(speed, 0.F);
}

TEST_F(PIDRotationTest, processRotationAngleHalfTurn)
{
auto target = 180.F;

auto speed = pid.processRotationAngle(target, yaw);

EXPECT_EQ(speed, halfturn_error_speed);
}

TEST_F(PIDRotationTest, processRotationAngleOneTurn)
{
auto target = 360.F;

auto speed = pid.processRotationAngle(target, yaw);

EXPECT_EQ(speed, one_turn_error_speed);
}

TEST_F(PIDRotationTest, processRotationAngle2Turn)
{
auto target = 720.F;

auto speed = pid.processRotationAngle(target, yaw);

EXPECT_EQ(speed, two_turn_error_speed);
}
81 changes: 81 additions & 0 deletions libs/MotionKit/tests/PIDStabilization_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 "PIDStabilization.hpp"

#include "gtest/gtest.h"

using namespace leka;

class PIDStabilizationTest : public ::testing::Test
{
protected:
PIDStabilizationTest() = default;

// void SetUp() override {}
// void TearDown() override {}

PIDStabilization pid {};

EulerAngles target {.pitch = 0.F, .roll = 0.F, .yaw = 0.F};
EulerAngles current {};

float quarterturn_error_speed = 0.690909147F; //? mapSpeed(((270-180)*Kp + (360-180)*Kd)/KDeltaT)
float halfturn_error_speed = 1.23181832F; //? mapSpeed(((360-180)*Kp + (360-180)*Kd)/KDeltaT)
};

TEST_F(PIDStabilizationTest, initialization)
{
ASSERT_NE(&pid, nullptr);
}

TEST_F(PIDStabilizationTest, processStabilizationAngleDefaultPosition)
{
current = {.pitch = 0.F, .roll = 0.F, .yaw = 0.F};

auto [speed, direction] = pid.processStabilizationAngle(target, current);

EXPECT_EQ(speed, 0.F);
EXPECT_EQ(direction, Rotation::clockwise);
}

TEST_F(PIDStabilizationTest, processStabilizationAngleRolledOverAHalfRight)
{
current = {.pitch = 0.F, .roll = 0.F, .yaw = 180.F};

auto [speed, direction] = pid.processStabilizationAngle(target, current);

EXPECT_EQ(speed, halfturn_error_speed);
EXPECT_EQ(direction, Rotation::counterClockwise);
}

TEST_F(PIDStabilizationTest, processStabilizationAngleRolledOverAQuarterRight)
{
current = {.pitch = 0.F, .roll = 0.F, .yaw = 90.F};

auto [speed, direction] = pid.processStabilizationAngle(target, current);

EXPECT_EQ(speed, quarterturn_error_speed);
EXPECT_EQ(direction, Rotation::counterClockwise);
}

TEST_F(PIDStabilizationTest, processStabilizationAngleRolledOverAQuarterLeft)
{
current = {.pitch = 0.F, .roll = 0.F, .yaw = -90.F};

auto [speed, direction] = pid.processStabilizationAngle(target, current);

EXPECT_EQ(speed, quarterturn_error_speed);
EXPECT_EQ(direction, Rotation::clockwise);
}

TEST_F(PIDStabilizationTest, processStabilizationAngleRolledOverAHalfLeft)
{
current = {.pitch = 0.F, .roll = 0.F, .yaw = -180.F};

auto [speed, direction] = pid.processStabilizationAngle(target, current);

EXPECT_EQ(speed, halfturn_error_speed);
EXPECT_EQ(direction, Rotation::clockwise);
}
125 changes: 0 additions & 125 deletions libs/MotionKit/tests/PID_test.cpp

This file was deleted.

8 changes: 2 additions & 6 deletions libs/ReinforcerKit/tests/ReinforcerKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
#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"
#include "stubs/leka/EventLoopKit.h"

using namespace leka;

using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::AtMost;
using ::testing::Sequence;

MATCHER_P(isSameAnimation, expected_animation, "")
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down

0 comments on commit 970fdb2

Please sign in to comment.