forked from moggieuk/Happy-Hare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmu_encoder.py
304 lines (261 loc) · 14 KB
/
mmu_encoder.py
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
# Happy Hare MMU Software
# Driver for encoder that supports movement measurement, runout/clog detection and flow rate calc
#
# Copyright (C) 2022 moggieuk#6538 (discord)
#
# Based on:
# Original Enraged Rabbit Carrot Feeder Project Copyright (C) 2021 Ette
# Generic Filament Sensor Module Copyright (C) 2019 Eric Callahan <[email protected]>
# Filament Motion Sensor Module Copyright (C) 2021 Joshua Wherrett <[email protected]>
#
# (\_/)
# ( *,*)
# (")_(") Happy Hare Ready
#
# This file may be distributed under the terms of the GNU GPLv3 license.
#
import logging, time
from . import pulse_counter
class MmuEncoder:
CHECK_MOVEMENT_TIMEOUT = 0.250
RUNOUT_DISABLED = 0
RUNOUT_STATIC = 1
RUNOUT_AUTOMATIC = 2
def __init__(self, config):
self.name = config.get_name().split()[-1]
self.printer = config.get_printer()
self.reactor = self.printer.get_reactor()
self.gcode = self.printer.lookup_object('gcode')
encoder_pin = config.get('encoder_pin')
self._logger = None
# For counter functionality
self.sample_time = config.getfloat('sample_time', 0.1, above=0.)
self.poll_time = config.getfloat('poll_time', 0.001, above=0.)
self.set_resolution(config.getfloat('encoder_resolution', 1., above=0.)) # Must be calibrated by user in Happy Hare
self._last_time = None
self._counts = self._last_count = 0
self._counter = pulse_counter.MCU_counter(self.printer, encoder_pin, self.sample_time, self.poll_time)
self._counter.setup_callback(self._counter_callback)
self._movement = False
# For clog/runout functionality
self.extruder_name = config.get('extruder', 'extruder')
# The runout headroom that MMU will attempt to maintain (closest MMU comes to triggering runout)
self.desired_headroom = config.getfloat('desired_headroom', 6., above=0.)
# The "damping" effect of last measurement. Higher value means clog_length will be reduced more slowly
self.average_samples = config.getint('average_samples', 4, minval=1)
# The extrusion interval where new detection_length is calculated (also done on toolchange)
self.next_calibration_point = self.calibration_length = config.getfloat('calibration_length', 10000., minval=50.) # 10m
# Detection length will be set by MMU calibration
self.detection_length = self.min_headroom = config.getfloat('detection_length', 10., above=2.)
self.event_delay = config.getfloat('event_delay', 3., above=0.)
gcode_macro = self.printer.load_object(config, 'gcode_macro')
self.runout_gcode = gcode_macro.load_template(config, 'runout_gcode', '__MMU_ENCODER_RUNOUT')
self.insert_gcode = gcode_macro.load_template(config, 'insert_gcode', '__MMU_ENCODER_INSERT')
self._enabled = True # Runout/Clog functionality
self.min_event_systime = self.reactor.NEVER
self.extruder = self.estimated_print_time = None
self.filament_detected = False
self.detection_mode = self.RUNOUT_STATIC
self.last_extruder_pos = self.filament_runout_pos = 0.
# For flowrate functionality
self.flowrate_last_encoder_pos = 0.
self.extrusion_flowrate = 0.
self.samples = []
self.flowrate_samples = config.getint('flowrate_samples', 20, minval=5)
# Register event handlers
self.printer.register_event_handler('klippy:ready', self._handle_ready)
self.printer.register_event_handler('klippy:connect', self._handle_connect)
self.printer.register_event_handler('idle_timeout:printing', self._handle_printing)
self.printer.register_event_handler('idle_timeout:ready', self._handle_not_printing)
self.printer.register_event_handler('idle_timeout:idle', self._handle_not_printing)
def _handle_connect(self):
try:
self.extruder = self.printer.lookup_object(self.extruder_name)
except Exception:
# Can set this later
pass
self.filament_runout_pos = self.min_headroom = self.detection_length
def _handle_ready(self):
self.min_event_systime = self.reactor.monotonic() + 2. # Don't process events too early
self.estimated_print_time = self.printer.lookup_object('mcu').estimated_print_time
self._reset_filament_runout_params()
self._extruder_pos_update_timer = self.reactor.register_timer(self._extruder_pos_update_event)
def _handle_printing(self, print_time):
self.reactor.update_timer(self._extruder_pos_update_timer, self.reactor.NOW) # Enabled
def _handle_not_printing(self, print_time):
self.reactor.update_timer(self._extruder_pos_update_timer, self.reactor.NEVER) # Disabled
def _get_extruder_pos(self, eventtime=None):
if eventtime is None:
eventtime = self.reactor.monotonic()
print_time = self.estimated_print_time(eventtime)
if self.extruder:
return self.extruder.find_past_position(print_time)
else:
return 0.
# Called periodically to check filament movement
def _extruder_pos_update_event(self, eventtime):
if self._enabled:
extruder_pos = self._get_extruder_pos(eventtime)
# First lets see if we got encoder movement since last invocation
if self._movement:
self._movement = False
self.filament_runout_pos = max(extruder_pos + self.detection_length, self.filament_runout_pos)
if extruder_pos >= self.next_calibration_point:
if self.next_calibration_point > 0:
self._update_detection_length()
self.next_calibration_point = extruder_pos + self.calibration_length
if self.filament_runout_pos - extruder_pos < self.min_headroom:
self.min_headroom = self.filament_runout_pos - extruder_pos
if self._logger and self.min_headroom < self.desired_headroom:
if self.detection_mode == self.RUNOUT_AUTOMATIC:
self._logger("Automatic clog detection: new min_headroom (< %.1fmm desired): %.1fmm" % (self.desired_headroom, self.min_headroom))
elif self.detection_mode == self.RUNOUT_STATIC:
self._logger("Warning: Only %.1fmm of headroom to clog/runout" % self.min_headroom)
self._handle_filament_event(extruder_pos < self.filament_runout_pos)
# Flowrate calc. Depends of calibration accuracy of encoder
encoder_pos = self.get_distance()
# If encoder has moved, record the extruder and encoder movement for flow rate calcs
if encoder_pos > self.flowrate_last_encoder_pos:
self._record(encoder_pos, extruder_pos)
self.flowrate_last_encoder_pos = encoder_pos
self.last_extruder_pos = extruder_pos
return eventtime + self.CHECK_MOVEMENT_TIMEOUT
def _reset_filament_runout_params(self, eventtime=None):
if eventtime is None:
eventtime = self.reactor.monotonic()
self.last_extruder_pos = self._get_extruder_pos(eventtime)
self.flowrate_last_encoder_pos = self.get_distance()
self.extrusion_flowrate = 0.
self.samples = []
self.filament_runout_pos = self.last_extruder_pos + self.detection_length + self.desired_headroom # Add headroom to decrease sensitivity on startup
self.next_calibration_point = self.last_extruder_pos + self.calibration_length
self.min_headroom = self.detection_length
# Called periodically to tune the clog detection length
def _update_detection_length(self, increase_only=False):
if not self._enabled: return
if self.detection_mode != self.RUNOUT_AUTOMATIC:
return
current_detection_length = self.detection_length
if self.min_headroom < self.desired_headroom:
# Maintain headroom
extra_length = min((self.desired_headroom - self.min_headroom), self.desired_headroom)
self.detection_length += extra_length
if self._logger:
self._logger("Automatic clog detection: maintaining headroom by adding %.1fmm to detection_length" % extra_length)
elif not increase_only:
# Average down
sample = self.detection_length - (self.min_headroom - self.desired_headroom)
self.detection_length = ((self.average_samples * self.detection_length) + self.desired_headroom - self.min_headroom) / self.average_samples
if self._logger:
self._logger("Automatic clog detection: averaging down detection_length with new %.1fmm measurement" % sample)
else:
return
self.min_headroom = self.detection_length
self.filament_runout_pos = self.last_extruder_pos + self.detection_length
if round(self.detection_length, 1) != round(current_detection_length, 1): # Persist if significant
if self._logger:
self._logger("Automatic clog detection: reset detection_length to %.1fmm" % self.min_headroom)
self.set_clog_detection_length(self.detection_length)
# Called to see if state update requires callback notification
def _handle_filament_event(self, filament_detected):
if self.filament_detected == filament_detected:
return
self.filament_detected = filament_detected
eventtime = self.reactor.monotonic()
if eventtime < self.min_event_systime or self.detection_mode == self.RUNOUT_DISABLED or not self._enabled:
return
is_printing = self.printer.lookup_object("idle_timeout").get_status(eventtime)["state"] == "Printing"
if filament_detected:
if not is_printing and self.insert_gcode is not None:
# Insert detected
self.min_event_systime = self.reactor.NEVER
logging.info("Encoder Sensor %s: insert event detected, Time %.2f" % (self.name, eventtime))
self.reactor.register_callback(self._insert_event_handler)
else:
if is_printing and self.runout_gcode is not None:
# Runout detected
self.min_event_systime = self.reactor.NEVER
logging.info("Encoder Sensor %s: runout event detected, Time %.2f" % (self.name, eventtime))
self.reactor.register_callback(self._runout_event_handler)
def _runout_event_handler(self, eventtime):
self._exec_gcode(self.runout_gcode)
def _insert_event_handler(self, eventtime):
self._exec_gcode(self.insert_gcode)
def _exec_gcode(self, template):
try:
self.gcode.run_script(template.render())
except Exception:
logging.exception("Script running error")
self.min_event_systime = self.reactor.monotonic() + self.event_delay
def get_clog_detection_length(self):
return self.detection_length
def set_clog_detection_length(self, clog_length):
clog_length = max(clog_length, 2.)
self.detection_length = clog_length
self._reset_filament_runout_params()
def update_clog_detection_length(self):
self._update_detection_length()
def set_mode(self, mode):
if mode >= self.RUNOUT_DISABLED and mode <= self.RUNOUT_AUTOMATIC:
self.detection_mode = mode
def set_extruder(self, extruder_name):
self.extruder = self.printer.lookup_object(extruder_name)
if not self.extruder:
raise self.printer.config.error("Extruder named `%s` not found" % extruder_name)
self.extruder_name = extruder_name
self.filament_runout_pos = self.min_headroom = self.detection_length
def set_logger(self, log):
self._logger = log
def enable(self):
self._reset_filament_runout_params()
self._enabled = True
def disable(self):
self._enabled = False
def is_enabled(self):
return self._enabled
def _record(self, encoder_pos, extruder_pos):
self.samples.append((encoder_pos, extruder_pos))
if len(self.samples) > self.flowrate_samples:
self.samples = self.samples[-self.flowrate_samples:]
encoder_movement = encoder_pos - self.samples[0][0]
extruder_movement = extruder_pos - self.samples[0][1]
new_extrusion_flowrate = (encoder_movement / extruder_movement) if extruder_movement > 0. else 1.
self.extrusion_flowrate = (self.extrusion_flowrate + new_extrusion_flowrate) / 2.
# Callback for MCU_counter
def _counter_callback(self, time, count, count_time):
if self._last_time is None: # First sample
self._last_time = time
elif count_time > self._last_time:
self._last_time = count_time
new_counts = count - self._last_count
self._counts += new_counts
self._movement = new_counts > 0
else: # No counts since last sample
self._last_time = time
self._last_count = count
def set_resolution(self, resolution):
self.resolution = resolution
def get_resolution(self):
return self.resolution
def get_counts(self):
return self._counts
def get_distance(self):
return self._counts * self.resolution
def set_distance(self, new_distance):
self._counts = int(round(new_distance / self.resolution))
def reset_counts(self):
self._counts = 0
def get_status(self, eventtime):
return {
'encoder_pos': round(self.get_distance(), 1),
'detection_length': round(self.detection_length, 1),
'min_headroom': round(self.min_headroom, 1),
'headroom': round(self.filament_runout_pos - self.last_extruder_pos, 1),
'desired_headroom': round(self.desired_headroom, 1),
'detection_mode': self.detection_mode,
'enabled': self._enabled,
'flow_rate': int(round(min(self.extrusion_flowrate, 1.) * 100))
}
def load_config_prefix(config):
return MmuEncoder(config)