2016-05-25 17:37:40 +02:00
|
|
|
# Printer fan support
|
|
|
|
#
|
2017-07-04 18:24:11 +02:00
|
|
|
# Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2017-08-21 17:25:26 +02:00
|
|
|
import extruder, pins
|
2017-07-04 18:24:11 +02:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
FAN_MIN_TIME = 0.1
|
2017-03-09 02:00:27 +01:00
|
|
|
PWM_CYCLE_TIME = 0.010
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
class PrinterFan:
|
|
|
|
def __init__(self, printer, config):
|
2016-08-25 17:24:18 +02:00
|
|
|
self.last_fan_value = 0.
|
2016-08-24 21:16:02 +02:00
|
|
|
self.last_fan_time = 0.
|
2017-07-04 17:38:44 +02:00
|
|
|
self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.)
|
2017-04-11 17:37:09 +02:00
|
|
|
self.kick_start_time = config.getfloat('kick_start_time', 0.1, minval=0.)
|
2017-08-21 17:25:26 +02:00
|
|
|
self.mcu_fan = pins.setup_pin(printer, 'pwm', config.get('pin'))
|
|
|
|
self.mcu_fan.setup_max_duration(0.)
|
|
|
|
self.mcu_fan.setup_cycle_time(PWM_CYCLE_TIME)
|
|
|
|
self.mcu_fan.setup_hard_pwm(config.getint('hard_pwm', 0))
|
2017-09-12 18:47:40 +02:00
|
|
|
def set_speed(self, print_time, value):
|
2017-07-04 17:38:44 +02:00
|
|
|
value = max(0., min(self.max_power, value))
|
2016-05-25 17:37:40 +02:00
|
|
|
if value == self.last_fan_value:
|
|
|
|
return
|
2017-09-12 18:47:40 +02:00
|
|
|
print_time = max(self.last_fan_time + FAN_MIN_TIME, print_time)
|
2017-07-04 17:38:44 +02:00
|
|
|
if (value and value < self.max_power
|
2016-08-24 21:16:02 +02:00
|
|
|
and not self.last_fan_value and self.kick_start_time):
|
2016-05-25 17:37:40 +02:00
|
|
|
# Run fan at full speed for specified kick_start_time
|
2017-09-12 18:47:40 +02:00
|
|
|
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
|
2016-05-25 17:37:40 +02:00
|
|
|
self.last_fan_value = value
|
2017-07-04 18:24:11 +02:00
|
|
|
|
|
|
|
class PrinterHeaterFan:
|
|
|
|
def __init__(self, printer, config):
|
|
|
|
self.fan = PrinterFan(printer, config)
|
2017-09-12 18:24:44 +02:00
|
|
|
self.mcu = printer.objects['mcu']
|
2017-07-04 18:24:11 +02:00
|
|
|
heater = config.get("heater", "extruder0")
|
|
|
|
self.heater = extruder.get_printer_heater(printer, heater)
|
|
|
|
self.heater_temp = config.getfloat("heater_temp", 50.0)
|
2017-10-12 17:31:12 +02:00
|
|
|
max_power = self.fan.max_power
|
|
|
|
self.fan_speed = config.getfloat(
|
|
|
|
"fan_speed", max_power, minval=0., maxval=max_power)
|
|
|
|
self.fan.mcu_fan.setup_shutdown_value(max_power)
|
2017-07-04 18:24:11 +02:00
|
|
|
printer.reactor.register_timer(self.callback, printer.reactor.NOW)
|
|
|
|
def callback(self, eventtime):
|
2017-10-12 04:16:02 +02:00
|
|
|
current_temp, target_temp = self.heater.get_temp(eventtime)
|
2017-07-04 18:24:11 +02:00
|
|
|
if not current_temp and not target_temp and not self.fan.last_fan_time:
|
|
|
|
# Printer still starting
|
|
|
|
return eventtime + 1.
|
|
|
|
power = 0.
|
|
|
|
if target_temp or current_temp > self.heater_temp:
|
2017-10-12 17:31:12 +02:00
|
|
|
power = self.fan_speed
|
2017-09-12 18:24:44 +02:00
|
|
|
print_time = self.mcu.estimated_print_time(eventtime) + FAN_MIN_TIME
|
|
|
|
self.fan.set_speed(print_time, power)
|
2017-07-04 18:24:11 +02:00
|
|
|
return eventtime + 1.
|
2017-04-29 19:57:02 +02:00
|
|
|
|
|
|
|
def add_printer_objects(printer, config):
|
|
|
|
if config.has_section('fan'):
|
|
|
|
printer.add_object('fan', PrinterFan(printer, config.getsection('fan')))
|
2017-07-04 18:24:11 +02:00
|
|
|
for s in config.get_prefix_sections('heater_fan '):
|
|
|
|
printer.add_object(s.section, PrinterHeaterFan(printer, s))
|