Explorar el Código

removed matplotlib

subDesTagesMitExtraKaese hace 4 años
padre
commit
013647744f

+ 1 - 1
software/analogPressure/mcp3008.py

@@ -17,7 +17,7 @@ class MCP3008:
         return data
 
     def getVoltage(self, channel):
-        return self.read(channel) / 1023.0 * 3.3
+        return self.read(channel) / 1023.0 * 5
             
     def close(self):
         self.spi.close()

+ 4 - 3
software/appWindow.py

@@ -101,6 +101,7 @@ class Main(tk.Tk, Table):
       b1.pack()
       
   def interval(self):
+    self.after(300,self.interval)
     start = time.time()
     adcValue = self.adc.getVoltage(0)
     windSpeed = adcValue * 1337
@@ -122,10 +123,10 @@ class Main(tk.Tk, Table):
     print("sensors: {:8.3f} ms".format((time.time() - start)*1000))
 
     start = time.time()
-    #for frame in self.frames:
-    self.currentFrame.update()
+    for frame in self.frames:
+      self.frames[frame].update(self.frames[frame] == self.currentFrame)
 
     print("draw:    {:8.3f} ms".format((time.time() - start)*1000))
-    self.after(300,self.interval)
+    
 
           

+ 14 - 15
software/ui/Page_1.py

@@ -1,15 +1,9 @@
 import tkinter as tk
 import tk_tools
+import time
 from .Plot import Plot
 from .globals import *
 
-import matplotlib
-from matplotlib.backends.backend_tkagg import (
-    FigureCanvasTkAgg, NavigationToolbar2Tk)
-from matplotlib.figure import Figure
-from matplotlib import style
-
-
 class Page_1(tk.Frame):
   def __init__(self, parent, controller):
     tk.Frame.__init__(self, parent)
@@ -17,11 +11,15 @@ class Page_1(tk.Frame):
     self.controller = controller
 
     # graph
-    self.serialPlot = Plot(20)
-    canvas = FigureCanvasTkAgg(self.serialPlot.fig, self)
-    canvas.draw()
-    canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
-    canvas._tkcanvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
+    self.serialPlot = Plot(nPoints=100, xaxis=(0, 100), yaxis=(0, 5), nGraphs=2,
+                      ytitle="Windgeschwindigkeit m/s",
+                      xtitle="Messpunkte [1/3-Sekunde]",
+                      title="Geschwindigkeitsverlauf",
+                      line_colors=["#2222ff", "#22ff22", "#ff2222"])
+
+    canvas = self.serialPlot.create_canvas(self)
+    canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
+
     # right menu
     left = tk.Frame(self, borderwidth=2, relief="solid")
     container = tk.Frame(left, borderwidth=2, relief="solid")
@@ -45,6 +43,7 @@ class Page_1(tk.Frame):
 
     controller.pid.SetPoint = 0.4 # m/s
 
-  def update(self):
-    self.serialPlot.update([self.controller.getLastValue("force_X_1")])
-    self.label4.config(text="{:3d} Nm".format(3))
+  def update(self, visible):
+    self.serialPlot.update([self.controller.getLastValue("adc_0"), (time.time()/0.3) % 1 * 5], visible=visible)
+    if visible:
+      self.label4.config(text="{:5.3f} V".format(self.controller.getLastValue("adc_0")))

+ 15 - 20
software/ui/Page_2.py

@@ -1,13 +1,7 @@
 import tkinter as tk
 import tk_tools
-
-from .globals import *
 from .Plot import Plot
-
-from matplotlib.backends.backend_tkagg import (
-    FigureCanvasTkAgg, NavigationToolbar2Tk)
-from matplotlib.figure import Figure
-from matplotlib import style
+from .globals import *
 
 class Page_2(tk.Frame):
   def __init__(self, parent, controller):
@@ -17,16 +11,17 @@ class Page_2(tk.Frame):
     label.pack(pady=10,padx=10)
     
     # graphs
-    self.forcePlots = [Plot(20, 3), Plot(20, 3), Plot(20, 3)]
-    for plot in self.forcePlots:
-      canvas = FigureCanvasTkAgg(plot.fig, self)
-      canvas.draw()
-      canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
-      canvas._tkcanvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
-  def update(self):
-    for i in range(3):
-      self.forcePlots[i].update([
-        self.controller.getLastValue("force_X_{}".format(i+1)),
-        self.controller.getLastValue("force_Y_{}".format(i+1)),
-        self.controller.getLastValue("force_Z_{}".format(i+1)),
-      ])
+    self.forcePlot = Plot(nPoints=100, xaxis=(-2, 2), yaxis=(-2, 2), nGraphs=3,
+          ytitle="y-Kraft [mV]",
+          xtitle="x-Kraft [mV]",
+          title="XY-Graph",
+          line_colors=["#2222ff", "#22ff22", "#ff2222"])
+    canvas = self.forcePlot.create_canvas(self)
+    canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
+
+  def update(self, visible):
+      self.forcePlot.update(
+        xs=[self.controller.getLastValue("force_X_{}".format(i+1)) for i in range(3)],
+        ys=[self.controller.getLastValue("force_Y_{}".format(i+1)) for i in range(3)],
+        visible=visible
+      )

+ 1 - 1
software/ui/Page_3.py

@@ -10,6 +10,6 @@ class Page_3(tk.Frame):
     label = tk.Label(self, text="Druck", font=LARGE_FONT)
     label.pack(pady=10,padx=10)
 
-  def update(self):
+  def update(self, visible):
     #self.controller.getLastValue('adc_0')
     pass

+ 1 - 1
software/ui/Page_4.py

@@ -8,5 +8,5 @@ class Page_4(tk.Frame):
     label = tk.Label(self, text="Einstellungen", font=LARGE_FONT)
     label.pack(pady=10,padx=10)
 
-  def update(self):
+  def update(self, visible):
     pass

+ 21 - 31
software/ui/Plot.py

@@ -1,40 +1,30 @@
-import matplotlib
-from matplotlib.figure import Figure
-from matplotlib import style
 import numpy as np
 
 from .globals import *
-
-style.use("ggplot")
+from .plotim import linear_plot
        
+class Plot(linear_plot):
+  def __init__(self, nPoints, xaxis, yaxis, nGraphs = 1, **kwargs):
 
-class Plot():
-  def __init__(self, points, lines = 1):
-    matplotlib.use('TkAgg')
-    self.fig = Figure()
-    self.ax = self.fig.add_subplot(111)
-    self.xs = range(points)
-    self.ys = np.ndarray(shape=(lines, points), dtype=float)
+    self.xs = np.ndarray(shape=(nGraphs, nPoints), dtype=float)
+    self.xs.fill(0)
+    self.ys = np.ndarray(shape=(nGraphs, nPoints), dtype=float)
+    self.ys.fill(float('NaN'))
     self.i = 0
-    self.points = points
-    self.plots = [None] * lines
-    for p in range(lines):
-      self.plots[p], = self.ax.plot(self.xs, self.ys[p], "#00A3E0", label="{}. Graph".format(p+1))
-    
-    self.ax.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2, borderaxespad=0)
-
-  def setTitle(self, title):
-    self.ax.set_title(title)
+    self.nPoints = nPoints
+    self.nGraphs = nGraphs
+    linear_plot.__init__(self, **kwargs)
+    self.set_scale(xaxis, yaxis)
 
-  def update(self, values):
+  def update(self, ys, xs=None, visible=True):
+    for g in range(self.nGraphs):
+      if xs == None:
+        self.xs[g][self.i] = self.i
+      else:
+        self.xs[g] = np.append([xs[g]], self.xs[g][:-1])
+      self.ys[g] = np.append([ys[g]], self.ys[g][:-1])
 
-    self.ax.draw_artist(self.ax.patch)
-    for p in range(len(self.plots)):
-      self.ys[p] = np.append([values[p]], self.ys[p][:-1])
-      self.plots[p].set_ydata(self.ys[p])
-      self.ax.draw_artist(self.plots[p])
-    
-    self.fig.canvas.draw()
+    if visible:
+      self.plot_data(self.xs, self.ys)
 
-    self.i = (self.i+1) % self.points
-    
+    self.i = (self.i+1) % self.nPoints

+ 259 - 0
software/ui/plotim.py

@@ -0,0 +1,259 @@
+# # P L O T . I M # #
+# https://github.com/tbartos14/plotim
+# version 0.5.5, 5/22/2018
+
+# readme needs update for residual plots
+
+
+import tkinter as tk
+import math
+
+
+def vertical_text(text):
+    newtext = ""
+    for character in text:
+        newtext += character + "\n"
+    return newtext
+
+
+class linear_plot(object):
+    def __init__(self, bordernorth=50, bordersouth=50, bordereast=30, borderwest=50,
+                 title="Linear Plot", \
+                 draw_lines=True, draw_points=False, ytitle="y", xtitle="x", line_colors=["#0000bb"],
+                 image=None):
+        self.bordernorth = bordernorth
+        self.bordersouth = bordersouth
+        self.bordereast = bordereast
+        self.borderwest = borderwest
+        self.originalyaxistitle = ytitle
+        self.yaxistitle = vertical_text(ytitle)
+        self.xaxistitle = xtitle
+        self.yaxis = [0, 10]
+        self.xaxis = [0, 10]
+        self.title = title
+        self.draw_lines = draw_lines
+        self.draw_points = draw_points
+        self.linecolors = line_colors
+        self.image = image
+
+    def create_canvas(self, master):
+        self.master = master
+
+        self.canvas = tk.Canvas(self.master)
+        self.canvas.bind("<Configure>", self.on_resize)
+        self.windowx = self.canvas.winfo_reqwidth()
+        self.windowy = self.canvas.winfo_reqheight()
+
+        self.on_resize()
+        return self.canvas
+
+    def set_scale(self, xaxis, yaxis):
+        self.yaxis = yaxis
+        self.xaxis = xaxis
+
+    def on_resize(self,event = None):
+
+        if event:
+          self.windowx = event.width - 4
+          self.windowy = event.height - 4
+
+        self.canvas.config(width=self.windowx, height=self.windowy)
+
+        self.canvas.delete("all")
+
+        self.graphx = self.windowx - self.bordereast - self.borderwest
+        self.graphy = self.windowy - self.bordernorth - self.bordersouth
+       
+        self.canvas.create_text(self.borderwest + self.graphx / 2, self.bordernorth / 2, text=self.title)
+        self.canvas.create_text(self.borderwest / 3, self.bordernorth + self.graphy / 2, text=self.yaxistitle)
+        self.canvas.create_text(self.borderwest + self.graphx / 2, self.windowy - self.bordersouth / 3,
+                                text=self.xaxistitle)
+
+        self.canvas.create_rectangle(self.bordernorth, self.borderwest, (self.windowx - self.bordereast),
+                                     (self.windowy - self.bordersouth), fill="white", outline="white")
+
+        # finding limits for axis
+
+        self.yrange = abs(self.yaxis[1] - self.yaxis[0]) - 1
+        self.xrange = abs(self.xaxis[1] - self.xaxis[0]) - 1
+
+        # choosing what kind of scale to use
+
+        self.yrangefactor = -round(math.log10(self.yrange) - 1)
+        self.xrangefactor = -round(math.log10(self.xrange) - 1)
+
+        # determining how many lines need to be placed
+        # minimums/maximums for ease of reading
+        self.yrangemin = ((int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (10 ** (self.yrangefactor)))
+        self.xrangemin = ((int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)))
+        self.yrangemax = ((int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (10 ** (self.yrangefactor)) + (
+                (int(self.yrange * (10 ** self.yrangefactor)) + 1) * (10 ** (-1 * self.yrangefactor))))
+        self.xrangemax = ((int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)) + (
+                (int(self.xrange * (10 ** self.xrangefactor)) + 1) * (10 ** (-1 * self.xrangefactor))))
+
+        # determining increments
+        # finding if scales are appropriate
+        self.additionalscaley = 0
+        self.additionalscalex = 0
+
+        # seeing if data needs more space (y)
+        while True:
+            if self.yaxis[1] > self.yrangemax:
+                self.additionalscaley = self.additionalscaley + 1
+                self.yrangemax = (
+                        (int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (10 ** (self.yrangefactor)) + (
+                        (int(self.yrange * (10 ** self.yrangefactor)) + 1 + self.additionalscaley) * (
+                        10 ** (-1 * self.yrangefactor))))
+            else:
+                break
+
+        # (x)
+        while True:
+            if self.xaxis[1] > self.xrangemax:
+                self.additionalscalex = self.additionalscalex + 1
+                self.xrangemax = (
+                        (int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)) + (
+                        (int(self.xrange * (10 ** self.xrangefactor)) + 1 + self.additionalscalex) * (
+                        10 ** (-1 * self.xrangefactor))))
+            else:
+                break
+        self.yincrement = int(self.yrange * (10 ** self.yrangefactor)) + self.additionalscaley + 1
+        self.xincrement = int(self.xrange * (10 ** self.xrangefactor)) + self.additionalscalex + 1
+
+        # now we determine y
+        for increment in range(0, self.yincrement + 1):
+            self.canvas.create_line(self.borderwest + 1,
+                                    (self.windowy - self.bordersouth) - (increment / self.yincrement) * (
+                                            self.windowy - self.bordernorth - self.bordersouth), \
+                                    self.borderwest + self.graphx,
+                                    (self.windowy - self.bordersouth) - (increment / self.yincrement) * (
+                                            self.windowy - self.bordernorth - self.bordersouth), fill="#bbbbbb",
+                                    dash=(2, 2), width=2 if increment % 5 == 0 else 1)
+            self.canvas.create_text(self.borderwest - 12,
+                                    (self.windowy - self.bordersouth) - (increment / self.yincrement) * (
+                                            self.windowy - self.bordernorth - self.bordersouth), \
+                                    text="{0:4.4}".format((int((self.yaxis[0]) * (10 ** (self.yrangefactor)))) / (
+                                            10 ** (self.yrangefactor)) + (
+                                                                  (increment) * (10 ** (-1 * self.yrangefactor)))))
+
+        # determining x
+        for increment in range(0, self.xincrement + 1):
+            self.canvas.create_line(
+                self.bordersouth + (increment / self.xincrement) * (self.windowx - self.bordereast - self.borderwest),
+                (self.windowy - self.bordersouth) - 1, \
+                self.bordersouth + (increment / self.xincrement) * (self.windowx - self.bordereast - self.borderwest),
+                (self.windowy - self.bordersouth - self.graphy), fill="#bbbbbb", dash=(2, 2), width=2 if increment % 5 == 0 else 1)
+            self.canvas.create_text(
+                self.borderwest + (increment / self.xincrement) * (self.windowx - self.bordereast - self.borderwest),
+                (self.windowy - self.bordersouth) + 12, \
+                text="{0:4.4}".format((int((self.xaxis[0]) * (10 ** (self.xrangefactor)))) / (10 ** (self.xrangefactor)) + (
+                        (increment) * (10 ** (-1 * self.xrangefactor)))))
+
+        self.canvas.create_line(self.bordernorth, self.borderwest, self.bordernorth, (self.windowy - self.bordersouth))
+        self.canvas.create_line(self.borderwest, (self.windowy - self.bordersouth), (self.windowx - self.bordereast),
+                                (self.windowy - self.bordersouth))
+        return self.canvas
+
+    def point_to_coords(self, x, y, offsetX=0, offsetY=0):
+      return ((x - self.xrangemin) * (self.windowx - self.bordereast - self.borderwest) / (self.xrangemax - self.xrangemin) + self.borderwest + offsetX, 
+              (self.windowy - self.bordersouth) - (y - self.yrangemin) * (self.windowy - self.bordernorth - self.bordersouth) / (self.yrangemax - self.yrangemin) + offsetY)
+
+    ovals = []
+    images = []
+    def plot_data(self, xs, ys):
+        #self.canvas.delete("graph")
+        # adding lines
+        if self.draw_lines == True:
+          self.canvas.delete("line")
+          for plot in range(len(xs)):
+            xpoints = xs[plot]
+            ypoints = ys[plot]
+            linecolor = self.linecolors[plot]
+            coords = []
+            for point in range(0, len(xpoints)):
+              if not math.isnan(ypoints[point]):
+                coord = self.point_to_coords(xpoints[point], ypoints[point])
+                coords.append(coord[0])
+                coords.append(coord[1])
+
+            if len(coords) > 2:
+              self.canvas.create_line(
+                  coords,
+                  fill=linecolor, tags="line", width=3)
+
+
+        # adding points!
+        if self.draw_points == True:
+          while len(self.ovals) < len(xs):
+            self.ovals.append([None] * len(xs[0]))
+          for plot in range(len(xs)):
+            xpoints = xs[plot]
+            ypoints = ys[plot]
+            for point in range(0, len(xpoints)):
+                if math.isnan(ypoints[point]):
+                  self.canvas.delete(self.ovals[plot][point])
+                  self.ovals[plot][point] = None
+                elif self.ovals[plot][point] != None:
+                  self.canvas.coords(
+                    self.ovals[plot][point],
+                    *self.point_to_coords(xpoints[point], ypoints[point], 3, 3),
+                    *self.point_to_coords(xpoints[point], ypoints[point], -3, -3),
+                  )
+                else:
+                  self.ovals[plot][point] = self.canvas.create_oval(
+                    *self.point_to_coords(xpoints[point], ypoints[point], 3, 3),
+                    *self.point_to_coords(xpoints[point], ypoints[point], -3, -3),
+                    fill="white", tags="oval")
+
+        # use an image?
+        if self.image != None:
+          while len(self.images) < len(xs):
+            self.images.append([None] * len(xs[0]))
+          for plot in range(len(xs)):
+            xpoints = xs[plot]
+            ypoints = ys[plot]
+            pointimage = tk.PhotoImage(file=self.image)
+            for point in range(0, len(xpoints)):
+                if math.isnan(ypoints[point]):
+                  self.canvas.delete(self.images[plot][point])
+                  self.images[plot][point] = None
+                elif self.images[plot][point] != None:
+                  self.canvas.coords(
+                    self.images[plot][point],
+                    *self.point_to_coords(xpoints[point], ypoints[point])
+                  )
+                else:
+                  self.images[plot][point] = self.canvas.create_image(
+                    *self.point_to_coords(xpoints[point], ypoints[point]),
+                    image=pointimage, tags="image")
+
+        return self.canvas
+
+
+
+if __name__ == "__main__":
+    # Datetime,RecNbr,WS_mph_Avg,PAR_Den_Avg,WS_mph_S_WVT,WindDir_SD1_WVT,AirTF_Avg,Rain_in_Tot,RH,WindDir_D1_WVT
+    #!/usr/bin/env python3
+    import urllib.request
+    import time
+
+    xpoints = []
+    ypoints = []
+    time = 0
+
+    newlines = range(0, 100)
+
+    for line in newlines:
+        xpoints.append(time)
+        time = time + (1 / 60)
+        ypoints.append(line)
+
+    plot1 = linear_plot(line_of_best_fit=True, \
+                        ytitle="Temperature F\u00B0", \
+                        xtitle="Time (hours)", \
+                        title="Temperature in Durham NH Today", \
+                        line_color="#2222ff", \
+                        windowx=1000, \
+                        windowy=700, )
+    plot1.set_data(xpoints, ypoints)
+    plot1.plot_data()