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

⚡ (power): CoreMotor - suspend/resume pwm when needed to allow deep sleep #1167

Merged
merged 5 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drivers/CoreMotor/include/CoreMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CoreMotor : public interface::Motor
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
_speed.suspend();
}

void spin(rotation_t rotation, float speed) override;
Expand Down
9 changes: 7 additions & 2 deletions drivers/CoreMotor/source/CoreMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ void CoreMotor::setDirections(int dir_1, int dir_2)

void CoreMotor::setSpeed(float speed)
{
if (speed < 0.0F) {
if (speed <= 0.0F) {
_speed.write(0);
_speed.suspend();
return;
}

} else if (speed > 1.0F) {
if (speed > 1.0F) {
_speed.write(1.0F);

} else {
_speed.write(speed);
}

_speed.resume();
}

} // namespace leka
28 changes: 28 additions & 0 deletions drivers/CoreMotor/tests/CoreMotor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,23 @@ TEST_F(CoreMotorTest, initialization)
ASSERT_NE(&motor, nullptr);
}

TEST_F(CoreMotorTest, suspendOnInitialization)
{
mbed::mock::DigitalOut local_dir_1 = {};
mbed::mock::DigitalOut local_dir_2 = {};
mock::PwmOut local_speed = {};

EXPECT_CALL(local_speed, suspend());

auto local_motor = CoreMotor {local_dir_1, local_dir_2, local_speed};
}

TEST_F(CoreMotorTest, rotateClockwiseNormalSpeed)
{
EXPECT_CALL(dir_1, write(1));
EXPECT_CALL(dir_2, write(0));
EXPECT_CALL(speed, write(0.5));
EXPECT_CALL(speed, resume());

motor.spin(Rotation::clockwise, 0.5);
}
Expand All @@ -55,6 +67,7 @@ TEST_F(CoreMotorTest, rotateClockwiseMaxSpeed)
EXPECT_CALL(dir_1, write(1));
EXPECT_CALL(dir_2, write(0));
EXPECT_CALL(speed, write(1));
EXPECT_CALL(speed, resume());

motor.spin(Rotation::clockwise, 1);
}
Expand All @@ -64,6 +77,7 @@ TEST_F(CoreMotorTest, rotateCounterClockwiseNormalSpeed)
EXPECT_CALL(dir_1, write(0));
EXPECT_CALL(dir_2, write(1));
EXPECT_CALL(speed, write(0.5));
EXPECT_CALL(speed, resume());

motor.spin(Rotation::counterClockwise, 0.5);
}
Expand All @@ -73,6 +87,7 @@ TEST_F(CoreMotorTest, rotateCounterClockwiseMaxSpeed)
EXPECT_CALL(dir_1, write(0));
EXPECT_CALL(dir_2, write(1));
EXPECT_CALL(speed, write(1));
EXPECT_CALL(speed, resume());

motor.spin(Rotation::counterClockwise, 1);
}
Expand All @@ -82,15 +97,27 @@ TEST_F(CoreMotorTest, stop)
EXPECT_CALL(dir_1, write(0));
EXPECT_CALL(dir_2, write(0));
EXPECT_CALL(speed, write(0));
EXPECT_CALL(speed, suspend());

motor.stop();
}

TEST_F(CoreMotorTest, speedValueEqualToZero)
{
MOCK_FUNCTION_silence_digital_write_unexpected_calls();

EXPECT_CALL(speed, write(0));
EXPECT_CALL(speed, suspend());

motor.spin(Rotation::clockwise, 0);
}

TEST_F(CoreMotorTest, speedValueNotGreaterThanOne)
{
MOCK_FUNCTION_silence_digital_write_unexpected_calls();

EXPECT_CALL(speed, write(1));
EXPECT_CALL(speed, resume());

motor.spin(Rotation::clockwise, 100);
}
Expand All @@ -100,6 +127,7 @@ TEST_F(CoreMotorTest, speedValueNotLowerThanZero)
MOCK_FUNCTION_silence_digital_write_unexpected_calls();

EXPECT_CALL(speed, write(0));
EXPECT_CALL(speed, suspend());

motor.spin(Rotation::clockwise, -100);
}
1 change: 1 addition & 0 deletions tests/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/boost_ut)

add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/core_imu)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/deep_sleep_core_buffered_serial)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/deep_sleep_core_motor)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/deep_sleep_core_pwm)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/deep_sleep_log_kit)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/deep_sleep_mbed_hal)
Expand Down
17 changes: 17 additions & 0 deletions tests/functional/tests/deep_sleep_core_motor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Leka - LekaOS
# Copyright 2022 APF France handicap
# SPDX-License-Identifier: Apache-2.0

register_functional_test(
TARGET
functional_ut_deep_sleep_core_motor

INCLUDE_DIRECTORIES

SOURCES
suite_core_motor.cpp

LINK_LIBRARIES
CoreMotor
CorePwm
)
176 changes: 176 additions & 0 deletions tests/functional/tests/deep_sleep_core_motor/suite_core_motor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "CoreMotor.h"
#include "CorePwm.h"
#include "tests/config.h"
#include "tests/utils.h"
#include "tests/utils_sleep.h"

using namespace leka;
using namespace boost::ut;
using namespace std::chrono;
using namespace boost::ut::bdd;

suite suite_core_motor = [] {
scenario("base system check") = [] {
given("motor is not instantiated") = [] {
then("I expect deep sleep TO BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(status.can_deep_sleep);
expect(status.test_check_ok);
};
};
};

scenario("motor initialization") = [] {
given("motor is in default configuration") = [] {
auto dir_1 = mbed::DigitalOut {MOTOR_LEFT_DIRECTION_1};
auto dir_2 = mbed::DigitalOut {MOTOR_LEFT_DIRECTION_2};
auto speed = CorePwm {MOTOR_LEFT_PWM};

auto motor = CoreMotor {dir_1, dir_2, speed};
rtos::ThisThread::sleep_for(5ms);

expect(neq(&motor, nullptr));

when("I do nothing") = [&] {
then("I expect deep sleep TO BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(status.can_deep_sleep);
expect(status.test_check_ok);
};
};
};
};

scenario("motor spin") = [] {
given("motor is in default configuration") = [] {
auto dir_1 = mbed::DigitalOut {MOTOR_LEFT_DIRECTION_1};
auto dir_2 = mbed::DigitalOut {MOTOR_LEFT_DIRECTION_2};
auto speed = CorePwm {MOTOR_LEFT_PWM};

auto motor = CoreMotor {dir_1, dir_2, speed};

expect(neq(&motor, nullptr));

when("I spin the motor at 50%") = [&] {
motor.spin(Rotation::clockwise, 0.5);

then("I expect deep sleep TO NOT BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(not status.can_deep_sleep);
expect(not status.test_check_ok);
};
};

when("I spin the motor at 100%") = [&] {
motor.spin(Rotation::clockwise, 1.0);

then("I expect deep sleep TO NOT BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(not status.can_deep_sleep);
expect(not status.test_check_ok);
};
};

when("I spin the motor at 0%") = [&] {
motor.spin(Rotation::clockwise, 0.0);
rtos::ThisThread::sleep_for(5ms);

then("I expect deep sleep TO BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(status.can_deep_sleep);
expect(status.test_check_ok);
};
};
};
};

scenario("motor spin, stop, spin, stop, spin, spin 0% (stop)") = [] {
given("motor is in default configuration") = [] {
auto dir_1 = mbed::DigitalOut {MOTOR_LEFT_DIRECTION_1};
auto dir_2 = mbed::DigitalOut {MOTOR_LEFT_DIRECTION_2};
auto speed = CorePwm {MOTOR_LEFT_PWM};

auto motor = CoreMotor {dir_1, dir_2, speed};

expect(neq(&motor, nullptr));

when("I spin the motor at 50%") = [&] {
motor.spin(Rotation::clockwise, 0.5);

then("I expect deep sleep TO NOT BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(not status.can_deep_sleep);
expect(not status.test_check_ok);
};
};

when("I stop the motor") = [&] {
motor.stop();
rtos::ThisThread::sleep_for(5ms);

then("I expect deep sleep TO BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(status.can_deep_sleep);
expect(status.test_check_ok);
};
};

when("I spin the motor at 100%") = [&] {
motor.spin(Rotation::clockwise, 1.0);

then("I expect deep sleep TO NOT BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(not status.can_deep_sleep);
expect(not status.test_check_ok);
};
};

when("I stop the motor") = [&] {
motor.stop();
rtos::ThisThread::sleep_for(5ms);

then("I expect deep sleep TO BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(status.can_deep_sleep);
expect(status.test_check_ok);
};
};

when("I spin the motor at 100%") = [&] {
motor.spin(Rotation::clockwise, 1.0);

then("I expect deep sleep TO NOT BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(not status.can_deep_sleep);
expect(not status.test_check_ok);
};
};

when("I spin the motor at 0%") = [&] {
motor.spin(Rotation::clockwise, 0.0);
rtos::ThisThread::sleep_for(5ms);

then("I expect deep sleep TO BE possible") = [] {
auto status = utils::sleep::system_deep_sleep_check();

expect(status.can_deep_sleep);
expect(status.test_check_ok);
};
};
};
};
};