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
import logging
from logging import debug
logging.basicConfig(level=logging.DEBUG)
# 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])
class Stepper():
def disable():
debug('Disabled')
GPIO.output(PINS['state'][0], 1)
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():
debug('Enabled')
GPIO.output(PINS['state'][0], 0)
def enable(self):
''' Enable the stepper motor driver '''
# debug('Enabled')
gpio.output(self.pins[0], 0)
def change_direction():
debug('changing direction')
GPIO.output(PINS['direction'][0], not GPIO.input(PINS['direction'][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]))
def change_state():
debug('changing state')
GPIO.output(PINS['state'][0], not GPIO.input(PINS['state'][0]))
def change_direction(self, direction=None):
''' Change the direction '''
# debug('changing direction')
gpio.output(self.pins[2], not gpio.input(self.pins[2]))
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)
def get_direction(self, direction):
''' Translate everything but left and ccw to right '''
return 0 if direction in ['left', 'ccw'] else 1
def set_direction(self, direction):
''' Set the gpio direction output pin to the desired direction '''
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__':
try:
while True:
# disable()
# debug('stepping ccw')
# step(200, 0)
#
# enable()
# debug('stepping cw')
# step(200, 1)
step(100)
change_direction()
change_state()
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')
except KeyboardInterrupt:
debug('cleaning up')
disable()
GPIO.cleanup()
# debug('cleaning up')
s.release()