Package: micropython-stepper
DescriptionA micropython library to use stepper motors in a tidy way.Your steppers should be connected to a stepper driver that is controlled using a step, a dir and optional an enable pin.This library uses one timer per stepper to achive controlled speeds for multiple steppers in a non blocking way.The steppers can be controlled by angle if the number of steps per rotation is supplied.Multiple steppers can share a common dir pin, allowing for N steppers controlled by N+1 output pins.ExamplesTwo steppersfromstepperimportStepperimporttimes1=Stepper(18,19,steps_per_rev=200,speed_sps=50)s2=Stepper(20,21,steps_per_rev=200,speed_sps=50)s1.target_deg(90)s2.target_deg(45)time.sleep(5.0)s1.target_deg(0)s2.target_deg(5)time.sleep(5.0)Calibrate absolute position using an endswitchimportmachineimporttimefromstepperimportSteppers1=Stepper(18,19,steps_per_rev=200)#create an input pin for the end switch (switch connects pin to GND)endswitch=machine.Pin(2,machine.Pin.IN,machine.Pin.PULL_UP)s1.speed(20)#use low speed for the calibrations1.free_run(-1)#move backwardswhileendswitch.value():#wait till the switch is triggeredpasss1.stop()#stop as soon as the switch is triggereds1.overwrite_pos(0)#set position as 0 points1.target(0)#set the target to the same value to avoid unwanted movements1.speed(100)#return to default speeds1.track_target()#start stepper again#calibration finished. Do something else below.s1.target_deg(45)time.sleep(5.0)Two steppers sharing a common dir pinimportmachinefromstepperimportStepperdir_pin=machine.Pin(19,machine.Pin.OUT)s1=Stepper(18,dir_pin,steps_per_rev=200,speed_sps=50)s2=Stepper(20,dir_pin,steps_per_rev=200,speed_sps=50)APIclass Stepper(step_pin,dir_pin,en_pin=None,steps_per_rev=200,speed_sps=10,invert_dir=False,timer_id=-1)step_pin: Pin id or machine.Pin object for the pin connected to the step input of the stepper driverdir_pin: Pin id or machine.Pin object for the pin connected to the direction select input of the stepper driveren_pin: (Optional) None or pin id or machine.Pin object for the pin connected to the enable input of the stepper driversteps_per_rev: Amount of stepper steps that would result in a 360° rotationspeed_sps: Speed in steps per secound (= step frequency in Hz)invert_dir: True if the direction of the stepper should be invertedtimer_id: Id of the timer that should be used. On most boards -1 will construct a new virtual timer (https://docs.micropython.org/en/latest/library/machine.Timer.html)speed(sps)set the speed of the steppersps: Speed in steps per secound (= step frequency in Hz)speed_rps(rps)set the speed of the stepperrps: Speed in rotations per secoundtarget(t)set a target position. The stepper will move towards that positiont: Target in stepstarget_deg(deg)set a target position. The stepper will move towards that positiondeg: Target in degreestarget_rad(rad)set a target position. The stepper will move towards that positionrad: Target in radiansget_pos()returns the current position in stepsget_pos_deg()returns the current position in degreesget_pos_rad()returns the current position in radiansoverwrite_pos(p)overwrites the current position. Used to calibrate the absolute positionp: Current position in stepsoverwrite_pos_deg(deg)overwrites the current position. Used to calibrate the absolute positiondeg: Current position in degreeoverwrite_pos_rad(rad)overwrites the current position. Used to calibrate the absolute positionrad: Current position in radiansstep(d)instantly moves the stepper by a single stepd: Direction of the step (d>0: Forwards, d<0: Backwards, d==0: No movement)free_run(d)moves the stepper continuously until stoppedd: Direction of movement (d>0: Forwards, d<0: Backwards, d==0: Stop)track_target()puts the stepper back in the default mode, where it follows the target position after free_run(d) or stop() was used.stop()stops the timer and all movement (single steps using the step() function are still possible)enable(e)enable or disable the stepper driver using the enable pin. While this stops the movement of the actual hardware, the Stepper class will still act as if the stepper is moving. This can be used for testing. Recalibration of the absolute position might be needed if the stepper was disabled.
-e: True or Falseis_enabled()returns the last status (True or False) of the enable(e) function