18import isystem.connect
as ic
25 isOpenPyXLInstalled =
True
26except (ImportError)
as ex:
27 isOpenPyXLInstalled =
False
31 isPyLabInstalled =
True
32except (ImportError)
as ex:
33 isPyLabInstalled =
False
37 print(
"Usage: dataRecorderWithDAQ.py <variableName1 samplingRateInSeconds1> ...")
39 print(
" If 'samplingRateInSeconds' == 0, the smallest possible sampling")
40 print(
" time is used. Supported values: 0.001, 0.01, 0.1, 1 second.")
41 print(
" See CDAQController::EDAQSampligFlags for all possible")
45 print(
"Example: dataRecorderWithDAQ.py main_loop_counter 0.1 g_baseStruct.i_base 0.01")
46 print(
" records main_loop_counter each 100 ms, and g_baseStruct.i_base with 10 ms period.")
51 This function initializes the target. Customize it according to
55 debugCtrl = ic.CDebugFacade(cmgr)
58 debugCtrl.runUntilFunction("main")
59 debugCtrl.waitUntilStopped()
66def realTimeToDAQTimeFlags(samplTime):
68 Converts the given sampling time in seconds to EDAQSamplingFlags.
71 return ic.CDAQController.daqSampleMax
72 elif samplTime < 0.01:
73 return ic.CDAQController.daqSample1ms
75 return ic.CDAQController.daqSample10ms
77 return ic.CDAQController.daqSample100ms
79 return ic.CDAQController.daqSample1s
82def recordVariables(cmgr,
85 recordingTimeInSeconds,
90 This function reads varibles and writes them to CSV file.
91 If isRecordToMemory ==
False, the amount of data that can be recorded
is
92 limited by the free disk size.
96 debugCtrl - iSYSTEM
's debugCtrl controller, which provides access to target
98 variables - array of two element arrays, where the first element contains
99 variable name and the second element contains sampling interval,
100 for example: [[
'main_loop_counter', 0.1], [
'g_baseStruct.i_base', 0]].
101 Sampling interval set to 0 means the smallest interval possible.
103 recordingTimeInSeconds - how long to record the data,
in seconds. Recording
104 can also be terminated by pressing the
'q' key
106 fileName - name of the output CSV file.
108 isRecordToMemory -
if True, then data
is also stored into memory
109 array
and returned
as function
return value. Be
110 aware of memory usage
in this case. If false,
111 an empty list
is returned.
113 isPrintToStdOut -
if True, each row
is printed to stdout during recording
116 List of rows, where each row
is a list containing time stamp
and recorded
117 values
in the same order
as variable names were specified. Example
for
118 two samples of three variables:
120 [0.1, 24, -525, 1.78]
124 daqCtrl = ic.CDAQController(cmgr)
127 daqInfo = daqCtrl.info()
128 if daqInfo.getMaxItems() == 0:
129 raise Exception(
"Data Acquisition (DAQ) system is not available.")
131 print(
'MaxItems = ', daqInfo.getMaxItems())
133 daqVariables = ic.DAQConfigVector()
134 for varSamplData
in variables:
135 varName = varSamplData[0]
136 samplTime = realTimeToDAQTimeFlags(varSamplData[1])
138 if varName.startswith(
'0x'):
140 memAddr = int(varName)
141 daqVariables.append(ic.CDAQConfigItem(4, 0, memAddr, samplTime))
144 daqVariables.append(ic.CDAQConfigItem(varName, samplTime))
147 daqTimeStart = daqCtrl.status().getTime()
149 daqCtrl.configure(daqVariables)
152 daqCtrl.enableGlobal(
True)
154 startTime = time.time()
155 endTime = startTime + recordingTimeInSeconds
157 numVars = len(variables)
160 with open(fileName,
'w')
as csvFile:
165 csvWriter = csv.writer(csvFile)
168 for varData
in variables:
169 varNames.append(varData[0])
171 csvWriter.writerow([
'Time'] + varNames)
176 row = [
''] * (1 + numVars)
178 while time.time() < endTime:
180 daqStatus = daqCtrl.status()
182 if daqStatus.getNumSamplesAvailable() > 0:
184 if daqStatus.getOverflow():
185 print(
'SAMPLING OVERFLOW!')
188 daqSamples = ic.DAQSampleVector()
189 daqCtrl.read(daqSamples)
192 for daqSample
in daqSamples:
194 sampleTime = daqSample.getTime() - daqTimeStart
195 columnIndex = daqSample.getIndex()+1
197 var = daqCtrl.getDataValue(daqSample)
199 if var.isTypeUnsigned()
or var.isTypeSigned():
200 value = var.getLong()
201 elif var.isTypeFloat():
202 value = var.getDouble()
203 elif var.isTypeAddress():
204 value = var.getAddress().m_aAddress
205 elif var.isTypeCompound():
209 if (sampleTime != lastTime):
214 csvWriter.writerow(row)
218 recordedData.append(row)
221 row = [
''] * (numVars+1)
225 lastTime = sampleTime
227 row[columnIndex] = value
231 csvWriter.writerow(row)
235 recordedData.append(row)
240def writeDataToXLSX(fileName, data, expressions):
241 book = openpyxl.Workbook()
242 sheet = book.create_sheet()
243 sheet.title =
'Variables'
246 sheet.append([
'Time'] + expressions)
256 if len(sys.argv) < 2:
268 for idx
in range(1, len(sys.argv), 2):
269 varName = sys.argv[idx]
270 varNames.append(varName)
271 samplingInfo.append([varName, float(sys.argv[idx + 1])])
273 cmgr = ic.ConnectionMgr()
274 cmgr.initLogger(
'daq',
'daqExample.log', ic.CLogger.PYTHON)
275 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
276 if not cmgr.isAttached():
277 print(
"The connection to winIDEA has not been established - exiting script.")
280 print(
"Established connection to winIDEA.")
281 debugCtrl = initTarget(cmgr)
284 recordingTimeInSeconds = 3
285 filePrefix =
'daqData'
286 isRecordToMemory =
True
287 isPrintToStdOut =
True
288 print(
'Recording ...')
290 daqResults = recordVariables(cmgr,
293 recordingTimeInSeconds,
298 if isOpenPyXLInstalled
and daqResults:
299 writeDataToXLSX(filePrefix +
'.xlsx', daqResults, varNames)
302 lineTypes = [
'k',
'r',
'g',
'b',
'k:',
'r:',
'g:',
'b:']
305 data = plab.array(daqResults).astype(
'S12')
308 for varName
in varNames:
309 plab.subplot(len(varNames), 1, plotIdx)
314 linesData = data[data[:, plotIdx] != b
'']
315 times = linesData[:, 0]
316 plab.plot(times, linesData[:, plotIdx], lineTypes[(plotIdx - 1)% len(lineTypes)])
325if __name__ ==
'__main__':