2019-10-12 21:23:41 +01:00
|
|
|
import RPi.GPIO as gpio
|
2019-10-11 23:01:01 +01:00
|
|
|
from time import sleep
|
2019-10-12 21:23:41 +01:00
|
|
|
# import logging
|
|
|
|
# from logging import debug
|
|
|
|
# logging.basicConfig(level=logging.DEBUG)
|
2019-10-11 23:01:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
# speed limit 0.0005 s = 500us (could be 1us) waiting between high and low
|
|
|
|
# je hoeher die motor spannugn, desto lauter
|
|
|
|
|
2019-10-12 21:23:41 +01:00
|
|
|
class Stepper():
|
|
|
|
|
|
|
|
def __init__(self, pins):
|
|
|
|
# pins = [state step direction]
|
|
|
|
self.pins = pins
|
|
|
|
|
|
|
|
gpio.setmode(gpio.BCM)
|
|
|
|
|
|
|
|
for pin in self.pins:
|
|
|
|
gpio.setup(pin, gpio.OUT)
|
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
''' Disable the stepper motor driver '''
|
|
|
|
# debug('Disabled')
|
|
|
|
gpio.output(self.pins[0], 1)
|
|
|
|
|
|
|
|
|
|
|
|
def enable(self):
|
|
|
|
''' Enable the stepper motor driver '''
|
|
|
|
# debug('Enabled')
|
|
|
|
gpio.output(self.pins[0], 0)
|
|
|
|
|
|
|
|
|
|
|
|
def change_state(self):
|
|
|
|
''' Toggle between enabled and disabled state '''
|
|
|
|
# debug('changing state')
|
|
|
|
gpio.output(self.pins[0], not gpio.input(self.pins[0]))
|
2019-10-11 23:01:01 +01:00
|
|
|
|
|
|
|
|
2019-10-12 21:23:41 +01:00
|
|
|
def change_direction(self, direction=None):
|
|
|
|
''' Change the direction '''
|
|
|
|
# debug('changing direction')
|
|
|
|
gpio.output(self.pins[2], not gpio.input(self.pins[2]))
|
2019-10-11 23:01:01 +01:00
|
|
|
|
|
|
|
|
2019-10-12 21:23:41 +01:00
|
|
|
def get_direction(self, direction):
|
|
|
|
''' Translate everything but left and ccw to right '''
|
|
|
|
return 0 if direction in ['left', 'ccw'] else 1
|
2019-10-11 23:01:01 +01:00
|
|
|
|
|
|
|
|
2019-10-12 21:23:41 +01:00
|
|
|
def set_direction(self, direction):
|
|
|
|
''' Set the gpio direction output pin to the desired direction '''
|
|
|
|
gpio.output(self.pins[2], self.get_direction(direction))
|
2019-10-11 23:01:01 +01:00
|
|
|
|
|
|
|
|
2019-10-12 21:23:41 +01:00
|
|
|
def single_step(self):
|
|
|
|
''' Do a single step pulse '''
|
|
|
|
gpio.output(self.pins[1], 1)
|
|
|
|
sleep(0.000001) # min interval in which high gets recognized
|
|
|
|
gpio.output(self.pins[1], 0)
|
|
|
|
|
|
|
|
|
|
|
|
def step(self, steps=1, direction=None):
|
|
|
|
''' Do *steps* into *direction* '''
|
|
|
|
self.set_direction(direction)
|
|
|
|
for step in range(steps):
|
|
|
|
self.single_step()
|
|
|
|
sleep(0.002)
|
|
|
|
|
|
|
|
|
|
|
|
def rotate(self, degrees=1.8, direction=None):
|
|
|
|
''' Rotate *degrees* into *direction* '''
|
|
|
|
self.set_direction(direction)
|
|
|
|
for step in range(int(degrees/1.8)):
|
|
|
|
self.single_step()
|
|
|
|
sleep(0.002)
|
|
|
|
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
self.disable()
|
|
|
|
gpio.cleanup()
|
2019-10-11 23:01:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-10-12 21:23:41 +01:00
|
|
|
|
2019-10-11 23:01:01 +01:00
|
|
|
try:
|
2019-10-12 21:23:41 +01:00
|
|
|
s = Stepper([22, 27, 17])
|
|
|
|
s.enable()
|
|
|
|
s.step(100, 'left')
|
|
|
|
sleep(1)
|
|
|
|
s.step(100)
|
|
|
|
sleep(1)
|
|
|
|
s.rotate(360)
|
|
|
|
sleep(1)
|
|
|
|
s.rotate(360, 'ccw')
|
|
|
|
sleep(1)
|
|
|
|
s.rotate(1)
|
|
|
|
# s.step(10)
|
|
|
|
# sleep(1)
|
|
|
|
# s.step()
|
|
|
|
# sleep(1)
|
|
|
|
# s.step(direction='left')
|
|
|
|
|
2019-10-11 23:01:01 +01:00
|
|
|
except KeyboardInterrupt:
|
2019-10-12 21:23:41 +01:00
|
|
|
# debug('cleaning up')
|
|
|
|
s.release()
|