heater: Don't peak into Heater class member variables

The control classes should not peak into the heater member variables.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-06-27 14:32:10 -04:00
parent 483c51e864
commit 7e497af184
2 changed files with 32 additions and 23 deletions

View File

@ -47,6 +47,7 @@ TUNE_PID_DELTA = 5.0
class ControlAutoTune:
def __init__(self, heater):
self.heater = heater
self.heater_max_power = heater.get_max_power()
# Heating control
self.heating = False
self.peak = 0.
@ -60,20 +61,21 @@ class ControlAutoTune:
# Heater control
def set_pwm(self, read_time, value):
if value != self.last_pwm:
self.pwm_samples.append((read_time + self.heater.pwm_delay, value))
self.pwm_samples.append(
(read_time + self.heater.get_pwm_delay(), value))
self.last_pwm = value
self.heater.set_pwm(read_time, value)
def temperature_callback(self, read_time, temp):
def temperature_update(self, read_time, temp, target_temp):
self.temp_samples.append((read_time, temp))
if self.heating and temp >= self.heater.target_temp:
if self.heating and temp >= target_temp:
self.heating = False
self.check_peaks()
elif (not self.heating
and temp <= self.heater.target_temp - TUNE_PID_DELTA):
and temp <= target_temp - TUNE_PID_DELTA):
self.heating = True
self.check_peaks()
if self.heating:
self.set_pwm(read_time, self.heater.max_power)
self.set_pwm(read_time, self.heater_max_power)
if temp < self.peak:
self.peak = temp
self.peak_time = read_time
@ -82,7 +84,7 @@ class ControlAutoTune:
if temp > self.peak:
self.peak = temp
self.peak_time = read_time
def check_busy(self, eventtime):
def check_busy(self, eventtime, last_temp, target_temp):
if self.heating or len(self.peaks) < 12:
return True
return False
@ -99,8 +101,7 @@ class ControlAutoTune:
def calc_pid(self, pos):
temp_diff = self.peaks[pos][0] - self.peaks[pos-1][0]
time_diff = self.peaks[pos][1] - self.peaks[pos-2][1]
max_power = self.heater.max_power
Ku = 4. * (2. * max_power) / (abs(temp_diff) * math.pi)
Ku = 4. * (2. * self.heater_max_power) / (abs(temp_diff) * math.pi)
Tu = time_diff
Ti = 0.5 * Tu
@ -109,7 +110,7 @@ class ControlAutoTune:
Ki = Kp / Ti
Kd = Kp * Td
logging.info("Autotune: raw=%f/%f Ku=%f Tu=%f Kp=%f Ki=%f Kd=%f",
temp_diff, max_power, Ku, Tu, Kp, Ki, Kd)
temp_diff, self.heater_max_power, Ku, Tu, Kp, Ki, Kd)
return Kp, Ki, Kd
def calc_final_pid(self):
cycle_times = [(self.peaks[pos][1] - self.peaks[pos-2][1], pos)

View File

@ -76,9 +76,13 @@ class Heater:
self.last_temp = temp
self.last_temp_time = read_time
self.can_extrude = (temp >= self.min_extrude_temp)
self.control.temperature_callback(read_time, temp)
self.control.temperature_update(read_time, temp, self.target_temp)
#logging.debug("temp: %.3f %f = %f", read_time, temp)
# External commands
def get_pwm_delay(self):
return self.pwm_delay
def get_max_power(self):
return self.max_power
def set_temp(self, print_time, degrees):
if degrees and (degrees < self.min_temp or degrees > self.max_temp):
raise error("Requested temperature (%.1f) out of range (%.1f:%.1f)"
@ -93,7 +97,8 @@ class Heater:
return self.last_temp, self.target_temp
def check_busy(self, eventtime):
with self.lock:
return self.control.check_busy(eventtime)
return self.control.check_busy(
eventtime, self.last_temp, self.target_temp)
def set_control(self, control):
with self.lock:
old_control = self.control
@ -122,19 +127,20 @@ class Heater:
class ControlBangBang:
def __init__(self, heater, config):
self.heater = heater
self.heater_max_power = heater.get_max_power()
self.max_delta = config.getfloat('max_delta', 2.0, above=0.)
self.heating = False
def temperature_callback(self, read_time, temp):
if self.heating and temp >= self.heater.target_temp+self.max_delta:
def temperature_update(self, read_time, temp, target_temp):
if self.heating and temp >= target_temp+self.max_delta:
self.heating = False
elif not self.heating and temp <= self.heater.target_temp-self.max_delta:
elif not self.heating and temp <= target_temp-self.max_delta:
self.heating = True
if self.heating:
self.heater.set_pwm(read_time, self.heater.max_power)
self.heater.set_pwm(read_time, self.heater_max_power)
else:
self.heater.set_pwm(read_time, 0.)
def check_busy(self, eventtime):
return self.heater.last_temp < self.heater.target_temp-self.max_delta
def check_busy(self, eventtime, last_temp, target_temp):
return last_temp < target_temp-self.max_delta
######################################################################
@ -147,17 +153,19 @@ PID_SETTLE_SLOPE = .1
class ControlPID:
def __init__(self, heater, config):
self.heater = heater
self.heater_max_power = heater.get_max_power()
self.Kp = config.getfloat('pid_Kp') / PID_PARAM_BASE
self.Ki = config.getfloat('pid_Ki') / PID_PARAM_BASE
self.Kd = config.getfloat('pid_Kd') / PID_PARAM_BASE
self.min_deriv_time = config.getfloat('pid_deriv_time', 2., above=0.)
imax = config.getfloat('pid_integral_max', heater.max_power, minval=0.)
imax = config.getfloat('pid_integral_max', self.heater_max_power,
minval=0.)
self.temp_integ_max = imax / self.Ki
self.prev_temp = AMBIENT_TEMP
self.prev_temp_time = 0.
self.prev_temp_deriv = 0.
self.prev_temp_integ = 0.
def temperature_callback(self, read_time, temp):
def temperature_update(self, read_time, temp, target_temp):
time_diff = read_time - self.prev_temp_time
# Calculate change of temperature
temp_diff = temp - self.prev_temp
@ -167,14 +175,14 @@ class ControlPID:
temp_deriv = (self.prev_temp_deriv * (self.min_deriv_time-time_diff)
+ temp_diff) / self.min_deriv_time
# Calculate accumulated temperature "error"
temp_err = self.heater.target_temp - temp
temp_err = target_temp - temp
temp_integ = self.prev_temp_integ + temp_err * time_diff
temp_integ = max(0., min(self.temp_integ_max, temp_integ))
# Calculate output
co = self.Kp*temp_err + self.Ki*temp_integ - self.Kd*temp_deriv
#logging.debug("pid: %f@%.3f -> diff=%f deriv=%f err=%f integ=%f co=%d",
# temp, read_time, temp_diff, temp_deriv, temp_err, temp_integ, co)
bounded_co = max(0., min(self.heater.max_power, co))
bounded_co = max(0., min(self.heater_max_power, co))
self.heater.set_pwm(read_time, bounded_co)
# Store state for next measurement
self.prev_temp = temp
@ -182,8 +190,8 @@ class ControlPID:
self.prev_temp_deriv = temp_deriv
if co == bounded_co:
self.prev_temp_integ = temp_integ
def check_busy(self, eventtime):
temp_diff = self.heater.target_temp - self.heater.last_temp
def check_busy(self, eventtime, last_temp, target_temp):
temp_diff = target_temp - last_temp
return (abs(temp_diff) > PID_SETTLE_DELTA
or abs(self.prev_temp_deriv) > PID_SETTLE_SLOPE)