00001 #!/usr/local/bin/python 00002 # 00003 # Copyright 2002 00004 # by 00005 # The Board of Trustees of the 00006 # Leland Stanford Junior University. 00007 # All rights reserved. 00008 # 00009 00010 __facility__ = "Online" 00011 __abstract__ = "Example TKR test application" 00012 __author__ = "R. Claus <Claus@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online" 00013 __date__ = "11/21/2002" 00014 __version__ = "$Revision: 1.1 $" 00015 __credits__ = "SLAC" 00016 00017 import LATTE.copyright_SLAC 00018 00019 import threading 00020 from sihippo import * 00021 from time import sleep, time 00022 import random 00023 import math 00024 import logging as log 00025 00026 from LATTE.runcontrol.rcTransitions import rcTransitions 00027 from LATTE.runcontrol.ArgumentImpl import ArgumentImpl 00028 00029 from support.MiniGLTExample import * 00030 00031 # This class puts up a GUI for inputting a value of some sort. It is used by 00032 # the userApplication example to get the number of self triggers to take. 00033 class userArgument(ArgumentImpl): 00034 "GUI for getting the user to input some sort of value." 00035 def __init__(self, parent = None, name = None, modal = 0, fl = 0): 00036 ArgumentImpl.__init__(self, parent, name, modal, fl) 00037 self._value = 0 00038 self.setCaption("Hello world!") 00039 00040 def OKButtonClicked(self): 00041 self._value = int(self.ArgumentList.text().latin1()) 00042 self.close() 00043 00044 def CancelButtonClicked(self): 00045 self.__value = None 00046 self.close() 00047 00048 def getValue(self, caption): 00049 self.setCaption(caption) 00050 self.show() 00051 self.exec_loop() 00052 return self._value 00053 00054 class userArgText(object): 00055 "Text user interface for getting the user to input some sort of value" 00056 def __init__(self): 00057 pass 00058 00059 def getValue(self, caption): 00060 return int(raw_input("%s: " % (caption))) 00061 00062 00063 # Example application implementation. 00064 # Note that the name of the class must be userApplication. 00065 # Look at rcTransitions for names of other transition methods that can be used. 00066 class userApplication(rcTransitions): 00067 "Implmentation class for a user application" 00068 def __init__(self, gui, userId, debug): 00069 rcTransitions.__init__(self, gui, userId, debug) 00070 log.debug("userApplication.__init__()") 00071 self.phase = 0.0 00072 00073 def getName(self): 00074 return __name__ 00075 00076 def setup(self): 00077 log.debug("userApplication.setup()") 00078 00079 if self.gui is None: 00080 self.__arg = userArgText() 00081 else: 00082 self.__arg = self.gui.createGUI(userArgument, self.gui, 'test1', 1) 00083 00084 self.gui.execGUImethod(self.__initHippo) 00085 00086 # A state transition can be rejected by not returning None 00087 return None 00088 00089 00090 def startRun(self): 00091 log.debug("userApplication.startRun()") 00092 self._cmdSynchQuit = False 00093 caption = "Enter number of data groups to read" 00094 if self.gui is None: 00095 cnt = self.__arg.getValue(caption) 00096 else: 00097 cnt = self.gui.execGUImethod(self.__arg.getValue, caption) 00098 00099 # The user pressed 'Cancel' 00100 if cnt is None: return -1 00101 self.__cnt = cnt 00102 00103 self.gui.execGUImethod(self.__initPlots) 00104 00105 #self.hippoDisplay = threading.Thread(None, self.initHippo, 'HippoDisplay', ()) 00106 #self.hippoDisplay.start() 00107 00108 # A state transition can be rejected by not returning None 00109 return None 00110 00111 def stopRun(self): 00112 self._cmdSynchQuit = True 00113 #self.__canvas.savePlotAsImage(self.__hist, "c:/temp/plot.jpg") 00114 #self.__canvas.savePlotAsImage(self.__hist2, "c:/temp/plot2.jpg") 00115 00116 # The STOP_RUN transition can not be rejected 00117 00118 def stop (self): 00119 return self.stopRun() 00120 00121 def teardown(self): 00122 log.debug("userApplication.teardown()") 00123 # Allow the C++ object to be deleted 00124 if self.gui is not None and self.__arg is not None: 00125 self.__arg.deleteLater() 00126 00127 def process(self, (status, buffer)): 00128 "Method called back for each data event taken" 00129 ###print "userApplication.process()" 00130 00131 def commandSynch(self): 00132 "Method called by the command synchronization task" 00133 count = self.__cnt 00134 cnt = 0 00135 while cnt < count and not self._cmdSynchQuit: 00136 if self.phase > math.pi - 0.0001: self.phase = 0.0 00137 cnt = cnt + 1 00138 volts = math.sin(self.phase) * (-1.0 +2.0*random.random()) 00139 amps = 0.8 - (2.0 * self.phase/math.pi) +0.4*random.random() 00140 self.__nt.addRow([cnt, volts]) 00141 self.__nt2.addRow([cnt, amps]) 00142 sleep(.1) 00143 self.phase += math.pi*0.02 00144 00145 def wait(self): 00146 pass 00147 00148 def __initHippo(self): 00149 label1 = ['Time', 'TEM Voltage'] 00150 label2 = ['Time', 'TEM Current'] 00151 ntc = NTupleController.instance() 00152 self.__nt = ntc.createCircularBuffer(2) 00153 self.__nt2 = ntc.createCircularBuffer(2) 00154 self.__nt.setLabels(label1) 00155 self.__nt2.setLabels(label2) 00156 self.__nt.setTitle('GTIC Test') 00157 self.__nt2.setTitle('GTIC Test2') 00158 self.__nt.reserve(100) 00159 self.__nt2.reserve(100) 00160 if __name__ == "__main__": 00161 app = QApplication ( sys.argv ) 00162 self.__canvas = CanvasWindow() 00163 else: 00164 wc = WindowController.instance() 00165 self.__canvas = wc.currentCanvas() 00166 00167 #hist.setRange ( 'y', 2660, 2680 ) 00168 #hist2.setRange ( 'y', 200, 300 ) 00169 00170 def __initPlots(self): 00171 self.__canvas.clear() 00172 self.__nt.clear() 00173 self.__nt2.clear() 00174 #self.__nt.setIntervalCount ( 500 ) 00175 #self.__nt2.setIntervalCount ( 100 ) 00176 dc = DisplayController.instance() 00177 self.__hist = dc.createDisplay('Strip Chart',self.__nt,['Time', 'TEM Voltage']) 00178 self.__hist2 = dc.createDisplay('Strip Chart',self.__nt2,['Time', 'TEM Current']) 00179 self.__canvas.addDisplay(self.__hist) 00180 self.__canvas.addDisplay(self.__hist2) 00181 #self.__canvas.setIntervalEnabled ( 1 ) 00182 self.__canvas.show() 00183 00184 00185 if __name__ == "__main__": 00186 ua = userApplication(None, 321, 0) 00187 ua.rcSetup('../repos/simpleTEMSchema.xml') 00188 ua.rcStartRun() 00189 ua.wait() 00190 ua.rcStopRun() 00191 ua.rcTeardown() 00192 raw_input("Press any key to end") 00193 00194 # History: 00195 # $Log: hTestStrip.py,v $ 00196 # Revision 1.1 2004/08/18 18:00:15 stuvi 00197 # Updated for LATTE 4. 00198 # All hippo functions go through the Qt GUI bridge now. 00199 # 00200 # Revision 1.9 2004/05/03 21:19:47 stuvi 00201 # Moved non-Runcontrol scripts to the support directory and modified imports accordingly. 00202 # 00203 # Revision 1.8 2004/04/17 02:22:17 stuvi 00204 # Various Hippo related fixes 00205 # Added teardown() to delete the C++ object for the userArgument 00206 # 00207 # Revision 1.7 2004/04/07 23:28:29 stuvi 00208 # Modularized hippodraw code. 00209 # 00210 # Revision 1.6 2004/03/31 01:03:21 stuvi 00211 # Fixed so that it works with the new trigger interface. 00212 # 00213 # Revision 1.5 2004/03/10 22:24:50 panetta 00214 # Upgraded trigger interface. 00215 # Removed dependencies on self.glt. 00216 # 00217 # Revision 1.4 2004/02/24 02:13:34 stuvi 00218 # Changes for long transition support. 00219 # 00220 # Revision 1.3 2003/07/17 00:01:35 stuvi 00221 # no message 00222 # 00223 # Revision 1.2 2003/03/31 19:28:53 stuvi 00224 # Fixed name 00225 # 00226 # Revision 1.1 2003/03/11 21:20:54 stuvi 00227 # Moved from RunControl directory 00228 # 00229 # Revision 1.2 2003/02/06 05:20:22 claus 00230 # Added resume; StopRun bug fix 00231 # 00232 # Revision 1.1 2003/01/10 01:41:52 stuvi 00233 # Example strip chart example for hippo 00234 # 00235 # Revision 1.1 2002/12/10 00:59:16 stuvi 00236 # Runcontrol example that uses Hippo. Currently it doesn't work in GUI mode. 00237 # 00238 # Revision 1.4 2002/12/02 17:17:09 claus 00239 # Added provision to allow application to run stand-alone 00240 # 00241 # Revision 1.3 2002/11/28 02:02:04 claus 00242 # Shuffled constructor arguments 00243 # 00244 # Revision 1.2 2002/11/27 23:49:27 claus 00245 # Initialize semaphore with zero count 00246 # 00247 # Revision 1.1 2002/11/27 05:02:48 claus 00248 # Initial version of Run Control 00249 #