Last active
October 4, 2016 17:08
-
-
Save jemerick/00fa208325b470c36691eadfce2a5164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import RPi.GPIO as GPIO | |
import time | |
import click | |
class Stepper(): | |
""" | |
""" | |
DIR_OPEN = GPIO.LOW | |
DIR_CLOSE = GPIO.HIGH | |
ENABLED = GPIO.HIGH | |
DISABLED = GPIO.LOW | |
def __init__(self, enable_pin, direction_pin, pulse_pin): | |
""" | |
""" | |
self.pulse_width = 300 # pulse width in microseconds | |
self.pulse_delay = self.pulse_width + 200 # delay between pulses in microseconds | |
self.enable_pin = enable_pin | |
self.direction_pin = direction_pin | |
self.pulse_pin = pulse_pin | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(enable_pin, GPIO.OUT, initial=Stepper.DISABLED) | |
GPIO.setup(direction_pin, GPIO.OUT, initial=Stepper.DIR_OPEN) | |
GPIO.setup(pulse_pin, GPIO.OUT, initial=GPIO.LOW) | |
def cleanup(self): | |
GPIO.cleanup() | |
# def set_speed(self, rpms): | |
# """ | |
# Sets the speed in revs per minute. The delay between each step in microseconds based on RPMs | |
# """ | |
# self.step_delay = 60L * 1000L * 1000L / self.number_of_steps / rpms; | |
def set_direction(self, direction): | |
GPIO.output(self.direction_pin, direction) | |
def step(self, steps): | |
GPIO.output(self.enable_pin, Stepper.ENABLED) | |
steps_left = steps | |
last_step_time = 0 | |
while steps_left > 0: | |
now = long(time.time() * 1000000) | |
if (now - last_step_time) >= self.pulse_delay: | |
last_step_time = now | |
steps_left -= 1 | |
self.step_motor() | |
GPIO.output(self.enable_pin, Stepper.DISABLED) | |
def step_motor(self): | |
GPIO.output(self.pulse_pin, GPIO.HIGH) | |
pulse_start = long(time.time() * 1000000) | |
now = pulse_start | |
while True: | |
now = long(time.time() * 1000000) | |
if (now - pulse_start) >= self.pulse_width: | |
GPIO.output(self.pulse_pin, GPIO.LOW) | |
break | |
@click.command() | |
@click.option('--rotations', default=1, help='Number of full rotations to make') | |
def main(rotations): | |
click.echo('%s rotations' % (rotations)) | |
stepper = Stepper(11, 13, 15) | |
if rotations < 0: | |
stepper.set_direction(Stepper.DIR_OPEN) | |
else: | |
stepper.set_direction(Stepper.DIR_CLOSE) | |
start_time = time.time() | |
stepper.step(200 * abs(rotations)) | |
end_time = time.time() | |
duration = end_time - start_time | |
click.echo('Duration %s seconds' % duration) | |
stepper.cleanup() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment