15matplotlib.use(
'TkAgg')
22import matplotlib.animation
as animation
24import isystem.connect
as ic
32 print(
' dataRecorderWithLivePlot.py <recordingTime> <viewTime> <miny> <maxy> <varName> <samplingTime>')
33 print(
' recordingTime - how long to record data in seconds')
34 print(
' viewTime - time interval shown on X axis in seconds')
35 print(
' minY - lower limit on Y axis')
36 print(
' maxY - upper limit on Y axis')
37 print(
' varName - name of the variable to record')
38 print(
' samplingTime - sampling time. It is rounded to the nearest ')
39 print(
' lower available number (1s, 100 ms,')
40 print(
' 10 ms, 1 ms). If specified as 0, the lowest available ')
41 print(
' sampling time is used.')
43 print(
' dataRecorderWithLivePlot.py 10 3 -300 20000 main_loop_counter 0.1 g_baseStruct.i_base 0')
51 raise Exception(
'Not enough command line parameters!')
55 recordingTime = float(sys.argv[1])
56 viewTime = float(sys.argv[2])
57 minY = float(sys.argv[3])
58 maxY = float(sys.argv[4])
61 for idx
in range(5, len(sys.argv), 2):
62 samplingInfo.append([sys.argv[idx], float(sys.argv[idx + 1])])
64 return [recordingTime, viewTime, minY, maxY], samplingInfo
68 cmgr = ic.ConnectionMgr()
69 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
71 debugCtrl = ic.CDebugFacade(cmgr)
72 print(
'Downloading ... ', end=
' ')
75 debugCtrl.runUntilFunction(
'main')
76 debugCtrl.waitUntilStopped()
79 return cmgr, debugCtrl
82def realTimeToDAQTimeFlags(samplTime):
84 Converts the given sampling time in seconds to EDAQSamplingFlags.
87 return ic.CDAQController.daqSampleMax
88 elif samplTime < 0.01:
89 return ic.CDAQController.daqSample1ms
91 return ic.CDAQController.daqSample10ms
93 return ic.CDAQController.daqSample100ms
95 return ic.CDAQController.daqSample1s
98def initDAQ(cmgr, samplingInfo):
99 daqCtrl = ic.CDAQController(cmgr)
102 daqInfo = daqCtrl.info()
103 if daqInfo.getMaxItems() == 0:
104 raise Exception(
"Data Acquisition (DAQ) system is not available.")
106 print(
'MaxItems = ', daqInfo.getMaxItems())
108 daqVariables = ic.DAQConfigVector()
109 for varSamplData
in samplingInfo:
110 varName = varSamplData[0]
111 if varSamplData[1] == 0:
112 daqVariables.append(ic.CDAQConfigItem(varName))
114 samplTime = realTimeToDAQTimeFlags(varSamplData[1])
115 daqVariables.append(ic.CDAQConfigItem(varName, samplTime))
118 daqTimeStart = daqCtrl.status().getTime()
120 daqCtrl.configure(daqVariables)
123 daqCtrl.enableGlobal(
True)
125 return [daqCtrl, daqTimeStart]
128def readValuesFromTarget():
129 timeStamps = pylab.array([])
131 daqStatus = daqCtrl.status()
133 if daqStatus.getNumSamplesAvailable() > 0:
134 if daqStatus.getOverflow():
135 print(
'SAMPLING OVERFLOW!')
138 daqSamples = ic.DAQSampleVector()
139 daqCtrl.read(daqSamples)
141 for daqSample
in daqSamples:
142 timeStamps = pylab.append(timeStamps, (daqSample.getTime() - daqTimeStart) / 1e6)
143 values.append(daqCtrl.getDataValue(daqSample).getLong())
145 return timeStamps, values
149 global timeOffset, xData, yData, endTime, line
151 timeStamps, values = readValuesFromTarget()
153 if time.time() < endTime
and values:
157 toff = timeStamps[-1] - viewTime
160 xData -= toff - timeOffset
163 xData = pylab.append(xData, timeStamps)
164 yData = pylab.append(yData, values)
166 xData = xData[xData > 0]
167 yData = yData[-len(xData):]
169 line.set_xdata(xData)
170 line.set_ydata(yData)
179 line.set_xdata(xData)
180 line.set_ydata(yData)
185 global daqCtrl, yData, xData, line, daqTimeStart, endTime, viewTime, timeOffset
187 plotParams, samplingInfo = parseCmdLine()
188 cmgr, debugCtrl = initTarget()
189 daqCtrl, daqTimeStart = initDAQ(cmgr, samplingInfo)
190 xData = pylab.array([])
191 yData = pylab.array([])
195 ax = fig.add_subplot(111)
196 line, = ax.plot([0], [0])
199 recordingTime, viewTime, minY, maxY = plotParams
200 pylab.axis([0, viewTime, minY, maxY])
202 startTime = time.time()
203 endTime = startTime + recordingTime
205 ani = animation.FuncAnimation(fig, animate, numpy.arange(0, 10), init_func=init,
206 interval=25, blit=
True)
211if __name__ ==
'__main__':