Add files via upload

This commit is contained in:
indohito 2022-07-17 13:29:44 -07:00 committed by GitHub
parent 90ac90f6fe
commit cbc0e13484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

69
serial_plot.py Normal file
View File

@ -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()

View File

@ -3,6 +3,7 @@ from datetime import datetime
from threading import * from threading import *
import os, json, sys, wx, subprocess import os, json, sys, wx, subprocess
import serial.tools.list_ports import serial.tools.list_ports
from serial_plot import *
import numpy as np import numpy as np
HEIGHT = 800 HEIGHT = 800
@ -119,6 +120,10 @@ def run(e):
frame.Close() frame.Close()
wx.MessageBox(save_diag, "Info", style=wx.ICON_INFORMATION) 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__': if __name__ == '__main__':
app = wx.App() app = wx.App()
@ -126,6 +131,7 @@ if __name__ == '__main__':
get_devices() get_devices()
frame.SetTitle("Cease your resistance! - a0.0.0") frame.SetTitle("Cease your resistance! - a0.0.0")
frame.btLaunch.Bind(wx.EVT_BUTTON, run) frame.btLaunch.Bind(wx.EVT_BUTTON, run)
frame.plot_but.Bind(wx.EVT_BUTTON, runPlot)
frame.Show() frame.Show()
frame.m_textCtrl26.SetValue("480") frame.m_textCtrl26.SetValue("480")
app.MainLoop() app.MainLoop()