import serial import serial.tools.list_ports import time, sched def wait_for_ack(): while True: # wait for the '*' character new_char = io.read() timeouts = 0 if new_char == '': timeouts += 1 if timeouts >= 5: break if new_char == '*': break # Search for and connect to the stepper board initialized = False io = None def init(): global io import serial.tools.list_ports #try to find the device's port number for port in serial.tools.list_ports.comports(): if 'Stepperboard' in port[1]: stepper_port = port[0] print "Stepper found on port " + str(stepper_port) break else: debug_msg("Tried to initialize. Stepper Board not found in port list.") return False try: io.close() #if the port's open, close it except Exception: pass try: io = serial.Serial( #try to open the serial port port=stepper_port, baudrate=115200, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE, timeout=0.1 ) except Exception: return False print "Stepperboard found and initialized!" return True #Awesome, we made it through! Board is initialized. #test the limits and center the gun between them in both axis. def zero_center(): io.write("B1000R") io.write("X10000GI") while '*' not in io.read(): pass io.flushInput() io.write("X-1?") received_string = '' while True: byte = io.read() if byte == '*': break else: received_string += byte x_lim_plus = int(received_string.strip()[5:]) print "plus limit = " + str(x_lim_plus) io.flushInput() io.write("X-10000GI") while '*' not in io.read(): pass io.write("X-1?") received_string = '' while True: byte = io.read() if byte == '*': break else: received_string += byte x_lim_minus = int(received_string.strip()[5:]) print "minus limit = " + str(x_lim_minus) x_range = x_lim_plus - x_lim_minus print "X total step range: " + str(x_range) io.write("X2000R") #ok, got the data we need, return to operating speed new_x_lim_minus = 0-(x_range/2) io.write("X" + str(new_x_lim_minus) + "=") while '*' not in io.read(): pass io.write("X0GI") while '*' not in io.read(): pass # OK, that's it for X! io.write("Y10000GI") while '*' not in io.read(): pass io.flushInput() io.write("Y-1?") received_string = '' while True: byte = io.read() if byte == '*': break else: received_string += byte y_lim_plus = int(received_string.strip()[5:]) print "plus limit = " + str(y_lim_plus) io.flushInput() io.write("Y-10000GI") while '*' not in io.read(): pass io.write("Y-1?") received_string = '' while True: byte = io.read() if byte == '*': break else: received_string += byte y_lim_minus = int(received_string.strip()[5:]) print "minus limit = " + str(y_lim_minus) y_range = y_lim_plus - y_lim_minus print "Y total step range: " + str(y_range) io.write("Y2000R") #ok, got the data we need, return to operating speed new_y_lim_minus = 0-(y_range/2) io.write("Y" + str(new_y_lim_minus) + "=") while '*' not in io.read(): pass io.write("Y0GI") while '*' not in io.read(): pass print "Leveling" io.write("Y-675GI"); while '*' not in io.read(): pass io.write("Y0=") while '*' not in io.read(): pass print "Turret zeroed!" io.flushInput() if (__name__ == "__main__"): init() zero_center() #track_target()