Skip to content

Commit

Permalink
Extras: override resonance commands to sync gcode position after swee…
Browse files Browse the repository at this point in the history
…ping
  • Loading branch information
miklschmidt committed Dec 18, 2024
1 parent 7cfd336 commit b90e707
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
44 changes: 44 additions & 0 deletions configuration/klippy/ratos.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, config):

self.load_settings()
self.register_commands()
self.register_command_overrides()
self.register_handler()

#####
Expand Down Expand Up @@ -75,6 +76,49 @@ def register_commands(self):
self.gcode.register_command('BEACON_APPLY_SCAN_COMPENSATION', self.cmd_BEACON_APPLY_SCAN_COMPENSATION, desc=(self.desc_BEACON_APPLY_SCAN_COMPENSATION))
self.gcode.register_command('TEST_PROCESS_GCODE_FILE', self.cmd_TEST_PROCESS_GCODE_FILE, desc=(self.desc_TEST_PROCESS_GCODE_FILE))
self.gcode.register_command('ALLOW_UNKNOWN_GCODE_GENERATOR', self.cmd_ALLOW_UNKNOWN_GCODE_GENERATOR, desc=(self.desc_ALLOW_UNKNOWN_GCODE_GENERATOR))
self.gcode.register_command('_SYNC_GCODE_POSITION', self.cmd_SYNC_GCODE_POSITION, desc=(self.desc_SYNC_GCODE_POSITION))

OverriddenCommands = {
'TEST_RESONANCES': None,
'SHAPER_CALIBRATE': None,
}

def register_command_overrides(self):
self.gcode.register_override('TEST_RESONANCES', self.override_TEST_RESONANCES, desc=(self.desc_TEST_RESONANCES))
self.gcode.register_override('SHAPER_CALIBRATE', self.override_SHAPER_CALIBRATE, desc=(self.desc_SHAPER_CALIBRATE))

def register_override(self, command, func, desc):
prev_cmd = self.gcode.register_command(command, None)
if prev_cmd is None:
raise self.printer.config_error("Existing command '%s' not found in RatOS override" % (command,))
if command not in self.OverriddenCommands:
raise self.printer.config_error("Command '%s' not found in RatOS override list" % (command,))
if self.OverriddenCommands[command] is not None:
raise self.printer.config_error("Command '%s' already overridden in RatOS" % (command,))
self.OverriddenCommands[command] = prev_cmd;
self.gcode.register_command(command, func, desc=(desc))

def get_prev_cmd(self, command):
if command not in self.OverriddenCommands or self.OverriddenCommands[command] is None:
raise self.printer.config_error("Previous function for command '%s' not found in RatOS override list" % (command,))
return self.OverriddenCommands[command]

desc_TEST_RESONANCES = ("Runs the resonance test for a specifed axis, positioning errors caused by sweeping are corrected by a RatOS override of this command.")
def override_TEST_RESONANCES(self, gcmd):
prev_cmd = self.get_prev_cmd('TEST_RESONANCES')
prev_cmd(gcmd)
self.cmd_SYNC_TOOLHEAD(gcmd)

desc_SHAPER_CALIBRATE = ("Runs the shaper calibration for a specifed axis, positioning errors caused by sweeping are corrected by a RatOS override of this command.")
def override_SHAPER_CALIBRATE(self, gcmd):
prev_cmd = self.get_prev_cmd('SHAPER_CALIBRATE')
prev_cmd(gcmd)
self.cmd_SYNC_TOOLHEAD(gcmd)

desc_SYNC_GCODE_POSITION = ("Syncs the toolhead position to the printer position, used internally to correct positioning errors caused by sweeping in resonance tests.")
def cmd_SYNC_GCODE_POSITION(self, gcmd):
toolhead = self.printer.lookup_object('toolhead')
toolhead.manual_move((None, None, None), 100)

desc_ALLOW_UNKNOWN_GCODE_GENERATOR = "Temporarily allow gcode from generators that cannot be identified by the postprocessor"
def cmd_ALLOW_UNKNOWN_GCODE_GENERATOR(self, gcmd):
Expand Down
3 changes: 3 additions & 0 deletions configuration/klippy/resonance_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ def _generate_resonances(self, gcmd, axes):
test_seq = self.generator.gen_test()
self.executor.run_test(test_seq, axis, gcmd)

# Sync the toolhead position to the printer position by using the _SYNC_GCODE_POSITION command in ratos.py.
self.gcode.run_script_from_command("_SYNC_GCODE_POSITION")

def _get_max_calibration_freq(self):
return 1.5 * self.generator.get_max_freq()

Expand Down

0 comments on commit b90e707

Please sign in to comment.