Last active
June 19, 2025 03:04
-
-
Save Spiritdude/f7b6cc18ba77b067e092a4ee8843440a to your computer and use it in GitHub Desktop.
MAF: Multi-Axis Framework for Klipper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[respond] | |
# == MAF: Multi-Axis Framework for Klipper == | |
# written by Rene K. Mueller <[email protected]> | |
# | |
# Define your G-code axis information in another file, and then: | |
# | |
# [include maf.cfg] # -- include this file | |
# | |
# [gcode_macro MY_MAF] | |
# variable_map = { | |
# "X": { "motor": "stepper_x", "dir":-1, "end_pos": 0 }, | |
# "Y": { "motor": "stepper_y", "dir":-1, "end_pos": 0 }, | |
# "Z": { "motor": "stepper_z", "dir":-1, "end_pos": 0, "bounce": 2 }, | |
# "E": { "motor": "extruder" }, | |
# ... | |
# "A": { "motor": "stepper_x1", "dir":1, "end_pos": 380 }, | |
# "B": { "motor": "stepper_x2", "dir":-1, "end_pos": 0 }, | |
# ... | |
# "U": { "motor": "extruder1" }, # -- extruders do not need homing info | |
# "V": { "motor": "extruder2" }, | |
# ... | |
# } | |
# # optional: | |
# variable_tools = { # -- if you declare this, you can use 'G0 T0 X100 T1 X100' or 'T1\nG1 X100' and it maps to the proper axes | |
# "T0": { "X": "X", "Y":"Y", "Z":"Z", "E":"E" }, | |
# "T1": { "X": "A", "E":"U" }, | |
# "T2": { "X": "B", "E":"V" }, | |
# ... | |
# } | |
# gcode: | |
# | |
# Features: | |
# T0 .. T9 ; reference extruder, extruder1 etc | |
# | |
# M82 ; abs extrusion (default) | |
# M83 ; rel extrusion | |
# | |
# G28 X Y Z U A B C | |
# G0 X100 U100 ... ; new axes become usable in G0 and G1 incl. relative extrusion | |
# G1 X100 U100 ... | |
# | |
# G92 X0 Y10 W10 U0 ; set value for axes | |
# | |
# G90 ; absolute positioning | |
# G91 ; relative positioning | |
# | |
# T2 ; set temps for multiple extruders selected by T<n> notion | |
# M104 S200 | |
# ; - or - | |
# M104 T2 S200 | |
# | |
# if tools (to axes) were declared as well, then the tool axes are mapped for you: | |
# T0 | |
# G0 X100 Y100 | |
# T1 | |
# G0 X200 | |
# ; - or - | |
# G0 T0 X100 Y100 T1 X200 | |
# | |
# MAF ; provides overview of current mappings and extruder<n> positions (n>=1) | |
# | |
# --- DO NOT EDIT ANYTHING BELOW --- but make changes outside in the file which includes maf.cfg | |
# so maf.cfg can be updated in the future without removing your changes you made here (or rather not). ;-) | |
# | |
# History: | |
# 2025/06/19: 0.2.1: making G1 alias of G0, removing duplicate code | |
# 2025/06/15: 0.2.0: optional: supporting tool axes, mapping tools to proper axes either same line or tool switching | |
# 2025/06/03: 0.1.0: G90/G91 on host is supported, but we track now position as well | |
# 2025/05/31: 0.0.8: adding position for most axis (still missing manual stepper not being extruders) | |
# 2025/05/28: 0.0.6: temp output for 'MAF', adding 'bounce' distance for homing (default 10mm), better documentation | |
# 2025/05/27: 0.0.5: omitting XYZE still properly G28 homes them, mentioning them uses new G28 procedure | |
# 2025/05/23: 0.0.4: renaming it to MAF for Multi-Axis Framework: maf.cfg, and MAF | |
# 2025/05/22: 0.0.3: unified stepper and home pos/dir in one structure, G28 works then too | |
# 2025/05/22: 0.0.2: including home_dir data structure to implement HOME_AXIS AXIS=U | |
# 2025/05/21: 0.0.1: initial version with extruder axes abs or relative based on M82, M83, and M104/109 with tools | |
[delayed_gcode _MAF] | |
initial_duration: 0.5 | |
gcode: | |
MAF | |
[gcode_macro MAF] | |
variable_version = "0.2.1" | |
variable_map = { } # -- will contain MY_MAF.map after first call | |
variable_tools = { } | |
variable_duplication = { } | |
variable_duplication_mode = '' | |
variable_misc = { } | |
variable_debug = 0 | |
variable_speed = 40 # -- in mm/s | |
variable_erel = 0 # -- relative extrusion (default: off) use M83 or M84 to switch back and forth | |
variable_extruder1_pos = 0. | |
variable_extruder2_pos = 0. | |
variable_extruder3_pos = 0. | |
variable_extruder4_pos = 0. | |
variable_extruder5_pos = 0. | |
variable_extruder6_pos = 0. | |
variable_extruder7_pos = 0. | |
variable_extruder8_pos = 0. | |
variable_extruder9_pos = 0. | |
variable_prel = 0 # -- relative position (default: off) use G90 or G91 to switch back and forth | |
variable_axis_a_pos = 0. # -- Note: variable_<name> is always lowercase to access even when we use uppercase in declaration !!! | |
variable_axis_b_pos = 0. | |
variable_axis_c_pos = 0. | |
variable_axis_d_pos = 0. | |
variable_axis_h_pos = 0. | |
variable_axis_i_pos = 0. | |
variable_axis_j_pos = 0. | |
variable_axis_k_pos = 0. | |
variable_axis_l_pos = 0. | |
variable_axis_n_pos = 0. | |
variable_axis_o_pos = 0. | |
variable_axis_p_pos = 0. | |
variable_axis_q_pos = 0. | |
variable_axis_r_pos = 0. | |
variable_axis_s_pos = 0. | |
variable_axis_t_pos = 0. | |
variable_axis_u_pos = 0. | |
variable_axis_v_pos = 0. | |
variable_axis_w_pos = 0. | |
variable_init = 0 | |
gcode: | |
# -- MY_MAF contains just the mapping, MAF contains the changing position of the extruders | |
RESPOND MSG="== MAF: Multi-Axis Framework {version} ==" | |
{% if init == 0 %} | |
# -- we save the map at MAF (hackish approach, we do it only **once** - see https://klipper.discourse.group/t/how-to-update-a-dictionary-variable/7366 | |
RESPOND MSG=" initializing ..." | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=map VALUE="{printer['gcode_macro MY_MAF'].map | pprint | replace("\n", "") | replace("\"", "\\\"")}" | |
{% for fmt in 'duplication,tools,misc'.split(',') %} | |
{% if printer['gcode_macro MY_MAF'].get(fmt) %} | |
RESPOND MSG=" - {fmt} mapping" | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE={fmt} VALUE="{printer['gcode_macro MY_MAF'][fmt] | pprint | replace("\n", "") | replace("\"", "\\\"")}" | |
{% endif %} | |
{% endfor %} | |
RESPOND MSG=" - axes motor mapping" | |
{% for k,v in printer['gcode_macro MY_MAF'].map.items() if k not in 'XYZE' %} | |
MANUAL_STEPPER STEPPER={v['motor']} SET_POSITION=0 SPEED=40 ACCEL=500 | |
MANUAL_STEPPER STEPPER={v['motor']} GCODE_AXIS={k} | |
{% endfor %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=init VALUE=1 | |
RESPOND MSG=" initializing done." | |
RESPOND MSG=" supporting G0, G1, G28, G90, G91, G92, M104, M109, M82, M83, and T0..T9" | |
{% if printer['gcode_macro MY_MAF'].get('tools') %} | |
RESPOND MSG=" supporting G0/G1 with tool select, e.g. 'G0 T0 X100 Y100 T1 X300 Y200' or 'T1\nG1 X100'" | |
{% endif %} | |
{% endif %} | |
{% if 'DEBUG' in params %} | |
{% set debug=params.DEBUG|int %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=debug value={debug} | |
{% endif %} | |
RESPOND MSG="debug: {debug}" | |
{% if 'DUPLICATION' in params and printer['gcode_macro MAF'].duplication %} | |
{% set duplication_mode=params.DUPLICATION %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=duplication_mode value="'{duplication_mode}'" | |
{% endif %} | |
RESPOND MSG="duplication: '{duplication_mode}'" | |
RESPOND MSG="speed: {'%d' % speed} mm/s" | |
{% for t,v in printer['gcode_macro MY_MAF'].tools.items() %} | |
{% if t != 'home' %} | |
RESPOND MSG="tool {t}: {% for a,_a in v.items() %}{a}:{_a} {% endfor %}" | |
{% endif %} | |
{% endfor %} | |
RESPOND TYPE=echo MSG="motion mode: {'relative' if prel else 'absolute'}" | |
RESPOND TYPE=echo MSG="extrude mode: {'relative' if erel else 'absolute'}" | |
{% for k,v in printer['gcode_macro MY_MAF'].map.items() %} | |
{% set ki = loop.index0 %} | |
{% set e = '0' %} | |
{% set pos = '' %} | |
{% if 'extruder' in v['motor'] and v['motor'] != 'extruder' %} | |
{% set e = v['motor'][-1] %} | |
{% endif %} | |
{% if e != '0' %} # -- extra extruder? | |
{% set pos = printer['gcode_macro MAF']['extruder'+e+'_pos'] %} | |
{% elif k in 'XYZE' %} # -- internal? | |
{% set ki = 'XYZE'.index(k) %} | |
{% set pos = printer['toolhead'].position[ki] %} | |
{% else %} | |
{% set pos = printer['gcode_macro MAF']['axis_'+k.lower()+'_pos'] %} # !!! variable_something never is uppercase | |
{% endif %} | |
RESPOND MSG="axis {k}: {v['motor']}{' / T'+e if k=='E' else (' / T'+e if e != '0' else '')}{', pos: '+(("%.4f" % pos)|string) if pos!='' else ''}" | |
{% if 'extruder' in v['motor'] %} | |
RESPOND MSG=" temperature: {printer[v['motor']].temperature}C / {printer[v['motor']].target}C" | |
{% endif %} | |
{% endfor %} | |
#{% for k,v in epos.items() %} | |
# RESPOND MSG="{k}: {v}" | |
#{% endfor %} | |
#{% for e in '123456789' %} | |
# RESPOND MSG="extruder{e}_pos: {printer['gcode_macro MAF']['extruder'+e+'_pos']}" | |
#{% endfor %} | |
#M105 # -- show all the temps | |
# -- helper macro (no need to call from outside, just use T0, T1 etc.) | |
# T0, T1, T2, T3 etc. -> TOOL_SELECT.tool | |
[gcode_macro TOOL_SELECT] | |
variable_tool: 0 | |
gcode: | |
{% set tool = params.TOOL|default(0)|int %} | |
SET_GCODE_VARIABLE MACRO=TOOL_SELECT VARIABLE=tool VALUE={tool} | |
[gcode_macro T0] | |
gcode: | |
TOOL_SELECT TOOL=0 | |
[gcode_macro T1] | |
gcode: | |
TOOL_SELECT TOOL=1 | |
[gcode_macro T2] | |
gcode: | |
TOOL_SELECT TOOL=2 | |
[gcode_macro T3] | |
gcode: | |
TOOL_SELECT TOOL=3 | |
[gcode_macro T4] | |
gcode: | |
TOOL_SELECT TOOL=4 | |
[gcode_macro T5] | |
gcode: | |
TOOL_SELECT TOOL=5 | |
[gcode_macro T6] | |
gcode: | |
TOOL_SELECT TOOL=6 | |
[gcode_macro T7] | |
gcode: | |
TOOL_SELECT TOOL=7 | |
[gcode_macro T8] | |
gcode: | |
TOOL_SELECT TOOL=8 | |
[gcode_macro T9] | |
gcode: | |
TOOL_SELECT TOOL=9 | |
# M104 and M109 for multiple extruders: | |
# T0 | |
# M104 S200 | |
# or | |
# M104 T0 S200 | |
[gcode_macro M104] | |
rename_existing: M104.1 | |
gcode: | |
{% set S = params.S|default(0)|float %} | |
{% set tool = (params.T|default(printer['gcode_macro TOOL_SELECT'].tool))|int %} | |
{% if tool == 0 %} | |
M104.1 S{S} | |
{% else %} | |
ACTIVATE_EXTRUDER EXTRUDER=extruder{tool} | |
M104.1 S{S} | |
ACTIVATE_EXTRUDER EXTRUDER=extruder | |
{% endif %} | |
[gcode_macro M109] | |
rename_existing: M109.1 | |
gcode: | |
{% set S = params.S|default(0)|float %} | |
{% set tool = (params.T|default(printer['gcode_macro TOOL_SELECT'].tool))|int %} | |
{% if tool == 0 %} | |
M109.1 S{S} | |
{% else %} | |
ACTIVATE_EXTRUDER EXTRUDER=extruder{tool} | |
M109.1 S{S} | |
ACTIVATE_EXTRUDER EXTRUDER=extruder | |
{% endif %} | |
[gcode_macro G28] | |
rename_existing: G28.1 | |
gcode: | |
# [stepper] usually allows | |
# position_endstop: 0 | |
# position_max: 390 | |
# | |
# but not manual_stepper, so via 'dir' and 'end_pos' we can define how to home each new axes | |
{% set conf = printer['gcode_macro MAF'] %} | |
{% if conf.duplication_mode != '' %} | |
RESPOND MSG="WARNING: MAF duplication was enabled ({conf.duplication_mode}), it will be disabled for homing" | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=duplication_mode VALUE="''" | |
{% endif %} | |
{% if params.keys()|length == 1 %} # -- homing all? | |
{% set new_params = { } %} | |
{% if printer['gcode_macro MAF'].misc.get('home_all') %} # -- dedicated misc.home_all? | |
{% for a in printer['gcode_macro MAF'].misc.home_all %} | |
{% set _ = new_params.setdefault(a,'') %} | |
{% endfor %} | |
RESPOND MSG="homing dedicated (misc.home_all): {' '.join(new_params.keys())}" | |
{% set params = new_params %} | |
{% elif printer['gcode_macro MAF'].get('tools') %} | |
{% set new_params = [ ] %} | |
{% for av in printer['gcode_macro MAF'].tools.keys() %} | |
{% set _ = new_params.append(av) %} | |
{% endfor %} | |
RESPOND MSG="homing tools: {' '.join(new_params)}" | |
{% set rawparams = ' '.join(new_params) %} | |
{% else %} | |
{% set new_params = { } %} | |
{% for a in printer['gcode_macro MAF'].map.keys() %} | |
{% if 'extruder' not in printer['gcode_macro MAF'].map[a].motor %} | |
{% set _ = new_params.setdefault(a,'') %} | |
{% endif %} | |
{% endfor %} | |
RESPOND MSG="homing all axes: {' '.join(new_params.keys())}" | |
{% set params = new_params %} | |
{% endif %} | |
{% endif %} | |
{% if printer['gcode_macro MAF'].get('tools') %} | |
{% set new_params = { } %} | |
{% for av in rawparams.split() %} | |
{% set a = av[0] %} | |
{% set v = av[1:] %} | |
{% if a == 'T' %} | |
{% if printer['gcode_macro MAF'].tools[av].get('home') %} | |
{% for ax in printer['gcode_macro MAF'].tools[av].get('home') if ax != 'E' %} | |
{% set _ = new_params.setdefault(printer['gcode_macro MAF'].tools[av][ax],'') %} | |
{% endfor %} | |
{% else %} | |
{% for ax,_ax in printer['gcode_macro MAF'].tools[av].items() if ax != 'E' %} | |
{% set _ = new_params.setdefault(_ax,'') %} | |
{% endfor %} | |
{% endif %} | |
{% endif %} | |
{% endfor %} | |
{% if new_params.keys() %} | |
RESPOND MSG="homing for tools: {' '.join(new_params.keys())}" | |
{% set params = new_params %} | |
{% endif %} | |
{% endif %} | |
{% for a in params if a != 'G' %} | |
{% if a in conf.map %} # -- if it was defined, try to do something here | |
{% set inf = conf.map[a] %} | |
{% set start_pos = 2000 if inf.dir<0 else 0 %} | |
{% set bounce = inf.get('bounce',10) %} | |
{% set wait_pos = inf.end_pos + inf.dir * -bounce %} | |
{% set speed = params.SPEED|default(40)|int %} | |
{% set accel = params.ACCEL|default(500)|int %} | |
{% if a not in "XYZE" %} | |
{% set stepper = inf['motor'] %} | |
MANUAL_STEPPER STEPPER={stepper} GCODE_AXIS= | |
MANUAL_STEPPER STEPPER={stepper} SET_POSITION={start_pos} | |
MANUAL_STEPPER STEPPER={stepper} MOVE={inf.end_pos} STOP_ON_ENDSTOP=1 SPEED={speed} ACCEL={accel} | |
MANUAL_STEPPER STEPPER={stepper} SET_POSITION={inf.end_pos} | |
MANUAL_STEPPER STEPPER={stepper} MOVE={wait_pos} SPEED={speed*2} | |
MANUAL_STEPPER STEPPER={stepper} GCODE_AXIS={a} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=axis_{a.lower()}_pos VALUE={wait_pos} | |
{% else %} # -- follow G28 {axis} and then bounce back a bit | |
G28.1 {a} | |
G0 {a}{wait_pos} F{speed*2*60} | |
G0 F{speed*60} | |
{% endif %} | |
{% else %} | |
G28.1 {a} | |
{% endif %} | |
{% endfor %} | |
[gcode_macro G92] # -- supporting manual steppers connected to G-code axis | |
rename_existing: G92.1 | |
gcode: | |
{% set conf = printer['gcode_macro MAF'] %} | |
{% for a in params if a not in 'GMXYZE' %} | |
RESPOND MSG="{a} {conf.map[a]['motor']}={params[a]}" | |
MANUAL_STEPPER STEPPER={conf.map[a]['motor']} GCODE_AXIS= | |
MANUAL_STEPPER STEPPER={conf.map[a]['motor']} SET_POSITION={params[a]} | |
{% if 'extruder' in conf.map[a]['motor'] %} | |
{% set enum = conf.map[a]['motor'][-1] %} # -- supports only 9 additional extruders (for now) | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=extruder{enum}_pos VALUE={params[a]} | |
{% else %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=axis_{a.lower()}_pos VALUE={params[a]} # -- any other manual stepper | |
{% endif %} | |
MANUAL_STEPPER STEPPER={conf.map[a]['motor']} GCODE_AXIS={a} | |
{% endfor %} | |
{% for a in params if a in 'XYZE' %} | |
G92.1 {a}{params[a]} | |
{% endfor %} | |
[gcode_macro G90] | |
rename_existing: G9000 | |
gcode: | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=prel VALUE=0 | |
G9000 | |
[gcode_macro G91] | |
rename_existing: G9100 | |
gcode: | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=prel VALUE=1 | |
G9100 | |
[gcode_macro G0] | |
rename_existing: G1000 | |
variable_tool = 'T0' | |
gcode: | |
{% set conf = printer['gcode_macro MAF'] %} | |
{% set cmd = 'G' + params.G %} | |
{% if conf.get('duplication') and printer['gcode_macro MAF'].duplication_mode != '' %} | |
{% set new_params = [ ] %} # -- NOTE: new_params is an array first | |
{% set amap = { 'X': 0, 'Y': 1 } %} | |
{% set dconf = conf.get('duplication') %} | |
{% set mode = printer['gcode_macro MAF'].duplication_mode %} | |
{% set tool_cnt = conf.tools.keys()|length %} | |
{% for t in range(tool_cnt) %} | |
{% set tool = 'T'+t|string %} | |
{% set _ = new_params.append(tool) %} | |
{% for a in 'XYEF' %} | |
{% if a in 'XY' %} | |
{% set v = params.get(a,printer.toolhead.position[a.lower()])|float %} | |
{% if mode == 'copy' or (t%2 == 0 and a=='X') or (dconf[tool].row%2 == 0 and a=='Y') %} | |
{% set v = dconf[tool].offset[amap[a]] + v %} | |
{% elif mode == 'mirror' %} | |
{% set v = dconf[tool].offset[amap[a]] + dconf[tool].size[amap[a]] - v %} | |
{% endif %} | |
{% set _ = new_params.append(a+('%.04f'%v)) %} | |
{% elif a in params %} | |
{% if t == 0 or a != 'F' %} | |
{% set _ = new_params.append(a+params[a]) %} | |
{% endif %} | |
{% endif %} | |
{% endfor %} | |
{% endfor %} | |
{% if printer['gcode_macro MAF'].debug>0 %} | |
RESPOND MSG="[duplication] {cmd} {' '.join(new_params)}" | |
{% endif %} | |
{% set rawparams = cmd+' '+' '.join(new_params) %} | |
{% endif %} | |
{% if conf.get('tools') and ('T' in params or 'T' in rawparams) %} # -- G0/G1 has inline tool selection | |
{% set new_params = { } %} | |
{% set tools = conf.get('tools') %} | |
{% set ns = namespace(tool=printer['gcode_macro G0'].tool) %} # -- making 'tool' make available as ns.tool in and outside of loop | |
{% for av in rawparams.split() %} | |
{% set a = av[0] %} | |
{% set v = av[1:] %} | |
{% if a == 'T' %} | |
#SET_GCODE_VARIABLE MACRO=G0 VARIABLE=tool value="'{av}'" | |
{% set ns.tool = av %} | |
{% elif a in printer['gcode_macro MAF'].tools[ns.tool] %} | |
{% set _a = printer['gcode_macro MAF'].tools[ns.tool][a] %} | |
{% set _ = new_params.setdefault(_a,v) %} | |
{% else %} | |
{% set _ = new_params.setdefault(a,v) %} | |
{% endif %} | |
{% endfor %} | |
{% if printer['gcode_macro MAF'].debug>0 %} | |
RESPOND MSG="[inline multi-tool] {new_params}" | |
{% endif %} | |
{% elif conf.get('tools') and printer['gcode_macro TOOL_SELECT'].get('tool') != 0 %} # -- did we switch deliberately a tool with T<n> | |
{% set new_params = { } %} | |
{% set tool = 'T'+printer['gcode_macro TOOL_SELECT'].tool|string %} | |
{% for a,v in params.items() %} | |
{% if tool in printer['gcode_macro MAF'].tools and a in printer['gcode_macro MAF'].tools[tool] %} | |
{% set _a = printer['gcode_macro MAF'].tools[tool][a] %} | |
{% set _ = new_params.setdefault(_a,v) %} | |
{% else %} | |
{% set _ = new_params.setdefault(a,v) %} | |
{% endif %} | |
{% endfor %} | |
{% if printer['gcode_macro MAF'].debug>0 %} | |
RESPOND MSG="[tool switch] {new_params}" | |
{% endif %} | |
{% else %} | |
{% set new_params = params %} | |
{% endif %} | |
{% set _ = new_params.pop('G',None) %} # -- remove Gxxx | |
{% for a in new_params if a not in 'GXYZET' %} # -- ignore the native axes/settings | |
{% set v = new_params[a]|default(0)|float %} | |
{% if a in conf.map and 'extruder' in conf.map[a]['motor'] %} | |
{% set en = conf.map[a]['motor'][-1] %} # -- number of extruder<n> | |
{% if conf.erel %} | |
{% set pos = conf['extruder'+en+'_pos']+v %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=extruder{en}_pos VALUE={pos} | |
{% set _ = new_params.update({a: pos}) %} | |
{% else %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=extruder{en}_pos VALUE={v} | |
{% set _ = new_params.update({a: v}) %} | |
{% endif %} | |
{% elif a == 'F' %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=speed VALUE={v/60} # -- storing in mm/s | |
{% set _ = new_params.update({a: v}) %} | |
{% else %} | |
{% if conf.prel %} | |
{% set pos = conf['axis_'+a.lower()+'_pos']+v %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=axis_{a.lower()}_pos VALUE={pos} | |
#{% set _ = new_params.update({a: pos}) %} | |
{% else %} | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=axis_{a.lower()}_pos VALUE={v} | |
#{% set _ = new_params.update({a: v}) %} | |
{% endif %} | |
{% endif %} | |
{% endfor %} | |
{% if 'F' not in params %} | |
{% set _ = new_params.update({"F": conf.speed*60 }) %} | |
{% endif %} | |
# -- we need to measure max distance for each axis to check if we need to adjust the overall feedrate F | |
{% set max_dist = [ ] %} | |
# -- we won't track F or extruders | |
{% for a,v in new_params.items() if not (a in 'FE' or (a in printer['gcode_macro MAF'].map and 'extruder' in printer['gcode_macro MAF'].map[a].motor)) %} | |
{% if a in 'XYZE' %} # -- internal? | |
{% set ki = 'XYZE'.index(a) %} | |
{% set old_pos = printer['toolhead'].position[ki] %} | |
{% else %} | |
{% set old_pos = printer['gcode_macro MAF']['axis_'+a.lower()+'_pos'] %} | |
{% endif %} | |
{% set d = old_pos - v|float %} | |
{% set _ = max_dist.append(d if d>0 else -d) %} | |
{% endfor %} | |
{% set reset_speed = 0 %} | |
{% if max_dist|length > 0 %} | |
{% set max_dist_val = max_dist | sort | last %} | |
{% set ref_dist = ((printer.toolhead.position['x']-new_params.get('X',0)|float)**2 + (printer.toolhead.position['y']-new_params.get('Y',0)|float)**2)**0.5 %} | |
{% if printer['gcode_macro MAF'].debug>1 %} | |
RESPOND MSG=" ref_dist={'%0.4f' % ref_dist} max_dist={'%0.4f' % max_dist_val} of {max_dist}" | |
{% endif %} | |
{% set speed = new_params.get('F',0)|float / 60 if 'F' in new_params else conf.speed %} | |
{% if max_dist_val > 0 and ref_dist > 0 %} # and (max_dist_val - 1 > ref_dist or max_dist_val + 1 < ref_dist) %} | |
{% set speed_ratio = max_dist_val / ref_dist %} | |
{% if speed_ratio > 1.1 %} | |
{% set _ = new_params.update({'F':'%d' % (speed*60 / speed_ratio)}) %} | |
{% if printer['gcode_macro MAF'].debug>1 %} | |
RESPOND MSG="=> F{'%d' % (speed*60 / speed_ratio)} (original speed {'%d' % (speed*60)})" | |
{% endif %} | |
{% set reset_speed = 1 %} | |
{% endif %} | |
{% endif %} | |
{% endif %} | |
{% if printer['gcode_macro MAF'].debug>0 %} | |
RESPOND MSG="[final] {cmd} {% for k,v in new_params.items() %}{k}{'%.4f'%v|float} {% endfor %}" | |
{% endif %} | |
G1000 {% for k,v in new_params.items() %}{k}{'%.4f'%v|float} {% endfor %} | |
[gcode_macro G1] | |
rename_existing: G1001 | |
gcode: | |
G0 {rawparams} # -- we make no distinction of G0 vs G1 | |
#G1001 {% for k,v in new_params.items() %}{k}{'%.4f'%v|float} {% endfor %} | |
[gcode_macro M82] # -- absolute extrusion | |
rename_existing: M82.1 | |
gcode: | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=erel value=0 | |
M82.1 | |
[gcode_macro M83] # -- relative extrusion | |
rename_existing: M83.1 | |
gcode: | |
SET_GCODE_VARIABLE MACRO=MAF VARIABLE=erel value=1 | |
M83.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment