"""
This script plots several variables in one chart. The chart is
automatically scaled so that all the data is visible.
To use this script as a module, import it to your scripts and
call function startRecorder() with the list of variable you want to
observe.
This script requires additional python modules: matplotlib ver 1.1,
and numpy.
"""
import time
import isystem.connect as ic
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
winidea_id = ''
def initTarget(cmgr):
"""
This function initializes the target. Customize it according to
your needs.
"""
debugCtrl = ic.CDebugFacade(cmgr)
debugCtrl.download()
debugCtrl.runUntilFunction("main")
debugCtrl.waitUntilStopped()
debugCtrl.run()
return debugCtrl
def _read_data(watches):
"""
This function reads variables from the target. It is called
periodically from updateChart()
"""
global g_startTime
row = []
for expression in watches:
value = g_debugCtrl.evaluate(ic.IConnectDebug.fRealTime, expression)
row.append(value.getInt())
current_time = time.time() - g_startTime
return current_time, row
def updateChart(num, watches, xRange, isScale):
"""
This function is periodically called by the animation loop.
"""
global xdata, ydata
t, y = _read_data(watches)
xdata = np.hstack((xdata, t))
ydata = np.row_stack((ydata, y))
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
is_redraw = False
if t >= xmax:
if isScale:
ax.set_xlim(xmin, 2*xmax)
is_redraw = True
else:
ax.set_xlim(t - xRange, t)
is_redraw = True
delta_y_max = np.max(ydata)
delta_y_min = np.min(ydata)
delta_y = (delta_y_max - delta_y_min) / 10
if delta_y_max > ymax:
ax.set_ylim(ymin, delta_y_max + delta_y)
is_redraw = True
if delta_y_max < ymin:
ax.set_ylim(delta_y_min - delta_y, ymax)
is_redraw = True
if is_redraw:
ax.figure.canvas.draw()
for i in range(len(g_lines)):
g_lines[i].set_data(xdata, ydata[:, i])
return g_lines,
def startRecorder(isInitTarget, watches, samplingInterval, xRange, isScale):
"""
This is the main function of this module.
Parameters:
isInitTarget - if true, function initTarget() is called, otherwise
the target must be initialized and running
watches - list of variables to plot, for example ['g_int', 'g_char']
samplingInterval - time in milliseconds between reading of samples
xRange - initiali range on x-axis
isScale if true, data is scale along x axis so that everything is visible,
otherwise x-axis is shifted.
"""
global g_lines, ax, xdata, ydata, \
g_debugCtrl, g_startTime
cmgr = ic.ConnectionMgr()
cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
if isInitTarget:
g_debugCtrl = initTarget(cmgr)
else:
g_debugCtrl = ic.CDebugFacade(cmgr)
g_startTime = time.time()
xdata = np.zeros(0)
ydata = np.zeros((0, len(watches)))
figure = plt.figure()
ax = figure.add_subplot(111)
g_lines = ax.plot([0], [[0]*len(watches)], lw=1)
ax.set_ylim(-1, 1)
ax.set_xlim(0, xRange)
ax.grid()
ani = animation.FuncAnimation(figure, updateChart, 40, blit=False,
interval=samplingInterval,
fargs=[watches, xRange, isScale])
plt.show()
if __name__ == '__main__':
startRecorder(True, ['g_complexStruct.i_cplx', 'g_char'], 10, 10, False)