Skip to content

Instantly share code, notes, and snippets.

@DanHickstein
Created June 15, 2017 21:47
Show Gist options
  • Save DanHickstein/9de02548cdba9697715dc046f522922c to your computer and use it in GitHub Desktop.
Save DanHickstein/9de02548cdba9697715dc046f522922c to your computer and use it in GitHub Desktop.
import ctypes as c
import numpy as np
import os, time
dllname = os.path.join(os.path.dirname(__file__), 'Thorlabs.MotionControl.IntegratedStepperMotors.dll')
if not os.path.exists(dllname):
print "ERROR: DLL not found"
p = c.windll.LoadLibrary(dllname)
def getHardwareInfo(SN):
modelNo = c.c_buffer(255)
sizeOfModelNo = c.c_ulong(255)
hardwareType = c.c_ushort()
numChannels = c.c_short()
notes = c.c_buffer(255)
sizeOfNotes = c.c_ulong(255)
firmwareVersion = c.c_ulong()
hardwareVersion = c.c_ushort()
modState = c.c_ushort()
# p.PCC_GetHardwareInfo(SN)
p.ISC_GetHardwareInfo(SN,
c.pointer(modelNo),
c.pointer(sizeOfModelNo),
c.pointer(hardwareType),
c.pointer(numChannels),
c.pointer(notes),
c.pointer(sizeOfNotes),
c.pointer(firmwareVersion),
c.pointer(hardwareVersion),
c.pointer(modState) )
return [x.value for x in (modelNo, sizeOfModelNo, hardwareType, numChannels, notes, sizeOfNotes, firmwareVersion, hardwareVersion, modState)]
def getMotorParamsExt(SN):
# this doesn't work for some reason...
stepsPerRev = c.c_double()
gearBoxRatio = c.c_double()
pitch = c.c_double()
print p.ISC_GetMotorParamsExt(SN, c.pointer(stepsPerRev),
c.pointer(gearBoxRatio),
c.pointer(pitch))
return stepsPerRev.value, gearBoxRatio.value, pitch.value
def getDeviceList():
p.TLI_BuildDeviceList()
receiveBuffer = c.c_buffer(255)
sizeOfBuffer = c.c_ulong(255)
p.TLI_GetDeviceListExt(c.pointer(receiveBuffer), c.pointer(sizeOfBuffer))
return [x for x in (receiveBuffer.value).split(',')[:-1]]
def MoveToPosition(SN, deviceUnits, timeout=10, queryDelay=0.01, tolerance=1):
"""
Moves the rotation stage to a certain position (given by device units).
This call blocks future action until the move is complete.
The timeout is in seconds
SN is a c_buffer of the serial number string
deviceUnits shold be a int.
tolerance is when the blocking should end (device units)
"""
p.ISC_MoveToPosition(SN, c.c_int(int(deviceUnits)))
t = time.time()
while time.time()<(t+timeout):
p.ISC_RequestStatus(SN) # order the stage to find out its location
currentPosition = p.ISC_GetPosition(SN)
error = currentPosition - deviceUnits
if np.abs(error)<tolerance:
return
else:
time.sleep(queryDelay)
print 'Timeout!'
print 'getting serial number...'
serialNumber = getDeviceList()[0]
print 'serial number is: %s'%serialNumber
SN = c.c_buffer(serialNumber)
print 'Opening communications to rotation stage...'
print p.ISC_Open(SN)
print 'Success!'
print getHardwareInfo(SN)
print p.ISC_StartPolling(SN,c.c_int(200))
print p.ISC_LoadSettings(SN)
p.ISC_RequestStatusBits(SN)
print bin(p.ISC_GetStatusBits(SN))
stepsPerRev, gearBoxRatio, pitch = getMotorParamsExt(SN)
print stepsPerRev, gearBoxRatio, pitch
microstepsPerFullstep = 2048 # from https://www.thorlabs.com/newgrouppage9.cfm?objectgroup_id=8750
conversion = stepsPerRev * microstepsPerFullstep * gearBoxRatio / pitch # convert to degrees
print conversion
print 'Moving to location zero'
MoveToPosition(SN, 0)
for degrees in np.arange(0,30,5):
deviceUnits = int(degrees*conversion) # this involved rounding!
print 'Moving to %5.3f degrees (%i Device Units)...'%(degrees, deviceUnits)
MoveToPosition(SN, deviceUnits)
new_position = p.ISC_GetPosition(SN)
new_degrees = new_position/conversion
print 'Reported: %5.3f degrees (%i Device Units).'%(new_degrees, new_position)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment