Tobias Simetsreiter %!s(int64=3) %!d(string=hai) anos
pai
achega
0148d95f2f
Modificáronse 2 ficheiros con 74 adicións e 41 borrados
  1. 8 6
      main.ino
  2. 66 35
      plot.py

+ 8 - 6
main.ino

@@ -21,7 +21,7 @@ int dial_pin_1 = 12;
 int dial_pin_2 = 14;
 Atm_encoder dial;
 
-float shouldTemp = 0;
+int shouldTemp = 0;
 float isTemp = 100;
 
 bool wpsRunning = false;
@@ -51,10 +51,10 @@ void setup() {
     // .onPress( led1, led1.EVT_TOGGLE );
 
   
-  dial.begin(dial_pin_1, dial_pin_2, 1)
+  dial.begin(dial_pin_1, dial_pin_2, 2)
     .range( 1, 120 )
     .onChange( [] (int idx, int v, int up ) {
-      shouldTemp = (float)v;
+      shouldTemp = v;
       // Serial.printf("Raw: %d ShouldTemp: %.10f\n", v, shouldTemp);
       checkTemp();
     });
@@ -103,6 +103,8 @@ void temperatureChanged(uint8_t index, float tempC)
     isTemp = tempC;
     // Serial.printf("isTemp: %.10f index %d\n", isTemp, index);
     checkTemp();
+  }  else {
+    isTemp = 120;
   }
 }
 
@@ -127,13 +129,13 @@ void wpsSetup(){
 
 unsigned long lastCheck;
 void checkTemp(){
-  if (isTemp < shouldTemp){
+  if (isTemp < (float)shouldTemp){
     led1.on();
   } else {
     led1.off();
   }
-  if ((millis() - lastCheck) > 1000) {
-    Serial.printf("%d,%.10f,%.10f\n", millis(),shouldTemp,isTemp);
+  if ((millis() - lastCheck) > 50) {
+    Serial.printf("%d,%d,%.10f\n", millis(),shouldTemp,isTemp);
     lastCheck = millis();
   }
 }

+ 66 - 35
plot.py

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