WITecCOM.py 17.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# -*- coding: utf-8 -*-
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für 
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>    

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program, see COPYING.  
If not, see <https://www.gnu.org/licenses/>.
"""

import os
import sys
try:
    import pythoncom
    import win32com.client
except ImportError:
    os.environ["NO_WITEC_CONTROL"] = "True"
    
from time import sleep, time
31 32 33 34 35 36
try:            #when running the witectesting, the paths have to be differently, as it is in the same directory as the other raman com modules
    from ramancom.ramanbase import RamanBase
    from ramancom.configRaman import RamanSettingParam
except:
    from ramanbase import RamanBase
    from configRaman import RamanSettingParam
37 38 39 40 41
from socket import gethostname


class WITecCOM(RamanBase):
    CLSID = "{C45E77CE-3D66-489A-B5E2-159F443BD1AA}"
42 43 44 45 46 47
    
    magn = 20
    
    ramanParameters = [RamanSettingParam('IntegrationTime (s)', 'double', default=0.5, minVal=0.01, maxVal=100),
                       RamanSettingParam('Accumulations', 'int', default=5, minVal=1, maxVal=100)]
    
48 49 50 51 52 53 54 55 56 57 58 59 60
    def __init__(self, hostname=None):
        super().__init__()
        if hostname is None:
            hostname = gethostname()
        self.IBUCSAccess = win32com.client.DispatchEx(self.CLSID, machine=hostname, 
                                                      clsctx=pythoncom.CLSCTX_REMOTE_SERVER)
        
    def connect(self):
        if not self.IBUCSAccess.RequestWriteAccess(True):
            self.connected = False
            return False
        
        IBUCSCore = win32com.client.CastTo(self.IBUCSAccess, 'IBUCSCore')
61
        self.VersionInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|Application|ProgramVersion")
Raman's avatar
Raman committed
62 63 64 65 66 67 68
        self.VersionMan = win32com.client.CastTo(self.VersionInterface, 'IBUCSStatusContainer')
        self.VersionMan.Update()
        version = self.VersionMan.GetSingleValueAsString()[1]
        try:
            vnr = version.split(",")[1].split(" Release")[0].strip()
        except:
            vnr = ""                
69 70 71 72 73
        try:
            vnr = float(vnr)
        except ValueError:
            print("WITec: unknown version format:", version)
            vnr = 0.0 # version format unknown        
Raman's avatar
Raman committed
74
        self.version = vnr
75
        
76 77 78 79 80 81 82 83
        self.PosXInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SamplePositioning|AbsolutePositionX")
        self.PosYInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SamplePositioning|AbsolutePositionY")
        self.PosZInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|ScanTable|PositionMicroscopeZ")
        self.GoToInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SamplePositioning|GoToPosition")
        self.PosXCurInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|SamplePositioner|CurrentPositionX")
        self.PosYCurInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|SamplePositioner|CurrentPositionY")
        self.PosZCurInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|SamplePositioner|CurrentPositionZ")
        self.PosZCurUserInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Hardware|Controller|DataChannels|StepperMotorPosition")
Lars Bittrich's avatar
Lars Bittrich committed
84
        
85 86 87 88 89 90 91 92 93
        self.PosXFloatMan = win32com.client.CastTo(self.PosXInterface, 'IBUCSFloat')
        self.PosYFloatMan = win32com.client.CastTo(self.PosYInterface, 'IBUCSFloat')
        self.PosZFloatMan = win32com.client.CastTo(self.PosZInterface, 'IBUCSFloat')
        self.PosXCurFloatMan = win32com.client.CastTo(self.PosXCurInterface, 'IBUCSStatusContainer')
        self.PosYCurFloatMan = win32com.client.CastTo(self.PosYCurInterface, 'IBUCSStatusContainer')
        self.PosZCurFloatMan = win32com.client.CastTo(self.PosZCurInterface, 'IBUCSStatusContainer')
        self.PosZCurUserFloatMan = win32com.client.CastTo(self.PosZCurUserInterface, 'IBUCSStatusContainer')
        self.GoToTrigger = win32com.client.CastTo(self.GoToInterface, 'IBUCSTrigger')
        
94 95 96 97 98 99 100 101 102 103 104 105 106
        if vnr<5.1:
            self.ImageNameInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MultiCommVideoSystem|BitmapFileName")
            self.ImageNameMan = win32com.client.CastTo(self.ImageNameInterface, 'IBUCSString')
            self.ImageSaveInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MultiCommVideoSystem|SaveColorBitmapToFile")
            self.ImageSaveMan = win32com.client.CastTo(self.ImageSaveInterface, 'IBUCSTrigger')
            
            self.ImageHeightInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageHeightMicrons")
            self.ImageHeightMan = win32com.client.CastTo(self.ImageHeightInterface, 'IBUCSFloat')
            self.ImageWidthInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageWidthMicrons")
            self.ImageWidthMan = win32com.client.CastTo(self.ImageWidthInterface, 'IBUCSFloat')
            self.ImageRotationInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|RotationDegrees")
            self.ImageRotationMan = win32com.client.CastTo(self.ImageRotationInterface, 'IBUCSFloat')
        else:
Raman's avatar
Raman committed
107
            self.ImageNameInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|VideoImageFileName")
108
            self.ImageNameMan = win32com.client.CastTo(self.ImageNameInterface, 'IBUCSString')
Raman's avatar
Raman committed
109
            self.ImageSaveInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|AcquireVideoImageToFile")
110 111
            self.ImageSaveMan = win32com.client.CastTo(self.ImageSaveInterface, 'IBUCSTrigger')
            
Raman's avatar
Raman committed
112
            self.ImageHeightInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageHeightMicrons")
113
            self.ImageHeightMan = win32com.client.CastTo(self.ImageHeightInterface, 'IBUCSFloat')
Raman's avatar
Raman committed
114
            self.ImageWidthInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageWidthMicrons")
115
            self.ImageWidthMan = win32com.client.CastTo(self.ImageWidthInterface, 'IBUCSFloat')
Raman's avatar
Raman committed
116
            self.ImageRotationInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|RotationDegrees")
117
            self.ImageRotationMan = win32com.client.CastTo(self.ImageRotationInterface, 'IBUCSFloat')
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        
        self.SequencerStartInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerSingleSpectrum|Start")
        self.SequencerStartTrigger = win32com.client.CastTo(self.SequencerStartInterface, 'IBUCSTrigger')
        
        self.SequencerBusyInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|Sequencers|IsASequencerActive")
        self.SequencerBusyStatus = win32com.client.CastTo(self.SequencerBusyInterface, 'IBUCSStatusContainer') 
        
        self.TimeSeriesSlowNumMeasurementsInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|AmountOfMeasurements")
        self.TimeSeriesSlowNumMeasurementsMan = win32com.client.CastTo(self.TimeSeriesSlowNumMeasurementsInterface, 'IBUCSInt')
        self.TimeSeriesSlowNameInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|Naming|DataName")
        self.TimeSeriesSlowNameMan = win32com.client.CastTo(self.TimeSeriesSlowNameInterface, 'IBUCSString')
        self.TimeSeriesSlowNumAccumulationsInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|SpectrumAcquisition|Accumulations")
        self.TimeSeriesSlowNumAccumulationsMan = win32com.client.CastTo(self.TimeSeriesSlowNumAccumulationsInterface, 'IBUCSInt')
        self.TimeSeriesSlowIntTimeInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|SpectrumAcquisition|IntegrationTime")
        self.TimeSeriesSlowIntTimeMan = win32com.client.CastTo(self.TimeSeriesSlowIntTimeInterface, 'IBUCSFloat')
        self.TimeSeriesSlowNumInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|Naming|DataNumber")
        self.TimeSeriesSlowNumMan = win32com.client.CastTo(self.TimeSeriesSlowNumInterface, 'IBUCSInt')
        self.TimeSeriesSlowModeInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|MeasurementMode")
        self.TimeSeriesSlowModeMan = win32com.client.CastTo(self.TimeSeriesSlowModeInterface, 'IBUCSEnum')
        self.TimeSeriesSlowNextInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|NextMeasurement")
        self.TimeSeriesSlowNextMan = win32com.client.CastTo(self.TimeSeriesSlowNextInterface, 'IBUCSTrigger')
        
        self.TimeSeriesSlowStartInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|Start")
        self.TimeSeriesSlowStartMan = win32com.client.CastTo(self.TimeSeriesSlowStartInterface, 'IBUCSTrigger')
        
        self.TimeSeriesSlowStopInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|StopSequencer")
        self.TimeSeriesSlowStopMan = win32com.client.CastTo(self.TimeSeriesSlowStopInterface, 'IBUCSTrigger')
        
        self.TimeSeriesSlowIndNextInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|IndexOfNextMeasurement")
        self.TimeSeriesSlowIndNextMan = win32com.client.CastTo(self.TimeSeriesSlowIndNextInterface, 'IBUCSInt')
        
        self.TimeSeriesSlowActivityInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|Sequencers|ActiveSequencer|CurrentActivity")
        self.TimeSeriesSlowActivityMan = win32com.client.CastTo(self.TimeSeriesSlowActivityInterface, 'IBUCSStatusContainer')
        
        self.RamanRelativeXInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|ProbePosition|RelativeX")
        self.RamanRelativeXMan = win32com.client.CastTo(self.RamanRelativeXInterface, 'IBUCSFloat')
        self.RamanRelativeYInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|ProbePosition|RelativeY")
        self.RamanRelativeYMan = win32com.client.CastTo(self.RamanRelativeYInterface, 'IBUCSFloat')
        
        self.BrightnessInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|WhiteLight|Top|BrightnessPercentage")
        self.BrightnessMan = win32com.client.CastTo(self.BrightnessInterface, 'IBUCSFloat')
        
        self.connected = True
        return True
    
    def getBrightness(self):
        assert self.connected
        return self.BrightnessMan.GetValue()
    
    def setBrightness(self, newval):
        if newval<0: 
            newval = 0.0
        elif newval>100.:
            newval = 100.0
        self.BrightnessMan.SetValue(newval)
        sleep(.1)
    
    def getRamanPositionShift(self):
        rx = .5-self.RamanRelativeXMan.GetValue() # current assumption is, that image center is current position
        ry = self.RamanRelativeYMan.GetValue()-.5
        width, height = self.ImageWidthMan.GetValue(), self.ImageHeightMan.GetValue()
Lars Bittrich's avatar
Lars Bittrich committed
179
        return rx*width, ry*height        
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    
    def disconnect(self):
        if self.connected:
            self.IBUCSAccess.RequestWriteAccess(False)
        self.connected = False
    
    def getPosition(self):
        assert self.connected
        self.PosXCurFloatMan.Update()
        x = self.PosXCurFloatMan.GetSingleValueAsDouble()[1]
        self.PosYCurFloatMan.Update()
        y = self.PosYCurFloatMan.GetSingleValueAsDouble()[1]
        self.PosZCurFloatMan.Update()
        z = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
        return x, y, z
    
    def getSoftwareZ(self):
        self.PosZCurFloatMan.Update()
        z = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
        return z
    
    def getUserZ(self):
        assert self.connected
        self.PosZCurUserFloatMan.Update()
        z = self.PosZCurUserFloatMan.GetSingleValueAsDouble()[1]
        return z
        
207
    def moveToAbsolutePosition(self, x, y, z=None, epsxy=0.11, epsz=0.011, debugReturn=False, measurementRunning=False):
208 209 210 211 212 213 214 215 216 217 218 219 220 221
        assert self.connected
        initpos = self.getPosition()
        # move only if new position is really different; repeat if new position is ignored (happens some times)
        while max(abs(initpos[0]-x), abs(initpos[1]-y))>epsxy:
            t0 = time()
            self.PosXFloatMan.SetValue(x)
            self.PosYFloatMan.SetValue(y)
            self.GoToTrigger.OperateTrigger()
            
            # wait till position is found within accuracy of epsxy; check if position changes at all
            distance = 2*epsxy
            while distance > epsxy:# and (lastpos is None or lastpos!=curpos):
                curpos = self.getPosition()
                distance = max(abs(curpos[0]-x), abs(curpos[1]-y))
222 223
                if ((time()-t0>0.5) and max(abs(curpos[0]-initpos[0]), abs(curpos[1]-initpos[1]))<epsxy) or (time()-t0>10.):
                    print("WARNING: signal ignored:", time()-t0, x, y, curpos, initpos)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
                    sys.stdout.flush()
                    break
                sleep(.01)
            sleep(.1)
            initpos = self.getPosition()
        if z is not None:
            self.moveZto(z, epsz)
            
    def moveZto(self, z, epsz=0.011):
        assert self.connected
        #z = round(z,2)
        while abs(self.PosZCurFloatMan.GetSingleValueAsDouble()[1]-z)>epsz:
            initpos = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
            t0 = time()
            self.PosZFloatMan.SetValue(z)
            distance = 2*epsz
            while distance > epsz:
                self.PosZCurFloatMan.Update()
                curpos = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
                distance = abs(curpos-z)
                if ((time()-t0>0.5) and abs(curpos-initpos)<epsz)  or (time()-t0>10.):
                    print("WARNING: signal z ignored:", time()-t0)
                    sys.stdout.flush()
                    break
                sleep(.01)
            sleep(.1)
        
    def saveImage(self, fname):
        assert self.connected
        self.ImageNameMan.SetValue(fname)
        self.ImageSaveMan.OperateTrigger()
        sleep(.1)
        
257
    def getImageDimensions(self, mode = 'df'):
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        """ Get the image width and height in um and the orientation angle in degrees.
        """
        assert self.connected
        width, height = self.ImageWidthMan.GetValue(), self.ImageHeightMan.GetValue()
        angle = self.ImageRotationMan.GetValue()
        return width, height, angle
    
    def startSinglePointScan(self):
        assert self.connected
        self.SequencerStartTrigger.OperateTrigger()
        # Wait until sequencer has finished
        while True:
            self.SequencerBusyStatus.Update()
            Busy = self.SequencerBusyStatus.GetSingleValueAsInt()[1]
            if not Busy:
                break
            
275
    def initiateMeasurement(self, ramanSettings):
276
        assert self.connected
277 278 279 280 281
        self.timeseries = ramanSettings['numPoints']
        self.TimeSeriesSlowNameMan.SetValue(ramanSettings['filename'])
        self.TimeSeriesSlowNumMeasurementsMan.SetValue(ramanSettings['numPoints'])
        self.TimeSeriesSlowNumAccumulationsMan.SetValue(ramanSettings['Accumulations'])
        self.TimeSeriesSlowIntTimeMan.SetValue(ramanSettings['IntegrationTime (s)'])
282 283 284
        self.TimeSeriesSlowModeMan.SetValueNumeric(0)
        self.TimeSeriesSlowNumMan.SetValue(0)
        self.TimeSeriesSlowStartMan.OperateTrigger()
285
        
286 287 288 289 290 291 292 293 294 295 296 297 298 299
        sleep(0.1)
        t1 = time()
        while True:
            self.TimeSeriesSlowActivityMan.Update()
            valid, act = self.TimeSeriesSlowActivityMan.GetSingleValueAsString()
            if act=="Waiting for next Measurement":
                break
            else:
                sleep(0.02)
                if time()-t1>3.:
                    print("Waiting for measurement ready...")
                    t1 = time()
        
        
300
    def triggerMeasurement(self, num):
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        assert self.timeseries
        self.TimeSeriesSlowNextMan.OperateTrigger()
        # Wait until sequencer has finished
        sleep(0.1)
        t1 = time()
        while True:
            ind = self.TimeSeriesSlowIndNextMan.GetValue()
            if ind>num:
                break
            else:
                sleep(0.02)
                if time()-t1>3.:
                    print("Waiting for next index...")
                    t1 = time()
        while True:
            valid, act = self.TimeSeriesSlowActivityMan.GetSingleValueAsString()
            if act=="Waiting for next Measurement":
                break
            else:
                sleep(0.02)
                if time()-t1>3.:
                    print("Waiting for measurement ready...")
                    t1 = time()
        if num==self.timeseries-1:
            self.timeseries = False

327 328
    def finishMeasurement(self):
        pass