-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheap.cfg
358 lines (335 loc) · 12.7 KB
/
eap.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# vim: tabstop=4 shiftwidth=4 expandtab
# Ejection Assisted Purge (EAP).
# Macros for Klipper.
# BRUSH
# PURGE
# PURGE_HOME
# PURGE_EJECT
[servo eap_servo]
pin: PG12
maximum_servo_angle: 180
minimum_pulse_width: 0.00050
maximum_pulse_width: 0.00250
[gcode_macro _PURGE_CFG]
# A central place for the configuration of all the macros in this file.
gcode:
#### Brushing.
# X,Y start position. A few mm past the right side of the brush.
variable_brush_start: 130, 305
# X stroke relative to the start. A few mm past the left of the brush.
variable_brush_stroke: -40
# X distance relative to the start position to clear away from the brush.
variable_brush_clear: 30
# Brushing speed in mm/s.
variable_brush_speed: 400
# How many times to brush.
variable_brush_count: 3
#
#### Purging.
# X, Y start position of the purging groove. A bit on right side of the end of
# the piston.
variable_groove_start: 50, 305
# Position relative to the start of the grove where to wait for cooling and
# ejection.
variable_cooling_pos: 15, -10
# Minimum cooling time in seconds. If less than the cooling time has elapsed,
# the next purge will wait for the end of the cooling period with the cooling
# fan atop.
variable_cooling_period: 15
# Default purging amount in mm³.
variable_volume: 140
# Default purging flow rate in mm³/s. Depends on the hotend, extruder, and
# viscocity of the plastic.
variable_flowrate: 20
# After a purge at a high flow rate, some pressure is left in the nozzle.
# A bit of retraction can even it out. Volume in mm³.
variable_retraction: 5.
# Retraction speed in mm/s.
variable_retraction_speed: 35
# Length of the groove relative (X) to the start.
variable_groove_len: 35
# Filament crosssection area in mm².
variable_fil_csa: 2.405 # 1.75mm filament.
# Groove crosssection area in mm².
variable_groove_csa: 5.3
#
#### Ejection.
# How long it takes to actuate the servo all the way.
# A MG90S servo is usually rated at 1.8ms/deg at 4.8V.
# Add some margin. For example 1.8ms*180 = 324ms -> 400ms.
variable_servo_dwell: 400
# How many actuations. Sometimes the pellet get stuck or missplaced, and
# another ejection fixes it.
variable_ejection_count: 2
#
#### General.
# Z limit. Below this height, refuses to do anything as a safety measure.
# The check can be turned off by passing ZCHECK=0 to the macros.
variable_z_min: 5
# Travel move speed in mm/s.
variable_travel_speed: 500
[gcode_macro BRUSH]
# This macro doesn't move in Z, only X and Y.
# Parameters:
# CLEAR=1
# ZHOP=-1
# ZCHECK=1
gcode:
# Clear away from the BRUSH at the end. Can be skipped to save a move.
{% set clear = params.CLEAR|default(1)|int %}
# Z hop if positive value.
{% set zhop = params.ZHOP|default(-1)|float %}
# Raise an error if z < z_min.
{% set zcheck = params.ZCHECK|default(1)|int %}
{% set cfg = printer['gcode_macro _PURGE_CFG'] %}
{% set tspeed = cfg.travel_speed * 60 %}
{% set wspeed = cfg.brush_speed * 60 %}
{% set sx = cfg.brush_start[0] %}
{% set sy = cfg.brush_start[1] %}
{% if "xyz" not in printer.toolhead.homed_axes %}
{ action_raise_error("Not homed") }
{% elif zcheck and printer.toolhead.position.z < cfg.z_min %}
{ action_raise_error("Z too low") }
{% else %}
SAVE_GCODE_STATE NAME=BRUSH
{% if zhop >= 0.0 %}
G91 # relative
G0 Z{zhop} F{tspeed}
{% endif %}
G90 # absolute
G0 X{sx} Y{sy} F{tspeed}
{% for _ in range(0, cfg.brush_count) %}
G0 X{sx + cfg.brush_stroke} F{wspeed}
G0 X{sx} F{wspeed}
{% endfor %}
{% if clear == 1 %}
G0 X{sx + cfg.brush_clear} F{tspeed}
{% endif %}
{% if zhop >= 0.0 %}
G91 # relative
G0 Z{-zhop} F{tspeed}
{% endif %}
RESTORE_GCODE_STATE NAME=BRUSH
{% endif %}
[gcode_macro PURGE]
# Eject the previous purge, then purge and plow through the brush.
# Parameters:
# VOL=cfg.volume
# FLOW=cfg.flowrate
# RET=cfg.retraction
# RETSPEED=cfg.retraction_speed
# BRUSH_CLEAR=1
# EJECT=1
# ZHOP=-1
# ZCHECK=1
#
# Maximum purge volume: abs(groove_len) * groove_csa.
# Example: 35mm * 5.3mm³ == 185mm³
variable_last_purge: 0.0
gcode:
{% set cfg = printer['gcode_macro _PURGE_CFG'] %}
# The amount to purge in mm³.
{% set vol = params.VOL|default(cfg.volume)|float %}
# The purging flow rate in mm³/s.
{% set flow = params.FLOW|default(cfg.flowrate)|float %}
# The retraction amount in mm³.
{% set retvol = params.RET|default(cfg.retraction)|float %}
# The retraction in mm/s.
{% set retspeed = params.RETSPEED|default(cfg.retraction_speed)|float %}
# Clear away from the BRUSH at the end. Can be skipped to save a move.
{% set brush_clear = params.BRUSH_CLEAR|default(1)|int %}
# Call PURGE_HOME before purging.
{% set eject = params.EJECT|default(1)|int %}
# Z hop if positive value.
{% set zhop = params.ZHOP|default(-1)|float %}
# Raise an error if z < z_min.
{% set zcheck = params.ZCHECK|default(1)|int %}
{% set tspeed = cfg.travel_speed * 60 %}
{% set max_vol = (cfg.groove_len|abs * cfg.groove_csa) %}
{% set sx = cfg.groove_start[0] %}
{% set sy = cfg.groove_start[1] %}
{% set initial_fan_speed = printer.fan.speed / printer.configfile.settings.fan.max_power %}
{% if "xyz" not in printer.toolhead.homed_axes %}
{ action_raise_error("Not homed") }
{% elif zcheck and printer.toolhead.position.z < cfg.z_min %}
{ action_raise_error("Z too low") }
{% elif vol > max_vol %}
{ action_raise_error("Purge volume %.3fmm\u00B3 greater than maximum %.3fmm\u00B3" % (vol, max_vol)) }
{% else %}
SAVE_GCODE_STATE NAME=PURGE
{% if zhop >= 0.0 %}
G91 # relative
G0 Z{zhop} F{tspeed}
{% endif %}
G90 # Absolute positioning.
{% if eject %}
PURGE_HOME ZCHECK={zcheck}
{% else %}
G0 X{sx} Y{sy} F{tspeed}
{% endif %}
M106 S255
M83 # relative extruder
# purge
{% set dist = vol / cfg.groove_csa %}
{% set feed = flow / cfg.groove_csa %}
{% set fil = vol / cfg.fil_csa %}
# Sometimes, randomly it almost seems, klipper decides it really wants ascii in some processing happening in action_respond_info. Maybe some logging going wrong?
#{ action_respond_info("purge: vol=%.3fmm\u00B3 flow=%.3fmm\u00B3/s dist=%.3fmm fil=%.3fmm rate=%.3fmm/s" % (vol, flow, dist, fil, feed)) }
{ action_respond_info("purge: vol=%.3fmm3 flow=%.3fmm3/s dist=%.3fmm fil=%.3fmm rate=%.3fmm/s" % (vol, flow, dist, fil, feed)) }
G1 X{sx + dist} E{fil} F{feed * 60}
# retraction
{% set ret_fil = retvol / cfg.fil_csa %}
G1 E-{ret_fil} F{retspeed * 60}
# brush
G0 X{cfg.brush_start[0]} Y{cfg.brush_start[1]} F{cfg.brush_speed * 60}
M106 S{initial_fan_speed * 255|int}
{% if brush_clear == 1 %}
G0 X{cfg.brush_start[0] + cfg.brush_clear} F{tspeed}
{% endif %}
{% if zhop >= 0.0 %}
G91 # relative
G0 Z-{zhop} F{tspeed}
{% endif %}
RESTORE_GCODE_STATE NAME=PURGE
{% endif %}
[gcode_macro PURGE_HOME]
# Position the toolhead close to the ejection system. Turn on the fan. Wait for
# the end of the cooling period if necessary. Eject the pellet. Position the
# toolhead above the purging groove. Turn off the fan.
#
# Example of use in prusaslicer tool change gcode:
# {if has_wipe_tower}
# T{next_extruder} ; ERCF swap { filament_preset[current_extruder] }
# {else}
# PURGE_HOME ZHOP=0.2 ZCHECK=0
# T{next_extruder} ; ERCF swap { filament_preset[current_extruder] }
# PURGE VOL=150 RET=0.5 RETSPEED=35 EJECT=0 BRUSH_CLEAR=0 ZCHECK=0
# {endif}
#
# Parameters:
# ZHOP=-1
# ZCHECK=1
gcode:
# Z hop if positive value.
{% set zhop = params.ZHOP|default(-1)|float %}
# Raise an error if z < z_min.
{% set zcheck = params.ZCHECK|default(1)|int %}
{% set cfg = printer['gcode_macro _PURGE_CFG'] %}
{% set mp = printer['gcode_macro PURGE'] %}
{% set tspeed = cfg.travel_speed * 60 %}
{% set sx = cfg.groove_start[0] %}
{% set sy = cfg.groove_start[1] %}
{% set cx = sx + cfg.cooling_pos[0] %}
{% set cy = sy + cfg.cooling_pos[1] %}
{% set initial_fan_speed = printer.fan.speed / printer.configfile.settings.fan.max_power %}
{% if "xyz" not in printer.toolhead.homed_axes %}
{ action_raise_error("Not homed") }
{% elif zcheck and printer.toolhead.position.z < cfg.z_min %}
{ action_raise_error("Z too low") }
{% else %}
SAVE_GCODE_STATE NAME=PURGE_HOME
{% if zhop >= 0.0 %}
G91 # relative
G0 Z{zhop} F{tspeed}
{% endif %}
G90 # absolute
G0 X{cx} Y{cy} F{tspeed}
M106 S255
{% if printer.idle_timeout.state == "Printing" %}
{% set elapsed = printer.idle_timeout.printing_time - mp.last_purge %}
{% if elapsed > 0.0 and elapsed < cfg.cooling_period %}
{ action_respond_info("Waiting for pellet to cool...") }
G4 P{cfg.cooling_period - elapsed}
{% endif %}
{% endif %}
SET_GCODE_VARIABLE MACRO=PURGE VARIABLE=last_purge VALUE={printer.idle_timeout.printing_time}
PURGE_EJECT
G0 X{sx} Y{sy} F{tspeed}
{% if zhop >= 0.0 %}
G91 # relative
G0 Z-{zhop} F{tspeed}
{% endif %}
M106 S{initial_fan_speed * 255|int}
RESTORE_GCODE_STATE NAME=PURGE_HOME
{% endif %}
[gcode_macro PURGE_EJECT]
description: Eject.
# This macro ejects the pellet.
#
# Async ejection state.
# -1: unknown.
# 0: closed.
# 1: opening.
# 2: closing.
#
# TODO: Waiting for some async gcode synced to the print time in klipper to make it truly async.
variable_e_state: -1
variable_e_stroke: 0
gcode:
{% set cfg = printer['gcode_macro _PURGE_CFG'] %}
{% set dwell_s = cfg.servo_dwell / 1000 %}
{% if e_state == -1 %} # unknown
# Add half a stroke for closing first just in case. Since we do not know the state.
SET_GCODE_VARIABLE MACRO=PURGE_EJECT VARIABLE=e_state VALUE=1 # opening
{% else %}
#PURGE_EJECT_WAIT
SET_GCODE_VARIABLE MACRO=PURGE_EJECT VARIABLE=e_state VALUE=2 # closing
{% endif %}
SET_GCODE_VARIABLE MACRO=PURGE_EJECT VARIABLE=e_stroke VALUE={cfg.ejection_count}
_PURGE_EJECT_WAIT
[gcode_macro _PURGE_EJECT_WAIT]
description: Wait for the last ejection to complete.
gcode:
{% set cfg = printer['gcode_macro _PURGE_CFG'] %}
{% set mp = printer['gcode_macro PURGE_EJECT'] %}
{% if mp.e_state > 0 %}
{% set dwell_ms = cfg.servo_dwell %}
UPDATE_DELAYED_GCODE ID=purge_ejection DURATION=0.0 # cancel async.
G4 P{dwell_ms|int} # Because we don't know how long elapsed since
# the delayed gcode was scheduled last; in the worst case; we have
# doubled the total dwell time. Not a big deal in practice.
# Compute the number of steps to complete the state machine.
# (strokes left) * (Closing + Opening) + half-stroke (Opening) + Turnoff.
{% set steps = mp.e_stroke * 2 + (mp.e_state == 1)|int + 1 %}
# And drive the state machine to completion.
{% for _ in range(steps) %}
_PURGE_EJECTION_NEXT SYNC=1
{% endfor %}
{% endif %}
[gcode_macro _PURGE_EJECTION_NEXT]
# Move the state machine forward.
gcode:
{% set sync = params.SYNC|int %}
{% set cfg = printer['gcode_macro _PURGE_CFG'] %}
{% set mp = printer['gcode_macro PURGE_EJECT'] %}
{% set done = false %}
#{ action_respond_info("%s %s" % (mp.e_state, mp.e_stroke)) }
{% if mp.e_state == 1 %} # opening
# Opened, now we close.
SET_SERVO SERVO=eap_servo ANGLE=0
SET_GCODE_VARIABLE MACRO=PURGE_EJECT VARIABLE=e_state VALUE=2 # closing
{% elif mp.e_state == 2 %} # closing
{% if mp.e_stroke > 0 %}
# Closed, open again.
SET_SERVO SERVO=eap_servo ANGLE=180
SET_GCODE_VARIABLE MACRO=PURGE_EJECT VARIABLE=e_stroke VALUE={mp.e_stroke - 1}
SET_GCODE_VARIABLE MACRO=PURGE_EJECT VARIABLE=e_state VALUE=1 # opening
{% else %}
# Closed and done.
SET_SERVO SERVO=eap_servo WIDTH=0 # turn off servo
SET_GCODE_VARIABLE MACRO=PURGE_EJECT VARIABLE=e_state VALUE=0 # closed
{% set done = true %}
{% endif %}
{% else %} # unknown or closed
{ action_raise_error("invalid state %d" % mp.e_state) }
{% endif %}
{% if not done %}
{% if sync %}
G4 P{cfg.servo_dwell}
{% else %}
UPDATE_DELAYED_GCODE ID=purge_ejection DURATION={cfg.servo_dwell/1000}
{% endif %}
{% endif %}
[delayed_gcode purge_ejection]
gcode:
_PURGE_EJECTION_NEXT SYNC=0