from components.pid.temp_controller_base import TempControllerBase


class TempController(TempControllerBase):
	def __init__(self, dt, params):
		TempControllerBase.__init__(self, dt, params)
		self.dt = dt
		self.last_theta_ist = 20
		self.heatrate_ist = 0

	def process(self):
		# Process Kalman
		self.theta_ist = self.theta_ist_set
		heatrate = (self.theta_ist - self.last_theta_ist)/self.dt*60

		alpha = 0.1
		self.heatrate_ist = (1-alpha) * self.heatrate_ist + alpha*heatrate
		self.last_theta_ist = self.theta_ist

		# Compensate for max heat rate to reduce overshoot
		hold_scale = 1.0/self.heatrate_soll_set if self.heatrate_soll_set > 0 else 1.0

		self.heatrate_soll = self.heatrate_soll_set * self.pid_hold.get_y()
		theta_err = self.theta_soll_set - self.theta_ist
		heatrate_err = self.heatrate_soll - self.heatrate_ist

		diff = self.theta_soll_set - self.theta_ist
		self.process_fsm(diff)
		self.process_pid(theta_err, heatrate_err, hold_scale)
