-
Notifications
You must be signed in to change notification settings - Fork 131
[APP-16401] add microsteps #6031
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,10 +58,18 @@ type PinConfig struct { | |
|
|
||
| // Config describes the configuration of a motor. | ||
| type Config struct { | ||
| Pins PinConfig `json:"pins"` | ||
| BoardName string `json:"board"` | ||
| StepperDelay int `json:"stepper_delay_usec,omitempty"` // When using stepper motors, the time to remain high | ||
| TicksPerRotation int `json:"ticks_per_rotation"` | ||
| Pins PinConfig `json:"pins"` | ||
| BoardName string `json:"board"` | ||
| // TicksPerRotation is the number of full motor steps per shaft revolution | ||
| TicksPerRotation int `json:"ticks_per_rotation"` | ||
| // Microsteps is the driver's microsteps (1, 2, 4, 8, 16, 32, ...), | ||
| Microsteps int `json:"microsteps,omitempty"` | ||
| // MaxRPM is the motor's maximum shaft speed and is the preferred way to cap | ||
| // the step-pin pulse frequency. Therefore maxFreq = MaxRPM * TicksPerRotation * Microsteps / 60. | ||
| MaxRPM float64 `json:"max_rpm,omitempty"` | ||
| // StepperDelay is the minimum delay between step pulses in microseconds. | ||
| // Deprecated: set MaxRPM instead. Still honored when MaxRPM is unset. | ||
| StepperDelay int `json:"stepper_delay_usec,omitempty"` | ||
| } | ||
|
|
||
| // Validate ensures all parts of the config are valid. | ||
|
|
@@ -79,6 +87,12 @@ func (cfg *Config) Validate(path string) ([]string, []string, error) { | |
| if cfg.Pins.Step == "" { | ||
| return nil, nil, resource.NewConfigValidationFieldRequiredError(path, "step") | ||
| } | ||
| if cfg.Microsteps < 0 { | ||
| return nil, nil, resource.NewConfigValidationError(path, errors.New("microsteps must be >= 0 (0 defaults to 1)")) | ||
| } | ||
| if cfg.MaxRPM < 0 { | ||
| return nil, nil, resource.NewConfigValidationError(path, errors.New("max_rpm must be >= 0")) | ||
| } | ||
| deps = append(deps, cfg.BoardName) | ||
| return deps, nil, nil | ||
| } | ||
|
|
@@ -123,10 +137,12 @@ func newGPIOStepper( | |
| return nil, errors.New("expected ticks_per_rotation in config for motor") | ||
| } | ||
|
|
||
| microsteps := max(mc.Microsteps, 1) | ||
|
nfranczak marked this conversation as resolved.
|
||
|
|
||
| m := &gpioStepper{ | ||
| Named: conf.ResourceName().AsNamed(), | ||
| theBoard: b, | ||
| stepsPerRotation: mc.TicksPerRotation, | ||
| stepsPerRotation: mc.TicksPerRotation * microsteps, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this feels incorrect.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when microstepping the total counts for a rev is stepPerRev*microsteps |
||
| logger: logger, | ||
| opMgr: operation.NewSingleOperationManager(), | ||
| } | ||
|
|
@@ -156,10 +172,30 @@ func newGPIOStepper( | |
| return nil, err | ||
| } | ||
|
|
||
| if mc.StepperDelay > 0 { | ||
| m.minDelay = time.Duration(mc.StepperDelay * int(time.Microsecond)) | ||
| switch { | ||
| case mc.MaxRPM > 0: | ||
| maxFreq := mc.MaxRPM * float64(m.stepsPerRotation) / 60.0 | ||
| m.minDelay = time.Duration(float64(time.Second) / maxFreq) | ||
| if mc.StepperDelay > 0 { | ||
| logger.Warnf( | ||
| "motor (%s) has both max_rpm and stepper_delay_usec set; max_rpm wins. "+ | ||
| "stepper_delay_usec is deprecated, drop it from the config.", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if stepper delay is deprecated why did you give it a value above?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because we technically still use it in the code to calculate requested frequency |
||
| conf.ResourceName().Name, | ||
| ) | ||
| } | ||
| case mc.StepperDelay > 0: | ||
| m.minDelay = time.Duration(mc.StepperDelay) * time.Microsecond | ||
| logger.Warnf( | ||
| "motor (%s) uses deprecated stepper_delay_usec; set max_rpm instead", | ||
| conf.ResourceName().Name, | ||
| ) | ||
| } | ||
|
|
||
| logger.Infof( | ||
| "motor (%s) configured with %d full steps/rev * %d microsteps = %d pulses/rev", | ||
| conf.ResourceName().Name, mc.TicksPerRotation, microsteps, m.stepsPerRotation, | ||
| ) | ||
|
|
||
| err = m.enable(ctx, false) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -247,6 +283,13 @@ func (m *gpioStepper) startPWM(ctx context.Context, forward bool, freqHz uint) ( | |
| actualFreq = float64(freqHz) | ||
| } | ||
|
|
||
| m.logger.Infow("PWM started on step pin", | ||
| "requested_freq_hz", freqHz, | ||
| "confirmed_freq_hz", actualFreq, | ||
| "forward", forward, | ||
| "steps_per_rotation", m.stepsPerRotation, | ||
| ) | ||
|
|
||
| return actualFreq, 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.
also make sure that it's not odd?
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.
we can't because 1 is a valid value