Skip to content

Commit

Permalink
add revolutions check
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston committed Sep 12, 2024
1 parent e9def7b commit 4de6e6e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 2 deletions.
4 changes: 4 additions & 0 deletions components/motor/dimensionengineering/sabertooth.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[s
return motor.NewZeroRPMError()
}

if err := motor.CheckRevolutions(revolutions); err != nil {
return err
}

powerPct, waitDur := goForMath(m.maxRPM, rpm, revolutions)
err := m.SetPower(ctx, powerPct, extra)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions components/motor/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func NewZeroRPMError() error {
return errors.New("Cannot move motor at an RPM that is nearly 0")
}

// NewZeroRPMError returns an error representing a request to move a motor at
// zero speed (i.e., moving the motor without moving the motor).
func NewZeroRevsError() error {
return errors.New("Cannot move motor for 0 revolutions")
}

// NewGoToUnsupportedError returns error when a motor is required to support GoTo feature.
func NewGoToUnsupportedError(motorName string) error {
return errors.Errorf("motor with name %s does not support GoTo", motorName)
Expand Down
10 changes: 8 additions & 2 deletions components/motor/fake/motor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
)

const defaultMaxRpm = 100
const defaultNonZeroRevs = -1

// PinConfig defines the mapping of where motor are wired.
type PinConfig struct {
Expand Down Expand Up @@ -300,6 +301,10 @@ func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[s
return err
}

if err := motor.CheckRevolutions(revolutions); err != nil {
return err
}

powerPct, waitDur, dir := goForMath(m.MaxRPM, rpm, revolutions)

var finalPos float64
Expand Down Expand Up @@ -347,8 +352,9 @@ func (m *Motor) GoTo(ctx context.Context, rpm, pos float64, extra map[string]int
if err != nil {
return err
}
if curPos == pos {
return nil

if err := motor.CheckRevolutions(pos - curPos); err != nil {
return err
}

revolutions := pos - curPos
Expand Down
4 changes: 4 additions & 0 deletions components/motor/gpio/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[s
return err
}

if err := motor.CheckRevolutions(revolutions); err != nil {
return err
}

powerPct, waitDur := goForMath(m.maxRPM, rpm, revolutions)
err = m.SetPower(ctx, powerPct, extra)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions components/motor/gpio/controlled.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ func (cm *controlledMotor) GoFor(ctx context.Context, rpm, revolutions float64,
return err
}

if err := motor.CheckRevolutions(revolutions); err != nil {
return err
}

currentTicks, _, err := cm.enc.Position(ctx, encoder.PositionTypeTicks, extra)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions components/motor/gpio/encoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ func (m *EncodedMotor) GoFor(ctx context.Context, rpm, revolutions float64, extr
return err
}

if err := motor.CheckRevolutions(revolutions); err != nil {
return err
}

goalPos, goalRPM, direction := encodedGoForMath(rpm, revolutions, currentTicks, m.ticksPerRotation)

if err := m.goForInternal(goalRPM, goalPos, direction); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions components/motor/gpiostepper/gpiostepper.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ func (m *gpioStepper) goForInternal(ctx context.Context, rpm, revolutions float6
return m.Stop(ctx, nil)
}

if err := motor.CheckRevolutions(revolutions); err != nil {
return err
}

var d int64 = 1
if math.Signbit(revolutions) != math.Signbit(rpm) {
d = -1
Expand Down
8 changes: 8 additions & 0 deletions components/motor/motor.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,11 @@ func CheckSpeed(rpm, max float64) (string, error) {
return "", nil
}
}

// CheckRevolutions checks if the input revolutions is non-zero
func CheckRevolutions(revs float64) error {
if revs == 0 {
return NewZeroRevsError()
}
return nil
}
4 changes: 4 additions & 0 deletions components/motor/roboclaw/roboclaw.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ func (m *roboclawMotor) GoFor(ctx context.Context, rpm, revolutions float64, ext
return err
}

if err := motor.CheckRevolutions(revolutions); err != nil {
return err
}

// If no encoders present, distance traveled is estimated based on max RPM.
if m.conf.TicksPerRotation == 0 {
if math.Abs(rpm) > maxRPM {
Expand Down

0 comments on commit 4de6e6e

Please sign in to comment.