import os, sys, io
import M5
from time import sleep
from M5 import *
from hardware import *
from unit import WEIGHTUnit
label0 = None
title0 = None
label1 = None
label2 = None
weight_0 = None
import math
gewicht = None
# Parameter zum Anpassen
gl_BufferSize = 7
gl_MessIntervall = 0.1
gl_MaxAbweichung = 2.0
# Globale Variablen
gl_Gewichte =[0] * gl_BufferSize
gl_WrPtr = 0
# zum Aufzeichnen der Kräfte die bei einem Anflug oder Abflug auftreten
gl_AnflugkurveSize = 100 # bei einem Messinterval von 0.1 S ist die Kurve maximal 10 s Lang
gl_Anflugkurve = []
def btnA_wasClicked_event(state):
global label0, weight_0
weight_0.set_calibrate_scale(978)
def mean(a):
Summe=0
for w in a:
Summe += w
return Summe / len(a)
def pstdev(a):
mittelwert = mean(a)
Summe = 0
for w in a:
delta = (w - Mittelwert)
Summe = Summe + (delta * delta)
return math.sqrt(Summe / len(a))
def setup():
global label0, title0, label1, label2, weight_0, gewicht
weight_0 = WEIGHTUnit((33,32))
M5.begin()
Widgets.fillScreen(0x222222)
label0 = Widgets.Label("label0", 30, 87, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu72)
title0 = Widgets.Title("Futter/Feed", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu40)
label1 = Widgets.Label("Kg", 30, 163, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu24)
label2 = Widgets.Label(">0<", 30, 224, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu12)
BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btnA_wasClicked_event)
weight_0.set_tare()
def loop():
global label0, title0, label1, label2, weight_0, gewicht, gl_BufferSize, gl_MessIntervall, gl_MaxAbweichung, gl_Gewichte, gl_WrPtr, gl_AnflugkurveSize, gl_Anflugkurve
M5.update()
global gl_Gewichte
global gl_MaxAbweichung
global gl_MessIntervall
global gl_Gewichte
global gl_WrPtr
global gl_Anflugkurve
global gl_AnflugkurveSize
Gewicht = weight_0.get_scale_weight
# Messwerte sind hier als Ringspeicher organisiert
gl_Gewichte[gl_WrPtr] = Gewicht
gl_WrPtr += 1
if (gl_WrPtr >= gl_BufferSize):
gl_WrPtr = 0
Abweichung = pstdev(gl_Gewichte)
if Abweichung < gl_MaxAbweichung:
Mittelwert = mean(gl_Gewichte)
# ==> auf das Display oder wo auch immer hin
label0.setText(str(Mittelwert)) # Nun kann man mit dem Wert machen was immer auch man will
# Anflugkurve auf den Plotter :-)
# plot(gl_Anflugkurve)
gl_Anflugkurve = [] # Kurve resetten für einen neuen Anflugversuch
else:
if len(gl_Anflugkurve) < gl_AnflugkurveSize:
gl_Anflugkurve.append(Gewicht)
sleep(gl_MessIntervall)
if __name__ == '__main__':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")