add calibration serial command
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import ttk
|
from tkinter import ttk
|
||||||
|
from tkinter.messagebox import showinfo
|
||||||
import threading
|
import threading
|
||||||
|
from time import sleep
|
||||||
|
from statistics import mean
|
||||||
|
|
||||||
from .serial_reader import WeightReader
|
from .serial_reader import SerialReader
|
||||||
from .config import DEFAULT_CALIB, DISPLAY_TYPES
|
from .config import DEFAULT_CALIB, DISPLAY_TYPES
|
||||||
from .views import NumberView
|
from .views import NumberView
|
||||||
|
|
||||||
class WeightApp(tk.Tk):
|
class WeightApp(tk.Tk):
|
||||||
def __init__(self, weight_reader: WeightReader):
|
def __init__(self, weight_reader: SerialReader):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.weight_reader = weight_reader
|
self.weight_reader = weight_reader
|
||||||
|
|
||||||
@@ -48,7 +51,7 @@ class WeightApp(tk.Tk):
|
|||||||
self.view_type.pack()
|
self.view_type.pack()
|
||||||
self.view_type_label = ttk.Label(self.view_type, text="Visual:")
|
self.view_type_label = ttk.Label(self.view_type, text="Visual:")
|
||||||
self.view_type_label.pack(side=tk.LEFT)
|
self.view_type_label.pack(side=tk.LEFT)
|
||||||
self.view_type_select = ttk.Combobox(self.view_type, values=[t.value for t in DISPLAY_TYPES], command=self.update_view)
|
self.view_type_select = ttk.Combobox(self.view_type, values=[t.value for t in DISPLAY_TYPES], postcommand=self.update_view)
|
||||||
self.view_type_select.set(DISPLAY_TYPES.NUMBER.value)
|
self.view_type_select.set(DISPLAY_TYPES.NUMBER.value)
|
||||||
self.view_type_select.pack(side=tk.LEFT)
|
self.view_type_select.pack(side=tk.LEFT)
|
||||||
|
|
||||||
@@ -89,7 +92,7 @@ class WeightApp(tk.Tk):
|
|||||||
|
|
||||||
def update_view(self):
|
def update_view(self):
|
||||||
selected_view = self.view_type_select.get()
|
selected_view = self.view_type_select.get()
|
||||||
if selected_view == DISPLAY_TYPES.NUMBER:
|
if selected_view == DISPLAY_TYPES.NUMBER.value:
|
||||||
self.view = NumberView(self,
|
self.view = NumberView(self,
|
||||||
tare_command=self.weight_reader.tare,
|
tare_command=self.weight_reader.tare,
|
||||||
calibrate_command=self.calibrate,
|
calibrate_command=self.calibrate,
|
||||||
@@ -104,10 +107,21 @@ class WeightApp(tk.Tk):
|
|||||||
self.after(100, self.update_weight_display)
|
self.after(100, self.update_weight_display)
|
||||||
|
|
||||||
def calibrate(self):
|
def calibrate(self):
|
||||||
...
|
showinfo("Calibration", "Remove all weights.")
|
||||||
|
self.weight_reader.reset()
|
||||||
|
sleep(2)
|
||||||
|
showinfo("Calibration", "Place Weight")
|
||||||
|
self.weight_reader.calibrating = True
|
||||||
|
sleep(30)
|
||||||
|
self.weight_reader.calibrating = False
|
||||||
|
print(self.weight_reader.calib_window)
|
||||||
|
self.calib_measurements.delete(0, 'end')
|
||||||
|
self.calib_measurements.insert(0, mean(self.weight_reader.calib_window))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
weight_reader = WeightReader()
|
weight_reader = SerialReader()
|
||||||
threading.Thread(target=weight_reader.read_weights, daemon=True).start()
|
threading.Thread(target=weight_reader.read_weights, daemon=True).start()
|
||||||
|
|
||||||
app = WeightApp(weight_reader)
|
app = WeightApp(weight_reader)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from serial.tools import list_ports
|
|||||||
|
|
||||||
from .config import DEFAULT_CALIB, DEFAULT_CALIB_WEIGHT, MOV_AVG_DEFAULTS
|
from .config import DEFAULT_CALIB, DEFAULT_CALIB_WEIGHT, MOV_AVG_DEFAULTS
|
||||||
|
|
||||||
class WeightReader:
|
class SerialReader:
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
return (self.current_raw_weight - self._tare) * self.calib_factor
|
return (self.current_raw_weight - self._tare) * self.calib_factor
|
||||||
@@ -21,8 +21,19 @@ class WeightReader:
|
|||||||
self.calib_factor = value
|
self.calib_factor = value
|
||||||
self._raw_reset_threshold = self.reset_threshold / value
|
self._raw_reset_threshold = self.reset_threshold / value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def calibrating(self):
|
||||||
|
return self._calibrating
|
||||||
|
|
||||||
|
@calibrating.setter
|
||||||
|
def calibrating(self, value):
|
||||||
|
if value:
|
||||||
|
self.calib_window = []
|
||||||
|
self._calibrating = value
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
|
self._calibrating = False
|
||||||
self.ports = [d.device for d in list_ports.grep('usbmodem')]
|
self.ports = [d.device for d in list_ports.grep('usbmodem')]
|
||||||
|
|
||||||
self.serial = None
|
self.serial = None
|
||||||
@@ -36,6 +47,7 @@ class WeightReader:
|
|||||||
self._tare = 0.0
|
self._tare = 0.0
|
||||||
|
|
||||||
self.window = []
|
self.window = []
|
||||||
|
self.calib_window = []
|
||||||
self.current_raw_weight = 0
|
self.current_raw_weight = 0
|
||||||
self.ignored_samples = 0
|
self.ignored_samples = 0
|
||||||
|
|
||||||
@@ -50,13 +62,23 @@ class WeightReader:
|
|||||||
self.serial.close()
|
self.serial.close()
|
||||||
self.serial = None
|
self.serial = None
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
if self.serial is not None:
|
||||||
|
self.serial.write('reset'.encode())
|
||||||
|
|
||||||
def read_weights(self):
|
def read_weights(self):
|
||||||
while self.running:
|
while self.running:
|
||||||
if self.serial is not None:
|
if self.serial is not None:
|
||||||
try:
|
try:
|
||||||
line = self.serial.readline().decode('utf-8')
|
line = self.serial.readline().decode('utf-8')
|
||||||
|
if line.startswith('calibrated'):
|
||||||
|
print('calibrated')
|
||||||
|
continue
|
||||||
raw_weight = int(line.split(',')[1])
|
raw_weight = int(line.split(',')[1])
|
||||||
|
|
||||||
|
if self._calibrating:
|
||||||
|
self.calib_window.append(raw_weight)
|
||||||
|
|
||||||
self.filter(raw_weight)
|
self.filter(raw_weight)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ class View(Frame):
|
|||||||
|
|
||||||
self.actions = Frame(self)
|
self.actions = Frame(self)
|
||||||
self.actions.pack()
|
self.actions.pack()
|
||||||
# self.calibrate_button = ttk.Button(self.actions, text="Calibrate", command=calibrate_command)
|
self.calibrate_button = ttk.Button(self.actions, text="Calibrate", command=calibrate_command)
|
||||||
# self.calibrate_button.pack()
|
self.calibrate_button.pack()
|
||||||
self.tare_button = ttk.Button(self.actions, text="Tare", command=tare_command)
|
self.tare_button = ttk.Button(self.actions, text="Tare", command=tare_command)
|
||||||
self.tare_button.pack()
|
self.tare_button.pack()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user