2019-01-10 20:12:15 +03:00
|
|
|
# Support a fan for cooling the MCU whenever a stepper or heater is on
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Nils Friedchen <nils.friedchen@googlemail.com>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2020-06-12 16:55:57 +03:00
|
|
|
from . import fan
|
2019-01-10 20:12:15 +03:00
|
|
|
|
|
|
|
PIN_MIN_TIME = 0.100
|
|
|
|
|
|
|
|
class ControllerFan:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
2019-11-12 20:49:21 +03:00
|
|
|
self.stepper_names = []
|
2020-05-05 21:10:30 +03:00
|
|
|
self.stepper_enable = self.printer.load_object(config, 'stepper_enable')
|
|
|
|
self.printer.load_object(config, 'heaters')
|
2019-09-16 14:13:45 +03:00
|
|
|
self.heaters = []
|
2020-07-04 06:23:24 +03:00
|
|
|
self.fan = fan.Fan(config)
|
|
|
|
self.fan_speed = config.getfloat('fan_speed', default=1.,
|
|
|
|
minval=0., maxval=1.)
|
2019-01-10 20:12:15 +03:00
|
|
|
self.idle_speed = config.getfloat(
|
2020-05-17 23:26:51 +03:00
|
|
|
'idle_speed', default=self.fan_speed, minval=0., maxval=1.)
|
2019-09-16 14:16:17 +03:00
|
|
|
self.idle_timeout = config.getint("idle_timeout", default=30, minval=0)
|
2019-01-10 20:12:15 +03:00
|
|
|
self.heater_name = config.get("heater", "extruder")
|
|
|
|
self.last_on = self.idle_timeout
|
2020-12-01 19:51:29 +03:00
|
|
|
self.last_speed = 0.
|
2019-01-10 20:12:15 +03:00
|
|
|
def handle_ready(self):
|
2020-04-25 20:27:41 +03:00
|
|
|
pheaters = self.printer.lookup_object('heaters')
|
|
|
|
self.heaters = [pheaters.lookup_heater(n.strip())
|
2019-01-10 20:12:15 +03:00
|
|
|
for n in self.heater_name.split(',')]
|
|
|
|
kin = self.printer.lookup_object('toolhead').get_kinematics()
|
2019-11-12 20:49:21 +03:00
|
|
|
self.stepper_names = [s.get_name() for s in kin.get_steppers()]
|
2019-01-10 20:12:15 +03:00
|
|
|
reactor = self.printer.get_reactor()
|
2020-12-01 19:51:29 +03:00
|
|
|
reactor.register_timer(self.callback, reactor.monotonic()+PIN_MIN_TIME)
|
2020-11-23 19:55:14 +03:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
return self.fan.get_status(eventtime)
|
2019-01-10 20:12:15 +03:00
|
|
|
def callback(self, eventtime):
|
2020-12-01 19:51:29 +03:00
|
|
|
speed = 0.
|
2019-01-10 20:12:15 +03:00
|
|
|
active = False
|
2019-11-12 20:49:21 +03:00
|
|
|
for name in self.stepper_names:
|
2019-11-12 21:55:50 +03:00
|
|
|
active |= self.stepper_enable.lookup_enable(name).is_motor_enabled()
|
2019-01-10 20:12:15 +03:00
|
|
|
for heater in self.heaters:
|
2019-09-16 14:15:23 +03:00
|
|
|
_, target_temp = heater.get_temp(eventtime)
|
|
|
|
if target_temp:
|
2019-01-10 20:12:15 +03:00
|
|
|
active = True
|
|
|
|
if active:
|
|
|
|
self.last_on = 0
|
2020-12-01 19:51:29 +03:00
|
|
|
speed = self.fan_speed
|
2019-09-16 14:20:49 +03:00
|
|
|
elif self.last_on < self.idle_timeout:
|
2020-12-01 19:51:29 +03:00
|
|
|
speed = self.idle_speed
|
2019-09-16 14:20:49 +03:00
|
|
|
self.last_on += 1
|
2020-12-01 20:31:22 +03:00
|
|
|
if speed != self.last_speed:
|
|
|
|
self.last_speed = speed
|
|
|
|
curtime = self.printer.get_reactor().monotonic()
|
|
|
|
print_time = self.fan.get_mcu().estimated_print_time(curtime)
|
|
|
|
self.fan.set_speed(print_time + PIN_MIN_TIME, speed)
|
2019-01-10 20:12:15 +03:00
|
|
|
return eventtime + 1.
|
|
|
|
|
2019-03-10 20:57:01 +03:00
|
|
|
def load_config_prefix(config):
|
2019-01-10 20:12:15 +03:00
|
|
|
return ControllerFan(config)
|