6This script opens a window with chart and reads and draws values of target
7variable given in command line in real-time for 5 seconds. Since
8data acquisition is used, sampling times can be around 1ms (target dependent).
10This script requires 'matplotlib', 'numpy', and 'scipy' to be
11installed. They are available at:
12 http://matplotlib.sourceforge.net/
13 http://www.scipy.org/Download
17matplotlib.use('TkAgg')
24import matplotlib.animation as animation
26import isystem.connect as ic
34 print(
' dataRecorderWithLivePlot.py <recordingTime> <viewTime> <miny> <maxy> <varName> <samplingTime>')
35 print(
' recordingTime - how long to record data in seconds')
36 print(
' viewTime - time interval shown on X axis in seconds')
37 print(
' minY - lower limit on Y axis')
38 print(
' maxY - upper limit on Y axis')
39 print(
' varName - name of the variable to record')
40 print(
' samplingTime - sampling time. It is rounded to the nearest ')
41 print(
' lower available number (1s, 100 ms,')
42 print(
' 10 ms, 1 ms). If specified as 0, the lowest available ')
43 print(
' sampling time is used.')
45 print(
' dataRecorderWithLivePlot.py 10 3 -300 20000 main_loop_counter 0.1 g_baseStruct.i_base 0')
53 raise Exception(
'Not enough command line parameters!')
57 recordingTime = float(sys.argv[1])
58 viewTime = float(sys.argv[2])
59 minY = float(sys.argv[3])
60 maxY = float(sys.argv[4])
63 for idx
in range(5, len(sys.argv), 2):
64 samplingInfo.append([sys.argv[idx], float(sys.argv[idx + 1])])
66 return [recordingTime, viewTime, minY, maxY], samplingInfo
70 cmgr = ic.ConnectionMgr()
71 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
73 debugCtrl = ic.CDebugFacade(cmgr)
74 print(
'Downloading ... ', end=
' ')
77 debugCtrl.runUntilFunction(
'main')
78 debugCtrl.waitUntilStopped()
81 return cmgr, debugCtrl
84def realTimeToDAQTimeFlags(samplTime):
86 Converts the given sampling time in seconds to EDAQSamplingFlags.
89 return ic.CDAQController.daqSampleMax
90 elif samplTime < 0.01:
91 return ic.CDAQController.daqSample1ms
93 return ic.CDAQController.daqSample10ms
95 return ic.CDAQController.daqSample100ms
97 return ic.CDAQController.daqSample1s
100def initDAQ(cmgr, samplingInfo):
101 daqCtrl = ic.CDAQController(cmgr)
104 daqInfo = daqCtrl.info()
105 if daqInfo.getMaxItems() == 0:
106 raise Exception(
"Data Acquisition (DAQ) system is not available.")
108 print(
'MaxItems = ', daqInfo.getMaxItems())
110 daqVariables = ic.DAQConfigVector()
111 for varSamplData
in samplingInfo:
112 varName = varSamplData[0]
113 if varSamplData[1] == 0:
114 daqVariables.append(ic.CDAQConfigItem(varName))
116 samplTime = realTimeToDAQTimeFlags(varSamplData[1])
117 daqVariables.append(ic.CDAQConfigItem(varName, samplTime))
120 daqTimeStart = daqCtrl.status().getTime()
122 daqCtrl.configure(daqVariables)
125 daqCtrl.enableGlobal(
True)
127 return [daqCtrl, daqTimeStart]
130def readValuesFromTarget():
131 timeStamps = pylab.array([])
133 daqStatus = daqCtrl.status()
135 if daqStatus.getNumSamplesAvailable() > 0:
136 if daqStatus.getOverflow():
137 print(
'SAMPLING OVERFLOW!')
140 daqSamples = ic.DAQSampleVector()
141 daqCtrl.read(daqSamples)
143 for daqSample
in daqSamples:
144 timeStamps = pylab.append(timeStamps, (daqSample.getTime() - daqTimeStart) / 1e6)
145 values.append(daqCtrl.getDataValue(daqSample).getLong())
147 return timeStamps, values
151 global timeOffset, xData, yData, endTime, line
153 timeStamps, values = readValuesFromTarget()
155 if time.time() < endTime
and values:
159 toff = timeStamps[-1] - viewTime
162 xData -= toff - timeOffset
165 xData = pylab.append(xData, timeStamps)
166 yData = pylab.append(yData, values)
168 xData = xData[xData > 0]
169 yData = yData[-len(xData):]
171 line.set_xdata(xData)
172 line.set_ydata(yData)
181 line.set_xdata(xData)
182 line.set_ydata(yData)
187 global daqCtrl, yData, xData, line, daqTimeStart, endTime, viewTime, timeOffset
189 plotParams, samplingInfo = parseCmdLine()
190 cmgr, debugCtrl = initTarget()
191 daqCtrl, daqTimeStart = initDAQ(cmgr, samplingInfo)
192 xData = pylab.array([])
193 yData = pylab.array([])
197 ax = fig.add_subplot(111)
198 line, = ax.plot([0], [0])
201 recordingTime, viewTime, minY, maxY = plotParams
202 pylab.axis([0, viewTime, minY, maxY])
204 startTime = time.time()
205 endTime = startTime + recordingTime
207 ani = animation.FuncAnimation(fig, animate, numpy.arange(0, 10), init_func=init,
208 interval=25, blit=
True)
213if __name__ ==
'__main__':