heater: Move M105 command handling from gcode.py to heater.py

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-04-25 13:06:51 -04:00
parent 44f868a802
commit d858498a53
5 changed files with 59 additions and 49 deletions

View File

@ -19,10 +19,10 @@ class PrinterHeaterBed:
# Set Bed Temperature # Set Bed Temperature
gcode = self.printer.lookup_object('gcode') gcode = self.printer.lookup_object('gcode')
temp = gcode.get_float('S', params, 0.) temp = gcode.get_float('S', params, 0.)
toolhead = self.printer.lookup_object('toolhead')
self.heater.set_temp(temp) self.heater.set_temp(temp)
if wait and temp: if wait and temp:
gcode.wait_for_temperature(self.heater) pheater = self.printer.lookup_object('heater')
pheater.wait_for_temperature(self.heater)
def cmd_M190(self, params): def cmd_M190(self, params):
# Set Bed Temperature and Wait # Set Bed Temperature and Wait
self.cmd_M140(params, wait=True) self.cmd_M140(params, wait=True)

View File

@ -31,7 +31,7 @@ class PIDCalibrate:
except self.printer.command_error as e: except self.printer.command_error as e:
heater.set_control(old_control) heater.set_control(old_control)
raise raise
self.gcode.wait_for_temperature(heater) pheater.wait_for_temperature(heater)
heater.set_control(old_control) heater.set_control(old_control)
if write_file: if write_file:
calibrate.write_file('/tmp/heattest.txt') calibrate.write_file('/tmp/heattest.txt')

View File

@ -59,7 +59,6 @@ class GCodeParser:
self.position_with_transform = (lambda: [0., 0., 0., 0.]) self.position_with_transform = (lambda: [0., 0., 0., 0.])
self.need_ack = False self.need_ack = False
self.toolhead = None self.toolhead = None
self.heaters = None
self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3} self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3}
def is_traditional_gcode(self, cmd): def is_traditional_gcode(self, cmd):
# A "traditional" g-code command is a letter and followed by a number # A "traditional" g-code command is a letter and followed by a number
@ -177,8 +176,6 @@ class GCodeParser:
def _handle_ready(self): def _handle_ready(self):
self.is_printer_ready = True self.is_printer_ready = True
self.gcode_handlers = self.ready_gcode_handlers self.gcode_handlers = self.ready_gcode_handlers
# Lookup printer components
self.heaters = self.printer.lookup_object('heater')
self.toolhead = self.printer.lookup_object('toolhead') self.toolhead = self.printer.lookup_object('toolhead')
if self.move_transform is None: if self.move_transform is None:
self.move_with_transform = self.toolhead.move self.move_with_transform = self.toolhead.move
@ -306,16 +303,19 @@ class GCodeParser:
return self.mutex return self.mutex
# Response handling # Response handling
def ack(self, msg=None): def ack(self, msg=None):
if not self.need_ack or self.is_fileinput: if not self.need_ack:
return return False
try: if self.is_fileinput:
return True
ok_msg = "ok\n"
if msg: if msg:
os.write(self.fd, "ok %s\n" % (msg,)) ok_msg = "ok %s\n" % (msg,)
else: try:
os.write(self.fd, "ok\n") os.write(self.fd, ok_msg)
except os.error: except os.error:
logging.exception("Write g-code ack") logging.exception("Write g-code ack")
self.need_ack = False self.need_ack = False
return True
def respond_raw(self, msg): def respond_raw(self, msg):
if self.is_fileinput: if self.is_fileinput:
return return
@ -389,32 +389,16 @@ class GCodeParser:
return eparams return eparams
except ValueError as e: except ValueError as e:
raise self.error("Malformed command '%s'" % (params['#original'],)) raise self.error("Malformed command '%s'" % (params['#original'],))
# Temperature wrappers
def _get_temp(self, eventtime):
# Tn:XXX /YYY B:XXX /YYY
out = []
if self.heaters is not None:
for gcode_id, sensor in sorted(self.heaters.get_gcode_sensors()):
cur, target = sensor.get_temp(eventtime)
out.append("%s:%.1f /%.1f" % (gcode_id, cur, target))
if not out:
return "T:0"
return " ".join(out)
def wait_for_temperature(self, heater):
# Helper to wait on heater.check_busy() and report M105 temperatures
if self.is_fileinput:
return
eventtime = self.reactor.monotonic()
while self.is_printer_ready and heater.check_busy(eventtime):
print_time = self.toolhead.get_last_move_time()
self.respond_raw(self._get_temp(eventtime))
eventtime = self.reactor.pause(eventtime + 1.)
# G-Code special command handlers # G-Code special command handlers
def cmd_default(self, params): def cmd_default(self, params):
cmd = params.get('#command')
if cmd == 'M105':
# Don't warn about temperature requests when not ready
self.ack("T:0")
return
if not self.is_printer_ready: if not self.is_printer_ready:
raise self.error(self.printer.get_state_message()) raise self.error(self.printer.get_state_message())
return return
cmd = params.get('#command')
if not cmd: if not cmd:
logging.debug(params['#original']) logging.debug(params['#original'])
return return
@ -446,7 +430,7 @@ class GCodeParser:
'G1', 'G4', 'G28', 'M400', 'G1', 'G4', 'G28', 'M400',
'G20', 'M82', 'M83', 'G90', 'G91', 'G92', 'M114', 'M220', 'M221', 'G20', 'M82', 'M83', 'G90', 'G91', 'G92', 'M114', 'M220', 'M221',
'SET_GCODE_OFFSET', 'SAVE_GCODE_STATE', 'RESTORE_GCODE_STATE', 'SET_GCODE_OFFSET', 'SAVE_GCODE_STATE', 'RESTORE_GCODE_STATE',
'M105', 'M112', 'M115', 'IGNORE', 'GET_POSITION', 'M112', 'M115', 'IGNORE', 'GET_POSITION',
'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP'] 'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP']
# G-Code movement commands # G-Code movement commands
cmd_G1_aliases = ['G0'] cmd_G1_aliases = ['G0']
@ -602,14 +586,6 @@ class GCodeParser:
self.last_position[:3] = state['last_position'][:3] self.last_position[:3] = state['last_position'][:3]
self.move_with_transform(self.last_position, speed) self.move_with_transform(self.last_position, speed)
# G-Code miscellaneous commands # G-Code miscellaneous commands
cmd_M105_when_not_ready = True
def cmd_M105(self, params):
# Get Extruder Temperature
msg = self._get_temp(self.reactor.monotonic())
if self.need_ack:
self.ack(msg)
else:
self.respond_raw(msg)
cmd_M112_when_not_ready = True cmd_M112_when_not_ready = True
def cmd_M112(self, params): def cmd_M112(self, params):
# Emergency Stop # Emergency Stop

View File

@ -231,12 +231,15 @@ class PrinterHeaters:
self.gcode_id_to_sensor = {} self.gcode_id_to_sensor = {}
self.available_heaters = [] self.available_heaters = []
self.available_sensors = [] self.available_sensors = []
self.has_started = False
self.printer.register_event_handler("klippy:ready", self._handle_ready)
self.printer.register_event_handler("gcode:request_restart", self.printer.register_event_handler("gcode:request_restart",
self.turn_off_all_heaters) self.turn_off_all_heaters)
# Register TURN_OFF_HEATERS command # Register commands
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,
desc=self.cmd_TURN_OFF_HEATERS_help) desc=self.cmd_TURN_OFF_HEATERS_help)
gcode.register_command("M105", self.cmd_M105, when_not_ready=True)
def add_sensor_factory(self, sensor_type, sensor_factory): def add_sensor_factory(self, sensor_type, sensor_factory):
self.sensor_factories[sensor_type] = sensor_factory self.sensor_factories[sensor_type] = sensor_factory
def setup_heater(self, config, gcode_id=None): def setup_heater(self, config, gcode_id=None):
@ -267,8 +270,6 @@ 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_gcode_sensors(self):
return self.gcode_id_to_sensor.items()
def register_sensor(self, config, psensor, gcode_id=None): def register_sensor(self, config, psensor, gcode_id=None):
if gcode_id is None: if gcode_id is None:
gcode_id = config.get('gcode_id', None) gcode_id = config.get('gcode_id', None)
@ -279,15 +280,48 @@ class PrinterHeaters:
"G-Code sensor id %s already registered" % (gcode_id,)) "G-Code sensor id %s already registered" % (gcode_id,))
self.gcode_id_to_sensor[gcode_id] = psensor self.gcode_id_to_sensor[gcode_id] = psensor
self.available_sensors.append(config.get_name()) self.available_sensors.append(config.get_name())
def get_status(self, eventtime):
return {'available_heaters': self.available_heaters,
'available_sensors': self.available_sensors}
def turn_off_all_heaters(self, print_time=0.): def turn_off_all_heaters(self, print_time=0.):
for heater in self.heaters.values(): for heater in self.heaters.values():
heater.set_temp(0.) heater.set_temp(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):
self.turn_off_all_heaters() self.turn_off_all_heaters()
def get_status(self, eventtime): # G-Code M105 temperature reporting
return {'available_heaters': self.available_heaters, def _handle_ready(self):
'available_sensors': self.available_sensors} self.has_started = True
def _get_temp(self, eventtime):
# Tn:XXX /YYY B:XXX /YYY
out = []
if self.has_started:
for gcode_id, sensor in sorted(self.gcode_id_to_sensor.items()):
cur, target = sensor.get_temp(eventtime)
out.append("%s:%.1f /%.1f" % (gcode_id, cur, target))
if not out:
return "T:0"
return " ".join(out)
def cmd_M105(self, params):
# Get Extruder Temperature
gcode = self.printer.lookup_object("gcode")
reactor = self.printer.get_reactor()
msg = self._get_temp(reactor.monotonic())
did_ack = gcode.ack(msg)
if not did_ack:
gcode.respond_raw(msg)
def wait_for_temperature(self, heater):
# Helper to wait on heater.check_busy() and report M105 temperatures
if self.printer.get_start_args().get('debugoutput') is not None:
return
toolhead = self.printer.lookup_object("toolhead")
gcode = self.printer.lookup_object("gcode")
reactor = self.printer.get_reactor()
eventtime = reactor.monotonic()
while not self.printer.is_shutdown() and heater.check_busy(eventtime):
print_time = toolhead.get_last_move_time()
gcode.respond_raw(self._get_temp(eventtime))
eventtime = reactor.pause(eventtime + 1.)
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))

View File

@ -167,7 +167,7 @@ class PrinterExtruder:
heater = extruder.get_heater() heater = extruder.get_heater()
heater.set_temp(temp) heater.set_temp(temp)
if wait and temp: if wait and temp:
gcode.wait_for_temperature(heater) self.printer.lookup_object('heater').wait_for_temperature(heater)
def cmd_M109(self, params): def cmd_M109(self, params):
# Set Extruder Temperature and Wait # Set Extruder Temperature and Wait
self.cmd_M104(params, wait=True) self.cmd_M104(params, wait=True)