diff --git a/micropython/drivers/a4988/a4988.py b/micropython/drivers/a4988/a4988.py new file mode 100644 index 000000000..7bae5371f --- /dev/null +++ b/micropython/drivers/a4988/a4988.py @@ -0,0 +1,51 @@ +import time + +# Constants +STEPS_PER_REV = 200 # Number of steps per motor revolution +MIN_DELAY = 0.001 # Minimum delay between steps (controls maximum speed) + +# Function to enable or disable the motor driver (active low) +def enable_motor(EN_PIN, enabled=True): + """Enable or disable the motor driver.""" + EN_PIN.value(0 if enabled else 1) + +# Function to move the motor a specific number of steps +def step_motor(STEP_PIN, DIR_PIN, steps=STEPS_PER_REV, direction=1, delay=0.005): + """Move the motor a specified number of steps in the given direction.""" + DIR_PIN.value(direction) # Set direction (1 = forward, 0 = backward) + for _ in range(steps): + STEP_PIN.value(1) + time.sleep(delay) + STEP_PIN.value(0) + time.sleep(delay) + +# Function to calculate current speed in steps per second and RPM +def calculate_speed(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 + +# Function to increase motor speed by reducing delay +def inc_speed(current_delay): + """Decrease delay to increase speed, respecting minimum delay limit.""" + new_delay = max(current_delay - 0.001, MIN_DELAY) + return new_delay + +# Function to decrease motor speed by increasing delay +def dec_speed(current_delay): + """Increase delay to decrease speed.""" + new_delay = current_delay + 0.001 + return new_delay + +# Function to move motor forward +def forward_motion(STEP_PIN, DIR_PIN, steps=STEPS_PER_REV, delay=0.005): + """Move motor forward for a given number of steps and delay.""" + step_motor(STEP_PIN, DIR_PIN, steps, direction=1, delay=delay) + +# Function to move motor backward +def backward_motion(STEP_PIN, DIR_PIN, steps=STEPS_PER_REV, delay=0.005): + """Move motor backward for a given number of steps and delay.""" + step_motor(STEP_PIN, DIR_PIN, steps, direction=0, delay=delay) diff --git a/micropython/drivers/a4988/a4988_test.py b/micropython/drivers/a4988/a4988_test.py new file mode 100644 index 000000000..38889a05d --- /dev/null +++ b/micropython/drivers/a4988/a4988_test.py @@ -0,0 +1,46 @@ +from machine import Pin +import time +import a4988 # Import functions from the A4988 motor driver module + +# Define motor control pins +DIR_PIN = Pin(12, Pin.OUT) # Direction pin +STEP_PIN = Pin(14, Pin.OUT) # Step pin (controls motor steps) +EN_PIN = Pin(21, Pin.OUT) # Enable pin for motor driver + +# Define pins for switches +switch_forward = Pin(26, Pin.IN) # Forward motion switch +switch_backward = Pin(27, Pin.IN) # Backward motion switch +speed_increase = Pin(33, Pin.IN) # Speed increase switch +speed_decrease = Pin(32, Pin.IN) # Speed decrease switch + +# Initialize motor driver +a4988.enable_motor(EN_PIN, enabled=True) + +# Set initial motor speed and debounce delay +current_delay = 0.005 # Initial delay between steps for motor speed +prev_speed_inc_state = 0 +prev_speed_dec_state = 0 +debounce_delay = 0.1 # Delay to avoid switch bouncing effects + +# Main control loop +while True: + # Motor directional control + if switch_forward.value() == 1: + a4988.forward_motion(STEP_PIN, DIR_PIN, a4988.Steps, delay=current_delay) + elif switch_backward.value() == 1: + a4988.backward_motion(STEP_PIN, DIR_PIN, a4988.Steps, delay=current_delay) + + # Speed increase control + current_inc_state = speed_increase.value() + if current_inc_state == 1 and prev_speed_inc_state == 0: + current_delay = a4988.inc_speed(current_delay) + prev_speed_inc_state = current_inc_state # Update state for next loop + + # Speed decrease control + current_dec_state = speed_decrease.value() + if current_dec_state == 1 and prev_speed_dec_state == 0: + current_delay = a4988.dec_speed(current_delay) + prev_speed_dec_state = current_dec_state # Update state for next loop + + # Small delay to debounce switch inputs and stabilize loop + time.sleep(debounce_delay) diff --git a/micropython/drivers/a4988/manifest.py b/micropython/drivers/a4988/manifest.py new file mode 100644 index 000000000..2ebb7c1f7 --- /dev/null +++ b/micropython/drivers/a4988/manifest.py @@ -0,0 +1 @@ +metadata(description="MicroPython driver for A4988 stepper motor controller", version="1.0.0")