Stepper mótorar

Hér eru nokkrar gagnlegar leiðbeiningar um það hvernig stýra má stepper-mótor með Raspberry Pi:

Dæmi um Python kóða

import RPi.GPIO as gpio
import time
import math

# Clean up GPIOs in case they have been preactivated.
gpio.cleanup()

# Use the BCM GPIO pin numbers rather than the physical pin numbers
# for portability.
gpio.setmode(gpio.BCM)
gpio.setwarnings(False)

# Set which pins we're using for the stepper control.
# The order in the list is:
# [blue, pink, yellow, orange]
stepper_pins = [6, 13, 19, 26]

# Set the pins as outputs.
for pin in stepper_pins:
    gpio.setup(pin, gpio.OUT)
    gpio.output(pin, False)

# Quick pause for things to finish.
time.sleep(0.5)

# Basic variables.
wait_time = 0.002

# Single step sequence.
single_seq = [
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1],
]


def set_step(stepper_pin_states):
    """Set GPIO pins high/low according to a list of states."""
    for i in range(len(stepper_pins)):
        gpio.output(stepper_pins[i], stepper_pin_states[i])


def turn_steps(num):
    """
    Turn the stepper motor by a given number of steps.

    The sign determines the direction. The gearing ratio results in
    approximately 2048 steps in a full circle.
    """
    count = 0
    direction = int(math.copysign(1, num))

    while abs(count) < abs(num):
        set_step(single_seq[count % len(single_seq)])
        time.sleep(wait_time)
        count += direction


def turn_circles(circ):
    """Shortcut to define the turn by a fraction of a circle."""
    turn_steps(int(circ * 2048))


def stepper_powerdown():
    """Set all pins low so the coils are not unnecessarily powered."""
    for pin in stepper_pins:
        gpio.output(pin, False)


# Main loop.
try:
    while True:
        if not gpio.input(21):
            turn_steps(-1024)
        else:
            stepper_powerdown()
            time.sleep(0.1)

except KeyboardInterrupt:
    print("Stopped by user.")

finally:
    stepper_powerdown()
    gpio.cleanup()