subDesTagesMitExtraKaese 4 years ago
parent
commit
3cbeb89514

+ 17 - 8
software/appWindow.py

@@ -12,7 +12,7 @@ from ui import *
 
 import tkinter as tk
 import tk_tools
-from time import *
+import time
 
 
 class Main(tk.Tk, Table):
@@ -26,6 +26,8 @@ class Main(tk.Tk, Table):
     self.forceSensors.start()
     self.motorController = PWM(32)
     self.pid = PID()
+
+    self.motorEnabled = False
     
     Table.__init__(self, 
     ["time", "windspeed", "motor_pwm"] + 
@@ -87,8 +89,8 @@ class Main(tk.Tk, Table):
     self.interval()
 
   def show_frame(self, cont):
-    frame = self.frames[cont]
-    frame.tkraise()
+    self.currentFrame = self.frames[cont]
+    self.currentFrame.tkraise()
 
   def popupmsg(self, msg=""):
       popup = tk.Toplevel(self.master)
@@ -99,24 +101,31 @@ class Main(tk.Tk, Table):
       b1.pack()
       
   def interval(self):
+    start = time.time()
     adcValue = self.adc.getVoltage(0)
     windSpeed = adcValue * 1337
-    pwmValue = self.PID.update(windSpeed)
+    pwmValue = self.pid.update(windSpeed)
     if not self.motorEnabled:
       pwmValue = 0
-    self.pwm.setDutyCycle(pwmValue)
+    self.motorController.setDutyCycle(pwmValue)
+
+    i2cValues = self.pressureSensors.getValues()
 
     self.addRow(
       [time.time(), windSpeed, pwmValue] + 
-      self.pressureSensors.getValues() +
+      i2cValues +
       [adcValue] +
       self.forceSensors.getForces(0) +
       self.forceSensors.getForces(1) +
       self.forceSensors.getForces(2)
     )
-    for frame in self.frames:
-      frame.update()
+    print("sensors: {:8.3f} ms".format((time.time() - start)*1000))
+
+    start = time.time()
+    #for frame in self.frames:
+    self.currentFrame.update()
 
+    print("draw:    {:8.3f} ms".format((time.time() - start)*1000))
     self.after(300,self.interval)
 
           

+ 1 - 1
software/database.py

@@ -18,7 +18,7 @@ class Table:
 
   def getLastValue(self, column):
     col = self.columnNames.index(column)
-    return self.getLastRow(col)
+    return self.getLastRow()[col]
 
   def saveAsCsv(self, filename):
     with open(filename,"w+", newline='') as my_csv:

+ 12 - 8
software/digitalPressure/sdp610Array.py

@@ -16,16 +16,20 @@ class Spd610Array():
     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
+    try:
+      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
+    except OSError as e:
+      print("ERROR:   I2C SPD610 {:X}: ".format(addr), e)
+      return None
 
   def getValues(self):
-    values = ()
-    for addr in self.i2cAddresses:
-      values.append(self.readValue(addr))
+    values = [0] * len(self.i2cAddresses)
+    for i in range(len(self.i2cAddresses)):
+      values[i] = self.readValue(self.i2cAddresses[i])
     return values
 
 if __name__ == "__main__":

+ 8 - 6
software/main.py

@@ -3,11 +3,13 @@
 
 import sys
 from appWindow import Main
+from ui.globals import *
 
+try:
+  app = Main()
+  app.geometry("1280x720")
+  app.mainloop()
+except KeyboardInterrupt as e:
+  running = False
 
-app = Main()
-app.geometry("1280x720")
-app.mainloop()
-
-
-sys.exit(ret)
+sys.exit()

+ 9 - 8
software/motorController/pwmOutput.py

@@ -1,26 +1,27 @@
 
-import RPi.GPIO as GPIO
+#import RPi.GPIO as GPIO
 import time
 import sys
 
 class PWM:
   def __init__(self, pin):
     self.pin = pin
-    GPIO.setup(self.pin, GPIO.OUT)
-    GPIO.output(self.pin, 0)
-    self.pwm = GPIO.PWM(self.pin, 100)
-    self.pwm.start(0)
+    #GPIO.setup(self.pin, GPIO.OUT)
+    #GPIO.output(self.pin, 0)
+    #self.pwm = GPIO.PWM(self.pin, 100)
+    #self.pwm.start(0)
 
   def setDutyCycle(self, val):
-    self.pwm.ChangeDutyCycle(val)
+    #self.pwm.ChangeDutyCycle(val)
+    pass
 
 if __name__ == '__main__':
   try:
-    GPIO.setmode(GPIO.BOARD)
+    #GPIO.setmode(GPIO.BOARD)
     p = PWM(32)
     while True:
       p.setDutyCycle(int(time.time()*100 % 100))
       time.sleep(.03)
   except KeyboardInterrupt:
-    GPIO.cleanup()
+    #GPIO.cleanup()
     sys.exit(0)

+ 1 - 1
software/ui/Page_1.py

@@ -46,5 +46,5 @@ 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.serialPlot.update([self.controller.getLastValue("force_X_1")])
     self.label4.config(text="{:3d} Nm".format(3))

+ 11 - 4
software/ui/Page_2.py

@@ -2,10 +2,17 @@ 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
 
 class Page_2(tk.Frame):
   def __init__(self, parent, controller):
     tk.Frame.__init__(self, parent)
+    self.controller = controller
     label = tk.Label(self, text="Kräfte", font=LARGE_FONT)
     label.pack(pady=10,padx=10)
     
@@ -18,8 +25,8 @@ class Page_2(tk.Frame):
       canvas._tkcanvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
   def update(self):
     for i in range(3):
-      self.forcePlots.update([
-        controller.getLastValue("force_X_{}".format(i)),
-        controller.getLastValue("force_Y_{}".format(i)),
-        controller.getLastValue("force_Z_{}".format(i)),
+      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)),
       ])

+ 3 - 1
software/ui/Page_3.py

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

+ 2 - 1
software/ui/Plot.py

@@ -10,6 +10,7 @@ style.use("ggplot")
 
 class Plot():
   def __init__(self, points, plots = 1):
+    matplotlib.use('TkAgg')
     self.fig = Figure()
     self.ax = self.fig.add_subplot(111)
     self.xs = range(points)
@@ -24,7 +25,7 @@ class Plot():
   def update(self, values):
     self.ax.clear()
     for p in range(self.plots):
-      self.ys[p][self.i] = values[p]
+      self.ys[p, self.i] = values[p]
       self.ax.plot(self.xs, self.ys[p], "#00A3E0", label="{}. Graph".format(p+1))
     
     self.i = (self.i+1) % self.points

+ 3 - 1
software/ui/globals.py

@@ -1,3 +1,5 @@
 
 
-LARGE_FONT= ("Verdana", 12)
+LARGE_FONT= ("Verdana", 12)
+
+running = True

+ 28 - 21
software/wirelessLoadCell/loadCells.py

@@ -1,8 +1,10 @@
 if __name__ == "__main__":
   from GSV4BT import GSV4BT
+  running = True
 else:
   from .GSV4BT import GSV4BT
-  
+  from ui.globals import *
+
 import time
 import bluetooth
 import threading
@@ -19,15 +21,26 @@ class LoadCells():
     success = True
     for cell in self.cells:
       if cell.isConnected():
-        pass
+        if cell.requiresSetup:
+          cell.setNormalMode()
+          cell.stopTransmission()
+          cell.setFrequency(20) # Hz
+          cell.setGain(0, '2mV')
+          cell.setGain(1, '2mV')
+          cell.setGain(2, '2mV')
+          mode = cell.getMode()
+          if mode and mode[0] == 0x01:
+            cell.requiresSetup = False
+
       elif cell.connect():
         cell.requiresSetup = True
       else:
         success = False
+        cell.requiresSetup = True
     return success
 
   def reconnectThread(self):
-    while True:
+    while running:
       self.connect()
       time.sleep(1)
 
@@ -37,18 +50,8 @@ class LoadCells():
 
   def getForces(self, id):
     cell = self.cells[id]
-    if not cell.isConnected():
-      return None
-    if cell.requiresSetup:
-      cell.setNormalMode()
-      cell.stopTransmission()
-      cell.setFrequency(20) # Hz
-      cell.setGain(0, '2mV')
-      cell.setGain(1, '2mV')
-      cell.setGain(2, '2mV')
-      mode = cell.getMode()
-      if mode and mode[0] == 0x01:
-        cell.requiresSetup = False
+    if (not cell.isConnected()) or cell.requiresSetup:
+      return [None,None,None]
 
     cell.getValue()
     return cell.getForces()
@@ -64,10 +67,14 @@ if __name__ == "__main__":
   cells = LoadCells()
 
   cells.scan()
-  cells.start()
-  while True:
+  try:
+    cells.start()
+    while True:
+      vals = cells.getForces(0)
+      if vals[0] != None:
+        print('cell 0: ' + ' '.join(["ch {}: {:8.3f} mV/V".format(i, vals[i]) for i in range(len(vals))]))
+      time.sleep(.3)
+
 
-    vals = cells.getForces(0)
-    if vals:
-      print('cell 0: ' + ' '.join(["ch {}: {:8.3f} mV/V".format(i, vals[i]) for i in range(len(vals))]))
-    time.sleep(.3)
+  except KeyboardInterrupt as e:
+    running = False