Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚚 (CoreMotor): Move & rename CoreMotorBase to interface/drivers/Motor.h #757

Merged
merged 8 commits into from
May 6, 2022
1 change: 1 addition & 0 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "CoreLL.h"
#include "CoreLTDC.hpp"
#include "CoreMCU.h"
#include "CoreMotor.h"
#include "CorePwm.h"
#include "CoreQSPI.h"
#include "CoreRFIDReader.h"
Expand Down
14 changes: 6 additions & 8 deletions drivers/CoreMotor/include/CoreMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@

#pragma once

#include "drivers/PwmOut.h"
#include "drivers/interfaces/InterfaceDigitalOut.h"

#include "CoreMotorBase.h"
#include "interface/drivers/DigitalOut.h"
#include "interface/drivers/Motor.h"
#include "interface/drivers/PwmOut.h"

namespace leka {

class CoreMotor : public CoreMotorBase
class CoreMotor : public interface::Motor
{
public:
CoreMotor(mbed::interface::DigitalOut &dir_1, mbed::interface::DigitalOut &dir_2, interface::PwmOut &speed)
CoreMotor(interface::DigitalOut &dir_1, interface::DigitalOut &dir_2, interface::PwmOut &speed)
: _dir_1(dir_1), _dir_2(dir_2), _speed(speed)
{
// nothing do to
Expand All @@ -28,8 +26,8 @@ class CoreMotor : public CoreMotorBase
void setDirections(int dir_1, int dir_2);
void setSpeed(float speed);

mbed::interface::DigitalOut &_dir_1;
mbed::interface::DigitalOut &_dir_2;
interface::DigitalOut &_dir_1;
interface::DigitalOut &_dir_2;
interface::PwmOut &_speed;
};

Expand Down
13 changes: 13 additions & 0 deletions include/interface/drivers/DigitalOut.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Leka - LekaOS
// Copyright 2020 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "drivers/interfaces/InterfaceDigitalOut.h"

namespace leka::interface {

using DigitalOut = mbed::interface::DigitalOut;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ enum class Rotation

using rotation_t = Rotation;

class CoreMotorBase
{
public:
virtual ~CoreMotorBase() = default;
namespace interface {

virtual void spin(rotation_t rotation, float speed) = 0;
virtual void stop() = 0;
};
class Motor

{
public:
virtual ~Motor() = default;

virtual void spin(rotation_t rotation, float speed) = 0;
virtual void stop() = 0;
};

} // namespace interface
} // namespace leka
9 changes: 5 additions & 4 deletions libs/BehaviorKit/include/BehaviorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

#pragma once

#include "CoreMotor.h"
#include "LedKit.h"
#include "interface/drivers/Motor.h"
#include "interface/libs/VideoKit.h"

namespace leka {

class BehaviorKit
{
public:
explicit BehaviorKit(interface::VideoKit &videokit, LedKit &ledkit, CoreMotor &motor_left, CoreMotor &motor_right)
explicit BehaviorKit(interface::VideoKit &videokit, LedKit &ledkit, interface::Motor &motor_left,
interface::Motor &motor_right)
: _videokit(videokit), _ledkit(ledkit), _motor_left(motor_left), _motor_right(motor_right)
{
// nothing do to
Expand Down Expand Up @@ -45,8 +46,8 @@ class BehaviorKit
private:
interface::VideoKit &_videokit;
LedKit &_ledkit;
CoreMotor &_motor_left;
CoreMotor &_motor_right;
interface::Motor &_motor_left;
interface::Motor &_motor_right;
};

} // namespace leka
77 changes: 21 additions & 56 deletions libs/BehaviorKit/tests/BehaviorKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

#include "rtos/tests/UNITTESTS/doubles/Thread_stub.h"

#include "CoreMotor.h"
#include "CorePwm.h"
#include "LedKit.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mocks/leka/CoreLED.h"
#include "mocks/leka/CoreMotor.h"
#include "mocks/leka/LEDAnimation.h"
#include "mocks/leka/PwmOut.h"
#include "mocks/leka/VideoKit.h"
Expand All @@ -25,7 +25,7 @@ using ::testing::InSequence;
class BehaviorKitTest : public ::testing::Test
{
protected:
BehaviorKitTest() : behaviorkit(mock_videokit, ledkit, motor_left, motor_right) {};
BehaviorKitTest() : behaviorkit(mock_videokit, ledkit, mock_motor_left, mock_motor_right) {};

// void SetUp() override {}
// void TearDown() override {}
Expand All @@ -42,16 +42,8 @@ class BehaviorKitTest : public ::testing::Test

mock::LEDAnimation mock_animation {};

mbed::mock::DigitalOut dir_1_left = {};
mbed::mock::DigitalOut dir_2_left = {};
mock::PwmOut speed_left = {};

mbed::mock::DigitalOut dir_1_right = {};
mbed::mock::DigitalOut dir_2_right = {};
mock::PwmOut speed_right = {};

CoreMotor motor_left {dir_1_left, dir_2_left, speed_left};
CoreMotor motor_right {dir_1_right, dir_2_right, speed_right};
mock::CoreMotor mock_motor_left {};
mock::CoreMotor mock_motor_right {};

BehaviorKit behaviorkit;
};
Expand All @@ -66,22 +58,15 @@ TEST_F(BehaviorKitTest, spinBlink)
auto expected_speed = 1;

EXPECT_CALL(mock_videokit, playVideo);

{
InSequence seq;

EXPECT_CALL(dir_1_left, write(0));
EXPECT_CALL(dir_2_left, write(1));
EXPECT_CALL(speed_left, write(expected_speed));
EXPECT_CALL(dir_1_right, write(0));
EXPECT_CALL(dir_2_right, write(1));
EXPECT_CALL(speed_right, write(expected_speed));

EXPECT_CALL(dir_1_left, write(0));
EXPECT_CALL(dir_2_left, write(0));
EXPECT_CALL(speed_left, write(0));
EXPECT_CALL(dir_1_right, write(0));
EXPECT_CALL(dir_2_right, write(0));
EXPECT_CALL(speed_right, write(0));
EXPECT_CALL(mock_motor_left, spin(Rotation::counterClockwise, expected_speed));
EXPECT_CALL(mock_motor_right, spin(Rotation::counterClockwise, expected_speed));

EXPECT_CALL(mock_motor_left, stop());
EXPECT_CALL(mock_motor_right, stop());
}

behaviorkit.spinBlink();
Expand All @@ -95,19 +80,11 @@ TEST_F(BehaviorKitTest, blinkGreen)
{
InSequence seq;

EXPECT_CALL(dir_1_left, write(1));
EXPECT_CALL(dir_2_left, write(0));
EXPECT_CALL(speed_left, write(expected_speed));
EXPECT_CALL(dir_1_right, write(1));
EXPECT_CALL(dir_2_right, write(0));
EXPECT_CALL(speed_right, write(expected_speed));

EXPECT_CALL(dir_1_left, write(0));
EXPECT_CALL(dir_2_left, write(0));
EXPECT_CALL(speed_left, write(0));
EXPECT_CALL(dir_1_right, write(0));
EXPECT_CALL(dir_2_right, write(0));
EXPECT_CALL(speed_right, write(0));
EXPECT_CALL(mock_motor_left, spin(Rotation::clockwise, expected_speed));
EXPECT_CALL(mock_motor_right, spin(Rotation::clockwise, expected_speed));

EXPECT_CALL(mock_motor_left, stop());
EXPECT_CALL(mock_motor_right, stop());
}

behaviorkit.blinkGreen();
Expand All @@ -117,12 +94,8 @@ TEST_F(BehaviorKitTest, spinLeftAnySpeed)
{
auto expected_speed = 0.7;

EXPECT_CALL(dir_1_left, write(1));
EXPECT_CALL(dir_2_left, write(0));
EXPECT_CALL(speed_left, write(expected_speed));
EXPECT_CALL(dir_1_right, write(1));
EXPECT_CALL(dir_2_right, write(0));
EXPECT_CALL(speed_right, write(expected_speed));
EXPECT_CALL(mock_motor_left, spin(Rotation::clockwise, expected_speed));
EXPECT_CALL(mock_motor_right, spin(Rotation::clockwise, expected_speed));

behaviorkit.spinLeft(expected_speed);
}
Expand All @@ -131,12 +104,8 @@ TEST_F(BehaviorKitTest, spinRightAnySpeed)
{
auto expected_speed = 0.3;

EXPECT_CALL(dir_1_left, write(0));
EXPECT_CALL(dir_2_left, write(1));
EXPECT_CALL(speed_left, write(expected_speed));
EXPECT_CALL(dir_1_right, write(0));
EXPECT_CALL(dir_2_right, write(1));
EXPECT_CALL(speed_right, write(expected_speed));
EXPECT_CALL(mock_motor_left, spin(Rotation::counterClockwise, expected_speed));
EXPECT_CALL(mock_motor_right, spin(Rotation::counterClockwise, expected_speed));

behaviorkit.spinRight(expected_speed);
}
Expand All @@ -156,12 +125,8 @@ TEST_F(BehaviorKitTest, stop)

EXPECT_CALL(mock_videokit, stopVideo);
EXPECT_CALL(mock_animation, stop).Times(1);
EXPECT_CALL(dir_1_left, write(0));
EXPECT_CALL(dir_2_left, write(0));
EXPECT_CALL(speed_left, write(0));
EXPECT_CALL(dir_1_right, write(0));
EXPECT_CALL(dir_2_right, write(0));
EXPECT_CALL(speed_right, write(0));
EXPECT_CALL(mock_motor_left, stop());
EXPECT_CALL(mock_motor_right, stop());

behaviorkit.stop();
}
8 changes: 4 additions & 4 deletions libs/CommandKit/include/commands/MotorsCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

#include <span>

#include "CoreMotorBase.h"
#include "Utils.h"
#include "interface/Command.h"
#include "interface/drivers/Motor.h"

namespace leka {

struct MotorsCommand : interface::Command {
MotorsCommand(CoreMotorBase &left, CoreMotorBase &right) : _left(left), _right(right) {}
MotorsCommand(interface::Motor &left, interface::Motor &right) : _left(left), _right(right) {}

auto id() -> uint8_t override { return cmd::id; }

Expand Down Expand Up @@ -69,8 +69,8 @@ struct MotorsCommand : interface::Command {
};

std::array<uint8_t, cmd::size> args {};
CoreMotorBase &_left;
CoreMotorBase &_right;
interface::Motor &_left;
interface::Motor &_right;
};

} // namespace leka
8 changes: 4 additions & 4 deletions libs/HardwareTest/LekaMotors/include/LekaMotors.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
#include "rtos/ThisThread.h"
#include "rtos/Thread.h"

#include "CoreMotorBase.h"
#include "TwoMotorsBase.h"
#include "interface/drivers/Motor.h"

namespace leka {

class Motors : TwoMotorsBase
{
public:
Motors(CoreMotorBase &motor_right, CoreMotorBase &motor_left);
Motors(interface::Motor &motor_right, interface::Motor &motor_left);
~Motors() {};

void move(direction_t direction, uint8_t speed);
void spin(rotation_t rotation, uint8_t speed);
void stop(void);

private:
CoreMotorBase &_motor_right;
CoreMotorBase &_motor_left;
interface::Motor &_motor_right;
interface::Motor &_motor_left;
};

} // namespace leka
2 changes: 2 additions & 0 deletions libs/HardwareTest/LekaMotors/include/TwoMotorsBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#pragma once

#include "interface/drivers/Motor.h"

namespace leka {

enum class Direction
Expand Down
2 changes: 1 addition & 1 deletion libs/HardwareTest/LekaMotors/source/LekaMotors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace leka {

using namespace mbed;

Motors::Motors(CoreMotorBase &motor_right, CoreMotorBase &motor_left)
Motors::Motors(interface::Motor &motor_right, interface::Motor &motor_left)
: _motor_right(motor_right), _motor_left(motor_left)
{
// nothing to do
Expand Down
13 changes: 7 additions & 6 deletions libs/RobotKit/include/RobotController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "BatteryKit.h"
#include "BehaviorKit.h"
#include "CommandKit.h"
#include "CoreMotor.h"
#include "CoreMutex.h"
#include "FileReception.h"
#include "LedKit.h"
Expand All @@ -29,6 +28,7 @@
#include "interface/drivers/Battery.h"
#include "interface/drivers/FirmwareUpdate.h"
#include "interface/drivers/LCD.hpp"
#include "interface/drivers/Motor.h"
#include "interface/drivers/Timeout.h"
#include "interface/libs/VideoKit.h"

Expand All @@ -42,9 +42,10 @@ class RobotController : public interface::RobotController
sm_t state_machine {static_cast<interface::RobotController &>(*this), logger};

explicit RobotController(interface::Timeout &timeout, interface::Battery &battery, SerialNumberKit &serialnumberkit,
interface::FirmwareUpdate &firmware_update, CoreMotor &motor_left, CoreMotor &motor_right,
interface::LED &ears, interface::LED &belt, LedKit &ledkit, interface::LCD &lcd,
interface::VideoKit &videokit, BehaviorKit &behaviorkit, CommandKit &cmdkit)
interface::FirmwareUpdate &firmware_update, interface::Motor &motor_left,
interface::Motor &motor_right, interface::LED &ears, interface::LED &belt, LedKit &ledkit,
interface::LCD &lcd, interface::VideoKit &videokit, BehaviorKit &behaviorkit,
CommandKit &cmdkit)
: _timeout(timeout),
_battery(battery),
_serialnumberkit(serialnumberkit),
Expand Down Expand Up @@ -309,8 +310,8 @@ class RobotController : public interface::RobotController
interface::FirmwareUpdate &_firmware_update;
std::function<void()> _on_update_loaded_callback {};

CoreMotor &_motor_left;
CoreMotor &_motor_right;
interface::Motor &_motor_left;
interface::Motor &_motor_right;
interface::LED &_ears;
interface::LED &_belt;
LedKit &_ledkit;
Expand Down
Loading