Last active
December 15, 2015 06:09
-
-
Save omsai/5214107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Move Andor FRAPPA galvos to bypass position | |
Usage: | |
python frappa_bypass.py <bypass> | |
Example: | |
C:\Python27\python C:\MM\frappa_bypass.py 6.5 | |
""" | |
# Default bypass value | |
BYPASS = 6.5 # degrees | |
from ctypes import CDLL, c_char_p, c_void_p, c_double, byref, Structure | |
from sys import exit, argv | |
# Get value from commandline if supplied | |
if len(argv) == 2: | |
BYPASS = float(argv[1]) | |
print('Setting FRAPPA bypass to %.2f degrees' % BYPASS) | |
galvo_max = 15.0 # degrees | |
galvo_min = -15.0 # degrees | |
gavo_res = 0.0005 | |
def degrees_to_steps(degrees): | |
conversion_factor = pow(2.0, 35.0) / galvo_max | |
steps = degrees * conversion_factor | |
return steps | |
error_code = { | |
0: 'no error', | |
1: 'unknown error', | |
2: 'method not implemented yet', | |
30: 'invalid first argument', | |
31: 'invalid second argument', | |
32: 'invalid third argument', | |
33: 'invalid fourth argument', | |
34: 'invalid fifth argument', | |
35: 'invalid sixth argument', | |
36: 'invalid seventh argument', | |
37: 'invalid eighth argument', | |
100: 'invalid LSM handle', | |
102: 'error opening the COM port', | |
100000: 'the LSM is not calibrated', | |
100001: 'failed to clear the scan protocol from the LSM', | |
100002: 'failed to set a new scan protocol', | |
100003: 'failed to start the execution of the scan protocol', | |
100004: 'failed to abort the scan protocol', | |
100005: 'unable to determine the blanking time between lines', | |
100006: 'protocol is not supported', | |
100007: 'error creating the protocol', | |
100008: 'error calculating intersections between scan lines and region to scan', | |
100009: 'number of created commands for a protocol is not as expected', | |
} | |
# Python print statements stop working after this CDLL line in | |
# Micro-Manager's Script Panel probably due to this library's log4cxx | |
# module. | |
lib = CDLL('C:/MM/tillimic.dll') | |
# Connect to a specific LSM device. | |
port = c_char_p('COM1') | |
handle = c_void_p(0) | |
print('Initializing FRAPPA...') | |
ret = -1 | |
ret = lib.LSM_Open(port, byref(handle)) | |
# int LSM_Open(const char *port, void **handle) | |
if (ret != 0): | |
exit('Error from LSM_Open(): %s (Code %d)' | |
% (error_code[ret], ret)) | |
### Set the resting position of the galvos. | |
##print('Setting Rest position to %.2f...' % BYPASS) | |
class LSM_Coordinate(Structure): # struct | |
"""Steps values for galvo""" | |
_fields_ = [("x", c_double), | |
("y", c_double)] | |
rest_position = LSM_Coordinate(degrees_to_steps(BYPASS), | |
degrees_to_steps(galvo_min)) | |
ret = -1 | |
ret = lib.LSM_SetRestPosition(handle, rest_position) | |
# int LSM_SetRestPosition (void *handle, LSM_Coordinate galvoPos) | |
if (ret != 0): | |
exit('Error from LSM_SetRestPosition(): %s (Code %d)' | |
% (error_code[ret], ret)) | |
# Gets the resting position of the galvos. | |
get_position = LSM_Coordinate(-999,-999) | |
ret = -1 | |
ret = lib.LSM_GetRestPosition(handle, byref(get_position)) | |
# int LSM_GetRestPosition (void *handle, LSM_Coordinate *galvoPos) | |
if (ret != 0): | |
exit('Error from LSM_GetRestPosition(): %s (Code %d)' | |
% (error_code[ret], ret)) | |
# Move both scanning galvo absolute compared to their central position | |
# (uncalibrated). | |
ret = lib.LSM_SetGalvoRawPosition(handle, rest_position) | |
# int LSM_SetGalvoRawPosition (void *handle, LSM_Coordinate *galvoCoordinate) | |
if (ret != 0): | |
exit('Error from LSM_SetGalvoRawPosition(): %s (Code %d)' | |
% (error_code[ret], ret)) | |
# Disconnect from device. | |
ret = -1 | |
ret = lib.LSM_Close(handle) | |
# int LSM_Close (void *handle) | |
if (ret != 0): | |
exit('Error from LSM_Close(): %s (Code %d)' | |
% (error_code[ret], ret)) | |
print('Rest position is now %.2f, %.2f' % (get_position.x, get_position.y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment