-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFDC.cfg
212 lines (173 loc) · 10.9 KB
/
FDC.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
[gcode_macro _FDC]
variable_z_height_temps: {999:999}
variable_last_trams: {'stepper_z': 0, 'stepper_z1': 0, 'stepper_z2': 0}
variable_z_trams_temps: {'stepper_z': {}, 'stepper_z1': {}, 'stepper_z2': {}}
variable_enable_tram: 0
variable_temp_min: 0.0
variable_temp_max: 999.9
variable_step: 0.1
variable_precision: 1
############################ DO NOT CHANGE ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING ####################################
variable_enable: 1
variable_thermistor_name: "z_thermal_adjust"
variable_last_temp: 0
variable_last_temp_range: []
gcode:
{% set current_temp = params.CURRENT_TEMP|float %}
{% set ref_temp = params.REF_TEMP|float %}
{% set precision = printer["gcode_macro _FDC"].precision %}
{% set step = printer["gcode_macro _FDC"].step %}
{% set temp_min = printer["gcode_macro _FDC"].temp_min %}
{% set temp_max = printer["gcode_macro _FDC"].temp_max %}
{% if temp_min <= ref_temp <= temp_max and temp_min <= current_temp <= temp_max and current_temp != ref_temp %}
{% set range_start = (ref_temp * (10 ** precision))|int %}
{% set range_end = (current_temp * (10 ** precision))|int %}
{% set range_step = (step * (10 ** precision))|int %}
{% if range_start > range_end %}
{% set range_step = range_step * -1 %}
{% endif %}
{% set temp_range = namespace(value=[]) %}
{% for i in range(range_start,range_end + range_step, range_step) %}
# This is how you append to a list in jinja2 with a namespace...yes...
{% set temp_range.value = temp_range.value + [i / (10**precision)] %}
{% endfor %}
# note to self: can't have a space between the var and value (i.e Malformed command)
# also for complex types like list and dict you need quotes
SET_GCODE_VARIABLE MACRO=_FDC VARIABLE=last_temp VALUE={current_temp}
SET_GCODE_VARIABLE MACRO=_FDC VARIABLE=last_temp_range VALUE="{temp_range.value}"
{% if printer["gcode_macro _FDC"].enable_tram %}
_TILT_AND_LOAD_MESH CURRENT_TEMP={current_temp} REF_TEMP={ref_temp}
{% else %}
BED_MESH_PROFILE LOAD={current_temp}
_Z_HEIGHT_ADJUST CURRENT_TEMP={current_temp} REF_TEMP={ref_temp}
{% endif %}
{% elif ref_temp != 0 and (ref_temp > temp_max or ref_temp < temp_min) %}
{ action_raise_error("ref_temp or current_temp is out of range "~ref_temp~", "~current_temp) }
{% endif %}
[gcode_macro _Z_HEIGHT_ADJUST]
variable_last_coeff: 0
gcode:
{% set total_mm= namespace(value=0) %}
{% set temp_coeff = namespace(value=0) %}
{% for temp in printer["gcode_macro _FDC"].last_temp_range %}
{% set total_mm.value = total_mm.value + printer["gcode_macro _FDC"].z_height_temps[temp|float] %}
{% endfor %}
# we take the temp diff abs because the z_thermal_adjust will handle the signs
# the temp_coeff should be low temp to high temp, it will handle the reverse
{% set temp_coeff.value = total_mm.value / (params.CURRENT_TEMP|float - params.REF_TEMP|float)|abs %}
#there is a bug in the comparison of this two floats
#coudln't for the life of me understand why, so I compare them as strings
{% if last_coeff|string != temp_coeff.value|string %}
SET_GCODE_VARIABLE MACRO=_Z_HEIGHT_ADJUST VARIABLE=last_coeff VALUE={temp_coeff.value}
SET_Z_THERMAL_ADJUST TEMP_COEFF={ temp_coeff.value }
{% endif %}
[gcode_macro _TILT_AND_LOAD_MESH]
gcode:
{% if printer["gcode_macro _FDC"].enable_tram %}
{% if ('applied' in printer.quad_gantry_level and printer.quad_gantry_level.applied) or ('applied' in printer.z_tilt and printer.z_tilt.applied) %}
{% set current_temp = params.CURRENT_TEMP|float %}
{% set ref_temp = params.REF_TEMP|float %}
{% set total_mm= namespace() %}
{% set total_mm.sign= 1 %}
{% set total_mm.value= dict() %}
{% set total_mm.diff= dict() %}
{% set total_mm.macro_string= "" %}
{% if current_temp < ref_temp %}
# If temps are going up we also need to flip the sign
# to un-tilt the bed mesh (from a the flat position to the reverse tilt position)
{% set total_mm.sign=-1 %}
{% endif %}
{% for stepper, offset in printer["gcode_macro _FDC"].z_trams_temps.items() %}
{% set _=total_mm.value.__setitem__(stepper, 0) %}
{% for temp in printer["gcode_macro _FDC"].last_temp_range %}
# This is how you set and add an item to a dict in jinja2 with a namespace...yes...
{% set _=total_mm.value.__setitem__(stepper,total_mm.value.__getitem__(stepper) + offset[temp|float]) %}
{% endfor %}
{% set _=total_mm.value.__setitem__(stepper, total_mm.value.__getitem__(stepper) * total_mm.sign) %}
# Subtract from the ref temp offset
# We need to subtract it because this is Z=0 for all steppers
#{% set _=total_mm.diff.__setitem__(stepper, total_mm.value.__getitem__(stepper) - offset[ref_temp]) %}
{% endfor %}
SET_GCODE_VARIABLE MACRO=_FDC variable=last_trams value="{total_mm.value}"
{% for stepper, stepper_total_mm in total_mm.value.items() %}
{% set total_mm.macro_string=total_mm.macro_string ~ stepper ~ '=' ~ stepper_total_mm ~ ' ' %}
{% endfor %}
# { action_respond_info("TILT_AND_LOAD String %s" % (total_mm.macro_string)) }
BED_MESH_PROFILE TILT_AND_LOAD={current_temp} CURRENT_TEMP={current_temp} REF_TEMP={ref_temp} {total_mm.macro_string}
{% else %}
{action_respond_info("Tram enabled but the bed is not trammed!")}
{% endif %}
{% else %}
{action_respond_info("TILT_AND_LOAD_MESH called but it's not enabled")}
{% endif %}
[delayed_gcode RUN_FDC]
initial_duration: 1
gcode:
{% if printer["gcode_macro _FDC"].temp_min|float == 0.0 or printer["gcode_macro _FDC"].temp_max|float == 999.9 %}
{ action_respond_info("FDC disabled: Stock temperature values present! Modify the config to reflect your own values and restart!") }
{% elif printer["gcode_macro _FDC"].z_height_temps.999 is defined and printer["gcode_macro _FDC"].z_height_temps.0 == 999 %}
{ action_respond_info("FDC disabled: Stock z heights values present! Modify the config to reflect your own values and restart!") }
{% elif printer["gcode_macro _FDC"].enable_tram and 'z_tilt' in printer and printer["gcode_macro _FDC"].z_trams_temps.keys()|length != 3 %}
{ action_respond_info("FDC disabled: z_tilt require 3 z steppers to be defined in variable_z_trams_temps!") }
{% elif printer["gcode_macro _FDC"].enable_tram and 'quad_gantry_level' in printer and printer["gcode_macro _FDC"].z_trams_temps.keys()|length != 4 %}
{ action_respond_info("FDC disabled: quad_gantry_level require 4 z steppers to be defined in variable_z_trams_temps!") }
{% elif printer["gcode_macro _FDC"].enable_tram and 'z_tilt' in printer and printer["gcode_macro _FDC"].last_trams.keys()|length != 3 %}
{ action_respond_info("FDC disabled: z_tilt require 3 z steppers to be defined variable_last_trams(init to zero)!") }
{% elif printer["gcode_macro _FDC"].enable_tram and 'quad_gantry_level' in printer and printer["gcode_macro _FDC"].last_trams.keys()|length != 4 %}
{ action_respond_info("FDC disabled: quad_gantry_level require 4 z steppers to be defined in variable_last_trams(init to zero)!") }
{% else %}
UPDATE_DELAYED_GCODE ID=RUN_FDC DURATION=10
{% if printer["gcode_macro _FDC"].enable %}
{% set precision = printer["gcode_macro _FDC"].precision %}
{% set step = printer["gcode_macro _FDC"].step %}
# Rounding current_temp and ref_temp to the step with precision in order to not be affected by small changes
# This will also improve temp_coeff fluctuation
{% set ref_temp = printer.z_thermal_adjust.z_adjust_ref_temperature %}
{% set ref_temp=(ref_temp / step)|round(0) %}
{% set ref_temp=(ref_temp * step)|round(precision) %}
{% set current_temp = printer[printer["gcode_macro _FDC"].thermistor_name].temperature|float %}
{% set current_temp=(current_temp / step)|round(0) %}
{% set current_temp=(current_temp * step)|round(precision) %}
{% set last_temp = printer["gcode_macro _FDC"].last_temp %}
#change the mesh only if it's needed, we don't want to spam klipper with gcodes
{% if printer.virtual_sdcard.is_active == True and last_temp != current_temp %}
_FDC CURRENT_TEMP={current_temp} REF_TEMP={ref_temp}
{% elif printer.virtual_sdcard.is_active == False and last_temp != 0 %}
{% set last_temp_range_empty=[] %}
{% set total_mm= namespace() %}
{% set total_mm.value= dict() %}
{% for stepper, offset in printer["gcode_macro _FDC"].z_trams_temps.items() %}
{% set _=total_mm.value.__setitem__(stepper, 0) %}
{% endfor %}
# reset the state vars
SET_GCODE_VARIABLE MACRO=_FDC VARIABLE=last_temp VALUE=0
SET_GCODE_VARIABLE MACRO=_FDC VARIABLE=last_temp_range VALUE="{last_temp_range_empty}"
SET_GCODE_VARIABLE MACRO=_FDC variable=last_trams value="{total_mm.value}"
{ action_respond_info("FDC: Resting state vars!\nlast_temp: %s to: %s\nlast_temp_range: %s to: %s\nlast_trams: %s to: %s" % (last_temp, 0, printer["gcode_macro _FDC"].last_temp_range, last_temp_range_empty, printer["gcode_macro _FDC"].last_trams, total_mm.value)) }
{% endif %}
{% endif %}
{% endif %}
[gcode_macro SET_FDC]
gcode:
#get current state if not provided by params
{% set ENABLE = params.ENABLE|default(printer["gcode_macro _FDC"].enable)|string %}
#input checks
{% if ENABLE == "0" or ENABLE == "1" %}
{% set enabled = printer["gcode_macro _FDC"].enable %} #current enable status
{% if not enabled|int == ENABLE|int %}
SET_GCODE_VARIABLE MACRO=_FDC VARIABLE=enable VALUE={ENABLE}
{% endif %}
QUERY_FDC
{% else %}
{ action_respond_info("Invalid parameter ENABLE value '%s' - must be 0 or 1" % (ENABLE|int)) }
{% endif %}
[gcode_macro QUERY_FDC]
gcode:
{% set enabled = printer["gcode_macro _FDC"].enable %}
{% set last_temp = printer["gcode_macro _FDC"].last_temp %}
{% set current_mesh = printer.bed_mesh.profile_name %}
{% set current_temp_coeff = printer["gcode_macro _FDC"].last_coeff %}
{% set current_last_trams = printer["gcode_macro _FDC"].last_trams %}
{% set current_z_adjust = printer.z_thermal_adjust.current_z_adjust %}
{% set ref_temp = printer.z_thermal_adjust.z_adjust_ref_temperature %}
{ action_respond_info("FDC: \n Enabled: %s \n Last temp: %s C \n Mesh profile loaded: %s \n current temp coeff: %s \n current last trams: %s \n current z adjust: %s \n ref_temp: %s" % (enabled, last_temp, current_mesh, current_temp_coeff, current_last_trams, current_z_adjust, ref_temp)) }