Skip to content

Stepper library #6444

Open
Open
@ktritz

Description

@ktritz

Using CircuitPython to control stepper motors is currently pretty ugly. At the moment, I use PWMOut, precalculate the frequency steps needed to provide pseudo acceleration/deceleration, compute the time necessary for the total steps in each PWM window, sit in a timing loop while monitoring a digital input 'limit switch' and end up sorta close to the total number of steps I wanted. I realize that I may get better precision with PulseIO or AudioPWM, but they have their own issues. It would be pretty nifty if there was a C-level bit banging library that implemented a Stepper library with something like the following capability:

Stepper module:

  • C-level generation of precise pulse train (total steps, frequency)
  • frequencies up to ~2-3 kHz * multistep value (e.g. 32)
  • built in smooth frequency acceleration/deceleration of pulse train
  • capable of asynchronous pulse generation
  • monitor "limit switch" input pins to stop motion
  • STEP/target position mode or RUN/continuous motion mode

Pins:

  • step_pin (digital): each pulse advances the stepper one step
  • dir_pin (digital, optional): pin state controls stepper direction
  • enable_pin (digital, optional): pin state enables stepper driver
  • forward_limit (digital, optional): activating pin stops forward motion
  • reverse_limit (digital, optional): activating pin stops reverse motion

Note: ideally select active HIGH/LOW for each pin

Properties/Functions:

acceleration (property) get/set

  • how fast velocity goes from 0 to velocity_limit
  • how fast velocity goes from velocity_limit to 0
  • MIN < value < MAX
  • scaled by scale_factor

mode (property) get/set

  • STEP: target position mode
  • RUN: async rotatation mode

engaged (property) get/set

  • enable stepper driver operation
  • holding current when stopped

failsafe (property) get/set

  • watchdog timer
  • stops operation if reset_failsafe is not called within time

is_moving (function)

  • returns whether the stepper is currently moving
  • either in RUN mode or async STEP mode

position (property) get

  • get the current position (scaled steps)

scale_factor (property) get/set

  • scale factor to translate steps into physical position
  • also affects velocity and acceleration

multistep (property) get/set

  • steps/velocity/acceleration multiplier
  • can modify driver multistep setting without changing Stepper motion values

reset_failsafe (function)

  • if failsafe is active, reset the timeout

target_position (property) get/set

  • gets or sets the (scaled) step location
  • blocks until motion is finished
  • stops motion if direction limit switch becomes active
  • doesn't move if direction limit switch is active

target_position_async (property) set

  • sets the (scaled) step location
  • immediately returns (non-blocking)
  • stops motion if direction limit switch becomes active
  • doesn't move if direction limit switch is active

velocity (property) get

  • get the current stepper (scaled) velocity

velocity_limit (property) get/set

  • get or set the (scaled) maximum velocity
  • 0 < value < MAX
  • immediately changes the velocity during RUN mode
  • stops motion if direction limit switch becomes active
  • doesn't move if direction limit switch is active

set_limits (function)

  • set digital input pins for motion limits
  • limit for forward and/or reverse directions
  • set input value which triggers limit (False | True)
set_limits(forward_limit=None,
		forward_active=True,
		reverse_limit=None,
		reverse_active=True)

get_limits (function)

  • get status of limit switches
  • e.g. returns forward_limit.value == forward_active

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions