[ui/plots] plotting now two different channels from the buffer

This commit is contained in:
wendtalexander 2024-10-24 11:51:17 +02:00
parent 1fe854b74e
commit acd41302c7

View File

@ -15,7 +15,8 @@ class Continously:
def __init__(self, figure: pg.GraphicsLayoutWidget, buffer: CircBuffer): def __init__(self, figure: pg.GraphicsLayoutWidget, buffer: CircBuffer):
self.figure = figure self.figure = figure
self.buffer = buffer self.buffer = buffer
self.last_plotted_index = 0 self.last_plotted_index_analog_in_0 = 0
self.last_plotted_index_analog_in_1 = 0
self.timer = QTimer() self.timer = QTimer()
def plot(self, *args, **kwargs): def plot(self, *args, **kwargs):
@ -24,14 +25,25 @@ class Continously:
prev_plot = self.figure.getItem(row=0, col=0) prev_plot = self.figure.getItem(row=0, col=0)
if prev_plot: if prev_plot:
self.figure.removeItem(prev_plot) self.figure.removeItem(prev_plot)
self.continous_ax = self.figure.addPlot(row=0, col=0) self.analog_in_0 = self.figure.addPlot(row=0, col=0)
self.analog_in_1 = self.figure.addPlot(row=1, col=0)
pen = pg.mkPen("red") pen = pg.mkPen("red")
self.time = np.zeros(self.buffer.size) self.time_analog_in_0 = np.zeros(self.buffer.size)
self.data = np.empty(self.buffer.size) self.data_analog_in_0 = np.empty(self.buffer.size)
self.line = self.continous_ax.plot( self.line_analog_in_0 = self.analog_in_0.plot(
self.time, self.time_analog_in_0,
self.data, self.data_analog_in_0,
pen=pen,
# symbol="o",
)
pen = pg.mkPen("red")
self.time_analog_in_1 = np.zeros(self.buffer.size)
self.data_analog_in_1 = np.empty(self.buffer.size)
self.line_analog_in_1 = self.analog_in_1.plot(
self.time_analog_in_1,
self.data_analog_in_1,
pen=pen, pen=pen,
# symbol="o", # symbol="o",
) )
@ -41,26 +53,61 @@ class Continously:
self.PLOT_HISTORY = 500_000 # The amount of data you want to keep on screen self.PLOT_HISTORY = 500_000 # The amount of data you want to keep on screen
self.timer.setInterval(150) self.timer.setInterval(150)
self.timer.timeout.connect(self.update_plot) self.timer.timeout.connect(self.update_plot)
self.timer.timeout.connect(self.update_plot_1)
self.timer.start() self.timer.start()
def update_plot(self): def update_plot(self):
current_index = self.buffer.write_index() current_index = self.buffer.write_index()
total_count = self.buffer.totalcount() total_count = self.buffer.totalcount(channel=0)
start_time = time.time()
if total_count - self.last_plotted_index_analog_in_0 >= self.CHUNK_PLOT:
try:
times, items = self.buffer.read(
self.last_plotted_index_analog_in_0,
extend=self.CHUNK_PLOT,
channel=0,
)
self.time_analog_in_0 = np.concatenate((self.time_analog_in_0, times))[
-self.PLOT_HISTORY :
]
self.data_analog_in_0 = np.concatenate((self.data_analog_in_0, items))[
-self.PLOT_HISTORY :
]
self.line_analog_in_0.setData(
self.time_analog_in_0,
self.data_analog_in_0,
)
self.last_plotted_index_analog_in_0 += self.CHUNK_PLOT
except IndexError:
log.error("Could not acces the data from the buffer for plotting")
end_time = time.time()
log.debug(f"total time for plotting {end_time - start_time}")
else:
pass
def update_plot_1(self):
total_count = self.buffer.totalcount(channel=1)
start_time = time.time() start_time = time.time()
if total_count - self.last_plotted_index >= self.CHUNK_PLOT: if total_count - self.last_plotted_index_analog_in_1 >= self.CHUNK_PLOT:
try: try:
times, items = self.buffer.read( times, items = self.buffer.read(
self.last_plotted_index, self.last_plotted_index_analog_in_1,
extend=self.CHUNK_PLOT, extend=self.CHUNK_PLOT,
channel=1,
) )
self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :] self.time_analog_in_1 = np.concatenate((self.time_analog_in_0, times))[
self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :] -self.PLOT_HISTORY :
self.line.setData( ]
self.time, self.data_analog_in_1 = np.concatenate((self.data_analog_in_0, items))[
self.data, -self.PLOT_HISTORY :
]
self.line_analog_in_1.setData(
self.time_analog_in_1,
self.data_analog_in_1,
) )
self.last_plotted_index += self.CHUNK_PLOT self.last_plotted_index_analog_in_1 += self.CHUNK_PLOT
except IndexError: except IndexError:
log.error("Could not acces the data from the buffer for plotting") log.error("Could not acces the data from the buffer for plotting")
end_time = time.time() end_time = time.time()
@ -70,19 +117,26 @@ class Continously:
def stop_plotting(self): def stop_plotting(self):
self.timer.stop() self.timer.stop()
if self.last_plotted_index > 0: if self.last_plotted_index_analog_in_0 > 0:
total_count = self.buffer.totalcount() total_count = self.buffer.totalcount()
times, items = self.buffer.read( times, items = self.buffer.read(
self.last_plotted_index, self.last_plotted_index_analog_in_0,
extend=total_count - self.last_plotted_index, extend=total_count - self.last_plotted_index_analog_in_0,
)
self.time_analog_in_0 = np.concatenate((self.time_analog_in_0, times))[
-self.PLOT_HISTORY :
]
self.data_analog_in_0 = np.concatenate((self.data_analog_in_0, items))[
-self.PLOT_HISTORY :
]
self.line_analog_in_0.setData(
self.time_analog_in_0,
self.data_analog_in_0,
) )
self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :] self.last_plotted_index_analog_in_0 += (
self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :] total_count - self.last_plotted_index_analog_in_0
self.line.setData(
self.time,
self.data,
) )
self.last_plotted_index += total_count - self.last_plotted_index
def refresh(self): def refresh(self):
self.continous_ax.enableAutoRange() self.analog_in_0.enableAutoRange()
self.analog_in_1.enableAutoRange()