def tare_ok(self, tare_list): i = 0 # We compare each value in the tare_list and see if it is within the allowed TARE_WIDTH # of the corresponding approximate expected reading in TARE_READINGS. Also the total # of the readings must be within the config total +/- * TARE_WIDTH * 2. # We will only return True if *all* tare readings and the tare_total are within acceptable bounds. tare_delta_total = 0 max_delta = 0 # we track max delta for debug purposes max_i = 0 while i < len(tare_list): tare_delta = tare_list[i] - self.settings["TARE_READINGS"][i] if abs(tare_delta) > max_delta: max_delta = tare_delta max_i = i tare_delta_total += tare_delta if abs(tare_delta) > self.settings["TARE_WIDTH"]: if self.settings["LOG_LEVEL"] <= 2: print( "tare_ok FAIL reading[{}] {:.0f} out of range vs {:.0f} +/- {}" .format(i, tare_list[i], self.settings["TARE_READINGS"][i], self.settings["TARE_WIDTH"])) return False else: i += 1 if tare_delta_total > self.settings["TARE_WIDTH"]: if self.settings["LOG_LEVEL"] == 1: print( "tare_ok total delta {} of [{}] is out of range for [{}] +/- {}" .format( tare_delta_total, list_to_string(tare_list, "{:+.0f}"), list_to_string(self.settings["TARE_READINGS"], "{:+.0f}"), self.settings["TARE_WIDTH"])) return False if self.settings["LOG_LEVEL"] == 1: print("tare_ok is OK, max delta[{}] was {:.0f}".format( max_i, max_delta)) return True
def tare_scales(self): t_start = time.process_time() tare_list = [] # we 'tare' each sensor, this will also update the tare value used in each HX771 object for hx in self.hx_list: # Here we initialize the 'empty weight' settings tare_list.append(hx.tare_A()) print("tare_scales readings [ {} ] completed at {:.3f} secs.".format( list_to_string(tare_list, "{:+.0f}"), time.process_time() - t_start)) # If the tare_list is 'ok' (i.e. within bounds) we will write it to the tare file and return it as the result if self.tare_ok(tare_list): print("tare_scales updating tare file.") self.write_tare_file(tare_list) return tare_list # Otherwise.. the tare reading was NOT ok... # The new tare readings are out of range, so use persisted values tare_dictionary = self.read_tare_file() tare_list = tare_dictionary["tares"] # As the measured tare values are not acceptable, we now update the HX711 objects with the persisted values. i = 0 for hx in self.hx_list: hx.set_offset_A(tare_list[i]) i += 1 output_string = "tare_scales readings out of range, using persisted values [ {} ]" print(output_string.format(list_to_string(tare_list, "{:+.0f}"))) return tare_list
def get_value(self): t_start = time.process_time() total_reading = 0 for hx in self.hx_list: # get_weight accepts a parameter 'number of times to sample weight and then average' reading = hx.get_weight_A(1) total_reading = total_reading + reading if self.settings["LOG_LEVEL"] == 1: output_string = "get_weight readings [ {} ] completed at {:.3f} secs." print( output_string.format(list_to_string(debug_list, "{:+.0f}"), time.process_time() - t_start)) return total_reading / self.settings["WEIGHT_FACTOR"] # grams
import sys from classes.time_buffer import TimeBuffer from classes.config import Config from classes.sensor import Sensor from classes.sensor_utils import list_to_string print("test.py started with {} arguments: [{}]".format(len(sys.argv), list_to_string(sys.argv))) if len(sys.argv) > 1 : filename = sys.argv[1] config = Config(filename) else: config = Config(None) config.settings["VERSION"] = "TEST_0.1" s = Sensor(settings = config.settings) s.begin() # for playback we can specify # sleep=0.1 for a fixed period between samples # or # realtime=True which will pause the time between recorded sample timestamps. # otherwise the playback will be as fast as possible. t = TimeBuffer(settings=config.settings)
from classes.config import Config from classes.sensor_utils import list_to_string VERSION = "0.60" # ---------------------------------------------------------------------- # ---------------------------------------------------------------------- # main code # ---------------------------------------------------------------------- # ---------------------------------------------------------------------- if __name__ == "__main__": print("main started with {} arguments {}".format(len(sys.argv), list_to_string(sys.argv))) if len(sys.argv) > 1: filename = sys.argv[1] config = Config(filename) else: config = Config() config.settings["VERSION"] = VERSION s = Sensor(settings=config.settings) weight_sensor = WeightSensor(config.settings) s.begin()