-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RSDK-9603] Move enable pin logic into doCycle
to allow SetPower and SetRPM to enable the configured EN pins
#4666
Conversation
func (m *gpioStepper) enable(ctx context.Context, high bool) error { | ||
var err error | ||
if m.enablePinHigh != nil { | ||
err = multierr.Combine(err, m.enablePinHigh.Set(ctx, high, nil)) | ||
} | ||
b, err := board.FromDependencies(deps, motorConfig.BoardName) | ||
if err != nil { | ||
return nil, nil, err | ||
|
||
if m.enablePinLow != nil { | ||
err = multierr.Combine(err, m.enablePinLow.Set(ctx, !high, nil)) | ||
} | ||
return b, motorConfig, nil | ||
|
||
return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved enable up with no changes.
Added a long warning not to use this outside of the constructor, Close and doCycle because we cowboy it and don't lock the pins stored on the motor struct.
@@ -208,8 +204,7 @@ 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 { | |||
if math.Abs(powerPct) <= .0001 { | |||
m.stop() | |||
return nil | |||
return m.Stop(ctx, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This calls Stop in lock.
# codegpt | ||
.codegpt/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove this but I think it's a good addition so people don't commit their codegpt files
@@ -288,7 +284,9 @@ func (m *gpioStepper) doCycle(ctx context.Context) (time.Duration, error) { | |||
func (m *gpioStepper) doStep(ctx context.Context, forward bool) error { | |||
err := multierr.Combine( | |||
m.dirPin.Set(ctx, forward, nil), | |||
m.stepPin.Set(ctx, true, nil)) | |||
m.stepPin.Set(ctx, true, nil), | |||
m.enable(ctx, true), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so as long as the motor thinks it should be moving (m.stepPosition == m.targetStepPosition
), the motor will be enabled.
seems like we might be able to put all of the m.enable calls inside of doCycle
, which might make the code easier to follow, but def not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, also +1 on making this code easier to read and reason about, it's in a state right now. I will follow up.
We do not set EnablePinHigh pins to high in
SetPower
andSetRPM
, new APIs that expanded this driver's capabilities, which is a problem since that will not allow a stepper motor driver with an enable pin that needs to be drawn high, this PR moves the enable logic to thedoStep
helper method so that the pins are set in each cycle for a stepper motor.I have also refactored the tests and the constructor a little. I can change it back if people don't like flatter constructor functions.
Tests conducted:
forwards: rpm+/rev+ and rpm-/rev-
backwards: rpm-/rev+ and rpm-/rev+
Tested with a pi5 board!