def check(): import sys n = 15000 try: del sys.modules['scripts.run.temp_sim'] except KeyError: pass from scripts.run.temp_sim import TempSim, PIDController sim = TempSim(D('28.183533'), 19, 0) pid = PIDController(37, 40, D('0.55')) pid.off_to_auto(sim.current_temp) pv = sim.current_temp #: @type: list[tuple[D]] steps1 = [] ap = steps1.append for _ in range(n): hd = pid.step_output(pv) t, pv = sim.step_heat(hd) ap((t, pv)) sim = TempSim(D('28.183533'), 19, 0) def auto_mode(self, pv): e_t = self.set_point - pv self.accumulated_error = - e_t * self.pgain * self.itime PIDController.off_to_auto = auto_mode pid = PIDController(37, 40, D('0.55')) pid.off_to_auto(sim.current_temp) pv = sim.current_temp #: @type: list[tuple[D]] steps2 = [] ap = steps2.append for _ in range(n): hd = pid.step_output(pv) t, pv = sim.step_heat(hd) ap((t, pv)) from decimal import Context ctxt = Context(prec=3) def eq(one, two, ctxt=ctxt, compare=D.compare): return not (compare(one[0], two[0], ctxt) and compare(one[1], two[1], ctxt)) diff = [(i, one, two) for i, (one, two) in enumerate(zip(steps1, steps2)) if not eq(one, two)] return diff
def plotpid2(n=15000): import sys try: del sys.modules['scripts.run.temp_sim'] except KeyError: pass from scripts.run.temp_sim import TempSim, PIDController sim = TempSim(D('28.183533'), 19, 0) pid = PIDController(37, 40, D('0.55')) # pid = PIDController(37, 40, 0.5) pid.off_to_auto(sim.current_temp) pv = sim.current_temp #: @type: list[tuple[D]] steps = [('0', '0', '0', '0', '0') for _ in range(11)] for _ in range(n): hd = pid.step_output(pv) t, pv = sim.step_heat(hd) steps.append((t, pv, pid.last_error, pid.accumulated_error, hd)) from officelib.xllib.xlcom import xlBook2 xl, wb = xlBook2('PID.xlsx') ws = wb.Worksheets(2) cells = ws.Cells firstcol = 30 cells.Range(cells(1, firstcol), cells(1, firstcol + 4)).Value = ( "Time", "Temp", "LastErr", "AccumErr", "HeatDuty%") cells.Range(cells(2, firstcol), cells(len(steps) + 1, len(steps[0]) + firstcol - 1)).Value = \ [tuple(map(str, data)) for data in steps]