Skip to content
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

Add A4988 stepper motor driver #932

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions micropython/drivers/a4988/a4988.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import time

# Constants
STEPS_PER_REV = 200 # Number of steps per motor revolution
MIN_DELAY = 0.001 # Minimum delay between steps (controls maximum speed)

class A4988:
def __init__(self, step_pin, dir_pin, en_pin=None):
self.step_pin = step_pin
self.dir_pin = dir_pin
self.en_pin = en_pin
if self.en_pin is not None:
self.enable_motor(enabled=True)

def enable_motor(self, enabled=True):
"""Enable or disable the motor driver (active low)."""
if self.en_pin:
self.en_pin.value(0 if enabled else 1)

def step_motor(self, steps=STEPS_PER_REV, direction=1, delay=0.005):
"""Move the motor a specified number of steps in the given direction."""
self.dir_pin.value(direction) # Set direction (1 = forward, 0 = backward)
for _ in range(steps):
self.step_pin.value(1)
time.sleep(delay)
self.step_pin.value(0)
time.sleep(delay)

def calculate_speed(self, current_delay):
"""Calculate the current speed in steps per second and RPM."""
if current_delay <= 0:
return 0, 0
steps_per_second = 1 / (2 * current_delay) # Delay is for each half-step
rpm = (steps_per_second / STEPS_PER_REV) * 60 # Convert to RPM
return steps_per_second, rpm

def inc_speed(self, current_delay):
"""Decrease delay to increase speed, respecting minimum delay limit."""
new_delay = max(current_delay - 0.001, MIN_DELAY)
return new_delay

def dec_speed(self, current_delay):
"""Increase delay to decrease speed."""
new_delay = current_delay + 0.001
return new_delay

def forward_motion(self, steps=STEPS_PER_REV, delay=0.005):
"""Move motor forward for a given number of steps and delay."""
self.step_motor(steps=steps, direction=1, delay=delay)

def backward_motion(self, steps=STEPS_PER_REV, delay=0.005):
"""Move motor backward for a given number of steps and delay."""
self.step_motor(steps=steps, direction=0, delay=delay)
1 change: 1 addition & 0 deletions micropython/drivers/a4988/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
metadata(description="MicroPython driver for A4988 stepper motor controller", version="1.0.0")
Loading