-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathercf.py
More file actions
1421 lines (1170 loc) · 73.1 KB
/
ercf.py
File metadata and controls
1421 lines (1170 loc) · 73.1 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from . import pulse_counter
from . import force_move
import toolhead
import configparser
import logging
import ast
import time
from contextlib import contextmanager
from itertools import product
from numpy import median
class StopConditionException(Exception):
pass
class FilamentSlipException(StopConditionException):
pass
class FatalPrinterError(Exception):
pass
class EncoderCounter:
def __init__(self, printer, pin, sample_time, poll_time, encoder_steps):
self._last_time = self._last_count = None
self._counts = 0
self._encoder_steps = encoder_steps
self._counter = pulse_counter.MCU_counter(printer, pin, sample_time,
poll_time)
self._counter.setup_callback(self._counter_callback)
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
self._counts += count - self._last_count
else: # No counts since last sample
self._last_time = time
self._last_count = count
def get_counts(self):
return self._counts
def get_distance(self):
return (self._counts/2.) * self._encoder_steps
def set_encoder_steps(self, new_steps):
self._encoder_steps = new_steps
def set_distance(self, new_distance):
self._counts = int( ( new_distance / self._encoder_steps ) * 2. )
def reset_counts(self):
self._counts = 0.
class ERCF(object):
MACRO_PAUSE = 'ERCF_PAUSE'
def __init__(self, config):
self.config = config
self.printer = config.get_printer()
# Read config
self.gear_stepper = None
self.gear_stepper_name = config.get('gear_stepper')
self.selector_stepper = None
self.selector_stepper_name = config.get('selector_stepper')
self.servo_name = config.get('servo')
self.servo = None
self.toolhead_sensor_name = config.get('toolhead_sensor', None)
self.toolhead_sensor = None
self.encoder_pin_name = config.get('encoder_pin')
self.encoder_sample_time = config.getfloat('encoder_sample_time', 0.1,
above=0.)
self.encoder_poll_time = config.getfloat('encoder_poll_time', 0.0001,
above=0.)
self.long_moves_speed = config.getfloat('long_moves_speed', 100.)
self.long_moves_accel = config.getfloat('long_moves_accel', 400.)
self.short_moves_speed = config.getfloat('short_moves_speed', 25.)
self.short_moves_accel = config.getfloat('short_moves_accel', 400.)
self.gear_stepper_long_move_threshold = config.getfloat('gear_stepper_long_move_threshold', 70)
self.gear_stepper_accel = config.getfloat('gear_stepper_accel', 0)
self.extruder_move_speed = config.getfloat('extruder_move_speed', None)
self.extruder_move_accel = config.getfloat('extruder_move_accel', None)
# Step distance
self.extra_move_margin = config.getfloat('extra_move_margin', 100)
self.long_move_distance = config.getfloat('long_move_distance', 30)
self.short_move_distance = config.getfloat('short_move_distance', 10)
self.minimum_step_distance = config.getfloat('minimum_step_distance', 5)
self.maximum_move_distance = config.getfloat('maximum_move_distance', 1500)
self.maximum_step_distance = config.getfloat('maximum_step_distance', 1500)
self.calibrate_move_distance_per_step = config.getfloat('calibrate_move_distance_per_step', 3)
self.servo_up_angle = config.getfloat('servo_up_angle')
self.servo_down_angle = config.getfloat('servo_down_angle')
self.extra_servo_dwell_up = config.getfloat('extra_servo_dwell_up', 0)
self.extra_servo_dwell_down = config.getfloat('extra_servo_dwell_down', 0)
self.servo_down_turn_off = config.getboolean('servo_down_turn_off', True)
# Others
self.selector_filament_engagement_retry = config.getint('selector_filament_engagement_retry', 2)
self.auto_home_selector = config.getboolean('auto_home_selector', True)
self.tip_forming_gcode_before_calibration = config.get('tip_forming_gcode_before_calibration', None)
self.slip_detection_ratio_threshold = config.getint('slip_detection_ratio_threshold', 3) # If the actual distance is less than 1/3 then it is considered as slip
self.variable_path = config.get('variable_path')
self.all_variables = {}
self.load_variables()
self.motion_counter = EncoderCounter(self.printer, self.encoder_pin_name,
self.encoder_sample_time,
self.encoder_poll_time,
self.all_variables['calibrated_encoder_resolution'])
# GCode commands
self.gcode = self.printer.lookup_object('gcode')
self.gcode.register_command('_ERCF_SERVO_UP',
self.cmd_ERCF_SERVO_UP,
desc='Lift the servo arm to release the gear')
self.gcode.register_command('_ERCF_SERVO_DOWN',
self.cmd_ERCF_SERVO_DOWN,
desc='Press the servo arm to engage the gear')
self.gcode.register_command('_ERCF_LOAD',
self.cmd_ERCF_LOAD,
desc='Load the filament to the nozzle')
self.gcode.register_command('_ERCF_LOAD_FRESH',
self.cmd_ERCF_LOAD_FRESH,
desc='Load the filament to the nozzle')
self.gcode.register_command('_ERCF_UNLOAD',
self.cmd_ERCF_UNLOAD,
desc='Unload the filament back to the selector')
self.gcode.register_command('_ERCF_HOME_SELECTOR',
self.cmd_ERCF_HOME_SELECTOR,
desc='Home the selector cart')
self.gcode.register_command('_ERCF_MOVE_SELECTOR_TO_TOOL',
self.cmd_ERCF_MOVE_SELECTOR_TO_TOOL,
desc='Move the selector cart to the corresponding tool')
self.gcode.register_command('_ERCF_CHANGE_TOOL',
self.cmd_ERCF_CHANGE_TOOL,
desc='Tool change gcode')
self.gcode.register_command('_ERCF_MOTORS_OFF',
self.ercf_motors_off,
desc='Turn off both the gear and selector stepper')
# Calibration
self.gcode.register_command('_ERCF_CALIBRATE_ENCODER_RESOLUTION',
self.cmd_CALIBRATE_ENCODER_RESOLUTION,
desc='Calibrate the resolution of the encoder')
self.gcode.register_command('_ERCF_CALIBRATE_COMPONENT_LENGTH',
self.cmd_ERCF_CALIBRATE_COMPONENT_LENGTH,
desc='Execute the calibration routine on the current tool')
self.gcode.register_command('_ERCF_CALIBRATE_GEAR_STEPPER_ROTATION_DISTANCE',
self.cmd_CALIBRATE_GEAR_STEPPER_ROTATION_DISTANCE,
desc='Calibrate the rotation distance')
self.gcode.register_command('_ERCF_CALIBRATE_SELECTOR_LOCATION',
self.cmd_ERCF_CALIBRATE_SELECTOR_LOCATION,
desc='Locate the selector')
self.gcode.register_command('_ERCF_CALIBRATE_EXTRUSION_FACTOR',
self.cmd_ERCF_CALIBRATE_EXTRUSION_FACTOR,
desc='Calibrate the extrusion factor against the reference channel')
# Register event
self.printer.register_event_handler('klippy:connect', self.handle_connect)
self.printer.register_event_handler('stepper_enable:motor_off', self._on_motor_off)
def handle_connect(self):
self.toolhead = self.printer.lookup_object('toolhead')
self.gear_stepper = self.printer.lookup_object(self.gear_stepper_name)
self.selector_stepper = self.printer.lookup_object(self.selector_stepper_name)
self.servo = self.printer.lookup_object(self.servo_name)
if self.toolhead_sensor_name is not None:
self.toolhead_sensor = self.printer.lookup_object(self.toolhead_sensor_name)
self.reference_gear_stepper_rotation_distance = self.gear_stepper.get_steppers()[0].get_rotation_distance()[0]
self.original_extruder_move_speed = self.toolhead.get_extruder().max_e_velocity
self.original_extruder_move_accel = self.toolhead.get_extruder().max_e_accel
# Read extruder move speed and acceleration
if self.extruder_move_speed is None:
self.extruder_move_speed = self.original_extruder_move_speed
if self.extruder_move_accel is None:
self.extruder_move_accel = self.original_extruder_move_accel
# Initialize state machine status
self._servo_status = None
self._current_tool = None
def load_variables(self):
allvars = {}
varfile = configparser.ConfigParser()
try:
varfile.read(self.variable_path)
if varfile.has_section('Variables'):
for name, val in varfile.items('Variables'):
allvars[name] = ast.literal_eval(val)
except:
msg = "Unable to parse existing variable file"
logging.exception(msg)
raise self.printer.command_error(msg)
self.all_variables = allvars
def save_variables(self):
varfile = configparser.ConfigParser()
varfile.add_section('Variables')
for name, value in sorted(self.all_variables.items()):
varfile.set('Variables', name, repr(value))
try:
with open(self.variable_path, 'w') as fp:
varfile.write(fp)
except:
msg = "Unable to write to config file"
logging.exception(msg)
raise self.printer.command_error(msg)
def get_status(self, eventtime):
return {'current_tool': self._current_tool}
def _on_motor_off(self, print_time=None):
# Unset the current tool location when all motors are off
self._current_tool = None
@contextmanager
def _gear_stepper_move_guard(self, lift_servo=True):
try:
yield
finally:
if lift_servo:
self.servo_up()
@contextmanager
def _command_exception_handler(self, gcmd):
try:
yield
except FatalPrinterError as e:
idle_timeout = self.printer.lookup_object('idle_timeout')
if idle_timeout is None:
raise self.printer.config_error("No idle timeout found")
status = idle_timeout.get_status()
if status['state'] == 'Printing':
gcmd.respond_info('Caught exception: {}, Calling {}'.format(e, self.MACRO_PAUSE))
self.gcode.run_script_from_command(self.MACRO_PAUSE)
else:
raise self.printer.command_error(e)
def cmd_ERCF_SERVO_UP(self, gcmd):
with self._command_exception_handler(gcmd):
self.servo_up()
def cmd_ERCF_SERVO_DOWN(self, gcmd):
with self._command_exception_handler(gcmd):
self.servo_down()
def cmd_ERCF_CALIBRATE_COMPONENT_LENGTH(self, gcmd):
with self._command_exception_handler(gcmd):
self.calibrate_component_length(gcmd)
def cmd_ERCF_LOAD(self, gcmd):
with self._command_exception_handler(gcmd):
self.ercf_load_from_unknown_location(gcmd)
def cmd_ERCF_LOAD_FRESH(self, gcmd):
with self._command_exception_handler(gcmd):
self.ercf_load_fresh(gcmd)
def cmd_ERCF_UNLOAD(self, gcmd):
with self._command_exception_handler(gcmd):
self.ercf_unload(gcmd)
def cmd_ERCF_HOME_SELECTOR(self, gcmd):
with self._command_exception_handler(gcmd):
self.ercf_home_selector(gcmd)
def cmd_ERCF_MOVE_SELECTOR_TO_TOOL(self, gcmd):
with self._command_exception_handler(gcmd):
tool_idx = gcmd.get_int('TOOL')
self.ercf_move_selector_to_tool(gcmd, tool_idx)
def cmd_ERCF_CHANGE_TOOL(self, gcmd):
with self._command_exception_handler(gcmd):
tool_idx = gcmd.get_int('TOOL')
self.ercf_change_tool(gcmd, tool_idx)
def cmd_ERCF_CALIBRATE_SELECTOR_LOCATION(self, gcmd):
with self._command_exception_handler(gcmd):
tool_idx = gcmd.get_int('TOOL')
self.calibrate_selector_location(gcmd, tool_idx)
def cmd_ERCF_CALIBRATE_EXTRUSION_FACTOR(self, gcmd):
with self._command_exception_handler(gcmd):
tool_idx = gcmd.get_int('TOOL')
self.calibrate_extrusion_factor(gcmd, tool_idx)
def cmd_CALIBRATE_GEAR_STEPPER_ROTATION_DISTANCE(self, gcmd):
with self._command_exception_handler(gcmd):
self.calibrate_gear_stepper_rotation_distance(gcmd)
def cmd_CALIBRATE_ENCODER_RESOLUTION(self, gcmd):
with self._command_exception_handler(gcmd):
self.calibrate_encoder_resolution(gcmd)
def servo_up(self):
if self._servo_status != 'up':
self._servo_status = 'up'
servo_name = self.servo_name.split()[1]
self.gcode.run_script_from_command('SET_SERVO SERVO={} ANGLE={}'.format(servo_name,
self.servo_up_angle))
self.toolhead.dwell(0.25 + self.extra_servo_dwell_up)
# Turn servo off
self.gcode.run_script_from_command('SET_SERVO SERVO={} WIDTH=0.0'.format(servo_name))
self.toolhead.wait_moves()
def servo_down(self):
if self._servo_status != 'down':
self._servo_status = 'down'
servo_name = self.servo_name.split()[1]
# do the gear meshing to ensure the proper alignment of the selector gear
self.gear_stepper.do_set_position(0)
self.gear_stepper.do_move(0.5, speed=25, accel=self.gear_stepper_accel, sync=False)
self.gcode.run_script_from_command('SET_SERVO SERVO={} ANGLE={}'.format(servo_name,
self.servo_down_angle))
self.toolhead.wait_moves()
self.toolhead.dwell(0.2)
self.gear_stepper.do_move(0.0, speed=25, accel=self.gear_stepper_accel, sync=False)
self.toolhead.dwell(0.1)
self.gear_stepper.do_move(-0.5, speed=25, accel=self.gear_stepper_accel, sync=False)
self.toolhead.dwell(0.1 + self.extra_servo_dwell_down)
self.gear_stepper.do_move(0.0, speed=25, accel=self.gear_stepper_accel, sync=False)
self.toolhead.wait_moves()
# Turn off if required
if self.servo_down_turn_off:
self.gcode.run_script_from_command('SET_SERVO SERVO={} WIDTH=0.0'.format(servo_name))
self.toolhead.wait_moves()
def _gear_stepper_move_wait_legacy(self, dist, wait=True, speed=None, accel=None):
# LEGACY METHOD. Pending removal
self.gear_stepper.do_set_position(0.)
is_long_move = abs(dist) > self.gear_stepper_long_move_threshold
if speed is None:
speed = self.long_moves_speed if is_long_move \
else self.short_moves_speed
if accel is None:
accel = self.long_moves_accel if is_long_move \
else self.short_moves_accel
self.gear_stepper.do_move(dist, speed, accel, True)
if wait:
self.toolhead.wait_moves()
def gear_stepper_move_wait(self, gcmd, target_move_distance,
step_distance=None, step_speed=None, step_accel=None,
raise_on_filament_slip=True, expect_partial_move=False,
lift_servo=True):
self.servo_down()
with self._gear_stepper_move_guard(lift_servo):
accumulated_move_distance = self.stepper_move_wait(gcmd,
target_move_distance=target_move_distance,
step_distance=step_distance, step_speed=step_speed, step_accel=step_accel,
stepper_block_move_callback=self._gear_stepper_block_move,
raise_on_filament_slip=raise_on_filament_slip,
expect_partial_move=expect_partial_move)
return accumulated_move_distance
def toolhead_move_wait(self, gcmd, target_move_distance,
step_distance=None, step_speed=None,
initial_condition_callback=None, stop_condition_callback=None,
raise_on_filament_slip=True,
expect_partial_move=False):
self.servo_up()
accumulated_move_distance = self.stepper_move_wait(gcmd,
target_move_distance=target_move_distance,
step_distance=step_distance, step_speed=step_speed,
stepper_block_move_callback=self._toolhead_block_move,
stepper_init_callback=self._toolhead_move_init,
stepper_stop_callback=self._toolhead_move_stop,
initial_condition_callback=initial_condition_callback,
stop_condition_callback=stop_condition_callback,
raise_on_filament_slip=raise_on_filament_slip,
expect_partial_move=expect_partial_move)
return accumulated_move_distance
def stepper_move_wait(self, gcmd, target_move_distance,
stepper_block_move_callback,
stepper_init_callback=None,
stepper_stop_callback=None,
step_distance=None, step_speed=None, step_accel=None,
initial_condition_callback=None, stop_condition_callback=None,
raise_on_filament_slip=True,
expect_partial_move=False):
if stop_condition_callback is None:
stop_condition_callback = lambda x=None: x
if initial_condition_callback is None:
initial_condition_callback = lambda x=None: x
if step_distance is None:
step_distance = min(self.long_move_distance, abs(target_move_distance))
# Calculate maximum single step distance based on the user config to avoid `Timer too close` issue.
step_distance = min(step_distance, self.maximum_step_distance)
if target_move_distance >= 0:
direction = 1
else:
direction = -1
if stepper_init_callback is None:
stepper_init_callback = lambda x=None:x
stepper_status = stepper_init_callback()
self.motion_counter.reset_counts()
accumulated_move_distance = 0
gcmd.respond_info('Requested stepper move distance: {} with step length: {}, accel: {}'.format(
target_move_distance, step_distance, step_accel))
try:
prev_state = initial_condition_callback()
remain_distance = abs(target_move_distance) - accumulated_move_distance
while remain_distance > 0:
# Calculate step
step_distance = max(min(step_distance, remain_distance), self.minimum_step_distance)
relative_step_distance = step_distance * direction
# Update default step speed and acceleration
if step_distance >= self.long_move_distance:
if step_speed is None:
step_speed = self.long_moves_speed
if step_accel is None:
step_accel = self.long_moves_accel
else:
if step_speed is None:
step_speed = self.short_moves_speed
if step_accel is None:
step_accel = self.short_moves_accel
# Move
self.motion_counter.reset_counts()
stepper_status = stepper_block_move_callback(stepper_status, relative_step_distance, step_speed, step_accel)
# Check move distance
filament_move_distance = self.motion_counter.get_distance()
accumulated_move_distance += filament_move_distance
remain_distance = abs(target_move_distance) - accumulated_move_distance
if filament_move_distance < (step_distance / self.slip_detection_ratio_threshold):
msg = 'Filament is not moving. Requested: {}, filament measured move: {}'.format(step_distance, filament_move_distance)
raise FilamentSlipException(msg)
# Check stop condition
prev_state = stop_condition_callback(prev_state)
else:
# NO event
if expect_partial_move:
raise self.printer.command_error('Full move is not expected. Actual moved distance: {}. Please check the calibration.'.format(accumulated_move_distance))
except FilamentSlipException as e:
msg = str(e)
if raise_on_filament_slip:
raise self.printer.command_error(msg)
else:
gcmd.respond_info(msg)
except StopConditionException:
pass
finally:
if stepper_stop_callback:
stepper_stop_callback(stepper_status)
gcmd.respond_info('Actual stepper move distance: {}'.format(accumulated_move_distance * direction))
return accumulated_move_distance * direction
def _toolhead_block_move(self, toolhead_position, relative_step_distance, speed, accel):
toolhead_position[3] += relative_step_distance
self.toolhead.manual_move(toolhead_position, speed)
self.toolhead.wait_moves()
return toolhead_position
def _toolhead_move_init(self):
self.gcode.run_script_from_command('G92 E0')
toolhead_position = self.toolhead.get_position()
# Set acceleration
extruder = self.toolhead.get_extruder()
extruder.max_e_velocity = self.extruder_move_speed
extruder.max_e_accel = self.extruder_move_accel
return toolhead_position
def _toolhead_move_stop(self, stepper_status):
# Restore speed and acceleration
extruder = self.toolhead.get_extruder()
extruder.max_e_velocity = self.original_extruder_move_speed
extruder.max_e_accel = self.original_extruder_move_accel
def _gear_stepper_block_move(self, stepper_status, relative_step_distance, speed, accel):
self.gear_stepper.do_set_position(0)
self.gear_stepper.do_move(relative_step_distance, speed, accel, sync=True)
self.toolhead.wait_moves() # WHY TOOLHEAD??
return None
def _toolhead_gear_stepper_synchronized_block_move(self, toolhead_position, relative_step_distance, speed, accel):
# Setup the gear stepper first
self.gear_stepper.do_set_position(0)
self.gear_stepper.do_move(relative_step_distance, speed, self.toolhead.max_accel, sync=False) # Do not use acceleration control in synchronous move
toolhead_position[3] += relative_step_distance
self.toolhead.manual_move(toolhead_position, speed)
self.toolhead.wait_moves()
return toolhead_position
def _stop_on_filament_present(self, prev_condition=None):
"""
The callback function to raise the exception when the filament sensor is triggered
"""
if self.toolhead_sensor.runout_helper.filament_present:
raise StopConditionException
return prev_condition
def ercf_unload_to_toolhead_sensor(self, gcmd):
if not self.toolhead_sensor:
raise self.printer.command_error('Filament sensor is not defined')
nozzle_to_sensor_length = self.all_variables.get('calibrated_nozzle_to_sensor_length')
if nozzle_to_sensor_length is None:
raise self.printer.command_error('Sensor before extruder setup is currently not supported')
def stop_on_filament_not_present(prev_condition=None):
if not self.toolhead_sensor.runout_helper.filament_present:
raise StopConditionException
return prev_condition
nozzle_to_sensor_length = self.all_variables.get('calibrated_nozzle_to_sensor_length')
accumulated_move_distance = self.toolhead_move_wait(gcmd, -nozzle_to_sensor_length - self.extra_move_margin, self.short_move_distance,
raise_on_filament_slip=True,
initial_condition_callback=stop_on_filament_not_present,
stop_condition_callback=stop_on_filament_not_present)
gcmd.respond_info('The filament tip is now parked right before the filament sensor. The total move distance: {}'
.format(accumulated_move_distance))
return accumulated_move_distance
def ercf_load_from_toolhead_sensor(self, gcmd):
if not self.toolhead_sensor:
raise self.printer.command_error('Filament sensor is not defined')
# Extrude until the toolhead sensor (should be relative short)
nozzle_to_sensor_length = self.all_variables.get('calibrated_nozzle_to_sensor_length')
accumulated_move_distance = self.toolhead_move_wait(gcmd,
target_move_distance=nozzle_to_sensor_length,
step_distance=self.minimum_step_distance,
step_speed=self.short_moves_speed,
initial_condition_callback=self._stop_on_filament_present,
stop_condition_callback=self._stop_on_filament_present,
expect_partial_move=True)
# Extrude to the toolhead (without feedback)
nozzle_to_sensor_length = self.all_variables.get('calibrated_nozzle_to_sensor_length')
accumulated_move_distance += self.toolhead_move_wait(gcmd,
target_move_distance=nozzle_to_sensor_length,
step_distance=nozzle_to_sensor_length,
step_speed=self.short_moves_speed,
raise_on_filament_slip=False)
gcmd.respond_info('The filament is loaded to the nozzle. The total move distance: {}'.format(accumulated_move_distance))
return accumulated_move_distance
def ercf_unload_from_toolhead_sensor_to_extruder(self, gcmd):
# First stage is to unload the filament to a proximate position with long move distance and speed
retract_distance = self.all_variables.get('calibrated_sensor_to_extruder_length') - self.long_move_distance
accumulated_move_distance = self.toolhead_move_wait(gcmd, -retract_distance, raise_on_filament_slip=False,)
gcmd.respond_info(
'The filament unloaded just passed the extruder. The total move distance: {}'.format(accumulated_move_distance))
return accumulated_move_distance
def ercf_unload_from_extruder_to_selector(self, gcmd):
accumulated_move_distance = 0
gcmd.respond_info('Unloading from extruder to the selector')
with self._gear_stepper_move_guard():
self.servo_down()
# Move a little by both toolhead and gear stepper to help pulling from the extruder
target_move_distance = self.short_move_distance
actual_move_distance = self.stepper_move_wait(gcmd,
target_move_distance=-target_move_distance,
stepper_block_move_callback=self._toolhead_gear_stepper_synchronized_block_move,
stepper_init_callback=self._toolhead_move_init,
stepper_stop_callback=self._toolhead_move_stop,
step_distance=self.short_move_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=True)
accumulated_move_distance += actual_move_distance
# Pull purely by the gear stepper
target_move_distance = max(0, self.all_variables['calibrated_extruder_to_selector_length'] - abs(actual_move_distance) - self.long_move_distance)
actual_move_distance = self.gear_stepper_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=target_move_distance,
step_speed=self.long_moves_speed,
step_accel=self.long_moves_accel,
raise_on_filament_slip=True, lift_servo=False)
accumulated_move_distance += actual_move_distance
# Retract back to the selector with short moves
target_move_distance = max(0, self.all_variables['calibrated_extruder_to_selector_length'] - abs(actual_move_distance)) + self.long_move_distance + self.extra_move_margin
actual_move_distance = self.gear_stepper_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=self.short_move_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=False, expect_partial_move=True,
lift_servo=False)
accumulated_move_distance += actual_move_distance
# Pull back a little
target_move_distance = 7
actual_move_distance = self.gear_stepper_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=self.short_move_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=False, lift_servo=False)
accumulated_move_distance += actual_move_distance
self.servo_up()
return accumulated_move_distance
def ercf_unload(self, gcmd):
if not self.toolhead_sensor:
raise self.printer.command_error('Filament sensor is not defined')
nozzle_to_sensor_length = self.all_variables.get('calibrated_nozzle_to_sensor_length')
if nozzle_to_sensor_length is None:
raise self.printer.command_error('Sensor before extruder setup is currently not supported')
# Lift the servo before attempting to move the toolhead
self.servo_up()
accumulated_move_distance = 0
if self.toolhead_sensor.runout_helper.filament_present:
# TODO: Make it a function instead of running as the macro
self.gcode.run_script_from_command('_ERCF_FORM_TIP_STANDALONE')
gcmd.respond_info('Unloading from nozzle to toolhead sensor')
self.ercf_unload_to_toolhead_sensor(gcmd)
# This is the clean retraction
gcmd.respond_info('Unloading from toolhead sensor to extruder')
target_move_distance = self.all_variables['calibrated_sensor_to_extruder_length']
actual_move_distance = self.toolhead_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=target_move_distance,
step_speed=self.long_moves_speed,
raise_on_filament_slip=False)
accumulated_move_distance += actual_move_distance
# Move a little to disengage with the extruder
target_move_distance = max(0, self.all_variables['calibrated_sensor_to_extruder_length'] - abs(actual_move_distance)) + self.long_move_distance
actual_move_distance = self.toolhead_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=self.short_move_distance,
step_speed=self.short_moves_speed,
raise_on_filament_slip=False,
expect_partial_move=True)
accumulated_move_distance += actual_move_distance
accumulated_move_distance += self.ercf_unload_from_extruder_to_selector(gcmd)
else:
gcmd.respond_info('Unloading from unknown location')
# Do a short move to verify the filament is still engaged with the extruder
actual_move_distance = self.toolhead_move_wait(gcmd,
target_move_distance=-self.short_move_distance,
step_distance=self.short_move_distance,
step_speed=self.short_move_distance,
raise_on_filament_slip=False)
accumulated_move_distance += actual_move_distance
if actual_move_distance != 0:
# Do the short move until not moving anymore
target_move_distance = self.all_variables['calibrated_sensor_to_extruder_length'] + self.long_move_distance
actual_move_distance = self.toolhead_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=self.short_move_distance,
step_speed=self.short_move_distance,
expect_partial_move=True,
raise_on_filament_slip=False)
accumulated_move_distance += actual_move_distance
accumulated_move_distance += self.ercf_unload_from_extruder_to_selector(gcmd)
else:
# Between extruder and the selector
with self._gear_stepper_move_guard():
self.servo_down()
# Move a little by both toolhead and gear stepper to help pulling from the extruder
target_move_distance = self.short_move_distance
actual_move_distance = self.stepper_move_wait(gcmd,
target_move_distance=-target_move_distance,
stepper_block_move_callback=self._toolhead_gear_stepper_synchronized_block_move,
stepper_init_callback=self._toolhead_move_init,
stepper_stop_callback=self._toolhead_move_stop,
step_distance=self.short_move_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=True)
accumulated_move_distance += actual_move_distance
# Move slowly until the end of the selector
target_move_distance = self.all_variables['calibrated_extruder_to_selector_length'] + self.long_move_distance + self.extra_move_margin
actual_move_distance = self.gear_stepper_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=self.short_move_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=False,
expect_partial_move=True,
lift_servo=False)
accumulated_move_distance += actual_move_distance
# Pull back a little
target_move_distance = 5
actual_move_distance = self.gear_stepper_move_wait(gcmd,
target_move_distance=-target_move_distance,
step_distance=self.short_move_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=False, lift_servo=False)
accumulated_move_distance += actual_move_distance
self.servo_up()
gcmd.respond_info('Filament is unloaded to the selector')
def ercf_load_from_unknown_location(self, gcmd):
if not self.toolhead_sensor:
raise self.printer.command_error('Filament sensor is not defined')
nozzle_to_sensor_length = self.all_variables.get('calibrated_nozzle_to_sensor_length')
if nozzle_to_sensor_length is None:
raise self.printer.command_error('Sensor before extruder setup is currently not supported')
if self.toolhead_sensor.runout_helper.filament_present:
self.ercf_load_from_toolhead_sensor(gcmd)
else:
with self._gear_stepper_move_guard():
# Check if the filament is engaged inside the selector
gcmd.respond_info('Check filament engagement inside selector')
test_move_distance = 0
for i in range(self.selector_filament_engagement_retry):
self.servo_down()
test_move_distance += self.stepper_move_wait(gcmd,
target_move_distance=30,
stepper_block_move_callback=self._toolhead_gear_stepper_synchronized_block_move,
stepper_init_callback=self._toolhead_move_init,
stepper_stop_callback=self._toolhead_move_stop,
step_distance=30,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=False)
if test_move_distance > 0:
break
else:
self.servo_up()
gcmd.respond_info('Filament is not engaged. Will retry')
else:
raise self.printer.command_error('Filament is not engaged inside the selector. Please insert the filament manually')
# Load with long step until the filament sensor has triggered
accumulated_step_distance = test_move_distance
target_distance = self.all_variables['calibrated_extruder_to_selector_length'] + self.all_variables['calibrated_sensor_to_extruder_length']
accumulated_step_distance += self.stepper_move_wait(gcmd,
target_move_distance=target_distance,
stepper_block_move_callback=self._toolhead_gear_stepper_synchronized_block_move,
stepper_init_callback=self._toolhead_move_init,
stepper_stop_callback=self._toolhead_move_stop,
step_distance=self.long_move_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
stop_condition_callback=self._stop_on_filament_present,
raise_on_filament_slip=True)
# When the filament sensor has triggered, then load from now on
self.ercf_load_from_unknown_location(gcmd)
def ercf_load_fresh(self, gcmd):
if not self.toolhead_sensor:
raise self.printer.command_error('Filament sensor is not defined')
nozzle_to_sensor_length = self.all_variables.get('calibrated_nozzle_to_sensor_length')
if nozzle_to_sensor_length is None:
raise self.printer.command_error('Sensor before extruder setup is currently not supported')
if self.toolhead_sensor.runout_helper.filament_present:
raise self.printer.command_error('Call ercf_load_from_unknown_location instead')
# Check if the filament is already loaded somehow. If the filament is already partially loaded then run the
# unload to start from clean state
if self.is_filament_in_selector(lift_servo=False, skip_filament_block_check=True):
self.ercf_unload(gcmd)
accumulated_step_distance = 0
with self._gear_stepper_move_guard():
# Check gear engagement
test_move_distance = 0
for i in range(self.selector_filament_engagement_retry):
self.servo_down()
test_move_distance += self.gear_stepper_move_wait(gcmd,
target_move_distance=30,
step_distance=30,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
raise_on_filament_slip=False,
lift_servo=False)
if test_move_distance > 0:
break
else:
self.servo_up()
gcmd.respond_info('Filament is not engaged. Will retry')
else:
raise self.printer.command_error(
'Filament is not engaged inside the selector. Please insert the filament manually')
# Do a single move to feed the filament to the extruder
gcmd.respond_info('Feeding from selector to just before the extruder -- long single move with just gear stepper')
accumulated_step_distance = test_move_distance
target_distance = max(0, self.all_variables['calibrated_extruder_to_selector_length'] - test_move_distance - self.long_move_distance)
actual_distance = self.gear_stepper_move_wait(gcmd,
target_move_distance=target_distance,
step_distance=target_distance,
raise_on_filament_slip=True,
lift_servo=False)
accumulated_step_distance += actual_distance
# Feed to the extruder
gcmd.respond_info('Feeding to the extruder -- short pulse move using both extruders and stop on filament slip')
target_distance = max(0, self.all_variables['calibrated_extruder_to_selector_length'] - actual_distance) + self.long_move_distance
actual_distance = self.stepper_move_wait(gcmd,
target_move_distance=target_distance,
step_distance=target_distance,
step_speed=self.short_moves_speed,
step_accel=self.short_moves_accel,
stepper_block_move_callback=self._toolhead_gear_stepper_synchronized_block_move,
stepper_init_callback=self._toolhead_move_init,
stepper_stop_callback=self._toolhead_move_stop,
raise_on_filament_slip=True)
accumulated_step_distance += actual_distance
# Release the gear stepper and move to next
self.servo_up()
# Move the toolhead single long move approaching the sensor
gcmd.respond_info('Feeding from the extruder to just before the sensor -- long single move with just the extruder, stop on filament sensor trigger')
target_distance = max(0, self.all_variables['calibrated_sensor_to_extruder_length'] - actual_distance - self.long_move_distance)
if target_distance < self.long_move_distance:
step_distance = self.short_move_distance
else:
step_distance = target_distance
actual_distance = self.toolhead_move_wait(gcmd,
target_move_distance=target_distance,
step_distance=step_distance,
initial_condition_callback=self._toolhead_move_init,
stop_condition_callback=self._stop_on_filament_present,
raise_on_filament_slip=True)
accumulated_step_distance += actual_distance
# Since we are closed to the toolhead sensor, we want do move slowly
gcmd.respond_info('Feeding to the sensor -- short pulse move and stop on filament sensor trigger')
target_distance = max(0, self.all_variables['calibrated_sensor_to_extruder_length'] - actual_distance) + self.long_move_distance
accumulated_step_distance += self.toolhead_move_wait(gcmd,
target_move_distance=target_distance,
step_distance=self.minimum_step_distance,
step_speed=self.short_moves_speed,
raise_on_filament_slip=True,
initial_condition_callback=self._toolhead_move_init,
stop_condition_callback=self._stop_on_filament_present)
# we are on the filament sensor, move the final distance
self.ercf_load_from_toolhead_sensor(gcmd)
def is_filament_in_selector(self, lift_servo=True, skip_filament_block_check=False):
with self._gear_stepper_move_guard(lift_servo):
self.motion_counter.reset_counts()
self.servo_down()
move_distance = self.motion_counter.get_distance()
# If the filament is moving while gear meshing then the filament is certainly inserted
if move_distance > 0:
return True
# Secondary check (tiny moving distance)
self.gear_stepper.do_set_position(0)
self.motion_counter.reset_counts()
self.gear_stepper.do_move(1, self.short_moves_speed, self.short_moves_accel, True)
self.gear_stepper.do_move(0, self.short_moves_speed, self.short_moves_accel, True)
self.toolhead.wait_moves()
move_distance = self.motion_counter.get_distance()
if move_distance > 0:
return True
# No need to check further, we can assume the filament is not present
if skip_filament_block_check:
return False
# Third check (move 8mm forward to check if the filament is just extruded from the filament block)
self.gear_stepper.do_set_position(0)
self.gear_stepper.do_move(8, self.short_moves_speed, self.short_moves_accel, True)
self.gear_stepper.do_move(0, self.short_moves_speed, self.short_moves_accel, True)
self.toolhead.wait_moves()
move_distance = self.motion_counter.get_distance()
if move_distance > 0:
return True
return False
def ercf_home_selector(self, gcmd):
num_channels = len(self.all_variables['color_selector_positions'])
homing_move_distance = 20 + num_channels * 21 + (num_channels/3.0) * 5
# Check the filament status
if self.is_filament_in_selector():
gcmd.respond_info('Filament is still in the selector cart. Will do the unload')
# There is chance this will fail as the filament already retracted in the block but require 1-3mm retraction
try:
self.ercf_unload(gcmd)
except Exception as e:
# Check if the filament is in the selector again. If clear then we should ignore the unload error
if self.is_filament_in_selector():
# Not good. Need some help from the user
gcmd.respond_info('Unable to clear the filament from the selector. Requires user attention')
raise
else:
gcmd.respond_info('Filament is clear from the selector. Will continue homing')
# TODO: Implement the sensorless homing
self.selector_stepper.do_set_position(0)
self.selector_stepper.do_homing_move(movepos=-homing_move_distance,
speed=100,
accel=self.short_moves_accel,
triggered=True,
check_trigger=True)
self.selector_stepper.do_set_position(0)
# Retract and do second homing
self.selector_stepper.do_move(movepos=5,
speed=100,
accel=self.short_moves_accel)
self.selector_stepper.do_homing_move(movepos=-10,
speed=20,
accel=self.short_moves_accel,
triggered=True,
check_trigger=True)
self.selector_stepper.do_set_position(0)
# Unset the selector
self._current_tool = None
self.ercf_move_selector_to_tool(gcmd, tool_idx=0, force=True)
gcmd.respond_info('Selector homed')
def ercf_move_selector_to_tool(self, gcmd, tool_idx, force=False):
color_selector_positions = self.all_variables['color_selector_positions']
if tool_idx >= len(color_selector_positions):
raise self.printer.command_error('Invalid tool index: {}'.format(tool_idx))
if self._current_tool != tool_idx:
if not force and self._current_tool is None:
if self.auto_home_selector:
self.ercf_home_selector(gcmd)
else:
raise self.printer.command_error('Selector must be homed before switching to the next tool')