diff --git a/Picomotor/__pycache__/picomotor_driver.cpython-38.pyc b/Picomotor/__pycache__/picomotor_driver.cpython-38.pyc new file mode 100644 index 0000000..72d62ab Binary files /dev/null and b/Picomotor/__pycache__/picomotor_driver.cpython-38.pyc differ diff --git a/Picomotor/picomotor_driver.py b/Picomotor/picomotor_driver.py index 11c7630..de71f34 100644 --- a/Picomotor/picomotor_driver.py +++ b/Picomotor/picomotor_driver.py @@ -4,27 +4,35 @@ import serial import time import numpy as np +#import os class picomotor: - axis_names = { "MOTz_x": "112", "MOTz_y": "122"} - steps_in_turn = { "MOTz_x": [1000,1000], "MOTz_y": [1000,1000]} # [CW, CCW] number of steps for 1 turn + axis_names = { "MOTz_x": "112", "MOTz_y": "122", "MOTy_x": "232", "MOTy_y": "242", "MOTx_x": "212", "MOTx_y": "222"} + steps_in_turn = { "MOTz_x": [15000, 16000], "MOTz_y":[16000, 16000], "MOTy_x": [16000, 16000], "MOTy_y": [16000, 16000], + "MOTx_x": [16000, 16000], "MOTx_y": [16000, 16000]} # [CW, CCW] number of steps for 1 turn - def __init__(self, COM_port ) + def __init__(self, COM_port): """Open serial connection for NewFocus 8732 controller""" """COM_port: ie 'COM3' (check device manager) """ self.serial = serial.Serial(timeout = 0.5) #19200 baud didn't work. 8 bits, no parity, 1 stop bit. self.serial.baudrate = 9600 self.serial.port = COM_port #'COM3' - #print("Serial connection open: ", self.serial.open()) - response = self.sendreceive(self, "*IDN?") - print(response) - + try: + self.serial.open() + #print("Serial connection open: ", self.serial.open()) + response = self.sendreceive("*IDN?") + print(response) + #print(self.serial.is_open) + except: + self.serial.close() # don't know if this will help + print(self.serial.is_open) + print('Cannot open.') # Initialize positions - self.positions = dict.fromkeys(axis_names, 0) #{'MOTz_x': 0, 'MOTz_y': 0, ...} #units steps + self.positions = dict.fromkeys(self.axis_names, 0) #{'MOTz_x': 0, 'MOTz_y': 0, ...} #units steps # Turn on the slots? #self.sendreceive(':INST 1') #? not sure if I need this? @@ -37,62 +45,89 @@ def sendreceive(self, cmd): """ 8732 ends with carriage return \r. Does not echo command, but always responds. """ line = cmd + '\r' retval = self.serial.write(bytes(line, encoding='ascii')) - time.sleep(0.01) #not sure if need + self.serial.flush() # wait for outgoing serial data to complete + #time.sleep(0.01) #not sure if needed response = self.serial.read_until(b'\r') - #self.serial.flush() #what does this do? + response = response[:-1] # get rid of \r return response - - def move_abs(self, axis, position, velocity) - """ Move to absolute position, and if CCW (negative steps), then use correct cw/ccw disparities""" - cw ccw = steps_in_turn[axis] #number steps in turn - current_position = self.positions[axis] #get current position - steps = position - current_position - - if steps < 0: - steps = steps*(ccw/cw) - - self.move(axis, steps, velocity) - self.positions[axis] = position - - - def move(self, axis, steps, velocity) + + def move(self, axis, steps, velocity=1500): """ Move relative number of setups. Position CW, Negative CCW""" """ Returns before finished moving. """ - cmd1 = "INST:NSEL " + axis_names[axis] + cmd1 = ":INST:NSEL " + self.axis_names[axis] + + cmd2 = ":SOUR:PULS:FREQ "+ str(round(velocity)) # integer divider of 1500 if steps < 0: - cmd2 = ":SOUR:DIR CWW" + cmd3 = ":SOUR:DIR CCW" else: - cmd2 = ":SOUR:DIR CW" - - cmd3 = "SOUR:PULS:FREQ "+ str(round(velocity)) # integer divider of 1500 + cmd3 = ":SOUR:DIR CW" cmd4 = ":SOUR:PULS:COUN " + str(round(abs(steps))) - cmd = cmd1 + "; " + cmd2 + "; " + cmd3 + "; " + cmd4 + cmd = cmd1 + "; " + cmd2 + "; " + cmd3 #+ "; " + cmd4 + print(cmd) self.sendreceive(cmd) + time.sleep(0.01) + self.sendreceive(cmd4) + + self.wait_for_move() + # Update position in move_abs and move_rel + #self.positions[axis] = self.positions[axis] + steps - # Update position - self.positions[axis] = self.positions[axis] + steps + + def move_abs(self, axis, position, velocity=1500, ccw_correction=True): + """ Move to absolute position, and if CCW (negative steps), then use correct cw/ccw disparities""" + cw, ccw = self.steps_in_turn[axis] #number steps in turn + current_position = self.positions[axis] #get current position + steps = position - current_position + + if steps < 0: + steps = steps*(ccw/cw) + + self.move(axis, steps, velocity) + self.positions[axis] = position + return position + + def move_rel(self, axis, steps, velocity=1500, ccw_correction=True): + """ Move to absolute position, and if CCW (negative steps), then use correct cw/ccw disparities""" + cw, ccw = self.steps_in_turn[axis] #number steps in turn + + if steps < 0: + steps = steps*(ccw/cw) + + self.move(axis, steps, velocity) + self.positions[axis] = self.positions[axis] + steps #get current position + return self.positions[axis] + - def wait_for_move(self) + def wait_for_move(self): """ Wait for picomotor to finish moving """ timeout = 120 # timeout seconds - moving = 1 - while moving: #OPC returns 1 if all channels are finished + moving = 0 + while moving==0: #OPC returns 1 if all channels are finished time.sleep(0.2) text = self.sendreceive("*OPC?") moving = int(text)# get 1 or 0 from text - def zero_positions(self) + def zero_positions(self): """ Zero all positions """ - self.positions = dict.fromkeys(axis_names, 0) + self.positions = dict.fromkeys(self.axis_names, 0) - def zero_position(self, axis) + def zero_position(self, axis): """ Zero single axis """ self.positions[axis] = 0 - \ No newline at end of file + + + def get_position(self, axis): + """ Return current position """ + return self.positions[axis] + + + def close(self): + self.serial.close() + print(self.serial.is_open) \ No newline at end of file diff --git a/Picomotor/test_driver.py b/Picomotor/test_driver.py new file mode 100644 index 0000000..b86a13f --- /dev/null +++ b/Picomotor/test_driver.py @@ -0,0 +1,30 @@ + +""" +Created on Mon Feb 8 12:01:46 2021 + +@author: hoodl +""" +from picomotor_driver import picomotor +p = picomotor("COM4") +vel = 1500 + +p.move("MOTz_x", 15000, velocity=vel) +#p.wait_for_move() + + +p.move("MOTz_y", 1000, velocity=vel) +#p.wait_for_move() + +p.move("MOTy_x", 1000, velocity=vel) +#p.wait_for_move() + +p.move("MOTy_y", 1000, velocity=vel) +#p.wait_for_move() + +p.move("MOTx_x", 1000, velocity=vel) +#p.wait_for_move() + +p.move("MOTx_y", 1000, velocity=vel) +#p.wait_for_move() + +p.close() \ No newline at end of file diff --git a/Picomotor/testing.py b/Picomotor/testing.py index 9debbc6..7604d6e 100644 --- a/Picomotor/testing.py +++ b/Picomotor/testing.py @@ -1,13 +1,14 @@ import serial #import io +import time ser = serial.Serial(timeout = 1) #19200 baud didn't work. 8 bits, no parity, 1 stop bit. ser.baudrate = 9600 -ser.port = 'COM3' -ser.xonxoff=0 +ser.port = 'COM4' +#ser.xonxoff=0 #sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser)) @@ -15,44 +16,43 @@ ser.open() print(ser.is_open) -#ser.close() -#print(ser.is_open) +# Close +ser.close() +print(ser.is_open) ser.write(b'*IDN?\r') +ser.flush() txt = ser.read_until(b'\r') -print(txt) +print(txt[:-2]) + +# Turn on picomotor controller +# I don't think this is needed ser.write(b':INST 1\r') txt = ser.read_until(b'\r') print(txt) +ser.write(b':INST:STATE?\r') +print(ser.read_until(b'\r')) +ser.write(b':INST:STATE 1\r') # turns slot on +print(ser.read_until(b'\r')) -ser.write(b'INST:NSEL?\r') -txt = ser.read_until(b'\r') -print(txt) +# Move picomotor +# ser.write(b':INST:NSEL?\r') +# txt = ser.read_until(b'\r') +# print(txt) -ser.write(b'INST:NSEL 242\r') # slot (1-2), connector (1-4), channel (A,B). +ser.write(b':INST:NSEL 142\r') # slot (1-2), connector (1-4), channel (A,B). txt = ser.read_until(b'\r') print(txt) - - - -ser.write(b':INST:STATE 1\r') # turns slot on -print(ser.read_until(b'\r')) - -ser.write(b':INST:STATE?\r') -print(ser.read_until(b'\r')) - - - ser.write(b':SOUR:PULS:FREQ 1500\r') # freqs are integer dividens of 1500 Hz txt = ser.read_until(b'\r') print(txt) @@ -61,10 +61,28 @@ txt = ser.read_until(b'\r') print(txt) +ser.write(b':INST:NSEL 122; :SOUR:PULS:FREQ 1500; :SOUR:DIR CW; :SOUR:PULS:COUN 1000\r') +txt = ser.read_until(b'\r') +print(txt[:-2]) +## Testing +ser.write(b':INST:NSEL 112\r') # slot (1-2), connector (1-4), channel (A,B). +txt = ser.read_until(b'\r') +print(txt) +time.sleep(0.01) +ser.write(b':SOUR:PULS:FREQ 1500; :SOUR:DIR CW; :SOUR:PULS:COUN 1000\r') +txt = ser.read_until(b'\r') +print(txt[:-2]) +ser.write(b':INST:NSEL 122; :SOUR:PULS:FREQ 1500; :SOUR:DIR CW\r') # slot (1-2), connector (1-4), channel (A,B). +txt = ser.read_until(b'\r') +print(txt) +time.sleep(0.01) +ser.write(b':SOUR:PULS:COUN 1000\r') +txt = ser.read_until(b'\r') +print(txt[:-2]) @@ -93,3 +111,9 @@ print(txt) + +## Operation complet + +ser.write(b'*OPC?\r') +txt = ser.read_until(b'\r') +print(txt) \ No newline at end of file