Browse Source

python 3.5 compat

subDesTagesMitExtraKaese 4 years ago
parent
commit
95bf283e07

+ 5 - 1
software/analogPressure/mcp3008.py

@@ -12,8 +12,12 @@ class MCP3008:
     
     def read(self, channel = 0):
         adc = self.spi.xfer2([1, (8 + channel) << 4, 0])
+        print(adc)
         data = ((adc[1] & 3) << 8) + adc[2]
         return data
+
+    def getVoltage(self, channel):
+        return self.read(channel) / 1023.0 * 3.3
             
     def close(self):
         self.spi.close()
@@ -21,5 +25,5 @@ class MCP3008:
 if __name__ == "__main__":
   adc = MCP3008(0,0)
   while True:
-    print("Rohwerte:", [adc.read(i) for i in range(8)])
+    print('\t'.join(["{}: {:8.3f} V".format(i, adc.getVoltage(i)) for i in range(4)]))
     time.sleep(.3)

+ 11 - 11
software/analogPressure/mcp3008BitBang.py

@@ -12,11 +12,11 @@ import RPi.GPIO as GPIO
 import time
 import sys
 
-CLK = 11
-MISO = 9
-MOSI = 10
-CS0 = 8
-CS1 = 7
+CLK = 23
+MISO = 21
+MOSI = 19
+CS0 = 24
+CS1 = 26
 
 def setupSpiPins(clkPin, misoPin, mosiPin, csPin):
     ''' Set all pins as an output except MISO (Master Input, Slave Output)'''
@@ -28,7 +28,7 @@ def setupSpiPins(clkPin, misoPin, mosiPin, csPin):
 
 def readAdc(channel, clkPin, misoPin, mosiPin, csPin):
     if (channel < 0) or (channel > 7):
-        print "Invalid ADC Channel number, must be between [0,7]"
+        print("Invalid ADC Channel number, must be between [0,7]")
         return -1
         
     # Datasheet says chip select must be pulled high between conversions
@@ -97,13 +97,13 @@ def recvBits(numBits, clkPin, misoPin):
     
 if __name__ == '__main__':
     try:
-        GPIO.setmode(GPIO.BCM)
-        setupSpiPins(CLK, MISO, MOSI, CS)
+        GPIO.setmode(GPIO.BOARD)
+        setupSpiPins(CLK, MISO, MOSI, CS0)
     
         while True:
-            val = readAdc(0, CLK, MISO, MOSI, CS)
-            print "ADC Result: ", str(val)
-            time.sleep(5)
+            val = readAdc(0, CLK, MISO, MOSI, CS0)
+            print("ADC Result: ", str(val))
+            time.sleep(.3)
     except KeyboardInterrupt:
         GPIO.cleanup()
         sys.exit(0)

+ 7 - 6
software/analogPressure/sdpArray.py

@@ -1,4 +1,8 @@
-from .mcp3008 import MCP3008
+if __name__ == "__main__":
+  from mcp3008 import MCP3008
+else:
+  from .mcp3008 import MCP3008
+
 import time
 
 class SdpArray:
@@ -10,12 +14,9 @@ class SdpArray:
 
   def getVoltage(self, channel):
     if channel < 8:
-      value = adc[0].read(channel) 
+      return adc[0].getVoltage(channel) 
     else:
-      value = adc[1].read(channel-8)
-
-    voltage = value / 1023.0 * 3.3
-    return voltage
+      return adc[1].getVoltage(channel-8)
 
 if __name__ == "__main__":
   sdps = SdpArray()

+ 5 - 5
software/appWindow.py

@@ -29,11 +29,11 @@ class Main(tk.Tk, Table):
     
     Table.__init__(self, 
     ["time", "windspeed", "motor_pwm"] + 
-    [f"pressure_{i}" for i in range(8)] + 
-    [f"adc_{i}" for i in range(1)] + 
-    [f"force_X_1", f"force_Y_1", f"force_Z_1"] +
-    [f"force_X_2", f"force_Y_2", f"force_Z_2"] + 
-    [f"force_X_3", f"force_Y_3", f"force_Z_3"])
+    ["pressure_{}".format(i) for i in range(8)] + 
+    ["adc_{}".format(i) for i in range(1)] + 
+    ["force_X_1", "force_Y_1", "force_Z_1"] +
+    ["force_X_2", "force_Y_2", "force_Z_2"] + 
+    ["force_X_3", "force_Y_3", "force_Z_3"])
     
     self.saveAsCsv("test.csv")
 

+ 1 - 1
software/motorController/pwmOutput.py

@@ -16,7 +16,7 @@ class PWM:
 
 if __name__ == '__main__':
   try:
-    GPIO.setmode(GPIO.BCM)
+    GPIO.setmode(GPIO.BOARD)
     p = PWM(32)
     while True:
       p.setDutyCycle(int(time.time()*100 % 100))

+ 3 - 3
software/ui/Page_2.py

@@ -19,7 +19,7 @@ class Page_2(tk.Frame):
   def update(self):
     for i in range(3):
       self.forcePlots.update([
-        controller.getLastValue(f"force_X_{i}"),
-        controller.getLastValue(f"force_Y_{i}"),
-        controller.getLastValue(f"force_Z_{i}"),
+        controller.getLastValue("force_X_{}".format(i)),
+        controller.getLastValue("force_Y_{}".format(i)),
+        controller.getLastValue("force_Z_{}".format(i)),
       ])

+ 1 - 1
software/ui/Plot.py

@@ -25,7 +25,7 @@ class Plot():
     self.ax.clear()
     for p in range(self.plots):
       self.ys[p][self.i] = values[p]
-      self.ax.plot(self.xs, self.ys[p], "#00A3E0", label=f"{p+1}. Graph")
+      self.ax.plot(self.xs, self.ys[p], "#00A3E0", label="{}. Graph".format(p+1))
     
     self.i = (self.i+1) % self.points
     self.ax.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3, ncol=2, borderaxespad=0)

+ 5 - 1
software/wirelessLoadCell/loadCells.py

@@ -1,4 +1,8 @@
-from .GSV4BT import GSV4BT
+if __name__ == "__main__":
+  from GSV4BT import GSV4BT
+else:
+  from .GSV4BT import GSV4BT
+  
 import time
 import bluetooth
 import threading