|
|
@@ -1,11 +1,9 @@
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
-import matplotlib.animation as animation
|
|
|
-from matplotlib import style
|
|
|
import numpy as np
|
|
|
-import random
|
|
|
import serial
|
|
|
+import sys,csv,time
|
|
|
|
|
|
#initialize serial port
|
|
|
ser = serial.Serial()
|
|
|
@@ -14,54 +12,90 @@ ser.port = "COM3"
|
|
|
ser.baudrate = 115200
|
|
|
ser.timeout = 10 #specify timeout when using readline()
|
|
|
ser.open()
|
|
|
+ser.flushInput()
|
|
|
+
|
|
|
if ser.is_open==True:
|
|
|
print("\nAll right, serial port now open. Configuration:\n")
|
|
|
print(ser, "\n") #print serial parameters
|
|
|
|
|
|
+'''
|
|
|
# Create figure for plotting
|
|
|
fig = plt.figure()
|
|
|
ax = fig.add_subplot(1, 1, 1)
|
|
|
xs = [] # time
|
|
|
ys = [] # shoult temp
|
|
|
rs = [] # is temp
|
|
|
+'''
|
|
|
+
|
|
|
|
|
|
-# This function is called periodically from FuncAnimation
|
|
|
-last_i = 1
|
|
|
-def animate(i, xs, ys):
|
|
|
- global last_i
|
|
|
+plot_window = 60
|
|
|
+x_var = np.array(np.zeros([plot_window]))
|
|
|
+y1_var = np.array(np.zeros([plot_window]))
|
|
|
+y2_var = np.array(np.zeros([plot_window]))
|
|
|
|
|
|
- #Aquire and parse data from serial port
|
|
|
- line=ser.readline() #ascii
|
|
|
+plt.ion()
|
|
|
+fig, ax = plt.subplots()
|
|
|
+# plt.axis([None, None, 0, 120])
|
|
|
+line1, = ax.plot(x_var, y1_var)
|
|
|
+line2, = ax.plot(x_var, y2_var)
|
|
|
+
|
|
|
+count = 0
|
|
|
+while True:
|
|
|
+ while ser.inWaiting()==0:
|
|
|
+ plt.pause(.1)
|
|
|
+ line=ser.readline()
|
|
|
try:
|
|
|
- line.decode()
|
|
|
- except:
|
|
|
- return
|
|
|
+ count = count + 1
|
|
|
+ if len(line) < 3:
|
|
|
+ continue
|
|
|
+
|
|
|
|
|
|
- line_as_list = line.split(b',')
|
|
|
- i = int(line_as_list[0].strip())
|
|
|
+ line = line.decode()
|
|
|
+ print(line.rstrip("\n"))
|
|
|
|
|
|
- if (i-last_i) < 1000:
|
|
|
- return
|
|
|
+ line_as_list = line.split(',')
|
|
|
+ if len(line_as_list) < 3:
|
|
|
+ continue
|
|
|
+
|
|
|
+ i = int(line_as_list[0].strip())
|
|
|
+
|
|
|
+ should_float = float(line_as_list[1].strip())
|
|
|
+ is_float = float(line_as_list[2].strip())
|
|
|
|
|
|
- print(line.decode().strip())
|
|
|
+ if len(sys.argv) > 1:
|
|
|
+ with open(sys.argv[1],"a") as f:
|
|
|
+ writer = csv.writer(f,delimiter=",")
|
|
|
+ writer.writerow([i, should_float, is_float])
|
|
|
|
|
|
- last_i = i
|
|
|
- should_float = float(line_as_list[1].strip())
|
|
|
- is_float = float(line_as_list[2].strip())
|
|
|
-
|
|
|
- # Add x and y to lists
|
|
|
- xs.append(i)
|
|
|
- ys.append(should_float)
|
|
|
- rs.append(is_float)
|
|
|
+ x_var = np.append(x_var, i)
|
|
|
+ x_var = x_var[1:plot_window+1]
|
|
|
+ y1_var = np.append(y1_var, should_float)
|
|
|
+ y1_var = y1_var[1:plot_window+1]
|
|
|
+ y2_var = np.append(y2_var, is_float)
|
|
|
+ y2_var = y2_var[1:plot_window+1]
|
|
|
|
|
|
- # Limit x and y lists to 20 items
|
|
|
- #xs = xs[-20:]
|
|
|
- #ys = ys[-20:]
|
|
|
+ # print(x_var)
|
|
|
+ # print(y1_var)
|
|
|
+ # print(y2_var)
|
|
|
+ line1.set_xdata(x_var[-count:])
|
|
|
+ line2.set_xdata(x_var[-count:])
|
|
|
+ line1.set_ydata(y1_var[-count:])
|
|
|
+ line2.set_ydata(y2_var[-count:])
|
|
|
+ ax.relim()
|
|
|
+ ax.autoscale_view()
|
|
|
+ fig.canvas.draw()
|
|
|
+ fig.canvas.flush_events()
|
|
|
+ except Exception as ex:
|
|
|
+ print(ex)
|
|
|
+ continue
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+'''
|
|
|
# Draw x and y lists
|
|
|
ax.clear()
|
|
|
- ax.plot(xs, ys, label="Experimental Probability")
|
|
|
- ax.plot(xs, rs, label="Theoretical Probability")
|
|
|
+ ax.plot(xs, ys, label="Temp Dial")
|
|
|
+ ax.plot(xs, rs, label="Temp Sensor")
|
|
|
|
|
|
# Format plot
|
|
|
plt.xticks(rotation=45, ha='right')
|
|
|
@@ -69,9 +103,6 @@ def animate(i, xs, ys):
|
|
|
plt.title('This is how I roll...')
|
|
|
plt.ylabel('Relative frequency')
|
|
|
plt.legend()
|
|
|
- plt.axis([None, None, 0, 120]) #Use for arbitrary number of trials
|
|
|
+ plt.axis([None, None, 0, 120])
|
|
|
#plt.axis([1, 100, 0, 1.1]) #Use for 100 trial demo
|
|
|
-
|
|
|
-# Set up plot to call animate() function periodically
|
|
|
-ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys))
|
|
|
-plt.show()
|
|
|
+'''
|