temperature_fan: Add support for temperature-triggered fan.

Signed-off-by: Michael Barbour <barbour.michael.0@gmail.com>
This commit is contained in:
Michael Barbour 2018-05-03 17:39:50 -04:00 committed by KevinOConnor
parent 6ad344c8a6
commit bf3c41cd06
3 changed files with 84 additions and 7 deletions

View File

@ -211,6 +211,37 @@
# is 1.0 # is 1.0
# Temperature-triggered cooling fans (one may define any number of
# sections with a "temperature_fan" prefix). A "temperature fan" is
# a fan that will be enabled whenever its associated sensor is above
# a set temperature. In the event of an MCU software error the
# temperature_fan will be set to its max_power.
#[temperature_fan my_temp_fan]
# See the "fan" section for fan configuration parameters.
#pin: ar4
# See the "heater" section for details about the sensor_type and
# sensor_pin parameters.
#sensor_type: EPCOS 100K B57560G104F
#sensor_pin: analog13
# The remaining variables are specific to temperature_fan.
#min_temp: 0
#max_temp: 100
# The maximum range of valid temperatures (in Celsius) that the
# sensor must remain within. This controls a safety feature
# implemented in the micro-controller code - should the measured
# temperature ever fall outside this range then the micro-controller
# will go into a shutdown state. Set this range just wide enough so
# that reasonable temperatures do not result in an error. These
# parameters must be provided.
#temp: 40.0
# A temperature (in Celsius) that the sensor must exceed before the
# fan is enabled. The default is 40 Celsius.
#fan_speed: 1.0
# The fan speed (expressed as a value from 0.0 to 1.0) that the fan
# will be set to when the sensor temperature exceeds the set value.
# The default is 1.0.
# Additional micro-controllers (one may define any number of sections # Additional micro-controllers (one may define any number of sections
# with an "mcu" prefix). Additional micro-controllers introduce # with an "mcu" prefix). Additional micro-controllers introduce
# additional pins that may be configured as heaters, steppers, fans, # additional pins that may be configured as heaters, steppers, fans,

View File

@ -0,0 +1,44 @@
# Support fans that are enabled when temperature exceeds a set threshold
#
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import fan
PIN_MIN_TIME = 0.100
KELVIN_TO_CELCIUS = -273.15
class TemperatureFan:
def __init__(self, config):
self.name = config.get_name()
self.printer = config.get_printer()
self.fan = fan.PrinterFan(config)
self.mcu = self.fan.mcu_fan.get_mcu()
self.fan_speed = config.getfloat('fan_speed', 1., minval=0., maxval=1.)
min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELCIUS)
max_temp = config.getfloat('max_temp', above=min_temp)
self.temp = config.getfloat('temp', 40. if max_temp > 40. else max_temp,
minval=min_temp, maxval=max_temp)
self.fan.set_shutdown_speed(1.)
self.last_temp = KELVIN_TO_CELCIUS
self.sensor = self.printer.lookup_object('heater').setup_sensor(config)
self.sensor.setup_minmax(min_temp, max_temp)
self.sensor.setup_callback(self.temperature_callback)
def printer_state(self, state):
if state == 'ready':
reactor = self.printer.get_reactor()
reactor.register_timer(self.callback, reactor.NOW)
def callback(self, eventtime):
power = 0.
if self.last_temp > self.temp:
power = self.fan_speed
print_time = self.mcu.estimated_print_time(eventtime) + PIN_MIN_TIME
self.fan.set_speed(print_time, power)
return eventtime + 1.
def temperature_callback(self, read_time, temp):
self.last_temp = temp
def stats(self, eventtime):
return True, '%s: temp=%.1f' % (self.name, self.last_temp)
def load_config_prefix(config):
return TemperatureFan(config)

View File

@ -206,13 +206,7 @@ class PrinterHeaters:
if heater_name in self.heaters: if heater_name in self.heaters:
raise config.error("Heater %s already registered" % (heater_name,)) raise config.error("Heater %s already registered" % (heater_name,))
# Setup sensor # Setup sensor
self.printer.try_load_module(config, "thermistor") sensor = self.setup_sensor(config)
self.printer.try_load_module(config, "adc_temperature")
sensor_type = config.get('sensor_type')
if sensor_type not in self.sensors:
raise self.printer.config_error("Unknown temperature sensor '%s'" % (
sensor_type,))
sensor = self.sensors[sensor_type](config)
# Create heater # Create heater
self.heaters[heater_name] = heater = Heater(config, sensor) self.heaters[heater_name] = heater = Heater(config, sensor)
return heater return heater
@ -223,6 +217,14 @@ class PrinterHeaters:
raise self.printer.config_error( raise self.printer.config_error(
"Unknown heater '%s'" % (heater_name,)) "Unknown heater '%s'" % (heater_name,))
return self.heaters[heater_name] return self.heaters[heater_name]
def setup_sensor(self, config):
self.printer.try_load_module(config, "thermistor")
self.printer.try_load_module(config, "adc_temperature")
sensor_type = config.get('sensor_type')
if sensor_type not in self.sensors:
raise self.printer.config_error("Unknown temperature sensor '%s'" % (
sensor_type,))
return self.sensors[sensor_type](config)
def add_printer_objects(printer, config): def add_printer_objects(printer, config):
printer.add_object('heater', PrinterHeaters(printer, config)) printer.add_object('heater', PrinterHeaters(printer, config))