Skip to content

Commit

Permalink
Move lock down in gpiostepper SetPower (#4690)
Browse files Browse the repository at this point in the history
  • Loading branch information
randhid authored Jan 8, 2025
1 parent f982115 commit 37ef816
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions components/motor/gpiostepper/gpiostepper.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func newGPIOStepper(
type gpioStepper struct {
resource.Named
resource.AlwaysRebuild
resource.TriviallyCloseable

// config
theBoard board.Board
Expand All @@ -201,12 +200,12 @@ type gpioStepper struct {

// SetPower sets the percentage of power the motor should employ between 0-1.
func (m *gpioStepper) SetPower(ctx context.Context, powerPct float64, extra map[string]interface{}) error {
m.lock.Lock()
defer m.lock.Unlock()
if math.Abs(powerPct) <= .0001 {
return m.Stop(ctx, nil)
}

m.lock.Lock()
defer m.lock.Unlock()
if m.minDelay == 0 {
return errors.Errorf(
"if you want to set the power, set 'stepper_delay_usec' in the motor config at "+
Expand Down Expand Up @@ -452,16 +451,11 @@ func (m *gpioStepper) IsMoving(ctx context.Context) (bool, error) {

// Stop turns the power to the motor off immediately, without any gradual step down.
func (m *gpioStepper) Stop(ctx context.Context, extra map[string]interface{}) error {
m.stop()
m.lock.Lock()
defer m.lock.Unlock()
return m.enable(ctx, false)
}

func (m *gpioStepper) stop() {
m.lock.Lock()
defer m.lock.Unlock()
m.targetStepPosition = m.stepPosition

return m.enable(ctx, false)
}

// IsPowered returns whether or not the motor is currently on. It also returns the percent power
Expand Down

0 comments on commit 37ef816

Please sign in to comment.