From 864a75754a05b676492f2a096c3aa203f9490080 Mon Sep 17 00:00:00 2001 From: Paolo Lamponi Date: Fri, 28 Dec 2018 21:48:19 -0700 Subject: [PATCH 1/4] PWMOut variable frequency not required, uses only one counter --- adafruit_motor/stepper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_motor/stepper.py b/adafruit_motor/stepper.py index ffa78bc..04c2159 100644 --- a/adafruit_motor/stepper.py +++ b/adafruit_motor/stepper.py @@ -73,10 +73,10 @@ class StepperMotor: def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16): self._coil = (ain2, bin1, ain1, bin2) - # set a safe pwm freq for each output + # reject unsafe (low) pwm freq for i in range(4): if self._coil[i].frequency < 1500: - self._coil[i].frequency = 2000 + raise ValueError("PWMOut: 'frequency' must be at least 1500") self._current_microstep = 0 if microsteps < 2: From c5d57771a1d1974d0581b0aaecd9fccd7034a9ac Mon Sep 17 00:00:00 2001 From: Paolo Lamponi Date: Sat, 29 Dec 2018 18:09:47 -0700 Subject: [PATCH 2/4] Updated docstring for StepperMotor class. New 'plain' example (no PCA9685). --- adafruit_motor/stepper.py | 3 +++ examples/stepper_motor.py | 39 ++++++++++--------------------- examples/stepper_motor_pca9685.py | 39 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 examples/stepper_motor_pca9685.py diff --git a/adafruit_motor/stepper.py b/adafruit_motor/stepper.py index 04c2159..7babf84 100644 --- a/adafruit_motor/stepper.py +++ b/adafruit_motor/stepper.py @@ -69,6 +69,9 @@ class StepperMotor: :param ~pulseio.PWMOut bin2: `pulseio.PWMOut`-compatible output connected to the driver for the fourth coil (unipolar) or second input to second coil (bipolar). :param int microsteps: Number of microsteps between full steps. Must be at least 2 and even. + + .. note:: The ``StepperMotor`` class requires PWM frequencies > 1500. Specify + ``frequency=1500`` or higher when instantiating the above ``PWMOut`` objects. """ def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16): self._coil = (ain2, bin1, ain1, bin2) diff --git a/examples/stepper_motor.py b/examples/stepper_motor.py index 39020af..44f8e89 100644 --- a/examples/stepper_motor.py +++ b/examples/stepper_motor.py @@ -1,39 +1,24 @@ -# This example uses an Adafruit Stepper and DC Motor FeatherWing to run a Stepper Motor. -# https://www.adafruit.com/product/2927 +# Run a Stepper Motor. Tested on ItsyBitsy M4 Express + DRV8833. +# https://www.adafruit.com/product/3800 +# https://www.adafruit.com/product/3297 import time -from board import SCL, SDA -import busio - -# Import the PCA9685 module. Available in the bundle and here: -# https://github.com/adafruit/Adafruit_CircuitPython_PCA9685 -from adafruit_pca9685 import PCA9685 - +import board +import pulseio from adafruit_motor import stepper -i2c = busio.I2C(SCL, SDA) - -# Create a simple PCA9685 class instance for the Motor FeatherWing's default address. -pca = PCA9685(i2c, address=0x60) -pca.frequency = 1600 +AIn1 = pulseio.PWMOut(board.D9, frequency=1600) +AIn2 = pulseio.PWMOut(board.D10, frequency=1600) +BIn1 = pulseio.PWMOut(board.D11, frequency=1600) +BIn2 = pulseio.PWMOut(board.D12, frequency=1600) -# Motor 1 is channels 9 and 10 with 8 held high. -# Motor 2 is channels 11 and 12 with 13 held high. -# Motor 3 is channels 3 and 4 with 2 held high. -# Motor 4 is channels 5 and 6 with 7 held high. +stepper_motor = stepper.StepperMotor(AIn1, AIn2, BIn1, BIn2) -pca.channels[7].duty_cycle = 0xffff -pca.channels[2].duty_cycle = 0xffff -stepper_motor = stepper.StepperMotor(pca.channels[4], pca.channels[3], # Motor 3 - pca.channels[5], pca.channels[6]) # Motor 4 - -for i in range(100): +for i in range(1000): stepper_motor.onestep() time.sleep(0.01) -for i in range(100): +for i in range(1000): stepper_motor.onestep(direction=stepper.BACKWARD) time.sleep(0.01) - -pca.deinit() diff --git a/examples/stepper_motor_pca9685.py b/examples/stepper_motor_pca9685.py new file mode 100644 index 0000000..39020af --- /dev/null +++ b/examples/stepper_motor_pca9685.py @@ -0,0 +1,39 @@ +# This example uses an Adafruit Stepper and DC Motor FeatherWing to run a Stepper Motor. +# https://www.adafruit.com/product/2927 + +import time + +from board import SCL, SDA +import busio + +# Import the PCA9685 module. Available in the bundle and here: +# https://github.com/adafruit/Adafruit_CircuitPython_PCA9685 +from adafruit_pca9685 import PCA9685 + +from adafruit_motor import stepper + +i2c = busio.I2C(SCL, SDA) + +# Create a simple PCA9685 class instance for the Motor FeatherWing's default address. +pca = PCA9685(i2c, address=0x60) +pca.frequency = 1600 + +# Motor 1 is channels 9 and 10 with 8 held high. +# Motor 2 is channels 11 and 12 with 13 held high. +# Motor 3 is channels 3 and 4 with 2 held high. +# Motor 4 is channels 5 and 6 with 7 held high. + +pca.channels[7].duty_cycle = 0xffff +pca.channels[2].duty_cycle = 0xffff +stepper_motor = stepper.StepperMotor(pca.channels[4], pca.channels[3], # Motor 3 + pca.channels[5], pca.channels[6]) # Motor 4 + +for i in range(100): + stepper_motor.onestep() + time.sleep(0.01) + +for i in range(100): + stepper_motor.onestep(direction=stepper.BACKWARD) + time.sleep(0.01) + +pca.deinit() From 369dc7fb8b68c5b07bd49a0640c114a786b73a96 Mon Sep 17 00:00:00 2001 From: Paolo Lamponi Date: Sat, 29 Dec 2018 18:16:50 -0700 Subject: [PATCH 3/4] Fixed bad-whitespace. --- examples/stepper_motor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/stepper_motor.py b/examples/stepper_motor.py index 44f8e89..07e92fe 100644 --- a/examples/stepper_motor.py +++ b/examples/stepper_motor.py @@ -8,7 +8,7 @@ import pulseio from adafruit_motor import stepper -AIn1 = pulseio.PWMOut(board.D9, frequency=1600) +AIn1 = pulseio.PWMOut(board.D9, frequency=1600) AIn2 = pulseio.PWMOut(board.D10, frequency=1600) BIn1 = pulseio.PWMOut(board.D11, frequency=1600) BIn2 = pulseio.PWMOut(board.D12, frequency=1600) From f7fb4d856f7674babd2f28222b401ebb43681f0e Mon Sep 17 00:00:00 2001 From: Paolo Lamponi Date: Sat, 29 Dec 2018 18:21:15 -0700 Subject: [PATCH 4/4] Fixed bad-whitespace. --- examples/stepper_motor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/stepper_motor.py b/examples/stepper_motor.py index 07e92fe..e56f5f0 100644 --- a/examples/stepper_motor.py +++ b/examples/stepper_motor.py @@ -1,5 +1,5 @@ # Run a Stepper Motor. Tested on ItsyBitsy M4 Express + DRV8833. -# https://www.adafruit.com/product/3800 +# https://www.adafruit.com/product/3800 # https://www.adafruit.com/product/3297 import time