add rotate function

put everything inside a class and a module
This commit is contained in:
koksnuss 2019-10-12 21:23:41 +01:00
parent f4e0cda1e2
commit a5e7a21be5
2 changed files with 87 additions and 48 deletions

0
__init__.py Normal file
View File

View File

@ -1,67 +1,106 @@
import RPi.GPIO as GPIO import RPi.GPIO as gpio
from time import sleep from time import sleep
import logging # import logging
from logging import debug # from logging import debug
logging.basicConfig(level=logging.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 # speed limit 0.0005 s = 500us (could be 1us) waiting between high and low
# je hoeher die motor spannugn, desto lauter # je hoeher die motor spannugn, desto lauter
# initialize class Stepper():
for name, pin in PINS.items():
GPIO.setup(pin[0], GPIO.OUT, initial=pin[1])
def disable(): def __init__(self, pins):
debug('Disabled') # pins = [state step direction]
GPIO.output(PINS['state'][0], 1) 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(): def enable(self):
debug('Enabled') ''' Enable the stepper motor driver '''
GPIO.output(PINS['state'][0], 0) # debug('Enabled')
gpio.output(self.pins[0], 0)
def change_direction(): def change_state(self):
debug('changing direction') ''' Toggle between enabled and disabled state '''
GPIO.output(PINS['direction'][0], not GPIO.input(PINS['direction'][0])) # debug('changing state')
gpio.output(self.pins[0], not gpio.input(self.pins[0]))
def change_state(): def change_direction(self, direction=None):
debug('changing state') ''' Change the direction '''
GPIO.output(PINS['state'][0], not GPIO.input(PINS['state'][0])) # debug('changing direction')
gpio.output(self.pins[2], not gpio.input(self.pins[2]))
def step(steps=1, direction=None): def get_direction(self, direction):
if direction: ''' Translate everything but left and ccw to right '''
GPIO.output(PINS['direction'][0], direction) return 0 if direction in ['left', 'ccw'] else 1
for step in range(steps):
GPIO.output(PINS['step'][0], 1)
sleep(0.005) def set_direction(self, direction):
GPIO.output(PINS['step'][0], 0) ''' Set the gpio direction output pin to the desired direction '''
sleep(0.005) gpio.output(self.pins[2], self.get_direction(direction))
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()
if __name__ == '__main__': if __name__ == '__main__':
try: try:
while True: s = Stepper([22, 27, 17])
# disable() s.enable()
# debug('stepping ccw') s.step(100, 'left')
# step(200, 0) sleep(1)
# s.step(100)
# enable() sleep(1)
# debug('stepping cw') s.rotate(360)
# step(200, 1) sleep(1)
step(100) s.rotate(360, 'ccw')
change_direction() sleep(1)
change_state() s.rotate(1)
# s.step(10)
# sleep(1)
# s.step()
# sleep(1)
# s.step(direction='left')
except KeyboardInterrupt: except KeyboardInterrupt:
debug('cleaning up') # debug('cleaning up')
disable() s.release()
GPIO.cleanup()