2018-01-20 07:46:01 +03:00
|
|
|
# Printer cooling fan
|
|
|
|
#
|
2020-07-04 06:23:24 +03:00
|
|
|
# Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
|
2018-01-20 07:46:01 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2021-09-30 20:06:17 +03:00
|
|
|
from . import pulse_counter
|
2018-01-20 07:46:01 +03:00
|
|
|
|
2018-01-29 20:54:06 +03:00
|
|
|
FAN_MIN_TIME = 0.100
|
2018-01-20 07:46:01 +03:00
|
|
|
|
2020-07-04 06:23:24 +03:00
|
|
|
class Fan:
|
2018-05-19 02:12:03 +03:00
|
|
|
def __init__(self, config, default_shutdown_speed=0.):
|
2020-07-04 06:23:24 +03:00
|
|
|
self.printer = config.get_printer()
|
2018-01-20 07:46:01 +03:00
|
|
|
self.last_fan_value = 0.
|
|
|
|
self.last_fan_time = 0.
|
2020-07-04 06:23:24 +03:00
|
|
|
# Read config
|
2018-01-20 07:46:01 +03:00
|
|
|
self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.)
|
2019-02-27 21:27:52 +03:00
|
|
|
self.kick_start_time = config.getfloat('kick_start_time', 0.1,
|
|
|
|
minval=0.)
|
2020-07-04 06:23:24 +03:00
|
|
|
self.off_below = config.getfloat('off_below', default=0.,
|
|
|
|
minval=0., maxval=1.)
|
2018-01-29 20:54:06 +03:00
|
|
|
cycle_time = config.getfloat('cycle_time', 0.010, above=0.)
|
|
|
|
hardware_pwm = config.getboolean('hardware_pwm', False)
|
2018-05-19 02:12:03 +03:00
|
|
|
shutdown_speed = config.getfloat(
|
|
|
|
'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.)
|
2020-07-04 06:23:24 +03:00
|
|
|
# Setup pwm object
|
|
|
|
ppins = self.printer.lookup_object('pins')
|
|
|
|
self.mcu_fan = ppins.setup_pin('pwm', config.get('pin'))
|
|
|
|
self.mcu_fan.setup_max_duration(0.)
|
|
|
|
self.mcu_fan.setup_cycle_time(cycle_time, hardware_pwm)
|
|
|
|
shutdown_power = max(0., min(self.max_power, shutdown_speed))
|
|
|
|
self.mcu_fan.setup_start_value(0., shutdown_power)
|
2021-02-07 02:11:29 +03:00
|
|
|
|
|
|
|
# Setup tachometer
|
|
|
|
self.tachometer = FanTachometer(config)
|
|
|
|
|
2020-07-04 06:23:24 +03:00
|
|
|
# Register callbacks
|
|
|
|
self.printer.register_event_handler("gcode:request_restart",
|
|
|
|
self._handle_request_restart)
|
2021-02-07 02:11:29 +03:00
|
|
|
|
2020-07-04 06:23:24 +03:00
|
|
|
def get_mcu(self):
|
|
|
|
return self.mcu_fan.get_mcu()
|
2018-01-20 07:46:01 +03:00
|
|
|
def set_speed(self, print_time, value):
|
2019-10-04 00:59:03 +03:00
|
|
|
if value < self.off_below:
|
|
|
|
value = 0.
|
2018-04-20 20:15:03 +03:00
|
|
|
value = max(0., min(self.max_power, value * self.max_power))
|
2018-01-20 07:46:01 +03:00
|
|
|
if value == self.last_fan_value:
|
|
|
|
return
|
|
|
|
print_time = max(self.last_fan_time + FAN_MIN_TIME, print_time)
|
2019-11-25 02:37:30 +03:00
|
|
|
if (value and value < self.max_power and self.kick_start_time
|
|
|
|
and (not self.last_fan_value or value - self.last_fan_value > .5)):
|
2018-01-20 07:46:01 +03:00
|
|
|
# Run fan at full speed for specified kick_start_time
|
|
|
|
self.mcu_fan.set_pwm(print_time, self.max_power)
|
|
|
|
print_time += self.kick_start_time
|
|
|
|
self.mcu_fan.set_pwm(print_time, value)
|
|
|
|
self.last_fan_time = print_time
|
|
|
|
self.last_fan_value = value
|
2020-07-04 06:23:24 +03:00
|
|
|
def set_speed_from_command(self, value):
|
2020-01-03 03:46:06 +03:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
toolhead.register_lookahead_callback((lambda pt:
|
|
|
|
self.set_speed(pt, value)))
|
2020-07-04 06:23:24 +03:00
|
|
|
def _handle_request_restart(self, print_time):
|
|
|
|
self.set_speed(print_time, 0.)
|
2021-02-07 02:11:29 +03:00
|
|
|
|
|
|
|
def get_status(self, eventtime):
|
|
|
|
tachometer_status = self.tachometer.get_status(eventtime)
|
|
|
|
return {
|
|
|
|
'speed': self.last_fan_value,
|
|
|
|
'rpm': tachometer_status['rpm'],
|
|
|
|
}
|
|
|
|
|
|
|
|
class FanTachometer:
|
|
|
|
def __init__(self, config):
|
|
|
|
printer = config.get_printer()
|
|
|
|
self._freq_counter = None
|
|
|
|
|
|
|
|
pin = config.get('tachometer_pin', None)
|
|
|
|
if pin is not None:
|
|
|
|
self.ppr = config.getint('tachometer_ppr', 2, minval=1)
|
|
|
|
poll_time = config.getfloat('tachometer_poll_interval',
|
|
|
|
0.0015, above=0.)
|
|
|
|
sample_time = 1.
|
|
|
|
self._freq_counter = pulse_counter.FrequencyCounter(
|
|
|
|
printer, pin, sample_time, poll_time)
|
|
|
|
|
2020-07-04 06:23:24 +03:00
|
|
|
def get_status(self, eventtime):
|
2021-03-01 22:57:40 +03:00
|
|
|
if self._freq_counter is not None:
|
|
|
|
rpm = self._freq_counter.get_frequency() * 30. / self.ppr
|
2021-02-07 02:11:29 +03:00
|
|
|
else:
|
|
|
|
rpm = None
|
|
|
|
return {'rpm': rpm}
|
2020-07-04 06:23:24 +03:00
|
|
|
|
|
|
|
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)
|
2020-04-25 05:59:38 +03:00
|
|
|
def cmd_M106(self, gcmd):
|
2019-11-13 19:29:27 +03:00
|
|
|
# Set fan speed
|
2020-04-25 05:59:38 +03:00
|
|
|
value = gcmd.get_float('S', 255., minval=0.) / 255.
|
2020-07-04 06:23:24 +03:00
|
|
|
self.fan.set_speed_from_command(value)
|
2020-04-25 05:59:38 +03:00
|
|
|
def cmd_M107(self, gcmd):
|
2019-11-13 19:29:27 +03:00
|
|
|
# Turn fan off
|
2020-07-04 06:23:24 +03:00
|
|
|
self.fan.set_speed_from_command(0.)
|
2018-01-20 07:46:01 +03:00
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return PrinterFan(config)
|