def setConditions(self, list): self.conditions=ConditionsContainer() for conditionArg in list: if debug: print conditionArg pvName, minimum, maximum, description=conditionArg.split(',') condition=Condition() condition.pvName=pvName condition.minimum=float(minimum) condition.maximum=float(maximum) condition.description=description self.conditions.conditions.append(condition)
def __init__(self, parent=None): super(ProcessControlWidget, self).__init__(parent) self.setupUi(self) self.programName="" self.parameters=FormContainer() self.conditions=ConditionsContainer() self.qprocess=QProcess() #Note that the process output needs to be UNBUFFERED otherwise you read the outputs only at exit! #See python -u , export PYTHONUNBUFFERED=True, Class Unbuffered self.connect(self.qprocess, SIGNAL("readyReadStandardOutput()"), self.readProcessStandardOutput) self.connect(self.qprocess, SIGNAL("readyReadStandardError()"), self.readProcessStandardError) self.connect(self.qprocess, SIGNAL("finished(int)"), self.processExited) self.debug=not(debug % 3)
def __init__(self): super(ProcessControlOvfProcess, self).__init__() self.conditions=ConditionsContainer() self.formContainer=FormContainer(self) self.configurationFile="processControlOvfProcess.tmp" self.dryRun=False
class ProcessControlOvfProcess(ProcessControlProcessTemplate): def __init__(self): super(ProcessControlOvfProcess, self).__init__() self.conditions=ConditionsContainer() self.formContainer=FormContainer(self) self.configurationFile="processControlOvfProcess.tmp" self.dryRun=False #---------------------------------------------------------------------- # Accessors for the command line def configurationFile(self): return self.formContainer.configurationFile def setConfigurationFile(self, str): self.formContainer.configurationFile=str self.conditions.configurationFile=str configurationFile=property(configurationFile, setConfigurationFile) def actuatorPv(self): return self.formContainer.actuatorPv def setActuatorPv(self, str): self.formContainer.actuatorPv=str actuatorPv=property(actuatorPv, setActuatorPv) def readbackPv(self): return self.formContainer.readbackPv def setReadbackPv(self, str): self.formContainer.readbackPv=str readbackPv=property(readbackPv, setReadbackPv) def holdValue(self): return self.formContainer.holdValue def setHoldValue(self, float): self.formContainer.holdValue=float holdValue=property(holdValue, setHoldValue) def numberOfSamplesPerIteration(self): return self.formContainer.numberOfSamplesPerIteration def setNumberOfSamplesPerIteration(self, int): self.formContainer.numberOfSamplesPerIteration=int numberOfSamplesPerIteration=property(numberOfSamplesPerIteration, setNumberOfSamplesPerIteration) def samplingRate(self): return self.formContainer.samplingRate def setSamplingRate(self, float): self.formContainer.samplingRate=float samplingRate=property(samplingRate, setSamplingRate) def actionUpperLimit(self): return self.formContainer.actionUpperLimit def setActionUpperLimit(self, float): self.formContainer.actionUpperLimit=float actionUpperLimit=property(actionUpperLimit, setActionUpper) def actionLowerLimit(self): return self.formContainer.actionLowerLimit def setActionLowerLimit(self, float): self.formContainer.actionLowerLimit=float actionLower=property(actionLowerLimit, setActionLower) def numberOfPreviousSamples(self): return self.formContainer.numberOfPreviousSamples def setNumberOfPreviousSamples(self, int): self.formContainer.numberOfPreviousSamples=int numberOfPreviousSamples=property(numberOfPreviousSamples, setNumberOfPreviousSamples) def operation(self): return self.formContainer.operation def setOperation(self, int): self.formContainer.operation=int operation=property(operation, setOperation) def gain(self): return self.formContainer.gain def setGain(self, float): self.formContainer.gain=float gain=property(gain, setGain) def offsetPv(self): return self.formContainer.offsetPv def setOffsetPv(self, str): self.formContainer.offsetPv=str offsetPv=property(offsetPv, setOffsetPv) def numberOfIterations(self): return self.formContainer.numberOfIterations def setNumberOfIterations(self, int): self.formContainer.numberOfIterations numberOfIterations=property(numberOfIterations, setNumberOfIterations) def pauseBetweenIterations(self): return self.formContainer.pauseBetweenIterations def setPauseBetweenIterations(self, float): self.formContainer.pauseBetweenIterations=float pauseBetweenIterations=property(pauseBetweenIterations, setPauseBetweenIterations) def conditions(self): return self.conditions @LtiInAndOut(debug) def setConditions(self, list): self.conditions=ConditionsContainer() for conditionArg in list: if debug: print conditionArg pvName, minimum, maximum, description=conditionArg.split(',') condition=Condition() condition.pvName=pvName condition.minimum=float(minimum) condition.maximum=float(maximum) condition.description=description self.conditions.conditions.append(condition) #---------------------------------------------------------------------- @LtiInAndOut(debug) def loadFromConfigurationFile(self): self.conditions.loadFromConfigurationFile() self.formContainer.loadFromConfigurationFile() @LtiInAndOut(debug) def runProcess(self): exception=None try: self.processIsRunning=True iteration=0 # Get local version so attributes are not volatile! conditions=copy.deepcopy(self.conditions) formContainer=copy.deepcopy(self.formContainer) if debug: print "\n" print formContainer print "\n" print conditions print "\n" print "dryRun=%s" % self.dryRun ans=raw_input("Ready? [y]/n") # Connect Pvs actuatorPv=LtiEpicsPv(formContainer.actuatorPv) readbackPv=LtiEpicsPv(formContainer.readbackPv) offsetPv=LtiEpicsPv(formContainer.offsetPv) time.sleep(1) if actuatorPv.connected: if debug: print "Connected to %s" % formContainer.actuatorPv else: print actuatorPv raise LtiEpicsCaError('Actuator Pv (%s) not connected' % formContainer.actuatorPv) if readbackPv.connected: if debug: print "Connected to %s" % formContainer.readbackPv else: print readbackPv raise LtiEpicsCaError('Readback Pv (%s) not connected' % formContainer.readbackPv) if offsetPv.connected: if debug: print "Connected to %s" % formContainer.offsetPv else: print offsetPv raise LtiEpicsCaError('Readback Pv (%s) not connected' % formContainer.offsetPv) conditions.connectPvs() # Get fifo sampleFifo=LtiFifoStack() sampleFifo.size=formContainer.numberOfSamplesPerIteration+formContainer.numberOfPreviousSamples while (self.processIsRunning and iteration<formContainer.numberOfIterations): if (not self.processIsPaused) : try: timestamp=time.asctime(time.localtime()) print "" print "+----------------------------------------------------------------------" print "| Iteration: %d / %d " % (iteration, formContainer.numberOfIterations) print "|" print "| Time : %s" % timestamp print "| processId: %d" % self.processId print "|" print "+----------------------------------------------------------------------" print "" # Check conditions are met conditions.checkAllConditionsAreMet() # Acquire samples newSamples=list() while len(newSamples) < formContainer.numberOfSamplesPerIteration: newSamples.append(readbackPv.lti_caget()) time.sleep(1/formContainer.samplingRate) sampleFifo+=newSamples if formContainer.operation==0 : goldenSample=sampleFifo.lti_getAverage() else: raise LtiStackOperation('Unknown operation') slope=formContainer.gain offset=offsetPv.lti_caget() newSetpoint=slope*(formContainer.holdValue-goldenSample) + offset if debug: print sampleFifo print "slope= %f" % slope print "holdValue= %f" % formContainer.holdValue print "goldenSample= %f" % goldenSample print "offset = %f" % offset print "" print "%f = %f * (%f - %f) + % f" % ( newSetpoint, slope, formContainer.holdValue, goldenSample, offset) print "" print "%f --> %s" % ( newSetpoint, actuatorPv.pvName ) if self.dryRun: print "*** DRY RUN ***" else: print "*** MOVING ***" actuatorPv.lti_caput(newSetpoint) except LtiEpicsPvOutOfBounds as e: print "All conditions are not met!" finally: iteration+=1 time.sleep(formContainer.pauseBetweenIterations) except LtiEpicsCaError as e: print e exception=e except LtiStackOperation as e: print e exception=e finally: self.stopProcess() if exception != None: raise exception
class ProcessControlWidget(QWidget, ui_processControlWidget.Ui_processControlWidget): readyWriteStandardOutput = pyqtSignal(str) readyWriteStandardError = pyqtSignal(str) processFinished = pyqtSignal(int) verboseLevelChanged = pyqtSignal(int) processStarted = pyqtSignal() processStopped = pyqtSignal() processPaused = pyqtSignal() processResumed = pyqtSignal() def __init__(self, parent=None): super(ProcessControlWidget, self).__init__(parent) self.setupUi(self) self.programName="" self.parameters=FormContainer() self.conditions=ConditionsContainer() self.qprocess=QProcess() #Note that the process output needs to be UNBUFFERED otherwise you read the outputs only at exit! #See python -u , export PYTHONUNBUFFERED=True, Class Unbuffered self.connect(self.qprocess, SIGNAL("readyReadStandardOutput()"), self.readProcessStandardOutput) self.connect(self.qprocess, SIGNAL("readyReadStandardError()"), self.readProcessStandardError) self.connect(self.qprocess, SIGNAL("finished(int)"), self.processExited) self.debug=not(debug % 3) #---------------------------------------------------------------------- # Public Slots @pyqtSlot('str') def setProgramName(self, str): sefl.programName=str @pyqtSlot('PyQt_PyObject') def setProgramConditions(self, obj): self.conditions=obj if self.debug: print self.conditions @pyqtSlot('PyQt_PyObject') def setProgramParamaters(self, obj): self.parameters=obj if self.debug: print self.parameters @pyqtSlot('bool') def readyToRun(self, flag): if (flag): self.runButton.setEnabled(True) else: self.runButton.setEnabled(False) self.pauseButton.setEnabled(False) self.resumeButton.setEnabled(False) self.stopButton.setEnabled(False) @pyqtSlot('int') def setVerboseLevel(self,level): self.verboseLevel=level self.writeOut("VERBOSE LEVEL (%d)" % level) self.verboseLevelChanged.emit(level) @pyqtSlot() def startProcess(self): self.writeOut("\n#==============================================#\n") commandLine='%s' % self.programName commandLine+=' %s' % self.parameters.getCommandLine() commandLine+=' %s' % self.conditions.getCommandLine() self.writeOut(commandLine) self.writeOut("\n#==============================================#\n") self.writeOut("PROCESS STARTED") self.processStarted.emit() #self.qprocess.start(commandLine) @pyqtSlot() def pauseProcess(self): self.writeOut("PROCESS PAUSED") if self.qprocess.pid()==0: print('Process is not running') else: os.kill(self.qprocess.pid(), signal.SIGUSR1) self.processPaused.emit() @pyqtSlot() def resumeProcess(self): self.writeOut("RESUMING PROCESS") if self.qprocess.pid()==0: print('Process is not running') else: os.kill(self.qprocess.pid(), signal.SIGUSR2) self.processResumed.emit() @pyqtSlot() def stopProcess(self): self.writeOut("PROCESS STOPPED") if self.qprocess.pid()==0: print('Process is not running') else: self.qprocess.kill() self.processStopped.emit() @pyqtSlot('str') def setProgramName(self,programName): self.programName=programName #---------------------------------------------------------------------- # Private slots @LtiInAndOut(debug) def readProcessStandardOutput(self): message=QString(self.qprocess.readAllStandardOutput()) self.writeOut(message) if debug: print message @LtiInAndOut(debug) def readProcessStandardError(self): message=QString(self.qprocess.readAllStandardError()) self.writeErr(message) #---------------------------------------------------------------------- # Signal emittors @LtiSignal(debug) def processExited(self, exitValue): if showSignals: print "processExited -->" self.processFinished.emit(exitValue) self.writeOut("PROCESS FINISHED (%d)" % exitValue) self.readyToRun(True) @LtiSignal(debug) def writeOut(self, message): if showSignals: print "writeOut (%s) -->" % message self.readyWriteStandardOutput.emit(message) @LtiSignal(debug) def writeErr(self, message): if showSignals: print "writeErr (%s) -->" % message self.readyWriteStandardError.emit(message) #---------------------------------------------------------------------- # Widget actions def on_runButton_pressed(self): ''' Starting process ''' self.runButton.setEnabled(False) self.pauseButton.setEnabled(True) self.resumeButton.setEnabled(False) self.stopButton.setEnabled(True) self.startProcess() def on_pauseButton_pressed(self): ''' Pausing feedback process ''' self.runButton.setEnabled(False) self.pauseButton.setEnabled(False) self.resumeButton.setEnabled(True) self.stopButton.setEnabled(True) self.pauseProcess() def on_resumeButton_pressed(self): ''' Resuming feedback process ''' self.runButton.setEnabled(False) self.pauseButton.setEnabled(True) self.resumeButton.setEnabled(False) self.stopButton.setEnabled(True) self.resumeProcess() def on_stopButton_pressed(self): ''' Stopping/killing feedback process ''' self.runButton.setEnabled(True) self.pauseButton.setEnabled(False) self.resumeButton.setEnabled(False) self.stopButton.setEnabled(False) self.stopProcess()
def __init__(self): super(ProcessControlOvfProcess, self).__init__() self.conditions=ConditionsContainer() self.parameters=FormContainer(self) self.configurationFile="processControlOvfProcess.tmp"