#!/usr/bin/python # # Stepper motor driven focuser # # 20160801 fm@dulle.fr # import time, sys, thread, tty, termios from time import sleep import RPi.GPIO as GPIO # # curses used for text base inteface # import curses from mx import DateTime # # curses initialisation # stdscr = curses.initscr() try: stdscr.curs_set(False) curses.curs_set(False) except: pass curses.noecho() # # Some cosmetics on the interface # s=" STEPPER FOCUS " stdscr.addstr(0,0,s,curses.A_REVERSE) s=" POS(um) SPD " stdscr.addstr(1,1,s,curses.A_BOLD) # # Stepperscaling : # 2 keys (eg arrow up, arrow dn) are used to navigate through step_scale # and select the number of steps that will be sent to the motor # on the next "move" command (eg right or left arrow keys) # step_scale=[1,2,5,10,20,50,100,200,500,1000,2000,5000,10000] # # Number of items in step_scale # max_range=12 # # Point to the first range of step_scale : # step_range=0 # # step_inc = direction (1 or -1) # step_inc=1 # # old_dir = last motion dir (can be used to determine # if some backlash correction should be applied) # old_dir=1 # # initial position (arbitrary unit, stepcount) # step_pos=0 # # delay between steps (motor dependent) # delay_step=0.001 # # number of steps to move after a dir change # backlash_param=135 # # # GPIO pin assingnment # o_step=20 o_dir=21 GPIO.setmode(GPIO.BCM) # # # GPIO pin dir settings # GPIO.setup(o_step, GPIO.OUT) GPIO.setup(o_dir, GPIO.OUT) # # GPIO pin init # GPIO.output(o_step,GPIO.LOW) GPIO.output(o_step,GPIO.HIGH) GPIO.output(o_dir,GPIO.LOW) GPIO.output(o_dir,GPIO.HIGH) # # # saving term settings # fd = sys.stdin.fileno() initial_settings = termios.tcgetattr(fd) # # # Creating getch call # try: from msvcrt import getch # try to import Windows version except ImportError: def getch(): # define non-Windows version fd = sys.stdin.fileno() # try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) return ch # finally: # termios.tcsetattr(fd, termios.TCSADRAIN, initial_settings) # return ch # # Init No key press # keycode = None # # Threaded kb lookup # def keypress(): global keycode keycode = ord(getch()) thread.start_new_thread(keypress, ()) def do_move(): global step_scale, step_range, step_inc, delay_step, step_pos, backlash_param, old_dir count=step_scale[step_range] if old_dir!=step_inc: count=count+backlash_param step_pos=step_pos-step_inc*backlash_param old_dir=step_inc if step_inc==1: GPIO.setup(o_dir, GPIO.OUT) else: GPIO.setup(o_dir, GPIO.HIGH) while count>0: step_pos=(step_pos+step_inc) count=count-1 GPIO.output(o_step, GPIO.HIGH) sleep(delay_step) GPIO.output(o_step, GPIO.LOW) # # # n=DateTime.now() while True: if keycode==27: keycode=ord(getch()) if keycode==91: keycode=ord(getch()) if keycode==65: # up step_range=min(max_range,step_range+1) elif keycode==66: # down step_range=max(0,step_range-1) elif keycode==67: # right step_inc=1; do_move() elif keycode==68: # left step_inc=-1; do_move() elif keycode==122: step_pos=0 elif keycode==3: termios.tcsetattr(fd, termios.TCSADRAIN, initial_settings) curses.nocbreak(); stdscr.keypad(0); curses.echo() curses.endwin() GPIO.cleanup() sys.exit() break if keycode is not None: #print keycode keycode=None thread.start_new_thread(keypress, ()) s="\n %+6d %5d\n\n %s\n"%(step_pos, step_scale[step_range],n.strftime("%H:%M:%S")) try: stdscr.addstr(2,1,s,curses.A_BOLD) except: print "%s Error (windwow too narrow ?)\r"%(n.strftime("%H:%M:%S")); stdscr.refresh() n=DateTime.now() time.sleep(0.01)