#!/usr/bin/python3 import logging import argparse import signal import json from controller import Controller, commands from plant_sim import Plant_sim from plant import Plant from stirrer_sim import Stirrer_sim from stirrer_pololu1376 import Stirrer_pololu1376 from matplotlib.pyplot import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show, subplot def results_plot(self): fp = open("results.dat", 'w') for n in range(0, len(self.time_v)): d = self.time_v[n] v1 = self.theta_v[n] v2 = self.heatrate_v[n] v3 = self.power_v[n] v4 = self.error_v[n] fp.write("{:6.3f} {:6.3f} {:6.3f} {:6.3f} {:6.3f}\n".format(d, v1, v2, v3, v4,)) fp.close() figure(1) subplot(4, 1, 1) plot(self.time_v, self.theta_v, 'r-', linewidth=1) title('Temperature') grid(True) ylabel('°C') subplot(4, 1, 2) plot(self.time_v, self.power_heat_v, self.time_v, self.power_hold_v, self.time_v, self.power_v, 'r-', linewidth=1) title('Power') grid(True) ylabel('W') subplot(4, 1, 3) plot(self.time_v, self.error_v, 'b-', linewidth=1) title('Error') grid(True) ylabel('°C') subplot(4, 1, 4) plot(self.time_v, self.heatrate_v, 'r-', linewidth=1) title('Heatrate') grid(True) ylabel('°C/min') xlabel('t/min') show() if __name__ == '__main__': logging.getLogger().setLevel(logging.INFO) FORMAT = '%(asctime)-15s %(user)-8s %(message)s' logging.basicConfig(format=FORMAT) parser = argparse.ArgumentParser(description='Brew some beer.') parser.add_argument('--receipe', help='the name of the receipe') parser.add_argument('--config', help='the name of the configuration file') args = parser.parse_args() if args.receipe is None: raise Exception("Invalid receipe") config = "brewpi.json" if args.config is not None: config = args.config stirr_isOn = False def handler(signum, frame): global stirr_isOn print('Signal handler called with signal', signum) if signum == signal.SIGINT: ablauf.command(commands.ABORT) if signum == signal.SIGSTOP: ablauf.command(commands.PAUSE) if signum == signal.SIGUSR1: ablauf.command(commands.RUN) if signum == signal.SIGHUP: if stirr_isOn: ablauf.command(commands.STIRR_OFF) stirr_isOn = False else: ablauf.command(commands.STIRR_ON) stirr_isOn = True # Set the signal handler and a 5-second alarm signal.signal(signal.SIGABRT, handler) signal.signal(signal.SIGINT, handler) signal.signal(signal.SIGUSR1, handler) signal.signal(signal.SIGHUP, handler) fp = open(config) config = json.load(fp) plant_class = globals()[config["Controller"]['plant_name']] plant = plant_class(config["Plant"]) ruehrer_class = globals()[config["Controller"]['stirrer_name']] ruehrer = ruehrer_class(config["Stirrer"]) fp = open(args.receipe) recipe = json.load(fp) ablauf = Controller(config["Controller"], plant, ruehrer, recipe) ablauf.wait_finished() results_plot(ablauf) print ("End of program.")