stepper/stepper.py

68 lines
1.5 KiB
Python
Raw Normal View History

import RPi.GPIO as GPIO
from time import sleep
import logging
from logging import debug
logging.basicConfig(level=logging.DEBUG)
GPIO.setmode(GPIO.BCM)
PINS = {
'direction': [17, 0], # 1 = ckw, 0 = ccw
'step': [27, 0],
'state': [22, 1] # 1 = disabled, 0 = enabled
}
# speed limit 0.0005 s = 500us (could be 1us) waiting between high and low
# je hoeher die motor spannugn, desto lauter
# initialize
for name, pin in PINS.items():
GPIO.setup(pin[0], GPIO.OUT, initial=pin[1])
def disable():
debug('Disabled')
GPIO.output(PINS['state'][0], 1)
def enable():
debug('Enabled')
GPIO.output(PINS['state'][0], 0)
def change_direction():
debug('changing direction')
GPIO.output(PINS['direction'][0], not GPIO.input(PINS['direction'][0]))
def change_state():
debug('changing state')
GPIO.output(PINS['state'][0], not GPIO.input(PINS['state'][0]))
def step(steps=1, direction=None):
if direction:
GPIO.output(PINS['direction'][0], direction)
for step in range(steps):
GPIO.output(PINS['step'][0], 1)
sleep(0.005)
GPIO.output(PINS['step'][0], 0)
sleep(0.005)
if __name__ == '__main__':
try:
while True:
# disable()
# debug('stepping ccw')
# step(200, 0)
#
# enable()
# debug('stepping cw')
# step(200, 1)
step(100)
change_direction()
change_state()
except KeyboardInterrupt:
debug('cleaning up')
disable()
GPIO.cleanup()