Skip to content

Commit

Permalink
RSDK-2357 FIX: only log maxrpm warning when maxrpm > 0 (#2342)
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston authored May 10, 2023
1 parent a7318eb commit cdc4e60
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 42 deletions.
8 changes: 5 additions & 3 deletions components/motor/dimensionengineering/sabertooth.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func NewMotor(ctx context.Context, c *Config, name resource.Name, logger golog.L
dirFlip: c.DirectionFlip,
minPowerPct: c.MinPowerPct,
maxPowerPct: c.MaxPowerPct,
maxRPM: c.MaxRPM,
}

if err := m.configure(c); err != nil {
Expand Down Expand Up @@ -345,9 +346,10 @@ func (m *Motor) SetPower(ctx context.Context, powerPct float64, extra map[string
rawSpeed := powerPct * maxSpeed
switch speed := math.Abs(rawSpeed); {
case speed < 0.1:
m.c.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
case speed > m.maxRPM-0.1:
m.c.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.maxRPM)
m.c.logger.Warn("motor speed is nearly 0 rev_per_min")
case m.maxRPM > 0 && speed > m.maxRPM-0.1:
m.c.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxRPM)
default:
}
if math.Signbit(rawSpeed) {
rawSpeed *= -1
Expand Down
12 changes: 12 additions & 0 deletions components/motor/dimensionengineering/sabertooth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestSabertoothMotor(t *testing.T) {
TestChan: c,
SerialAddress: 128,
DirectionFlip: false,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand Down Expand Up @@ -102,6 +103,7 @@ func TestSabertoothMotor(t *testing.T) {
TestChan: c,
SerialAddress: 128,
DirectionFlip: false,
MaxRPM: 1,
}

m2, err := motorReg.Constructor(context.Background(), deps, resource.Config{Name: "motor2", ConvertedAttributes: &mc2}, logger)
Expand Down Expand Up @@ -167,6 +169,7 @@ func TestSabertoothMotorDirectionFlip(t *testing.T) {
TestChan: c,
SerialAddress: 128,
DirectionFlip: true,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand Down Expand Up @@ -222,6 +225,7 @@ func TestSabertoothMotorDirectionFlip(t *testing.T) {
TestChan: c,
SerialAddress: 128,
DirectionFlip: true,
MaxRPM: 1,
}

m2, err := motorReg.Constructor(context.Background(), deps, resource.Config{Name: "motor2", ConvertedAttributes: &mc2}, logger)
Expand Down Expand Up @@ -286,6 +290,7 @@ func TestSabertoothRampConfig(t *testing.T) {
TestChan: c,
SerialAddress: 128,
RampValue: 100,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand Down Expand Up @@ -318,6 +323,7 @@ func TestSabertoothAddressMapping(t *testing.T) {
MotorChannel: 1,
TestChan: c,
SerialAddress: 129,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand All @@ -342,6 +348,7 @@ func TestInvalidMotorChannel(t *testing.T) {
MotorChannel: 3,
TestChan: c,
SerialAddress: 129,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand All @@ -364,6 +371,7 @@ func TestInvalidBaudRate(t *testing.T) {
TestChan: c,
SerialAddress: 129,
BaudRate: 1,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand All @@ -385,6 +393,7 @@ func TestInvalidSerialAddress(t *testing.T) {
MotorChannel: 1,
TestChan: c,
SerialAddress: 140,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand All @@ -408,6 +417,7 @@ func TestInvalidMinPowerPct(t *testing.T) {
SerialAddress: 129,
MinPowerPct: 0.7,
MaxPowerPct: 0.5,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand All @@ -431,6 +441,7 @@ func TestInvalidMaxPowerPct(t *testing.T) {
SerialAddress: 129,
MinPowerPct: 0.7,
MaxPowerPct: 1.5,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand All @@ -455,6 +466,7 @@ func TestMultipleInvalidParameters(t *testing.T) {
SerialAddress: 140,
MinPowerPct: 1.7,
MaxPowerPct: 1.5,
MaxRPM: 1,
}

motorReg, ok := resource.LookupRegistration(motor.API, sabertoothModel)
Expand Down
37 changes: 20 additions & 17 deletions components/motor/dmc4000/dmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Motor struct {
c *controller
Axis string
TicksPerRotation int
MaxRPM float64
maxRPM float64
MaxAcceleration float64
HomeRPM float64
jogging bool
Expand Down Expand Up @@ -149,22 +149,22 @@ func NewMotor(ctx context.Context, c *Config, name resource.Name, logger golog.L
c: ctrl,
Axis: c.Axis,
TicksPerRotation: c.TicksPerRotation,
MaxRPM: c.MaxRPM,
maxRPM: c.MaxRPM,
MaxAcceleration: c.MaxAcceleration,
HomeRPM: c.HomeRPM,
powerPct: 0.0,
}

if m.MaxRPM <= 0 {
m.MaxRPM = 1000 // arbitrary high value
if m.maxRPM <= 0 {
m.maxRPM = 1000 // arbitrary high value
}

if m.MaxAcceleration <= 0 {
m.MaxAcceleration = 1000 // rpm/s, arbitrary/safe value (1s to max)
}

if m.HomeRPM <= 0 {
m.HomeRPM = m.MaxRPM / 4
m.HomeRPM = m.maxRPM / 4
}

if err := m.configure(c); err != nil {
Expand Down Expand Up @@ -395,8 +395,8 @@ func (c *controller) sendCmd(cmd string) (string, error) {
// Convert rpm to DMC4000 counts/sec.
func (m *Motor) rpmToV(rpm float64) int {
rpm = math.Abs(rpm)
if rpm > m.MaxRPM {
rpm = m.MaxRPM
if rpm > m.maxRPM {
rpm = m.maxRPM
}
speed := rpm * float64(m.TicksPerRotation) / 60

Expand Down Expand Up @@ -445,14 +445,15 @@ func (m *Motor) SetPower(ctx context.Context, powerPct float64, extra map[string

switch pow := math.Abs(powerPct); {
case pow < 0.1:
m.c.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.c.logger.Warn("motor speed is nearly 0 rev_per_min")
return m.Stop(ctx, extra)
case pow*m.MaxRPM > m.MaxRPM-0.1:
m.c.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.MaxRPM)
case m.maxRPM > 0 && pow*m.maxRPM > m.maxRPM-0.1:
m.c.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxRPM)
default:
}

m.powerPct = powerPct
return m.Jog(ctx, powerPct*m.MaxRPM)
return m.Jog(ctx, powerPct*m.maxRPM)
}

// Jog moves indefinitely at the specified RPM.
Expand Down Expand Up @@ -497,10 +498,11 @@ func (m *Motor) stopJog() error {
func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error {
switch speed := math.Abs(rpm); {
case speed < 0.1:
m.c.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.c.logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
case speed > m.MaxRPM-0.1:
m.c.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.MaxRPM)
case m.maxRPM > 0 && speed > m.maxRPM-0.1:
m.c.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxRPM)
default:
}
ctx, done := m.opMgr.New(ctx)
defer done()
Expand Down Expand Up @@ -786,9 +788,10 @@ func (m *Motor) doGoTo(rpm, position float64) error {

switch speed := math.Abs(rpm); {
case speed < 0.1:
m.c.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
case speed > m.MaxRPM-0.1:
m.c.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.MaxRPM)
m.c.logger.Warn("motor speed is nearly 0 rev_per_min")
case m.maxRPM > 0 && speed > m.maxRPM-0.1:
m.c.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxRPM)
default:
}

// Speed
Expand Down
6 changes: 4 additions & 2 deletions components/motor/fake/motor.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[s
case speed < 0.1:
m.Logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
case speed > m.MaxRPM-0.1:
case m.MaxRPM > 0 && speed > m.MaxRPM-0.1:
m.Logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.MaxRPM)
default:
}

powerPct, waitDur, dir := goForMath(m.MaxRPM, rpm, revolutions)
Expand Down Expand Up @@ -308,8 +309,9 @@ func (m *Motor) GoTo(ctx context.Context, rpm, pos float64, extra map[string]int
switch speed := math.Abs(rpm); {
case speed < 0.1:
m.Logger.Warn("motor speed is nearly 0 rev_per_min")
case speed > m.MaxRPM-0.1:
case m.MaxRPM > 0 && speed > m.MaxRPM-0.1:
m.Logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.MaxRPM)
default:
}

curPos, err := m.Position(ctx, nil)
Expand Down
7 changes: 4 additions & 3 deletions components/motor/gpio/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,11 @@ func (m *Motor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[s

switch speed := math.Abs(rpm); {
case speed < 0.1:
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
case speed > m.maxRPM-0.1:
m.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.maxRPM)
case m.maxRPM > 0 && speed > m.maxRPM-0.1:
m.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxRPM)
default:
}

powerPct, waitDur := goForMath(m.maxRPM, rpm, revolutions)
Expand Down
7 changes: 4 additions & 3 deletions components/motor/gpio/motor_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,11 @@ func (m *EncodedMotor) goForInternal(ctx context.Context, rpm, revolutions float

switch speed := math.Abs(rpm); {
case speed < 0.1:
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
case speed > m.cfg.MaxRPM-0.1:
m.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.cfg.MaxRPM)
case m.cfg.MaxRPM > 0 && speed > m.cfg.MaxRPM-0.1:
m.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.cfg.MaxRPM)
default:
}

m.stateMu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion components/motor/gpiostepper/gpiostepper.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (m *gpioStepper) goForInternal(ctx context.Context, rpm, revolutions float6

speed := math.Abs(rpm)
if speed < 0.1 {
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.logger.Warn("motor speed is nearly 0 rev_per_min")
return m.Stop(ctx, nil)
}

Expand Down
7 changes: 4 additions & 3 deletions components/motor/i2cmotors/ezopmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,11 @@ func (m *Ezopmp) SetPower(ctx context.Context, powerPct float64, extra map[strin
func (m *Ezopmp) GoFor(ctx context.Context, mLPerMin, mins float64, extra map[string]interface{}) error {
switch speed := math.Abs(mLPerMin); {
case speed < 0.1:
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
case speed > m.maxFlowRate-0.1:
m.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.maxFlowRate)
case m.maxFlowRate > 0 && speed > m.maxFlowRate-0.1:
m.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxFlowRate)
default:
}

ctx, done := m.opMgr.New(ctx)
Expand Down
2 changes: 1 addition & 1 deletion components/motor/roboclaw/roboclaw.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (m *roboclawMotor) SetPower(ctx context.Context, powerPct float64, extra ma
func (m *roboclawMotor) GoFor(ctx context.Context, rpm, revolutions float64, extra map[string]interface{}) error {
speed := math.Abs(rpm)
if speed < 0.1 {
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
}

Expand Down
16 changes: 9 additions & 7 deletions components/motor/tmcstepper/stepper_motor_tmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,10 @@ func (m *Motor) doJog(ctx context.Context, rpm float64) error {

switch speed0 := math.Abs(rpm); {
case speed0 < 0.1:
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
case speed0 > m.maxRPM:
m.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.maxRPM)
m.logger.Warn("motor speed is nearly 0 rev_per_min")
case m.maxRPM > 0 && speed0 > m.maxRPM:
m.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxRPM)
default:
}

speed := m.rpmToV(math.Abs(rpm))
Expand All @@ -437,7 +438,7 @@ func (m *Motor) doJog(ctx context.Context, rpm float64) error {
// Note: if both are negative the motor will spin in the forward direction.
func (m *Motor) GoFor(ctx context.Context, rpm, rotations float64, extra map[string]interface{}) error {
if math.Abs(rpm) < 0.1 {
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
}

Expand Down Expand Up @@ -488,9 +489,10 @@ func (m *Motor) GoTo(ctx context.Context, rpm, positionRevolutions float64, extr

switch speed := math.Abs(rpm); {
case speed < 0.1:
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
case speed > m.maxRPM-0.1:
m.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), m.maxRPM)
m.logger.Warn("motor speed is nearly 0 rev_per_min")
case m.maxRPM > 0 && speed > m.maxRPM-0.1:
m.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", m.maxRPM)
default:
}

err := multierr.Combine(
Expand Down
5 changes: 3 additions & 2 deletions components/motor/ulnstepper/28byj-48.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,12 @@ func (m *uln28byj) GoFor(ctx context.Context, rpm, revolutions float64, extra ma

switch speed := math.Abs(rpm); {
case speed < 0.1:
m.logger.Warnf("motor (%s) speed is nearly 0 rev_per_min", m.Name())
m.logger.Warn("motor speed is nearly 0 rev_per_min")
return motor.NewZeroRPMError()
case speed > 146-0.1:
m.logger.Warnf("motor (%s) speed is nearly the max rev_per_min (%f)", m.Name(), 146)
m.logger.Warnf("motor speed is nearly the max rev_per_min (%f)", 146)
return m.Stop(ctx, nil)
default:
}

m.lock.Lock()
Expand Down

0 comments on commit cdc4e60

Please sign in to comment.