fan: Separate out the part cooling fan code from the generic fan code

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-07-03 23:23:24 -04:00
parent 4bdc11a8b3
commit 3a57608642
4 changed files with 46 additions and 38 deletions

View File

@ -15,10 +15,9 @@ class ControllerFan:
self.stepper_enable = self.printer.load_object(config, 'stepper_enable') self.stepper_enable = self.printer.load_object(config, 'stepper_enable')
self.printer.load_object(config, 'heaters') self.printer.load_object(config, 'heaters')
self.heaters = [] self.heaters = []
self.fan = fan.PrinterFan(config) self.fan = fan.Fan(config)
self.mcu = self.fan.mcu_fan.get_mcu() self.fan_speed = config.getfloat('fan_speed', default=1.,
self.fan_speed = config.getfloat( minval=0., maxval=1.)
'fan_speed', default=1., minval=0., maxval=1.)
self.idle_speed = config.getfloat( self.idle_speed = config.getfloat(
'idle_speed', default=self.fan_speed, minval=0., maxval=1.) 'idle_speed', default=self.fan_speed, minval=0., maxval=1.)
self.idle_timeout = config.getint("idle_timeout", default=30, minval=0) self.idle_timeout = config.getint("idle_timeout", default=30, minval=0)
@ -47,8 +46,8 @@ class ControllerFan:
elif self.last_on < self.idle_timeout: elif self.last_on < self.idle_timeout:
power = self.idle_speed power = self.idle_speed
self.last_on += 1 self.last_on += 1
print_time = self.mcu.estimated_print_time(eventtime) + PIN_MIN_TIME print_time = self.fan.get_mcu().estimated_print_time(eventtime)
self.fan.set_speed(print_time, power) self.fan.set_speed(print_time + PIN_MIN_TIME, power)
return eventtime + 1. return eventtime + 1.
def load_config_prefix(config): def load_config_prefix(config):

View File

@ -1,40 +1,38 @@
# Printer cooling fan # Printer cooling fan
# #
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
FAN_MIN_TIME = 0.100 FAN_MIN_TIME = 0.100
class PrinterFan: class Fan:
def __init__(self, config, default_shutdown_speed=0.): def __init__(self, config, default_shutdown_speed=0.):
self.printer = config.get_printer()
self.last_fan_value = 0. self.last_fan_value = 0.
self.last_fan_time = 0. self.last_fan_time = 0.
self.printer = config.get_printer() # Read config
self.printer.register_event_handler("gcode:request_restart",
self.handle_request_restart)
self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.) self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.)
self.kick_start_time = config.getfloat('kick_start_time', 0.1, self.kick_start_time = config.getfloat('kick_start_time', 0.1,
minval=0.) minval=0.)
self.off_below = config.getfloat( self.off_below = config.getfloat('off_below', default=0.,
'off_below', default=0., minval=0., maxval=1.) minval=0., maxval=1.)
cycle_time = config.getfloat('cycle_time', 0.010, above=0.)
hardware_pwm = config.getboolean('hardware_pwm', False)
shutdown_speed = config.getfloat(
'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.)
# Setup pwm object
ppins = self.printer.lookup_object('pins') ppins = self.printer.lookup_object('pins')
self.mcu_fan = ppins.setup_pin('pwm', config.get('pin')) self.mcu_fan = ppins.setup_pin('pwm', config.get('pin'))
self.mcu_fan.setup_max_duration(0.) self.mcu_fan.setup_max_duration(0.)
cycle_time = config.getfloat('cycle_time', 0.010, above=0.)
hardware_pwm = config.getboolean('hardware_pwm', False)
self.mcu_fan.setup_cycle_time(cycle_time, hardware_pwm) self.mcu_fan.setup_cycle_time(cycle_time, hardware_pwm)
shutdown_speed = config.getfloat( shutdown_power = max(0., min(self.max_power, shutdown_speed))
'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.) self.mcu_fan.setup_start_value(0., shutdown_power)
self.mcu_fan.setup_start_value( # Register callbacks
0., max(0., min(self.max_power, shutdown_speed))) self.printer.register_event_handler("gcode:request_restart",
# Register commands self._handle_request_restart)
if config.get_name() == 'fan': def get_mcu(self):
gcode = self.printer.lookup_object('gcode') return self.mcu_fan.get_mcu()
gcode.register_command("M106", self.cmd_M106)
gcode.register_command("M107", self.cmd_M107)
def handle_request_restart(self, print_time):
self.set_speed(print_time, 0.)
def set_speed(self, print_time, value): def set_speed(self, print_time, value):
if value < self.off_below: if value < self.off_below:
value = 0. value = 0.
@ -50,19 +48,31 @@ class PrinterFan:
self.mcu_fan.set_pwm(print_time, value) self.mcu_fan.set_pwm(print_time, value)
self.last_fan_time = print_time self.last_fan_time = print_time
self.last_fan_value = value self.last_fan_value = value
def get_status(self, eventtime): def set_speed_from_command(self, value):
return {'speed': self.last_fan_value}
def _delayed_set_speed(self, value):
toolhead = self.printer.lookup_object('toolhead') toolhead = self.printer.lookup_object('toolhead')
toolhead.register_lookahead_callback((lambda pt: toolhead.register_lookahead_callback((lambda pt:
self.set_speed(pt, value))) self.set_speed(pt, value)))
def _handle_request_restart(self, print_time):
self.set_speed(print_time, 0.)
def get_status(self, eventtime):
return {'speed': self.last_fan_value}
class PrinterFan:
def __init__(self, config):
self.fan = Fan(config)
# Register commands
gcode = config.get_printer().lookup_object('gcode')
gcode.register_command("M106", self.cmd_M106)
gcode.register_command("M107", self.cmd_M107)
def get_status(self, eventtime):
return self.fan.get_status(eventtime)
def cmd_M106(self, gcmd): def cmd_M106(self, gcmd):
# Set fan speed # Set fan speed
value = gcmd.get_float('S', 255., minval=0.) / 255. value = gcmd.get_float('S', 255., minval=0.) / 255.
self._delayed_set_speed(value) self.fan.set_speed_from_command(value)
def cmd_M107(self, gcmd): def cmd_M107(self, gcmd):
# Turn fan off # Turn fan off
self._delayed_set_speed(0.) self.fan.set_speed_from_command(0.)
def load_config(config): def load_config(config):
return PrinterFan(config) return PrinterFan(config)

View File

@ -1,6 +1,6 @@
# Support fans that are enabled when a heater is on # Support fans that are enabled when a heater is on
# #
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
from . import fan from . import fan
@ -15,8 +15,7 @@ class PrinterHeaterFan:
self.heater_name = config.get("heater", "extruder") self.heater_name = config.get("heater", "extruder")
self.heater_temp = config.getfloat("heater_temp", 50.0) self.heater_temp = config.getfloat("heater_temp", 50.0)
self.heaters = [] self.heaters = []
self.fan = fan.PrinterFan(config, default_shutdown_speed=1.) self.fan = fan.Fan(config, default_shutdown_speed=1.)
self.mcu = self.fan.mcu_fan.get_mcu()
self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.) self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.)
def handle_ready(self): def handle_ready(self):
pheaters = self.printer.lookup_object('heaters') pheaters = self.printer.lookup_object('heaters')
@ -32,8 +31,8 @@ class PrinterHeaterFan:
current_temp, target_temp = heater.get_temp(eventtime) current_temp, target_temp = heater.get_temp(eventtime)
if target_temp or current_temp > self.heater_temp: if target_temp or current_temp > self.heater_temp:
power = self.fan_speed power = self.fan_speed
print_time = self.mcu.estimated_print_time(eventtime) + PIN_MIN_TIME print_time = self.fan.get_mcu().estimated_print_time(eventtime)
self.fan.set_speed(print_time, power) self.fan.set_speed(print_time + PIN_MIN_TIME, power)
return eventtime + 1. return eventtime + 1.
def load_config_prefix(config): def load_config_prefix(config):

View File

@ -1,6 +1,6 @@
# Support fans that are enabled when temperature exceeds a set threshold # Support fans that are enabled when temperature exceeds a set threshold
# #
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
from . import fan from . import fan
@ -14,7 +14,7 @@ class TemperatureFan:
def __init__(self, config): def __init__(self, config):
self.name = config.get_name().split()[1] self.name = config.get_name().split()[1]
self.printer = config.get_printer() self.printer = config.get_printer()
self.fan = fan.PrinterFan(config, default_shutdown_speed=1.) self.fan = fan.Fan(config, default_shutdown_speed=1.)
self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELSIUS) self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELSIUS)
self.max_temp = config.getfloat('max_temp', above=self.min_temp) self.max_temp = config.getfloat('max_temp', above=self.min_temp)
pheaters = self.printer.load_object(config, 'heaters') pheaters = self.printer.load_object(config, 'heaters')