Last active
December 22, 2016 17:16
-
-
Save PatchworkBoy/7511b99e85b3e62ad4feaaa3fa18327b to your computer and use it in GitHub Desktop.
Python Stepper Script for UL2003 driver triggered by GPIO pins defined at line 76
This file contains 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
#!/usr/bin/python | |
#-------------------------------------- | |
# ___ ___ _ ____ | |
# / _ \/ _ \(_) __/__ __ __ | |
# / , _/ ___/ /\ \/ _ \/ // / | |
# /_/|_/_/ /_/___/ .__/\_, / | |
# /_/ /___/ | |
# | |
# Stepper Motor Test | |
# | |
# A simple script to control | |
# a stepper motor. | |
# | |
# Author : Matt Hawkins | |
# Date : 28/09/2015 | |
# | |
# http://www.raspberrypi-spy.co.uk/ | |
# | |
#-------------------------------------- | |
# Import required libraries | |
import sys | |
import time | |
import RPi.GPIO as GPIO | |
import os | |
import atexit | |
import signal | |
StepCounter = 0 | |
totalStep = 0 | |
LOG_FILE="/var/log/focus_max.log" | |
LOG_FILE2="/var/log/focus_curr.log" | |
if os.path.isfile(LOG_FILE): | |
with open(LOG_FILE, 'r') as l: | |
last = None | |
for line in (line for line in l if line.rstrip('\n')): | |
last = line | |
maxposition=int(last) | |
else: | |
maxposition=50000 | |
if os.path.isfile(LOG_FILE2): | |
with open(LOG_FILE2, 'r') as l: | |
last = None | |
for line in (line for line in l if line.rstrip('\n')): | |
last = line | |
currposition=int(last) | |
else: | |
currposition=0 | |
minposition=0 | |
if len(sys.argv)<2: | |
print >> sys.stdout,"\r\n Usage: focus <ms> <stepsize> OR focus set min (to set the zero point)" | |
print >> sys.stdout,"\r\n <ms> can be a positive integer between 1 and 250" | |
print >> sys.stdout," <stepsize> can be -2, -1, 1 or 2 (positive figure moves clockwise, negative anticlockwise)\r\n" | |
print >> sys.stdout," eg: focus 10 1, focus 2 -2, focus 1 1, focus 250 1\r\n\r\n" | |
sys.exit() | |
@atexit.register | |
def handleSIGTERM(): | |
print >> sys.stdout, str(currposition) | |
print >> sys.stderr, "DONE" | |
current = currposition | |
f = open(LOG_FILE2,'a') | |
f.write(str(current)+"\n") | |
f.close() | |
GPIO.cleanup() | |
signal.signal(signal.SIGTERM, handleSIGTERM) | |
# Use BCM GPIO references | |
# instead of physical pin numbers | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
# Define GPIO signals to use | |
StepPins = [17,25,23,24] | |
# Set all pins as output | |
for pin in StepPins: | |
GPIO.setup(pin,GPIO.OUT) | |
GPIO.output(pin, False) | |
# Define advanced sequence | |
# as shown in manufacturers datasheet | |
Seq = [[1,0,0,1], | |
[1,0,0,0], | |
[1,1,0,0], | |
[0,1,0,0], | |
[0,1,1,0], | |
[0,0,1,0], | |
[0,0,1,1], | |
[0,0,0,1]] | |
StepCount = len(Seq) | |
if sys.argv[1] == "set": | |
if sys.argv[2] == "min": | |
currposition=0 | |
f = open(LOG_FILE2,'a') | |
f.write(str(currposition)+"\n") | |
f.close() | |
else: | |
f = open(LOG_FILE,'a') | |
f.write(str(currposition)+"\n") | |
f.close() | |
elif sys.argv[1] =="clear": | |
f = open(LOG_FILE,'a') | |
f.write(str(45000)+"\n") | |
f.close() | |
f = open(LOG_FILE2,'a') | |
currposition=22500 | |
f.write(str(22500)+"\n") | |
f.close() | |
else: | |
# Read wait time from command line | |
if len(sys.argv)>1: | |
WaitTime = int(sys.argv[1])/float(1000) | |
else: | |
WaitTime = 10/float(1000) | |
StepDir = int(sys.argv[2]) | |
# Start main loop | |
try: | |
while True: | |
if ((currposition<maxposition) or (currposition==maxposition and (currposition+StepDir<maxposition))) and currposition+StepDir>minposition: | |
for pin in range(0, 4): | |
xpin = StepPins[pin] | |
if Seq[StepCounter][pin]!=0: | |
GPIO.output(xpin, True) | |
else: | |
GPIO.output(xpin, False) | |
StepCounter += StepDir | |
totalStep += StepDir | |
currposition += StepDir | |
#print >> sys.stdout, str(currposition)+"\n" | |
#print >> sys.stdout, str(StepCounter)+"|" + str(totalStep)+"\r\n" | |
# If we reach the end of the sequence | |
# start again | |
if (StepCounter>=StepCount): | |
StepCounter = 0 | |
if (StepCounter<0): | |
StepCounter = StepCount+StepDir | |
# Wait before moving on | |
time.sleep(WaitTime) | |
else: | |
print >> sys.stderr,"Limit Reached" | |
break | |
except (KeyboardInterrupt, SystemExit): | |
for pin in StepPins: | |
f = open(LOG_FILE2,'a') | |
f.write(str(currposition)+"\n") | |
f.close() | |
print >> sys.stdout,str(currposition) | |
GPIO.output(pin, False) | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment