Class for stepper motor driven by Easy Driver

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
vtt
Posts: 6
Joined: Mon Nov 02, 2020 4:34 pm

Class for stepper motor driven by Easy Driver

Post by vtt » Mon Dec 28, 2020 5:12 pm

I use this lib: Stepper.py

Code: Select all

"""Micropython module for stepper motor driven by Easy Driver."""
from machine import Pin
from time import sleep_us


class Stepper:
    """Class for stepper motor driven by Easy Driver."""

    def __init__(self, step_pin, dir_pin, sleep_pin):
        """Initialise stepper."""
        self.stp = step_pin
        self.dir = dir_pin
        self.slp = sleep_pin

        self.stp.init(Pin.OUT)
        self.dir.init(Pin.OUT)
        self.slp.init(Pin.OUT)

        self.step_time = 20  # us
        self.steps_per_rev = 1600
        self.current_position = 0

    def power_on(self):
        """Power on stepper."""
        self.slp.value(1)

    def power_off(self):
        """Power off stepper."""
        self.slp.value(0)
        self.current_position = 0

    def steps(self, step_count):
        """Rotate stepper for given steps."""
        self.dir.value(0 if step_count > 0 else 1)
        for i in range(abs(step_count)):
            self.stp.value(1)
            sleep_us(self.step_time)
            self.stp.value(0)
            sleep_us(self.step_time)
        self.current_position += step_count

    def rel_angle(self, angle):
        """Rotate stepper for given relative angle."""
        steps = int(angle / 360 * self.steps_per_rev)
        self.steps(steps)

    def abs_angle(self, angle):
        """Rotate stepper for given absolute angle since last power on."""
        steps = int(angle / 360 * self.steps_per_rev)
        steps -= self.current_position % self.steps_per_rev
        self.steps(steps)

    def revolution(self, rev_count):
        """Perform given number of full revolutions."""
        self.steps(rev_count * self.steps_per_rev)

    def set_step_time(self, us):
        """Set time in microseconds between each step."""
        if us < 20:  # 20 us is the shortest possible for esp8266
            self.step_time = 20
        else:
            self.step_time = us
With this test file:

Code: Select all

#TB6560_01.py
from Stepper import Stepper

#Stepper variales
#[step_pin, dir_pin, sleep_pin]
testStepper = Stepper(16,17,18)
And get this error message:

Code: Select all

>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
  File "Stepper.py", line 15, in __init__
AttributeError: 'int' object has no attribute 'init'
>>> 
I'm a beginner and it would be great if someone could help me.

User avatar
mathieu
Posts: 50
Joined: Fri Nov 10, 2017 9:57 pm

Re: Class for stepper motor driven by Easy Driver

Post by mathieu » Mon Dec 28, 2020 9:29 pm

Did you try replacing

Code: Select all

self.stp = step_pin
by

Code: Select all

self.stp = machine.Pin(step_pin)
or something like that?

SirN Check
Posts: 9
Joined: Fri Dec 15, 2017 11:11 am

Re: Class for stepper motor driven by Easy Driver

Post by SirN Check » Tue Dec 29, 2020 3:48 am

So, to overcome the first obstacle:

Code: Select all

class Stepper:
    """Class for stepper motor driven by Easy Driver."""

    def __init__(self, step_pin, dir_pin, sleep_pin):
        """Initialise stepper."""
        self.stp = Pin(step_pin, Pin.OUT)
        self.dir = Pin(dir_pin, Pin.OUT)
        self.slp = Pin(sleep_pin, Pin.OUT)
        self.step_time = 20  # us
        self.steps_per_rev = 1600
        self.current_position = 0

    def ...
    
#I tested it on a ESP8266 so I used some valid pins. E.g:
testStepper = Stepper(12,13,14)


vtt
Posts: 6
Joined: Mon Nov 02, 2020 4:34 pm

Re: Class for stepper motor driven by Easy Driver

Post by vtt » Tue Dec 29, 2020 11:33 am

Thank you, now it works.

User avatar
mathieu
Posts: 50
Joined: Fri Nov 10, 2017 9:57 pm

Re: Class for stepper motor driven by Easy Driver

Post by mathieu » Tue Dec 29, 2020 9:53 pm

By the way, I've been playing with the EasyDriver recently (but managed to fry one), as well as this one: https://www.pololu.com/product/2966. Does anybody have an idea of what differences exist between these two options? The second one is cheaper and smaller, what would I be getting by opting for to the EasyDriver?

Post Reply