diff --git a/components/motor/gpio/basic.go b/components/motor/gpio/basic.go index 178fdd07c25..66951e504f0 100644 --- a/components/motor/gpio/basic.go +++ b/components/motor/gpio/basic.go @@ -177,8 +177,13 @@ func (m *Motor) setPWM(ctx context.Context, powerPct float64, extra map[string]i var errs error powerPct = math.Min(powerPct, m.maxPowerPct) powerPct = math.Max(powerPct, -1*m.maxPowerPct) + if math.Abs(powerPct) < m.minPowerPct { + powerPct = sign(powerPct) * m.minPowerPct + } + m.powerPct = powerPct m.on = true + if m.EnablePinLow != nil { errs = multierr.Combine(errs, m.EnablePinLow.Set(ctx, false, extra)) } @@ -214,7 +219,6 @@ func (m *Motor) setPWM(ctx context.Context, powerPct float64, extra map[string]i } powerPct = math.Max(math.Abs(powerPct), m.minPowerPct) - m.powerPct = powerPct return multierr.Combine( errs, pwmPin.SetPWMFreq(ctx, m.pwmFreq, extra), diff --git a/components/motor/gpio/basic_test.go b/components/motor/gpio/basic_test.go index 6abb4b83b6c..d523cde0765 100644 --- a/components/motor/gpio/basic_test.go +++ b/components/motor/gpio/basic_test.go @@ -95,7 +95,7 @@ func TestMotorABPWM(t *testing.T) { on, powerPct, err = m.IsPowered(ctx, nil) test.That(t, err, test.ShouldBeNil) test.That(t, on, test.ShouldBeTrue) - test.That(t, powerPct, test.ShouldEqual, 0.44) + test.That(t, powerPct, test.ShouldEqual, -0.44) test.That(t, m.SetPower(ctx, 0, nil), test.ShouldBeNil) test.That(t, mustGetGPIOPinByName(b, "1").Get(context.Background()), test.ShouldEqual, false)