Skip to content
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

Merged
merged 8 commits into from
Jan 3, 2025

Conversation

randhid
Copy link
Member

@randhid randhid commented Dec 31, 2024

We do not set EnablePinHigh pins to high in SetPower and SetRPM, 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 the doStep 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:

  • SetRPM with EnLow Pins configured on an A4988 moves the stepper motor forward with positive rpm and backwards with negative rpm.
  • SetRPM without EnLow Pins configured on an A4988 moves the stepper motor forward with positive rpm and backwards with negative rpm.
  • SetPower with EnLow Pins configured moves the stepper motor forward with positive poiwer and backwards with negative power.
  • SetPower without EnLow Pins configured moves the stepper motor forward with positive poiwer and backwards with negative power.
  • GoFor with EnLow pins moves the motor in the following directions with the following rpm/rev combos
    forwards: rpm+/rev+ and rpm-/rev-
    backwards: rpm-/rev+ and rpm-/rev+
  • SetPower, SetRPM, GoFor, GoTo all interrupt each other when buttons are clicked on app. Stop interrupts each API call and halts the motor from moving in each API call.
  • Erroneously setting the EnablePin on the A4988 motor to "High" stops the motor from moving completely - this is expected as is is setting the motor driver to its disabled state and can be moved by hand freely at all times.

Tested with a pi5 board!

@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Dec 31, 2024
@randhid randhid requested a review from JohnN193 December 31, 2024 22:01
Comment on lines +98 to +108
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
Copy link
Member Author

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)
Copy link
Member Author

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.

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
Comment on lines +96 to +97
# codegpt
.codegpt/*
Copy link
Member Author

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

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@@ -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),
Copy link
Member

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.

Copy link
Member Author

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.

@randhid randhid merged commit bb66c37 into viamrobotics:main Jan 3, 2025
16 checks passed
@randhid randhid deleted the gpio-stepper-issues branch January 3, 2025 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants