Skip to content

Commit

Permalink
Allow IsPowered to return negative power percentages. (viamrobotics#3703
Browse files Browse the repository at this point in the history
)
  • Loading branch information
randhid authored Mar 19, 2024
1 parent 0b706b1 commit c6ba80a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion components/motor/gpio/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion components/motor/gpio/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c6ba80a

Please sign in to comment.