Skip to content

Commit 7e2fe2f

Browse files
committed
Set motors to idle when speed is 0
This will allow the motors to handle cases where no PWM pin is specified and the motor driver's enable pin is directly wired to VCC
1 parent 49dad75 commit 7e2fe2f

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Smartcar shield
2-
version=7.0.0
2+
version=7.0.1
33
author=Dimitris Platis
44
maintainer=Dimitris Platis <dimitris@plat.is>
55
sentence=Arduino library for controlling the Smartcar platform

src/motor/analog/pwm/BrushedMotor.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ void BrushedMotor::setSpeed(int speed)
4242
mRuntime.setPinState(kForwardPin, kLow);
4343
mRuntime.setPinState(kBackwardPin, kHigh);
4444
}
45-
else
45+
else if (speed > 0)
4646
{
4747
// Set forward direction
4848
mRuntime.setPinState(kForwardPin, kHigh);
4949
mRuntime.setPinState(kBackwardPin, kLow);
5050
}
51+
else
52+
{
53+
// Set idle
54+
mRuntime.setPinState(kForwardPin, kLow);
55+
mRuntime.setPinState(kBackwardPin, kLow);
56+
}
5157

5258
// Get the absolute speed within the [0, 100] range
5359
auto absoluteSpeed = getAbsolute(getConstrain(speed, kMinMotorSpeed, kMaxMotorSpeed));

test/ut/BrushedMotor_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ TEST_F(BrushedMotorTest, setSpeed_WhenPositiveSpeed_WillSetDirectionForward)
7373
mBrushedMotor->setSpeed(50);
7474
}
7575

76+
TEST_F(BrushedMotorTest, setSpeed_WhenZeroSpeed_WillSetMotorsToIdle)
77+
{
78+
EXPECT_CALL(mRuntime, setPinState(kForwardPin, kLow));
79+
EXPECT_CALL(mRuntime, setPinState(kBackwardPin, kLow));
80+
81+
mBrushedMotor->setSpeed(0);
82+
}
83+
7684
TEST_F(BrushedMotorTest, setSpeed_WhenSpeedOverBounds_WillSetMaxSpeedForward)
7785
{
7886
EXPECT_CALL(mRuntime, setPinState(kForwardPin, kHigh));

0 commit comments

Comments
 (0)