2016-05-25 18:37:40 +03:00
|
|
|
# Printer heater support
|
|
|
|
#
|
2018-01-20 06:22:17 +03:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 18:37:40 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-04-04 21:53:47 +03:00
|
|
|
import logging, threading
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2017-08-17 20:18:13 +03:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Heater
|
|
|
|
######################################################################
|
|
|
|
|
2018-04-04 21:53:47 +03:00
|
|
|
KELVIN_TO_CELCIUS = -273.15
|
2016-05-25 18:37:40 +03:00
|
|
|
MAX_HEAT_TIME = 5.0
|
|
|
|
AMBIENT_TEMP = 25.
|
2016-08-25 18:24:18 +03:00
|
|
|
PID_PARAM_BASE = 255.
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2018-04-04 00:01:10 +03:00
|
|
|
class Heater:
|
2019-02-26 04:30:37 +03:00
|
|
|
def __init__(self, config, sensor):
|
2019-02-14 04:15:11 +03:00
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.gcode = self.printer.lookup_object("gcode")
|
|
|
|
self.name = config.get_name().split()[-1]
|
2018-07-06 20:27:05 +03:00
|
|
|
# Setup sensor
|
|
|
|
self.sensor = sensor
|
2018-03-12 21:36:45 +03:00
|
|
|
self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELCIUS)
|
2017-04-11 18:37:09 +03:00
|
|
|
self.max_temp = config.getfloat('max_temp', above=self.min_temp)
|
2018-04-04 21:13:59 +03:00
|
|
|
self.sensor.setup_minmax(self.min_temp, self.max_temp)
|
|
|
|
self.sensor.setup_callback(self.temperature_callback)
|
2018-04-04 21:53:47 +03:00
|
|
|
self.pwm_delay = self.sensor.get_report_time_delta()
|
2018-07-06 20:27:05 +03:00
|
|
|
# Setup temperature checks
|
2017-04-11 18:37:09 +03:00
|
|
|
self.min_extrude_temp = config.getfloat(
|
2019-02-27 21:09:24 +03:00
|
|
|
'min_extrude_temp', 170.,
|
|
|
|
minval=self.min_temp, maxval=self.max_temp)
|
|
|
|
is_fileoutput = (self.printer.get_start_args().get('debugoutput')
|
|
|
|
is not None)
|
2018-07-06 20:27:05 +03:00
|
|
|
self.can_extrude = self.min_extrude_temp <= 0. or is_fileoutput
|
2017-04-11 18:37:09 +03:00
|
|
|
self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.)
|
2018-07-06 20:27:05 +03:00
|
|
|
self.smooth_time = config.getfloat('smooth_time', 2., above=0.)
|
|
|
|
self.inv_smooth_time = 1. / self.smooth_time
|
2016-05-25 18:37:40 +03:00
|
|
|
self.lock = threading.Lock()
|
2018-07-06 20:27:05 +03:00
|
|
|
self.last_temp = self.smoothed_temp = self.target_temp = 0.
|
2016-08-24 22:16:02 +03:00
|
|
|
self.last_temp_time = 0.
|
2018-07-06 20:27:05 +03:00
|
|
|
# pwm caching
|
|
|
|
self.next_pwm_time = 0.
|
|
|
|
self.last_pwm_value = 0.
|
|
|
|
# Setup control algorithm sub-class
|
2017-01-11 02:36:43 +03:00
|
|
|
algos = {'watermark': ControlBangBang, 'pid': ControlPID}
|
2017-03-13 05:43:05 +03:00
|
|
|
algo = config.getchoice('control', algos)
|
2018-07-06 20:27:05 +03:00
|
|
|
self.control = algo(self, config)
|
|
|
|
# Setup output heater pin
|
2017-03-13 05:43:05 +03:00
|
|
|
heater_pin = config.get('heater_pin')
|
2019-02-14 04:15:11 +03:00
|
|
|
ppins = self.printer.lookup_object('pins')
|
2016-08-25 18:24:18 +03:00
|
|
|
if algo is ControlBangBang and self.max_power == 1.:
|
2018-04-04 19:07:41 +03:00
|
|
|
self.mcu_pwm = ppins.setup_pin('digital_out', heater_pin)
|
2017-01-11 02:36:43 +03:00
|
|
|
else:
|
2018-04-04 19:07:41 +03:00
|
|
|
self.mcu_pwm = ppins.setup_pin('pwm', heater_pin)
|
2017-12-31 02:43:03 +03:00
|
|
|
pwm_cycle_time = config.getfloat(
|
2018-04-04 21:53:47 +03:00
|
|
|
'pwm_cycle_time', 0.100, above=0., maxval=self.pwm_delay)
|
2017-12-31 02:43:03 +03:00
|
|
|
self.mcu_pwm.setup_cycle_time(pwm_cycle_time)
|
2017-08-21 18:25:26 +03:00
|
|
|
self.mcu_pwm.setup_max_duration(MAX_HEAT_TIME)
|
2018-03-18 18:23:20 +03:00
|
|
|
# Load additional modules
|
2019-02-14 04:15:11 +03:00
|
|
|
self.printer.try_load_module(config, "verify_heater %s" % (self.name,))
|
|
|
|
self.printer.try_load_module(config, "pid_calibrate")
|
2019-02-27 21:09:24 +03:00
|
|
|
self.gcode.register_mux_command(
|
|
|
|
"SET_HEATER_TEMPERATURE", "HEATER", self.name,
|
|
|
|
self.cmd_SET_HEATER_TEMPERATURE,
|
|
|
|
desc=self.cmd_SET_HEATER_TEMPERATURE_help)
|
2016-08-24 22:16:02 +03:00
|
|
|
def set_pwm(self, read_time, value):
|
2017-03-13 20:13:31 +03:00
|
|
|
if self.target_temp <= 0.:
|
|
|
|
value = 0.
|
|
|
|
if ((read_time < self.next_pwm_time or not self.last_pwm_value)
|
|
|
|
and abs(value - self.last_pwm_value) < 0.05):
|
|
|
|
# No significant change in value - can suppress update
|
2016-05-25 18:37:40 +03:00
|
|
|
return
|
2018-04-04 21:53:47 +03:00
|
|
|
pwm_time = read_time + self.pwm_delay
|
2016-08-24 22:16:02 +03:00
|
|
|
self.next_pwm_time = pwm_time + 0.75 * MAX_HEAT_TIME
|
2016-05-25 18:37:40 +03:00
|
|
|
self.last_pwm_value = value
|
2017-09-27 18:43:14 +03:00
|
|
|
logging.debug("%s: pwm=%.3f@%.3f (from %.3f@%.3f [%.3f])",
|
|
|
|
self.name, value, pwm_time,
|
|
|
|
self.last_temp, self.last_temp_time, self.target_temp)
|
2016-08-24 22:16:02 +03:00
|
|
|
self.mcu_pwm.set_pwm(pwm_time, value)
|
2018-04-04 21:13:59 +03:00
|
|
|
def temperature_callback(self, read_time, temp):
|
2016-05-25 18:37:40 +03:00
|
|
|
with self.lock:
|
2018-07-06 20:27:05 +03:00
|
|
|
time_diff = read_time - self.last_temp_time
|
2016-05-25 18:37:40 +03:00
|
|
|
self.last_temp = temp
|
2016-08-24 22:16:02 +03:00
|
|
|
self.last_temp_time = read_time
|
2018-06-27 21:32:10 +03:00
|
|
|
self.control.temperature_update(read_time, temp, self.target_temp)
|
2018-07-06 20:27:05 +03:00
|
|
|
temp_diff = temp - self.smoothed_temp
|
|
|
|
adj_time = min(time_diff * self.inv_smooth_time, 1.)
|
|
|
|
self.smoothed_temp += temp_diff * adj_time
|
|
|
|
self.can_extrude = (self.smoothed_temp >= self.min_extrude_temp)
|
2018-04-04 21:13:59 +03:00
|
|
|
#logging.debug("temp: %.3f %f = %f", read_time, temp)
|
2016-05-25 18:37:40 +03:00
|
|
|
# External commands
|
2018-06-27 21:32:10 +03:00
|
|
|
def get_pwm_delay(self):
|
|
|
|
return self.pwm_delay
|
|
|
|
def get_max_power(self):
|
|
|
|
return self.max_power
|
2018-07-06 20:27:05 +03:00
|
|
|
def get_smooth_time(self):
|
|
|
|
return self.smooth_time
|
2016-05-25 18:37:40 +03:00
|
|
|
def set_temp(self, print_time, degrees):
|
2017-02-21 18:48:42 +03:00
|
|
|
if degrees and (degrees < self.min_temp or degrees > self.max_temp):
|
2019-12-17 03:16:58 +03:00
|
|
|
raise self.printer.command_error(
|
|
|
|
"Requested temperature (%.1f) out of range (%.1f:%.1f)"
|
|
|
|
% (degrees, self.min_temp, self.max_temp))
|
2016-05-25 18:37:40 +03:00
|
|
|
with self.lock:
|
|
|
|
self.target_temp = degrees
|
2017-10-12 05:16:02 +03:00
|
|
|
def get_temp(self, eventtime):
|
2018-04-04 21:13:59 +03:00
|
|
|
print_time = self.mcu_pwm.get_mcu().estimated_print_time(eventtime) - 5.
|
2016-05-25 18:37:40 +03:00
|
|
|
with self.lock:
|
2017-10-12 05:16:02 +03:00
|
|
|
if self.last_temp_time < print_time:
|
|
|
|
return 0., self.target_temp
|
2018-07-06 20:27:05 +03:00
|
|
|
return self.smoothed_temp, self.target_temp
|
2016-05-25 18:37:40 +03:00
|
|
|
def check_busy(self, eventtime):
|
|
|
|
with self.lock:
|
2018-06-27 21:32:10 +03:00
|
|
|
return self.control.check_busy(
|
2018-07-06 20:27:05 +03:00
|
|
|
eventtime, self.smoothed_temp, self.target_temp)
|
2018-03-18 18:23:20 +03:00
|
|
|
def set_control(self, control):
|
2016-05-25 18:37:40 +03:00
|
|
|
with self.lock:
|
2018-03-18 18:23:20 +03:00
|
|
|
old_control = self.control
|
|
|
|
self.control = control
|
|
|
|
self.target_temp = 0.
|
|
|
|
return old_control
|
2018-06-27 21:39:20 +03:00
|
|
|
def alter_target(self, target_temp):
|
|
|
|
if target_temp:
|
|
|
|
target_temp = max(self.min_temp, min(self.max_temp, target_temp))
|
|
|
|
self.target_temp = target_temp
|
2018-01-29 07:37:56 +03:00
|
|
|
def stats(self, eventtime):
|
|
|
|
with self.lock:
|
|
|
|
target_temp = self.target_temp
|
|
|
|
last_temp = self.last_temp
|
|
|
|
last_pwm_value = self.last_pwm_value
|
2018-02-05 21:55:59 +03:00
|
|
|
is_active = target_temp or last_temp > 50.
|
|
|
|
return is_active, '%s: target=%.0f temp=%.1f pwm=%.3f' % (
|
2018-01-29 07:37:56 +03:00
|
|
|
self.name, target_temp, last_temp, last_pwm_value)
|
2018-02-21 04:50:06 +03:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
with self.lock:
|
|
|
|
target_temp = self.target_temp
|
2018-07-06 20:27:05 +03:00
|
|
|
smoothed_temp = self.smoothed_temp
|
|
|
|
return {'temperature': smoothed_temp, 'target': target_temp}
|
2019-02-14 04:15:11 +03:00
|
|
|
cmd_SET_HEATER_TEMPERATURE_help = "Sets a heater temperature"
|
|
|
|
def cmd_SET_HEATER_TEMPERATURE(self, params):
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
temp = self.gcode.get_float('TARGET', params, 0.)
|
|
|
|
self.set_temp(print_time, temp)
|
2016-05-25 18:37:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Bang-bang control algo
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class ControlBangBang:
|
|
|
|
def __init__(self, heater, config):
|
|
|
|
self.heater = heater
|
2018-06-27 21:32:10 +03:00
|
|
|
self.heater_max_power = heater.get_max_power()
|
2017-04-11 18:37:09 +03:00
|
|
|
self.max_delta = config.getfloat('max_delta', 2.0, above=0.)
|
2016-05-25 18:37:40 +03:00
|
|
|
self.heating = False
|
2018-06-27 21:32:10 +03:00
|
|
|
def temperature_update(self, read_time, temp, target_temp):
|
|
|
|
if self.heating and temp >= target_temp+self.max_delta:
|
2016-05-25 18:37:40 +03:00
|
|
|
self.heating = False
|
2018-06-27 21:32:10 +03:00
|
|
|
elif not self.heating and temp <= target_temp-self.max_delta:
|
2016-05-25 18:37:40 +03:00
|
|
|
self.heating = True
|
|
|
|
if self.heating:
|
2018-06-27 21:32:10 +03:00
|
|
|
self.heater.set_pwm(read_time, self.heater_max_power)
|
2016-05-25 18:37:40 +03:00
|
|
|
else:
|
2016-08-25 18:24:18 +03:00
|
|
|
self.heater.set_pwm(read_time, 0.)
|
2018-07-06 20:27:05 +03:00
|
|
|
def check_busy(self, eventtime, smoothed_temp, target_temp):
|
|
|
|
return smoothed_temp < target_temp-self.max_delta
|
2016-05-25 18:37:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Proportional Integral Derivative (PID) control algo
|
|
|
|
######################################################################
|
|
|
|
|
2018-01-03 18:44:01 +03:00
|
|
|
PID_SETTLE_DELTA = 1.
|
|
|
|
PID_SETTLE_SLOPE = .1
|
|
|
|
|
2016-05-25 18:37:40 +03:00
|
|
|
class ControlPID:
|
|
|
|
def __init__(self, heater, config):
|
|
|
|
self.heater = heater
|
2018-06-27 21:32:10 +03:00
|
|
|
self.heater_max_power = heater.get_max_power()
|
2016-08-25 18:24:18 +03:00
|
|
|
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
|
2018-07-06 20:27:05 +03:00
|
|
|
self.min_deriv_time = heater.get_smooth_time()
|
2018-06-27 21:32:10 +03:00
|
|
|
imax = config.getfloat('pid_integral_max', self.heater_max_power,
|
|
|
|
minval=0.)
|
2016-05-25 18:37:40 +03:00
|
|
|
self.temp_integ_max = imax / self.Ki
|
|
|
|
self.prev_temp = AMBIENT_TEMP
|
2016-08-24 22:16:02 +03:00
|
|
|
self.prev_temp_time = 0.
|
2016-05-25 18:37:40 +03:00
|
|
|
self.prev_temp_deriv = 0.
|
|
|
|
self.prev_temp_integ = 0.
|
2018-06-27 21:32:10 +03:00
|
|
|
def temperature_update(self, read_time, temp, target_temp):
|
2016-08-24 22:16:02 +03:00
|
|
|
time_diff = read_time - self.prev_temp_time
|
2016-05-25 18:37:40 +03:00
|
|
|
# Calculate change of temperature
|
|
|
|
temp_diff = temp - self.prev_temp
|
|
|
|
if time_diff >= self.min_deriv_time:
|
|
|
|
temp_deriv = temp_diff / time_diff
|
|
|
|
else:
|
|
|
|
temp_deriv = (self.prev_temp_deriv * (self.min_deriv_time-time_diff)
|
|
|
|
+ temp_diff) / self.min_deriv_time
|
|
|
|
# Calculate accumulated temperature "error"
|
2018-06-27 21:32:10 +03:00
|
|
|
temp_err = target_temp - temp
|
2016-05-25 18:37:40 +03:00
|
|
|
temp_integ = self.prev_temp_integ + temp_err * time_diff
|
|
|
|
temp_integ = max(0., min(self.temp_integ_max, temp_integ))
|
|
|
|
# Calculate output
|
2016-08-25 18:24:18 +03:00
|
|
|
co = self.Kp*temp_err + self.Ki*temp_integ - self.Kd*temp_deriv
|
2017-09-27 18:43:14 +03:00
|
|
|
#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)
|
2018-06-27 21:32:10 +03:00
|
|
|
bounded_co = max(0., min(self.heater_max_power, co))
|
2016-08-24 22:16:02 +03:00
|
|
|
self.heater.set_pwm(read_time, bounded_co)
|
2016-05-25 18:37:40 +03:00
|
|
|
# Store state for next measurement
|
|
|
|
self.prev_temp = temp
|
2016-08-24 22:16:02 +03:00
|
|
|
self.prev_temp_time = read_time
|
2016-05-25 18:37:40 +03:00
|
|
|
self.prev_temp_deriv = temp_deriv
|
|
|
|
if co == bounded_co:
|
|
|
|
self.prev_temp_integ = temp_integ
|
2018-07-06 20:27:05 +03:00
|
|
|
def check_busy(self, eventtime, smoothed_temp, target_temp):
|
|
|
|
temp_diff = target_temp - smoothed_temp
|
2018-01-03 18:44:01 +03:00
|
|
|
return (abs(temp_diff) > PID_SETTLE_DELTA
|
|
|
|
or abs(self.prev_temp_deriv) > PID_SETTLE_SLOPE)
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2018-04-04 00:01:10 +03:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Sensor and heater lookup
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class PrinterHeaters:
|
2018-07-13 05:26:32 +03:00
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
2019-02-26 04:20:37 +03:00
|
|
|
self.sensor_factories = {}
|
2018-04-04 00:01:10 +03:00
|
|
|
self.heaters = {}
|
2019-02-26 04:30:37 +03:00
|
|
|
self.gcode_id_to_sensor = {}
|
2019-12-12 15:33:23 +03:00
|
|
|
self.available_heaters = []
|
|
|
|
self.available_sensors = []
|
2019-02-26 05:26:27 +03:00
|
|
|
self.printer.register_event_handler("gcode:request_restart",
|
|
|
|
self.turn_off_all_heaters)
|
2018-09-18 23:00:13 +03:00
|
|
|
# Register TURN_OFF_HEATERS command
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
gcode.register_command("TURN_OFF_HEATERS", self.cmd_TURN_OFF_HEATERS,
|
|
|
|
desc=self.cmd_TURN_OFF_HEATERS_help)
|
2019-02-26 04:20:37 +03:00
|
|
|
def add_sensor_factory(self, sensor_type, sensor_factory):
|
|
|
|
self.sensor_factories[sensor_type] = sensor_factory
|
2019-02-26 04:30:37 +03:00
|
|
|
def setup_heater(self, config, gcode_id=None):
|
2019-02-14 04:15:11 +03:00
|
|
|
heater_name = config.get_name().split()[-1]
|
2018-04-04 00:01:10 +03:00
|
|
|
if heater_name in self.heaters:
|
|
|
|
raise config.error("Heater %s already registered" % (heater_name,))
|
2018-04-04 21:53:47 +03:00
|
|
|
# Setup sensor
|
2018-05-04 00:39:50 +03:00
|
|
|
sensor = self.setup_sensor(config)
|
2018-04-04 21:53:47 +03:00
|
|
|
# Create heater
|
2019-02-26 04:30:37 +03:00
|
|
|
self.heaters[heater_name] = heater = Heater(config, sensor)
|
2019-02-26 06:30:13 +03:00
|
|
|
self.register_sensor(config, heater, gcode_id)
|
2019-12-12 15:33:23 +03:00
|
|
|
self.available_heaters.append(config.get_name())
|
2018-04-04 00:01:10 +03:00
|
|
|
return heater
|
|
|
|
def lookup_heater(self, heater_name):
|
|
|
|
if heater_name not in self.heaters:
|
|
|
|
raise self.printer.config_error(
|
|
|
|
"Unknown heater '%s'" % (heater_name,))
|
|
|
|
return self.heaters[heater_name]
|
2018-05-04 00:39:50 +03:00
|
|
|
def setup_sensor(self, config):
|
|
|
|
self.printer.try_load_module(config, "thermistor")
|
|
|
|
self.printer.try_load_module(config, "adc_temperature")
|
2018-05-04 21:16:21 +03:00
|
|
|
self.printer.try_load_module(config, "spi_temperature")
|
2018-05-04 00:39:50 +03:00
|
|
|
sensor_type = config.get('sensor_type')
|
2019-02-26 04:20:37 +03:00
|
|
|
if sensor_type not in self.sensor_factories:
|
|
|
|
raise self.printer.config_error(
|
|
|
|
"Unknown temperature sensor '%s'" % (sensor_type,))
|
|
|
|
return self.sensor_factories[sensor_type](config)
|
2019-02-26 04:30:37 +03:00
|
|
|
def get_gcode_sensors(self):
|
|
|
|
return self.gcode_id_to_sensor.items()
|
2019-02-26 06:30:13 +03:00
|
|
|
def register_sensor(self, config, psensor, gcode_id=None):
|
|
|
|
if gcode_id is None:
|
|
|
|
gcode_id = config.get('gcode_id', None)
|
|
|
|
if gcode_id is None:
|
|
|
|
return
|
|
|
|
if gcode_id in self.gcode_id_to_sensor:
|
|
|
|
raise self.printer.config_error(
|
|
|
|
"G-Code sensor id %s already registered" % (gcode_id,))
|
|
|
|
self.gcode_id_to_sensor[gcode_id] = psensor
|
2019-12-12 15:33:23 +03:00
|
|
|
self.available_sensors.append(config.get_name())
|
2019-02-26 05:26:27 +03:00
|
|
|
def turn_off_all_heaters(self, print_time):
|
|
|
|
for heater in self.heaters.values():
|
|
|
|
heater.set_temp(print_time, 0.)
|
2018-09-18 23:00:13 +03:00
|
|
|
cmd_TURN_OFF_HEATERS_help = "Turn off all heaters"
|
|
|
|
def cmd_TURN_OFF_HEATERS(self, params):
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
2019-02-26 05:26:27 +03:00
|
|
|
self.turn_off_all_heaters(print_time)
|
2019-12-12 15:33:23 +03:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
return {'available_heaters': self.available_heaters,
|
|
|
|
'available_sensors': self.available_sensors}
|
2018-04-04 00:01:10 +03:00
|
|
|
|
2018-07-13 05:26:32 +03:00
|
|
|
def add_printer_objects(config):
|
|
|
|
config.get_printer().add_object('heater', PrinterHeaters(config))
|