Skip to content

Optotune adjustable lens #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hardwarelibrary/communication/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions hardwarelibrary/communication/.idea/communication.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions hardwarelibrary/communication/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions hardwarelibrary/communication/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions hardwarelibrary/communication/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions hardwarelibrary/communication/serialport.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def isOpen(self):
else:
return self.port.is_open

def open(self):
def open(self, baudRate=57600, timeout=0.3):
if self.port is None:
self.port = serial.Serial(self.portPath, 57600, timeout=0.3)
self.port = serial.Serial(self.portPath, baudRate, timeout=timeout)
else:
self.port.open()

Expand Down
127 changes: 127 additions & 0 deletions hardwarelibrary/motion/sutter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from struct import *
import time
import serial
from motion import sutterDevice

# class SutterDevice:
# def __init__(self):
# """
# SutterDevice represents a XYZ stage.
# """

# self.port = serial.Serial("/dev/cu.usbserial-SI8YCLBE", timeout=5, baudrate=128000)

# self.microstepsPerMicrons = 16

# def initializeDevice(self):
# """
# We do a late initialization: if the device is not present at creation, it can still be
# initialized later.
# """
# if self.port is not None:
# return

# self.port = serial.Serial("/dev/cu.usbserial-SI8YCLBE", timeout=5, baudrate=128000)
# if self.port is None:
# raise IOError("Can't find Sutter device")

# def shutdownDevice(self):
# """
# If the device fails, we shut everything down. We should probably flush the buffers also.
# """
# self.port.close()

# def sendCommand(self, commandBytes):
# """ The function to write a command to the endpoint. It will initialize the device
# if it is not alread initialized. On failure, it will warn and shutdown."""
# try:
# if self.port is None:
# self.initializeDevice()

# self.port.write(commandBytes)

# except Exception as err:
# print('Error when sending command: {0}'.format(err))
# self.shutdownDevice()

# def readReply(self, size, format) -> tuple:
# """ The function to read a reply from the endpoint. It will initialize the device
# if it is not already initialized. On failure, it will warn and shutdown.
# It will unpack the reply into a tuple.
# """
# try:
# if self.port is None:
# self.initializeDevice()

# replyBytes = self.port.read(size)
# if len(replyBytes) == size:
# theTuple = unpack(format, replyBytes)
# return theTuple

# except Exception as err:
# print('Error when reading reply: {0}'.format(err))
# self.shutdownDevice()
# return None

# def positionInMicrosteps(self) -> (int,int,int):
# """ Returns the position in microsteps """
# commandBytes = pack('<cc', b'C', b'\r')
# self.sendCommand(commandBytes)
# return self.readReply(size=14, format='<clllc')

# def moveInMicrostepsTo(self, position):
# """ Move to a position in microsteps """
# x,y,z = position
# commandBytes = pack('<clllc', b'M', int(x), int(y), int(z), b'\r')
# self.sendCommand(commandBytes)

# def position(self) -> (float, float, float):
# """ Returns the position in microns """

# position = self.positionInMicrosteps()
# if position is not None:
# return (position[1]/self.microstepsPerMicrons,
# position[2]/self.microstepsPerMicrons,
# position[3]/self.microstepsPerMicrons)
# else:
# return None

# def moveTo(self, position):
# """ Move to a position in microns """
# x,y,z = position
# positionInMicrosteps = (x*self.microstepsPerMicrons,
# y*self.microstepsPerMicrons,
# z*self.microstepsPerMicrons)
# self.moveInMicrostepsTo(positionInMicrosteps)

# def moveBy(self, delta) -> bool:
# #Move by a delta displacement (dx, dy, dz) from current position in microns
# dx,dy,dz = delta
# position = self.position()
# if position is not None:
# x,y,z = position
# self.moveTo((x+dx, y+dy, z+dz))


if __name__ == "__main__":
device = SutterDevice(serialNumber="debug")
nbDonnees = 5
dist = 1000

for i in range(nbDonnees+1):
if i == 0:
#device.home()
device.work()
print(device.position())
time.sleep(1)
if i % 2 == 0:
fac = 1
else:
fac = -1
for ii in range(nbDonnees+1):
if ii != nbDonnees:
device.moveBy((dist*fac, 0, 0))
else:
device.moveBy((0, dist, 0))
print(device.position())
time.sleep(1)
98 changes: 93 additions & 5 deletions hardwarelibrary/motion/sutterdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import time
from struct import *

class SutterDevice(PhysicalDevice, LinearMotionDevice):
class SutterDevice(PhysicalDevice):

def __init__(self, bsdPath=None, portPath=None, serialNumber: str = None,
productId: np.uint32 = None, vendorId: np.uint32 = None):
Expand All @@ -21,15 +21,14 @@ def __init__(self, bsdPath=None, portPath=None, serialNumber: str = None,
self.portPath = None

PhysicalDevice.__init__(self, serialNumber, vendorId, productId)
LinearMotionDevice.__init__(self)
self.port = None
self.xMinLimit = 0
self.yMinLimit = 0
self.zMinLimit = 0
self.xMaxLimit = 25000
self.yMaxLimit = 25000
self.zMaxLimit = 25000

self.microstepsPerMicrons = 16

def __del__(self):
try:
Expand All @@ -43,12 +42,12 @@ def doInitializeDevice(self):
if self.portPath == "debug":
self.port = SutterDebugSerialPort()
else:
self.port = CommunicationPort(portPath=self.portPath)
self.port = SerialPort(idVendor=4930, idProduct=0x0001)

if self.port is None:
raise PhysicalDeviceUnableToInitialize("Cannot allocate port {0}".format(self.portPath))

self.port.open()
self.port.open(baudRate=128000, timeout=10)
self.doGetPosition()

except Exception as error:
Expand All @@ -65,6 +64,95 @@ def doShutdownDevice(self):
self.port = None
return

def sendCommand(self, commandBytes):
""" The function to write a command to the endpoint. It will initialize the device
if it is not alread initialized. On failure, it will warn and shutdown."""
try:
if self.port is None:
self.initializeDevice()

self.port.writeData(commandBytes)
reply = self.readReply(1)

except Exception as err:
print('Error when sending command: {0}'.format(err))
self.shutdownDevice()

def readReply(self, size, format=None) -> tuple:
""" The function to read a reply from the endpoint. It will initialize the device
if it is not already initialized. On failure, it will warn and shutdown.
It will unpack the reply into a tuple.
"""
try:
if self.port is None:
self.initializeDevice()

replyBytes = self.port.readData(size)
if len(replyBytes) == size:
if format is not None:
theTuple = unpack(format, replyBytes)
return theTuple
else:
return ()
else:
raise Exception("Not enough bytes read")

except Exception as err:
print('Error when reading reply: {0}'.format(err))
self.shutdownDevice()
return None

def positionInMicrosteps(self) -> (int,int,int):
""" Returns the position in microsteps """
commandBytes = pack('<cc', b'C', b'\r')
self.sendCommand(commandBytes)
return self.readReply(size=13, format='<lllc')

def moveInMicrostepsTo(self, position):
""" Move to a position in microsteps """
x,y,z = position
commandBytes = pack('<clllc', b'M', int(x), int(y), int(z), b'\r')
self.sendCommand(commandBytes)

def position(self) -> (float, float, float):
""" Returns the position in microns """

position = self.positionInMicrosteps()
if position is not None:
return (position[0]/self.microstepsPerMicrons,
position[1]/self.microstepsPerMicrons,
position[2]/self.microstepsPerMicrons)
else:
return (None, None, None)

def moveTo(self, position):
""" Move to a position in microns """
x,y,z = position
positionInMicrosteps = (x*self.microstepsPerMicrons,
y*self.microstepsPerMicrons,
z*self.microstepsPerMicrons)
self.moveInMicrostepsTo(positionInMicrosteps)

def moveBy(self, delta) -> bool:
#Move by a delta displacement (dx, dy, dz) from current position in microns
dx,dy,dz = delta
position = self.position()
if position is not None:
x,y,z = position
self.moveTo((x+dx, y+dy, z+dz))

def home(self):
commandBytes = pack('<cc', b'H', b'\r')

self.sendCommand(commandBytes)

def work(self):
self.home()
commandBytes = pack('<cc', b'Y', b'\r')

self.sendCommand(commandBytes)


class SutterDebugSerialPort(CommunicationPort):
def __init__(self):
super(SutterDebugSerialPort,self).__init__()
Expand Down
4 changes: 0 additions & 4 deletions hardwarelibrary/tests/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@
)

# append module root directory to sys.path
sys.path.insert(0, "{0}/hardwarelibrary/communication".format(root))
sys.path.insert(0, "{0}/hardwarelibrary/spectrometers".format(root))
sys.path.insert(0, "{0}/hardwarelibrary/lasersources".format(root))
sys.path.insert(0, "{0}/hardwarelibrary".format(root))
sys.path.insert(0, root)
Loading