Skip to content

Commit

Permalink
⚡ (power): CoreMotor - suspend/resume pwm when needed to allow deep s…
Browse files Browse the repository at this point in the history
…leep
  • Loading branch information
ladislas committed Nov 28, 2022
1 parent 25fc281 commit e7822a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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
17 changes: 17 additions & 0 deletions drivers/CoreMotor/tests/CoreMotor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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 +56,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 +66,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 +76,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 +86,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 +116,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);
}

0 comments on commit e7822a7

Please sign in to comment.