|
@@ -15,9 +15,11 @@ import tk_tools
|
|
|
from tkinter import filedialog
|
|
|
import time
|
|
|
import numpy as np
|
|
|
+import threading
|
|
|
|
|
|
|
|
|
class Main(tk.Tk, Table):
|
|
|
+ running = True
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
print('initializing window...')
|
|
|
tk.Tk.__init__(self, *args, **kwargs)
|
|
@@ -93,6 +95,15 @@ class Main(tk.Tk, Table):
|
|
|
|
|
|
print('program ready!')
|
|
|
|
|
|
+ self.windspeed = 0
|
|
|
+ self.adcValue = 0
|
|
|
+ self.pwmValue = 0
|
|
|
+ self.pidDelay = 100
|
|
|
+
|
|
|
+ self.pidThread = threading.Thread(target=self.pidThreadFunction)
|
|
|
+ self.pidThread.daemon = True
|
|
|
+ self.pidThread.start()
|
|
|
+
|
|
|
self.intervalDelay = 300 # ms
|
|
|
self.interval()
|
|
|
|
|
@@ -114,6 +125,28 @@ class Main(tk.Tk, Table):
|
|
|
self.saveAsCsv(f)
|
|
|
f.close()
|
|
|
|
|
|
+ def pidThreadFunction(self):
|
|
|
+ while self.running:
|
|
|
+ start = time.time()
|
|
|
+ self.adcValue = self.adc.getVoltage(0)
|
|
|
+ self.windSpeed = self.adcValue * 5
|
|
|
+ tmp = self.pid.update(self.windSpeed, start)
|
|
|
+
|
|
|
+ if self.motorEnabled.get() == 0:
|
|
|
+ self.pid.clear()
|
|
|
+ self.pwmValue = 0
|
|
|
+ elif tmp > 100:
|
|
|
+ self.pwmValue = 100
|
|
|
+ elif tmp < 0:
|
|
|
+ self.pwmValue = 0
|
|
|
+ else:
|
|
|
+ self.pwmValue = tmp
|
|
|
+
|
|
|
+ self.motorController.setDutyCycle(self.pwmValue)
|
|
|
+
|
|
|
+ elapsed = time.time() - start
|
|
|
+ time.sleep(max(0, self.pidDelay / 1000 - elapsed))
|
|
|
+
|
|
|
def interval(self):
|
|
|
self.after(self.intervalDelay, self.interval)
|
|
|
start = time.time()
|
|
@@ -121,27 +154,13 @@ class Main(tk.Tk, Table):
|
|
|
setValue = self.frames['Page_1'].speedSlider.get()
|
|
|
self.pid.setInput(setValue)
|
|
|
|
|
|
- adcValue = self.adc.getVoltage(0)
|
|
|
- windSpeed = adcValue * 5
|
|
|
- pwmValue = self.pid.update(windSpeed)
|
|
|
-
|
|
|
- if self.motorEnabled.get() == 0:
|
|
|
- self.pid.clear()
|
|
|
- pwmValue = 0
|
|
|
- elif pwmValue > 100:
|
|
|
- pwmValue = 100
|
|
|
- elif pwmValue < 0:
|
|
|
- pwmValue = 0
|
|
|
-
|
|
|
- self.motorController.setDutyCycle(pwmValue)
|
|
|
-
|
|
|
i2cValues = self.pressureSensors.getValues()
|
|
|
btValues = self.forceSensors.getForces()
|
|
|
|
|
|
self.addRow(
|
|
|
- [windSpeed, setValue, pwmValue] +
|
|
|
+ [self.windSpeed, setValue, self.pwmValue] +
|
|
|
i2cValues +
|
|
|
- [adcValue] +
|
|
|
+ [self.adcValue] +
|
|
|
list(np.sum(btValues, axis = 0, initial=0)) +
|
|
|
list(btValues.flatten())
|
|
|
)
|
|
@@ -154,8 +173,10 @@ class Main(tk.Tk, Table):
|
|
|
print("draw: {:8.3f} ms".format((time.time() - start)*1000))
|
|
|
|
|
|
def stop(self):
|
|
|
- self.forceSensors.stop()
|
|
|
self.motorEnabled.set(0)
|
|
|
- self.motorController.setDutyCycle(0)
|
|
|
+ self.running = False
|
|
|
+ self.forceSensors.stop()
|
|
|
+ self.pidThread.join()
|
|
|
self.adc.close()
|
|
|
+ self.motorController.stop()
|
|
|
self.quit()
|