From f4e0cda1e28358da554476658daa9d9474f5e4a7 Mon Sep 17 00:00:00 2001 From: koksnuss Date: Fri, 11 Oct 2019 23:01:01 +0100 Subject: [PATCH] added basic functions for stepping, changing direction and enabling disabling the driver --- stepper.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 stepper.py diff --git a/stepper.py b/stepper.py new file mode 100644 index 0000000..9f47f17 --- /dev/null +++ b/stepper.py @@ -0,0 +1,67 @@ +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()