import RPi.GPIO as gpio from time import sleep # import logging # from logging import debug # logging.basicConfig(level=logging.DEBUG) # speed limit 0.0005 s = 500us (could be 1us) waiting between high and low # je hoeher die motor spannugn, desto lauter 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])) def change_direction(self, direction=None): ''' Change the direction ''' # debug('changing direction') gpio.output(self.pins[2], not gpio.input(self.pins[2])) 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: s = Stepper([22, 27, 17]) # s.enable() # s.step(100, 'left') # sleep(1) # s.step(100) # sleep(1) s.rotate(360000) 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') s.release()