From cbc0e13484b46e8fa2d893a6fa6601898ba605f7 Mon Sep 17 00:00:00 2001 From: indohito <107958888+indohito@users.noreply.github.com> Date: Sun, 17 Jul 2022 13:29:44 -0700 Subject: [PATCH] Add files via upload --- serial_plot.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ test.py | 6 +++++ 2 files changed, 75 insertions(+) create mode 100644 serial_plot.py diff --git a/serial_plot.py b/serial_plot.py new file mode 100644 index 0000000..0b25086 --- /dev/null +++ b/serial_plot.py @@ -0,0 +1,69 @@ +import matplotlib.pyplot as plt +import matplotlib.animation as animate +import math, json, traceback + +try: + import matplotlib + matplotlib.use("QtAgg") # to force use one of the matplot's UI backend instead of an IDE's choice +except ImportError: + traceback.print_exc() + + +def plotter(): + a = json.load(open('settings.json', 'r')) + sensors = len(a['sensor_ports']) + windowsize = a['winSize'] + delay = a["delay"] / 1000 + file = open(a["file_name"], "r") + + colors = ['blue', 'orange', 'green', 'yellow'] + + fig, axs = plt.subplots(1, 1) + + timeStamps = [] + sensorsData = [] + + i = 0 + while i <= sensors: + sensorsData.append([]) + i = i + 1 + + def animation(t): + next_line = file.readline() + if next_line: + line = next_line.split(',') + if len(line) >= sensors: + i = 1 + timeStamps.append(line[0]) + # idk what this condition does.... Nathan wrote this without saying much + if windowsize != 0 and len(timeStamps) > math.ceil(windowsize / delay): + timeStamps.pop(0) + val_lists = [] + while i <= sensors: + if not line[i]: # pass on invalid values + continue + sensorsData[i - 1].append(float(line[i])) # cast it to float so the y-axis will not jump around + # idk what this condition does.... Nathan wrote this without saying much + if windowsize != 0 and len(sensorsData[i - 1]) > math.ceil(windowsize / delay): + sensorsData[i - 1].pop(0) + val_lists.append(sensorsData[i - 1]) + i += 1 + for j in range(len(val_lists)): + axs.plot(val_lists[j], color=colors[j], + label=f'sensor {j + 1}') # TODO: display sensor number to the actual arduino's port & + # axs.annotate('%0.2f' % val_lists[j][-1], xy=(1, val_lists[j][-1]), xytext=(8, 0), + # xycoords=('axes fraction', 'data'), textcoords='offset points') + + + # Acknowledgement: https://stackoverflow.com/a/13589144 + handles, labels = plt.gca().get_legend_handles_labels() + by_label = dict(zip(labels, handles)) + axs.legend(by_label.values(), by_label.keys(), loc='best') + + ani = animate.FuncAnimation(plt.gcf(), func=animation) # , interval=delay * 500) + # ani.save("plot.gif") + # plt.ion() + plt.show(block=True) + + +#plotter() diff --git a/test.py b/test.py index a2943be..c6e2a3a 100644 --- a/test.py +++ b/test.py @@ -3,6 +3,7 @@ from datetime import datetime from threading import * import os, json, sys, wx, subprocess import serial.tools.list_ports +from serial_plot import * import numpy as np HEIGHT = 800 @@ -119,6 +120,10 @@ def run(e): frame.Close() wx.MessageBox(save_diag, "Info", style=wx.ICON_INFORMATION) +def runPlot(e): + plotter() + wx.MessageBox("Your data is being plotted", "Info", style = wx.ICON_INFORMATION) + if __name__ == '__main__': app = wx.App() @@ -126,6 +131,7 @@ if __name__ == '__main__': get_devices() frame.SetTitle("Cease your resistance! - a0.0.0") frame.btLaunch.Bind(wx.EVT_BUTTON, run) + frame.plot_but.Bind(wx.EVT_BUTTON, runPlot) frame.Show() frame.m_textCtrl26.SetValue("480") app.MainLoop()