Bläddra i källkod

Merge branch 'master' of https://dyndns.wheretonext.de:8810/subDesTagesMitExtraKaese/windkanal

kay 4 år sedan
förälder
incheckning
42441106b4

+ 13 - 5
software/analogPressure/sdpArray.py

@@ -1,16 +1,24 @@
-from MCP3008 import MCP3008
+from .mcp3008 import MCP3008
+import time
 
-class SdpArray():
-  __init__(self):
+class SdpArray:
+  def __init__(self):
     self.adcs = (
       MCP3008(0, 0), 
       MCP3008(0, 1)
     )
 
-  read(self, channel):
+  def getVoltage(self, channel):
     if channel < 8:
       value = adc[0].read(channel) 
     else:
       value = adc[1].read(channel-8)
 
-    print("Anliegende Spannung: %.2f" % (value / 1023.0 * 3.3) )
+    voltage = value / 1023.0 * 3.3
+    return voltage
+
+if __name__ == "__main__":
+  sdps = SdpArray()
+  while True:
+    print("Anliegende Spannung:", sdps.getVoltage(0))
+    time.sleep(1)

+ 68 - 16
software/appWindow.py

@@ -1,23 +1,75 @@
-#!/usr/bin/python3
-# -*- coding: utf-8 -*-
+# The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
+# License: http://creativecommons.org/licenses/by-sa/3.0/	
 
-from PyQt5.QtWidgets import *
-from PyQt5.QtCore import (QSettings, QThread, pyqtSignal, pyqtSlot)
+from analogPressure.sdpArray import SdpArray
+from digitalPressure.spd610Array import Spd610Array
+from wirelessLoadCell.loadCells import LoadCells
+from ui import *
 
-from ui.windkanal import Ui_Form
-from ui.main_ui import Ui_MainWindow
+import matplotlib
+from matplotlib.backends.backend_tkagg import (
+    FigureCanvasTkAgg, NavigationToolbar2Tk)
+from matplotlib.figure import Figure
+import matplotlib.animation as animation
+from matplotlib import style
 
-class AppWindow(QMainWindow):
+import tkinter as tk
+import tk_tools
+from time import *
 
-  def __init__(self):
-    super().__init__()
-    self.ui = Ui_MainWindow()
-    self.ui.setupUi(self)
-    self.ui.saveButton.clicked.connect(self.save_pressed)
+LARGE_FONT= ("Verdana", 12)
+style.use("ggplot")
+       
 
-    self.show()
+class Main(tk.Tk):
+  def __init__(self, *args, **kwargs):
+    tk.Tk.__init__(self, *args, **kwargs)
+    tk.Tk.wm_title(self, "Windkanal-Tool")
+    
+    
+    container = tk.Frame(self)
+    container.pack(side="top", fill="both", expand = True)
+    container.grid_rowconfigure(0, weight=1)
+    container.grid_columnconfigure(0, weight=1)
 
-  def save_pressed(self):
-    self.ui.lcdNumber_pressure.intValue(123)
-    print(dir(self.ui.lcdNumber_pressure))
 
+    menubar = tk.Menu(container)
+    filemenu = tk.Menu(menubar, tearoff=0)
+    filemenu.add_command(label="Save settings", command = lambda: self.popupmsg("Not supported just yet!"))
+    filemenu.add_separator()
+    filemenu.add_command(label="Exit", command=quit)
+    menubar.add_cascade(label="File", menu=filemenu)
+
+    tk.Tk.config(self, menu=menubar)
+
+    self.frames = {}
+
+    for F in (Page_1, Page_2, Page_3, Page_4):
+
+      frame = F(container, self)
+
+      self.frames[F] = frame
+
+      frame.grid(row=0, column=0, sticky="nsew")
+
+    self.show_frame(Page_1)
+
+  def show_frame(self, cont):
+    frame = self.frames[cont]
+    frame.tkraise()
+
+  def popupmsg(self, msg=""):
+      popup = tk.Toplevel(self.master)
+      popup.wm_title("Error!")
+      label = tk.Label(popup, text=msg, font=LARGE_FONT)
+      label.pack(side="top", fill="x", pady=10)
+      b1 = tk.Button(popup, text="Okay", command=popup.destroy)
+      b1.pack()
+  def interval(self):
+    
+    for frame in self.frames:
+      frame.update()
+
+    self.after(300,self.interval)
+
+          

+ 0 - 8
software/digitalPressure/sdp610.py

@@ -1,8 +0,0 @@
-
-class Spd610():
-  i2cAddress = 12
-
-  __init__(self, i2cAddress):
-    self.i2cAddress = i2cAddress
-
-  readValue(self):

+ 36 - 0
software/digitalPressure/sdp610Array.py

@@ -0,0 +1,36 @@
+import smbus
+import time
+
+class Spd610Array():
+  i2cAddresses = (
+    0x21,
+    0x41,
+    0x61,
+    0x31,
+    0x39,
+    0x29,
+    0x35,
+    0x40
+  )
+  def __init__(self):
+    self.bus = smbus.SMBus(0)
+
+  def readValue(self, addr):
+    self.bus.write_byte_data(addr, 0, 0xF1)
+    block = self.bus.read_i2c_block_data(addr, 0, 3)
+    value = (block[0] * 256 + block[1]) / 240.0
+    crc = block[2]
+    return value
+
+  def getValues(self):
+    values = ()
+    for addr in self.i2cAddresses:
+      values.append(self.readValue(addr))
+    return values
+
+if __name__ == "__main__":
+  sdp = Spd610Array()
+  while True:
+    print(sdp.getValues())
+    time.sleep(1)
+

+ 4 - 8
software/main.py

@@ -2,16 +2,12 @@
 # -*- coding: utf-8 -*-
 
 import sys
+from appWindow import Main
 
-from PyQt5.QtWidgets import *
-from PyQt5.QtCore import (QThread, pyqtSignal, pyqtSlot)
 
-from appWindow import AppWindow
+app = Main()
+app.geometry("1280x720")
+app.mainloop()
 
 
-
-qApp = QApplication(sys.argv)
-w = AppWindow()
-ret = qApp.exec_()
-
 sys.exit(ret)

+ 56 - 0
software/ui/Page_1.py

@@ -0,0 +1,56 @@
+import tkinter as tk
+import tk_tools
+
+class Page_1(tk.Frame):
+  def __init__(self, parent, controller):
+    tk.Frame.__init__(self, parent)
+    self.t = 0
+    label = tk.Label(self, text="Bedienelemente", font=LARGE_FONT)
+    label.pack(pady=10,padx=10)
+    # top menu
+    top = tk.Frame(self, borderwidth=2, relief="solid")
+    button1 = tk.Button(top, text="Bedienelemente", command=lambda: controller.show_frame(Page_0))
+    button1.pack(side=tk.LEFT)
+
+    button2 = tk.Button(top, text="Kräfte", command=lambda: controller.show_frame(Page_2))
+    button2.pack(side=tk.LEFT)
+
+    button3 = tk.Button(top, text="Druck", command=lambda: controller.show_frame(Page_2))
+    button3.pack(side=tk.LEFT)
+
+    button4 = tk.Button(top,text="Einstellungen",command=lambda: controller.show_frame(Page_2))
+    button4.pack(side=tk.LEFT)
+
+    button5 = tk.Button(top, text="QUIT", fg="red",command=quit)
+    button5.pack(side=tk.LEFT)
+    top.pack(side="top", expand=True, fill="both")
+    # graph
+    self.serialPlot = Plot()
+    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)
+    # right menu
+    left = tk.Frame(self, borderwidth=2, relief="solid")
+    container = tk.Frame(left, borderwidth=2, relief="solid")
+    label1 = tk.Label(container, text="I could be a canvas, but I'm a label right now")
+
+    self.label4 = tk.Label(self,font=("Arial","30"),fg="red")
+    self.label4.pack()
+    self.label4.config(text=str(self.t))
+
+    SendButton = tk.Button(left, text='Quit', command=quit)
+    label2 = tk.Label(left, text="I could be a button")
+    label3 = tk.Label(left, text="So could I")
+
+    left.pack(side="left", expand=True, fill="both")
+    container.pack(expand=True, fill="both", padx=7, pady=5)
+    SendButton.pack()
+
+    label1.pack()
+    label2.pack()
+    label3.pack()
+
+  def update(self):
+    self.serialPlot.update(3)
+    self.label4.config(text="{:3d} Nm".format(3))

+ 12 - 0
software/ui/Page_2.py

@@ -0,0 +1,12 @@
+import tkinter as tk
+import tk_tools
+
+class Page_2(tk.Frame):
+  def __init__(self, parent, controller):
+    tk.Frame.__init__(self, parent)
+    label = tk.Label(self, text="Kräfte", font=LARGE_FONT)
+    label.pack(pady=10,padx=10)
+    button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
+    button1.pack()
+    button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
+    button2.pack()

+ 12 - 0
software/ui/Page_3.py

@@ -0,0 +1,12 @@
+import tkinter as tk
+import tk_tools
+
+class Page_3(tk.Frame):
+  def __init__(self, parent, controller):
+    tk.Frame.__init__(self, parent)
+    label = tk.Label(self, text="Druck", font=LARGE_FONT)
+    label.pack(pady=10,padx=10)
+    button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
+    button1.pack()
+    button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
+    button2.pack()

+ 12 - 0
software/ui/Page_4.py

@@ -0,0 +1,12 @@
+import tkinter as tk
+import tk_tools
+
+class Page_4(tk.Frame):
+  def __init__(self, parent, controller):
+    tk.Frame.__init__(self, parent)
+    label = tk.Label(self, text="Einstellungen", font=LARGE_FONT)
+    label.pack(pady=10,padx=10)
+    button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
+    button1.pack()
+    button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
+    button2.pack()

+ 24 - 0
software/ui/Plot.py

@@ -0,0 +1,24 @@
+import matplotlib
+from matplotlib.figure import Figure
+from matplotlib import style
+
+
+class Plot():
+  def __init__(self, n):
+    self.fig = Figure()
+    self.ax = self.fig.add_subplot(111)
+    self.xs = range(n)
+    self.ys = [0] * n
+    self.i = 0
+    self.n = n
+
+  def update(self, y):
+    # Add x and y to lists
+    ys[i] = y
+    i = i+1 if i+1 < self.n else 0
+
+
+    ax.clear()
+    ax.plot(xs, ys, "#00A3E0", label="1. Graph")
+    ax.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2, borderaxespad=0)
+    ax.set_title('Windkanal')

+ 7 - 0
software/ui/__init__.py

@@ -0,0 +1,7 @@
+from Page_1 import Page_1
+from Page_2 import Page_2
+from Page_3 import Page_3
+from Page_4 import Page_4
+from Plot import Plot
+
+__all__ = ["Page_1", "Page_2", "Page_3", "Page_4", "Plot"]

+ 0 - 238
software/ui/tkinter_windkanal.py

@@ -1,238 +0,0 @@
-# The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
-# License: http://creativecommons.org/licenses/by-sa/3.0/	
-
-import matplotlib
-from matplotlib.backends.backend_tkagg import (
-    FigureCanvasTkAgg, NavigationToolbar2Tk)
-from matplotlib.figure import Figure
-import matplotlib.animation as animation
-from matplotlib import style
-
-import tkinter as tk
-import serial
-import tk_tools
-import threading
-import queue
-from time import *
-
-LARGE_FONT= ("Verdana", 12)
-style.use("ggplot")
-
-#initialize serial port
-ser = serial.Serial()
-ser.port = '/dev/ttyUSB0' #Arduino serial port
-ser.baudrate = 9600
-ser.timeout = 10 #specify timeout when using readline()
-ser.open()
-if ser.is_open==True:
-    print("\nAll right, serial port now open. Configuration:\n")
-    print(ser, "\n") #print serial parameters
-
-fig = Figure()
-ax = fig.add_subplot(111)
-xs = [] #store trials here (n)
-ys = [] #store relative frequency here
-
-class getdata(threading.Thread):
-    def __init__(self, label, t, q):
-        threading.Thread.__init__(self)       
-        self.jobqueue = q # get job queue for sending commands
-        self.t = t 
-
-       
-    def run(self):
-        while True:# and self.mb.entrycget(1,"label")=="Stop":
-            sleep(0.1) # wait a second
-            SerialObj = Serial()
-            self.t = SerialObj.animate(self)
-
-            # send to job queue !!!
-            self.jobqueue.put(("cnt_label",self.t))
-
-class Serial(tk.Tk):
-    def __init__(self):
-        tk.Frame.__init__(self)
-
-    def animate(i,self):
-        line=ser.readline()      #ascii
-        line_as_list = line.split(b',')
-        i = int(line_as_list[0])
-        relProb = line_as_list[1]
-        relProb_as_list = relProb.split(b'\n')
-        relProb_float = float(relProb_as_list[0])
-        # Add x and y to lists
-        xs.append(i)
-        ys.append(relProb_float)
-
-
-        ax.clear()
-        ax.plot(xs, ys, "#00A3E0", label="1. Graph")
-        ax.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2, borderaxespad=0)
-        ax.set_title('Windkanal')
-
-        return relProb_float
-       
-
-class Main(tk.Tk):
-    def __init__(self, *args, **kwargs):
-        tk.Tk.__init__(self, *args, **kwargs)
-        tk.Tk.wm_title(self, "Windkanal-Tool")
-        
-        
-        container = tk.Frame(self)
-        container.pack(side="top", fill="both", expand = True)
-        container.grid_rowconfigure(0, weight=1)
-        container.grid_columnconfigure(0, weight=1)
-
-
-        menubar = tk.Menu(container)
-        filemenu = tk.Menu(menubar, tearoff=0)
-        filemenu.add_command(label="Save settings", command = lambda: self.popupmsg("Not supported just yet!"))
-        filemenu.add_separator()
-        filemenu.add_command(label="Exit", command=quit)
-        menubar.add_cascade(label="File", menu=filemenu)
-
-        tk.Tk.config(self, menu=menubar)
-
-        self.frames = {}
-
-        for F in (Page_1, Page_2, Page_3, Page_4):
-
-            frame = F(container, self)
-
-            self.frames[F] = frame
-
-            frame.grid(row=0, column=0, sticky="nsew")
-
-        self.show_frame(Page_1)
-
-    def show_frame(self, cont):
-        frame = self.frames[cont]
-        frame.tkraise()
-
-    def popupmsg(self, msg=""):
-        popup = tk.Toplevel(self.master)
-        popup.wm_title("Error!")
-        label = tk.Label(popup, text=msg, font=LARGE_FONT)
-        label.pack(side="top", fill="x", pady=10)
-        b1 = tk.Button(popup, text="Okay", command=popup.destroy)
-        b1.pack()
-
-class Page_4(tk.Frame):
-    def __init__(self, parent, controller):
-        tk.Frame.__init__(self, parent)
-        label = tk.Label(self, text="Einstellungen", font=LARGE_FONT)
-        label.pack(pady=10,padx=10)
-        button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
-        button1.pack()
-        button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
-        button2.pack()
-
-class Page_3(tk.Frame):
-    def __init__(self, parent, controller):
-        tk.Frame.__init__(self, parent)
-        label = tk.Label(self, text="Druck", font=LARGE_FONT)
-        label.pack(pady=10,padx=10)
-        button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
-        button1.pack()
-        button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
-        button2.pack()
-
-class Page_2(tk.Frame):
-    def __init__(self, parent, controller):
-        tk.Frame.__init__(self, parent)
-        label = tk.Label(self, text="Kräfte", font=LARGE_FONT)
-        label.pack(pady=10,padx=10)
-        button1 = tk.Button(self, text="Page_0", command=lambda: controller.show_frame(Page_0))
-        button1.pack()
-        button2 = tk.Button(self, text="Page_1", command=lambda: controller.show_frame(Page_1))
-        button2.pack()
-
-
-
-class Page_1(tk.Frame):
-    def __init__(self, parent, controller):
-        tk.Frame.__init__(self, parent)
-        self.t = 0
-        label = tk.Label(self, text="Bedienelemente", font=LARGE_FONT)
-        label.pack(pady=10,padx=10)
-        # top menu
-        top = tk.Frame(self, borderwidth=2, relief="solid")
-        button1 = tk.Button(top, text="Bedienelemente", command=lambda: controller.show_frame(Page_0))
-        button1.pack(side=tk.LEFT)
-
-        button2 = tk.Button(top, text="Kräfte", command=lambda: controller.show_frame(Page_2))
-        button2.pack(side=tk.LEFT)
-
-        button3 = tk.Button(top, text="Druck", command=lambda: controller.show_frame(Page_2))
-        button3.pack(side=tk.LEFT)
-
-        button4 = tk.Button(top,text="Einstellungen",command=lambda: controller.show_frame(Page_2))
-        button4.pack(side=tk.LEFT)
-
-        button5 = tk.Button(top, text="QUIT", fg="red",command=quit)
-        button5.pack(side=tk.LEFT)
-        top.pack(side="top", expand=True, fill="both")
-        # graph
-        canvas = FigureCanvasTkAgg(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)
-        # right menu
-        left = tk.Frame(self, borderwidth=2, relief="solid")
-        container = tk.Frame(left, borderwidth=2, relief="solid")
-        label1 = tk.Label(container, text="I could be a canvas, but I'm a label right now")
-
-        SerialObj = Serial()
-        #p = tk_tools.RotaryScale(self, max_value=22.0, unit='m/s')
-        #p.pack()
-        #p.set_value(SerialObj.animate(self))
-        #print(SerialObj.animate(self))
-
-        self.seconds = SerialObj.animate(self)
-
-        self.label4 = tk.Label(self,font=("Arial","30"),fg="red")
-        self.label4.pack()
-        self.label4.config(text=str(self.t))
-
-        self.q = queue.Queue() # Make job queue (Queue)
-        offswitch = threading.Event() # Make offswitch (Event)
-        
-        #self.label4.after(100,self.label_update)
-        
-        cd = getdata(self.label4, self.t, self.q)
-        cd.start()
-
-        SendButton = tk.Button(left, text='Quit', command=quit)
-        label2 = tk.Label(left, text="I could be a button")
-        label3 = tk.Label(left, text="So could I")
-
-        left.pack(side="left", expand=True, fill="both")
-        container.pack(expand=True, fill="both", padx=7, pady=5)
-        SendButton.pack()
-
-        label1.pack()
-        label2.pack()
-        label3.pack()
-
-    def label_update(self):
-        job = self.q.get() #get job form queue!!!
-
-        
-        if job[0] == "cnt_label":
-            self.label4.config(text=str(job[1]) + " Nm")
-        else:
-            print("Unknown job:", job)
-            
-        self.label4.after(100,self.label_update)
-
-
-    def sendFactorToMCU(self):
-        self.serialReference.sendSerialData(self.entry.get() + '%')
-
-
-app = Main()
-app.geometry("1280x720")
-SerialObj = Serial()
-ani = animation.FuncAnimation(fig, SerialObj.animate, interval=500)
-app.mainloop()

+ 0 - 20
software/wirelessLoadCell/GSV4-BT.py

@@ -1,20 +0,0 @@
-
-
-
-# simple inquiry example
-import bluetooth
-
-nearby_devices = bluetooth.discover_devices(lookup_names=True)
-print("Found {} devices.".format(len(nearby_devices)))
-
-for addr, name in nearby_devices:
-  print("  {} - {}".format(addr, name))
-
-
-class GSV4-BT():
-  __init__(self):
-
-    pass
-
-  scan(self):
-   

+ 40 - 0
software/wirelessLoadCell/GSV4BT.py

@@ -0,0 +1,40 @@
+import sys
+import bluetooth
+
+class GSV4BT():
+  def __init__(self, addr):
+    self.addr = addr
+    self.uuid = None
+    self.sock = None
+
+  def connect(self):
+    if self.sock:
+      return True
+
+    service_matches = bluetooth.find_service(address=self.addr)
+    if len(service_matches) == 0:
+      print("BT device {} not found.".format(self.addr))
+      return False
+    
+    first_match = service_matches[0]
+    self.uuid = first_match["uuid"]
+    self.port = first_match["port"]
+    self.name = first_match["name"]
+    self.host = first_match["host"]
+
+    print("Connecting to \"{}\" on {}".format(self.name, self.host))
+
+    self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
+    return self.sock.connect((self.host, self.port))
+  
+  def sendRaw(self, data):
+    self.sock.send(data)
+
+  def recvRaw(self):
+    return self.sock.recv(1024)
+
+  def getForces(self):
+    return (0, 0, 0)
+
+  def close(self):
+    self.sock.close()

+ 41 - 0
software/wirelessLoadCell/loadCells.py

@@ -0,0 +1,41 @@
+from GSV4BT import GSV4BT
+import time
+import bluetooth
+
+class LoadCells():
+  def __init__(self):
+    self.cells = (
+      GSV4BT("addr1"),
+      GSV4BT("addr2"),
+      GSV4BT("addr3"),
+    )
+  
+  def connect(self):
+    success = True
+    for cell in self.cells:
+      if not cell.connect():
+        success = False
+    return success
+
+  def getForces(self):
+    return (
+      self.cells[0].getForces(),
+      self.cells[1].getForces(),
+      self.cells[2].getForces()
+    )
+
+
+if __name__ == "__main__":
+  cells = LoadCells()
+
+  while True:
+    nearby_devices = bluetooth.discover_devices(lookup_names=True)
+    print("Found {} devices.".format(len(nearby_devices)))
+
+    for addr, name in nearby_devices:
+      print("  {} - {}".format(addr, name))
+
+    cells.connect()
+    print(cells.getForces())
+    cells.scan()
+    time.sleep(1)