gcode: Use an event to handle restart request actions
Instead of directly turning off motors, heaters, and fans from gcode.py, raise a new event and allow the heater, fan, and toolhead to handle the event as needed. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
ff9605c082
commit
adf6040e9e
|
@ -10,9 +10,12 @@ class PrinterFan:
|
||||||
def __init__(self, config, default_shutdown_speed=0.):
|
def __init__(self, config, default_shutdown_speed=0.):
|
||||||
self.last_fan_value = 0.
|
self.last_fan_value = 0.
|
||||||
self.last_fan_time = 0.
|
self.last_fan_time = 0.
|
||||||
|
printer = config.get_printer()
|
||||||
|
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, minval=0.)
|
self.kick_start_time = config.getfloat('kick_start_time', 0.1, minval=0.)
|
||||||
ppins = config.get_printer().lookup_object('pins')
|
ppins = 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.)
|
cycle_time = config.getfloat('cycle_time', 0.010, above=0.)
|
||||||
|
@ -22,6 +25,8 @@ class PrinterFan:
|
||||||
'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.)
|
'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.)
|
||||||
self.mcu_fan.setup_start_value(
|
self.mcu_fan.setup_start_value(
|
||||||
0., max(0., min(self.max_power, shutdown_speed)))
|
0., max(0., min(self.max_power, shutdown_speed)))
|
||||||
|
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):
|
||||||
value = max(0., min(self.max_power, value * self.max_power))
|
value = max(0., min(self.max_power, value * self.max_power))
|
||||||
if value == self.last_fan_value:
|
if value == self.last_fan_value:
|
||||||
|
|
|
@ -673,13 +673,8 @@ class GCodeParser:
|
||||||
gcode_pos, base_pos, homing_pos))
|
gcode_pos, base_pos, homing_pos))
|
||||||
def request_restart(self, result):
|
def request_restart(self, result):
|
||||||
if self.is_printer_ready:
|
if self.is_printer_ready:
|
||||||
self.toolhead.motor_off()
|
|
||||||
print_time = self.toolhead.get_last_move_time()
|
print_time = self.toolhead.get_last_move_time()
|
||||||
if self.heaters is not None:
|
self.printer.send_event("gcode:request_restart", print_time)
|
||||||
for heater in self.heaters.get_all_heaters():
|
|
||||||
heater.set_temp(print_time, 0.)
|
|
||||||
if self.fan is not None:
|
|
||||||
self.fan.set_speed(print_time, 0.)
|
|
||||||
self.toolhead.dwell(0.500)
|
self.toolhead.dwell(0.500)
|
||||||
self.toolhead.wait_moves()
|
self.toolhead.wait_moves()
|
||||||
self.printer.request_exit(result)
|
self.printer.request_exit(result)
|
||||||
|
|
|
@ -230,6 +230,8 @@ class PrinterHeaters:
|
||||||
self.sensor_factories = {}
|
self.sensor_factories = {}
|
||||||
self.heaters = {}
|
self.heaters = {}
|
||||||
self.gcode_id_to_sensor = {}
|
self.gcode_id_to_sensor = {}
|
||||||
|
self.printer.register_event_handler("gcode:request_restart",
|
||||||
|
self.turn_off_all_heaters)
|
||||||
# Register TURN_OFF_HEATERS command
|
# Register TURN_OFF_HEATERS command
|
||||||
gcode = self.printer.lookup_object('gcode')
|
gcode = self.printer.lookup_object('gcode')
|
||||||
gcode.register_command("TURN_OFF_HEATERS", self.cmd_TURN_OFF_HEATERS,
|
gcode.register_command("TURN_OFF_HEATERS", self.cmd_TURN_OFF_HEATERS,
|
||||||
|
@ -265,15 +267,15 @@ class PrinterHeaters:
|
||||||
raise self.printer.config_error(
|
raise self.printer.config_error(
|
||||||
"Unknown temperature sensor '%s'" % (sensor_type,))
|
"Unknown temperature sensor '%s'" % (sensor_type,))
|
||||||
return self.sensor_factories[sensor_type](config)
|
return self.sensor_factories[sensor_type](config)
|
||||||
def get_all_heaters(self):
|
|
||||||
return self.heaters.values()
|
|
||||||
def get_gcode_sensors(self):
|
def get_gcode_sensors(self):
|
||||||
return self.gcode_id_to_sensor.items()
|
return self.gcode_id_to_sensor.items()
|
||||||
|
def turn_off_all_heaters(self, print_time):
|
||||||
|
for heater in self.heaters.values():
|
||||||
|
heater.set_temp(print_time, 0.)
|
||||||
cmd_TURN_OFF_HEATERS_help = "Turn off all heaters"
|
cmd_TURN_OFF_HEATERS_help = "Turn off all heaters"
|
||||||
def cmd_TURN_OFF_HEATERS(self, params):
|
def cmd_TURN_OFF_HEATERS(self, params):
|
||||||
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
||||||
for heater in self.heaters.values():
|
self.turn_off_all_heaters(print_time)
|
||||||
heater.set_temp(print_time, 0.)
|
|
||||||
|
|
||||||
def add_printer_objects(config):
|
def add_printer_objects(config):
|
||||||
config.get_printer().add_object('heater', PrinterHeaters(config))
|
config.get_printer().add_object('heater', PrinterHeaters(config))
|
||||||
|
|
|
@ -205,6 +205,8 @@ class ToolHead:
|
||||||
self.mcu = self.all_mcus[0]
|
self.mcu = self.all_mcus[0]
|
||||||
self.move_queue = MoveQueue()
|
self.move_queue = MoveQueue()
|
||||||
self.commanded_pos = [0., 0., 0., 0.]
|
self.commanded_pos = [0., 0., 0., 0.]
|
||||||
|
self.printer.register_event_handler("gcode:request_restart",
|
||||||
|
self._handle_request_restart)
|
||||||
self.printer.register_event_handler("klippy:shutdown",
|
self.printer.register_event_handler("klippy:shutdown",
|
||||||
self._handle_shutdown)
|
self._handle_shutdown)
|
||||||
# Velocity and acceleration control
|
# Velocity and acceleration control
|
||||||
|
@ -416,6 +418,8 @@ class ToolHead:
|
||||||
return { 'status': status, 'print_time': print_time,
|
return { 'status': status, 'print_time': print_time,
|
||||||
'estimated_print_time': estimated_print_time,
|
'estimated_print_time': estimated_print_time,
|
||||||
'printing_time': print_time - last_print_start_time }
|
'printing_time': print_time - last_print_start_time }
|
||||||
|
def _handle_request_restart(self, print_time):
|
||||||
|
self.motor_off()
|
||||||
def _handle_shutdown(self):
|
def _handle_shutdown(self):
|
||||||
self.move_queue.reset()
|
self.move_queue.reset()
|
||||||
self.reset_print_time()
|
self.reset_print_time()
|
||||||
|
|
Loading…
Reference in New Issue