2018-01-20 05:46:01 +01:00
|
|
|
# Printer cooling fan
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2018-01-29 18:54:06 +01:00
|
|
|
FAN_MIN_TIME = 0.100
|
2018-01-20 05:46:01 +01:00
|
|
|
|
|
|
|
class PrinterFan:
|
2018-05-19 01:12:03 +02:00
|
|
|
def __init__(self, config, default_shutdown_speed=0.):
|
2018-01-20 05:46:01 +01:00
|
|
|
self.last_fan_value = 0.
|
|
|
|
self.last_fan_time = 0.
|
2019-02-26 03:26:27 +01:00
|
|
|
printer = config.get_printer()
|
|
|
|
printer.register_event_handler("gcode:request_restart",
|
|
|
|
self.handle_request_restart)
|
2018-01-20 05:46:01 +01:00
|
|
|
self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.)
|
2019-02-27 19:27:52 +01:00
|
|
|
self.kick_start_time = config.getfloat('kick_start_time', 0.1,
|
|
|
|
minval=0.)
|
2019-02-26 03:26:27 +01:00
|
|
|
ppins = printer.lookup_object('pins')
|
2018-04-04 18:07:41 +02:00
|
|
|
self.mcu_fan = ppins.setup_pin('pwm', config.get('pin'))
|
2018-01-20 05:46:01 +01:00
|
|
|
self.mcu_fan.setup_max_duration(0.)
|
2018-01-29 18:54:06 +01:00
|
|
|
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)
|
2018-05-19 01:12:03 +02:00
|
|
|
shutdown_speed = config.getfloat(
|
|
|
|
'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.)
|
|
|
|
self.mcu_fan.setup_start_value(
|
|
|
|
0., max(0., min(self.max_power, shutdown_speed)))
|
2019-02-26 03:26:27 +01:00
|
|
|
def handle_request_restart(self, print_time):
|
|
|
|
self.set_speed(print_time, 0.)
|
2018-01-20 05:46:01 +01:00
|
|
|
def set_speed(self, print_time, value):
|
2018-04-20 19:15:03 +02:00
|
|
|
value = max(0., min(self.max_power, value * self.max_power))
|
2018-01-20 05:46:01 +01:00
|
|
|
if value == self.last_fan_value:
|
|
|
|
return
|
|
|
|
print_time = max(self.last_fan_time + FAN_MIN_TIME, print_time)
|
|
|
|
if (value and value < self.max_power
|
|
|
|
and not self.last_fan_value and self.kick_start_time):
|
|
|
|
# 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
|
2018-02-21 02:50:06 +01:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
return {'speed': self.last_fan_value}
|
2018-01-20 05:46:01 +01:00
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return PrinterFan(config)
|