diff --git a/.gitignore b/.gitignore index 35300ae..d17affe 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ thumbs.db #storage/profiles #config.py .idea/* +state.json diff --git a/config.py b/config.py index 3fff867..e6abc80 100644 --- a/config.py +++ b/config.py @@ -204,7 +204,7 @@ ignore_tc_too_many_errors = False # cleaned up (deleted) by the OS on boot. # The state file is written to disk every sensor_time_wait seconds (2s by default) # and is written in the same directory as config.py. -automatic_restarts = True +automatic_restarts = False automatic_restart_window = 15 # max minutes since power outage automatic_restart_state_file = os.path.abspath(os.path.join(os.path.dirname( __file__ ),'state.json')) diff --git a/lib/oven.py b/lib/oven.py index 57efb7d..dffc7c0 100644 --- a/lib/oven.py +++ b/lib/oven.py @@ -344,6 +344,9 @@ class Oven(threading.Thread): self.reset() self.save_automatic_restart_state() + def get_start_time(self): + return datetime.datetime.now() - datetime.timedelta(milliseconds = self.runtime * 1000) + def kiln_must_catch_up(self): '''shift the whole schedule forward in time by one time_step to wait for the kiln to catch up''' @@ -353,11 +356,11 @@ class Oven(threading.Thread): # kiln too cold, wait for it to heat up if self.target - temp > config.pid_control_window: log.info("kiln must catch up, too cold, shifting schedule") - self.start_time = datetime.datetime.now() - datetime.timedelta(milliseconds = self.runtime * 1000) + self.start_time = self.get_start_time() # kiln too hot, wait for it to cool down if temp - self.target > config.pid_control_window: log.info("kiln must catch up, too hot, shifting schedule") - self.start_time = datetime.datetime.now() - datetime.timedelta(milliseconds = self.runtime * 1000) + self.start_time = self.get_start_time() def update_runtime(self): @@ -505,6 +508,7 @@ class SimulatedOven(Oven): self.R_o_nocool = config.sim_R_o_nocool self.R_ho_noair = config.sim_R_ho_noair self.R_ho = self.R_ho_noair + self.speedup_factor = 1000 # set temps to the temp of the surrounding environment self.t = self.t_env # deg C temp of oven @@ -516,6 +520,21 @@ class SimulatedOven(Oven): self.start() log.info("SimulatedOven started") + # runtime is in sped up time, start_time is actual time of day + def get_start_time(self): + return datetime.datetime.now() - datetime.timedelta(milliseconds = self.runtime * 1000 / self.speedup_factor) + + def update_runtime(self): + + runtime_delta = datetime.datetime.now() - self.start_time + if runtime_delta.total_seconds() < 0: + runtime_delta = datetime.timedelta(0) + + self.runtime = runtime_delta.total_seconds() * self.speedup_factor + + def update_target_temp(self): + self.target = self.profile.get_target_temperature(self.runtime) + def heating_energy(self,pid): # using pid here simulates the element being on for # only part of the time_step @@ -539,9 +558,11 @@ class SimulatedOven(Oven): self.board.temp_sensor.simulated_temperature = self.t def heat_then_cool(self): + now_simulator = self.start_time + datetime.timedelta(milliseconds = self.runtime * 1000) pid = self.pid.compute(self.target, - self.board.temp_sensor.temperature() + - config.thermocouple_offset) + self.board.temp_sensor.temperature + + config.thermocouple_offset, now_simulator) + heat_on = float(self.time_step * pid) heat_off = float(self.time_step * (1 - pid)) @@ -580,7 +601,7 @@ class SimulatedOven(Oven): # we don't actually spend time heating & cooling during # a simulation, so sleep. - time.sleep(self.time_step) + time.sleep(self.time_step / self.speedup_factor) class RealOven(Oven): @@ -602,8 +623,9 @@ class RealOven(Oven): def heat_then_cool(self): pid = self.pid.compute(self.target, - self.board.temp_sensor.temperature() + - config.thermocouple_offset) + self.board.temp_sensor.temperature + + config.thermocouple_offset, datetime.datetime.now()) + heat_on = float(self.time_step * pid) heat_off = float(self.time_step * (1 - pid)) @@ -685,8 +707,7 @@ class PID(): # settled on -50 to 50 and then divide by 50 at the end. This results # in a larger PID control window and much more accurate control... # instead of what used to be binary on/off control. - def compute(self, setpoint, ispoint): - now = datetime.datetime.now() + def compute(self, setpoint, ispoint, now): timeDelta = (now - self.lastNow).total_seconds() window_size = 100 diff --git a/lib/ovenWatcher.py b/lib/ovenWatcher.py index 3e47e4f..be13146 100644 --- a/lib/ovenWatcher.py +++ b/lib/ovenWatcher.py @@ -1,4 +1,6 @@ import threading,logging,json,time,datetime + +import config from oven import Oven log = logging.getLogger(__name__) @@ -32,7 +34,12 @@ class OvenWatcher(threading.Thread): else: self.recording = False self.notify_all(oven_state) - time.sleep(self.oven.time_step) + + if config.simulate: + time.sleep(self.oven.time_step) + else: + time.sleep(self.oven.time_step) + def lastlog_subset(self,maxpts=50): '''send about maxpts from lastlog by skipping unwanted data'''