forked from prusa3d/MM-control-01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion.cpp
770 lines (627 loc) · 16.3 KB
/
motion.cpp
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
#include "motion.h"
#include "shr16.h"
#include "tmc2130.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <Arduino.h>
#include "main.h"
#include "mmctl.h"
#include "Buttons.h"
const int selector_steps_after_homing = -3700;
const int idler_steps_after_homing = -130;
const int selector_steps = 2790/4;
const int idler_steps = 1420 / 4; // 2 msteps = 180 / 4
const int idler_parking_steps = (idler_steps / 2) + 40; // 40
const int bowden_length = 1000;
// endstop to tube - 30 mm, 550 steps
int selector_steps_for_eject = 0;
int idler_steps_for_eject = 0;
int8_t filament_type[EXTRUDERS] = {-1, -1, -1, -1, -1};
int set_idler_direction(int _steps);
int set_selector_direction(int _steps);
int set_pulley_direction(int _steps);
void cut_filament();
void park_idler(bool _unpark);
void load_filament_inPrinter();
void load_filament_withSensor();
void do_pulley_step();
void do_idler_step();
void set_positions(int _current_extruder, int _next_extruder);
bool checkOk();
void cut_filament()
{
}
void set_positions(int _current_extruder, int _next_extruder)
{
// steps to move to new position of idler and selector
int _selector_steps = ((_current_extruder - _next_extruder) * selector_steps) * -1;
int _idler_steps = (_current_extruder - _next_extruder) * idler_steps;
// move both to new position
move_proportional(_idler_steps, _selector_steps);
}
void eject_filament(int extruder)
{
//move selector sideways and push filament forward little bit, so user can catch it, unpark idler at the end to user can pull filament out
int selector_position = 0;
int steps = 0;
int8_t selector_offset_for_eject = 0;
int8_t idler_offset_for_eject = 0;
//if there is still filament detected by PINDA unload it first
if (isFilamentLoaded) unload_filament_withSensor();
if (isIdlerParked) park_idler(true); // if idler is in parked position un-park him get in contact with filament
tmc2130_init_axis_current(0, 1, 30);
//if we are want to eject fil 0-2, move seelctor to position 4 (right), if we want to eject filament 3 - 4, move selector to position 0 (left)
//maybe we can also move selector to service position in the future?
if (extruder <= 2) selector_position = 4;
else selector_position = 0;
//count offset (number of positions) for desired selector and idler position for ejecting
selector_offset_for_eject = active_extruder - selector_position;
idler_offset_for_eject = active_extruder - extruder;
//count number of desired steps for selector and idler and store it in static variable
selector_steps_for_eject = (selector_offset_for_eject * selector_steps) * -1;
idler_steps_for_eject = idler_offset_for_eject * idler_steps;
//move selector and idler to new position
move_proportional(idler_steps_for_eject, selector_steps_for_eject);
//push filament forward
do
{
do_pulley_step();
steps++;
delayMicroseconds(1500);
} while (steps < 2500);
//unpark idler so user can easily remove filament
park_idler(false);
tmc2130_init_axis_current(0, 0, 0);
}
void recover_after_eject()
{
//restore state before eject filament
//if (isIdlerParked) park_idler(true); // if idler is in parked position un-park him get in contact with filament
tmc2130_init_axis_current(0, 1, 30);
move_proportional(-idler_steps_for_eject, -selector_steps_for_eject);
tmc2130_init_axis_current(0, 0, 0);
//unpark idler
//park_idler(false);
}
void load_filament_withSensor()
{
if (isIdlerParked) park_idler(true); // if idler is in parked position un-park him get in contact with filament
tmc2130_init_axis_current(0, 1, 30);
set_pulley_dir_push();
int _loadSteps = 0;
int _endstop_hit = 0;
// load filament until FINDA senses end of the filament, means correctly loaded into the selector
// we can expect something like 570 steps to get in sensor
do
{
do_pulley_step();
_loadSteps++;
delayMicroseconds(5500);
} while (digitalRead(A1) == 0 && _loadSteps < 1500);
// filament did not arrived at FINDA, let's try to correct that
if (digitalRead(A1) == 0)
{
for (int i = 6; i > 0; i--)
{
if (digitalRead(A1) == 0)
{
// attempt to correct
set_pulley_dir_pull();
for (int i = 200; i >= 0; i--)
{
do_pulley_step();
delayMicroseconds(1500);
}
set_pulley_dir_push();
_loadSteps = 0;
do
{
do_pulley_step();
_loadSteps++;
delayMicroseconds(4000);
if (digitalRead(A1) == 1) _endstop_hit++;
} while (_endstop_hit<100 && _loadSteps < 500);
}
}
}
// still not at FINDA, error on loading, let's wait for user input
if (digitalRead(A1) == 0)
{
bool _continue = false;
bool _isOk = false;
park_idler(false);
do
{
shr16_set_led(0x000);
delay(800);
if (!_isOk)
{
shr16_set_led(2 << 2 * (4 - active_extruder));
}
else
{
shr16_set_led(1 << 2 * (4 - active_extruder));
delay(100);
shr16_set_led(2 << 2 * (4 - active_extruder));
delay(100);
}
delay(800);
switch (buttonClicked())
{
case 4:
// just move filament little bit
park_idler(true);
set_pulley_dir_push();
for (int i = 0; i < 200; i++)
{
do_pulley_step();
delayMicroseconds(5500);
}
park_idler(false);
break;
case 2:
// check if everything is ok
park_idler(true);
_isOk = checkOk();
park_idler(false);
break;
case 1:
// continue with loading
park_idler(true);
_isOk = checkOk();
park_idler(false);
if (_isOk) //pridat do podminky flag ze od tiskarny prislo continue
{
_continue = true;
}
break;
}
} while ( !_continue );
park_idler(true);
// TODO: do not repeat same code, try to do it until succesfull load
_loadSteps = 0;
do
{
do_pulley_step();
_loadSteps++;
delayMicroseconds(5500);
} while (digitalRead(A1) == 0 && _loadSteps < 1500);
// ?
}
else
{
// nothing
}
float _speed = 4500;
for (int i = 0; i < 7900 + ( lengthCorrection * 10 ); i++)
{
do_pulley_step();
if (i > 10 && i < 4000 && _speed > 650) _speed = _speed - 4;
if (i > 100 && i < 4000 && _speed > 650) _speed = _speed - 1;
if (i > 8000 && _speed < 3000) _speed = _speed + 2;
delayMicroseconds(_speed);
}
tmc2130_init_axis_current(0, 0, 0);
isFilamentLoaded = true; // filament loaded
}
void unload_filament_withSensor()
{
// unloads filament from extruder - filament is above Bondtech gears
tmc2130_init_axis_current(0, 1, 30);
if (isIdlerParked) park_idler(true); // if idler is in parked position un-park him get in contact with filament
set_pulley_dir_pull();
float _speed = 2000;
float _first_point = 1800;
float _second_point = 8700;
int _endstop_hit = 0;
// unload until FINDA senses end of the filament
int _unloadSteps = 10000;
do
{
do_pulley_step();
_unloadSteps--;
if (_unloadSteps < 1400 && _speed < 6000) _speed = _speed + 3;
if (_unloadSteps < _first_point && _speed < 2500) _speed = _speed + 2;
if (_unloadSteps < _second_point && _unloadSteps > 5000 && _speed > 550) _speed = _speed - 2;
delayMicroseconds(_speed);
if (digitalRead(A1) == 0 && _unloadSteps < 2500) _endstop_hit++;
} while (_endstop_hit < 100 && _unloadSteps > 0);
// move a little bit so it is not a grinded hole in filament
for (int i = 100; i > 0; i--)
{
do_pulley_step();
delayMicroseconds(5000);
}
// FINDA is still sensing filament, let's try to unload it once again
if (digitalRead(A1) == 1)
{
for (int i = 6; i > 0; i--)
{
if (digitalRead(A1) == 1)
{
set_pulley_dir_push();
for (int i = 150; i > 0; i--)
{
do_pulley_step();
delayMicroseconds(4000);
}
set_pulley_dir_pull();
int _steps = 4000;
_endstop_hit = 0;
do
{
do_pulley_step();
_steps--;
delayMicroseconds(3000);
if (digitalRead(A1) == 0) _endstop_hit++;
} while (_endstop_hit < 100 && _steps > 0);
}
delay(100);
}
}
// error, wait for user input
if (digitalRead(A1) == 1)
{
bool _continue = false;
bool _isOk = false;
park_idler(false);
do
{
shr16_set_led(0x000);
delay(100);
if (!_isOk)
{
shr16_set_led(2 << 2 * (4 - active_extruder));
}
else
{
shr16_set_led(1 << 2 * (4 - active_extruder));
delay(100);
shr16_set_led(2 << 2 * (4 - active_extruder));
delay(100);
}
delay(100);
switch (buttonClicked())
{
case 4:
// just move filament little bit
park_idler(true);
set_pulley_dir_pull();
for (int i = 0; i < 200; i++)
{
do_pulley_step();
delayMicroseconds(5500);
}
park_idler(false);
break;
case 2:
// check if everything is ok
park_idler(true);
_isOk = checkOk();
park_idler(false);
break;
case 1:
// continue with unloading
park_idler(true);
_isOk = checkOk();
park_idler(false);
if (_isOk)
{
_continue = true;
}
break;
}
} while (!_continue);
shr16_set_led(1 << 2 * (4 - previous_extruder));
park_idler(true);
}
else
{
// correct unloading
_speed = 5000;
// unload to PTFE tube
set_pulley_dir_pull();
for (int i = 450; i > 0; i--) // 570
{
do_pulley_step();
delayMicroseconds(_speed);
}
}
park_idler(false);
tmc2130_init_axis_current(0, 0, 0);
isFilamentLoaded = false; // filament unloaded
}
void load_filament_inPrinter()
{
// loads filament after confirmed by printer into the Bontech pulley gears so they can grab them
if (isIdlerParked) park_idler(true); // if idler is in parked position un-park him get in contact with filament
set_pulley_dir_push();
//PLA
tmc2130_init_axis_current(0, 1, 15);
for (int i = 0; i <= 320; i++)
{
if (i == 150) { tmc2130_init_axis_current(0, 1, 10); };
do_pulley_step();
delayMicroseconds(2600);
}
//PLA
tmc2130_init_axis_current(0, 1, 3);
for (int i = 0; i <= 450; i++)
{
do_pulley_step();
delayMicroseconds(2200);
}
park_idler(false);
tmc2130_init_axis_current(0, 0, 0);
}
void init_Pulley()
{
float _speed = 3000;
set_pulley_dir_push();
for (int i = 50; i > 0; i--)
{
do_pulley_step();
delayMicroseconds(_speed);
shr16_set_led(1 << 2 * (int)(i/50));
}
set_pulley_dir_pull();
for (int i = 50; i > 0; i--)
{
do_pulley_step();
delayMicroseconds(_speed);
shr16_set_led(1 << 2 * (4-(int)(i / 50)));
}
}
void do_pulley_step()
{
PORTB |= 0x10;
asm("nop");
PORTB &= ~0x10;
asm("nop");
}
void do_idler_step()
{
PORTD |= 0x40;
asm("nop");
PORTD &= ~0x40;
asm("nop");
}
void park_idler(bool _unpark)
{
if (_unpark) // get idler in contact with filament
{
move(idler_parking_steps, 0,0);
isIdlerParked = false;
}
else // park idler so filament can move freely
{
move(idler_parking_steps*-1, 0,0);
isIdlerParked = true;
}
}
bool home_idler()
{
int _c = 0;
int _l = 0;
for (int c = 1; c > 0; c--) // not really functional, let's do it rather more times to be sure
{
move(0, (c * 5) * -1,0);
delay(50);
for (int i = 0; i < 2000; i++)
{
move(1, 0,0);
delayMicroseconds(100);
uint16_t sg = tmc2130_read_sg(0);
_c++;
if (i == 1000) { _l++; }
if (_c > 100) { shr16_set_led(1 << 2 * _l); };
if (_c > 200) { shr16_set_led(0x000); _c = 0; };
}
}
return true;
}
bool home_selector()
{
int _c = 0;
int _l = 2;
for (int c = 5; c > 0; c--) // not really functional, let's do it rather more times to be sure
{
move(0, (c*20) * -1,0);
delay(50);
for (int i = 0; i < 4000; i++)
{
move(0, 1,0);
uint16_t sg = tmc2130_read_sg(1);
if ((i > 16) && (sg < 10)) break;
_c++;
if (i == 3000) { _l++; }
if (_c > 100) { shr16_set_led(1 << 2 * _l); };
if (_c > 200) { shr16_set_led(0x000); _c = 0; };
}
}
return true;
}
bool home()
{
move(-10, -100,0); // move a bit in opposite direction
// home both idler and selector
home_idler();
home_selector();
shr16_set_led(0x155);
move(idler_steps_after_homing, selector_steps_after_homing,0); // move to initial position
active_extruder = 0;
park_idler(false);
shr16_set_led(0x000);
isFilamentLoaded = false;
shr16_set_led(1 << 2 * (4-active_extruder));
isHomed = true;
}
void move_proportional(int _idler, int _selector)
{
// gets steps to be done and set direction
_idler = set_idler_direction(_idler);
_selector = set_selector_direction(_selector);
float _idler_step = (float)_idler/(float)_selector;
float _idler_pos = 0;
int _speed = 2500;
int _start = _selector - 250;
int _end = 250;
do
{
if (_idler_pos >= 1)
{
if (_idler > 0) { PORTD |= 0x40; }
}
if (_selector > 0) { PORTD |= 0x10; }
asm("nop");
if (_idler_pos >= 1)
{
if (_idler > 0) { PORTD &= ~0x40; _idler--; }
}
if (_selector > 0) { PORTD &= ~0x10; _selector--; }
asm("nop");
if (_idler_pos >= 1)
{
_idler_pos = _idler_pos - 1;
}
_idler_pos = _idler_pos + _idler_step;
delayMicroseconds(_speed);
if (_speed > 900 && _selector > _start) { _speed = _speed - 10; }
if (_speed < 2500 && _selector < _end) { _speed = _speed + 10; }
} while (_selector != 0 || _idler != 0 );
}
void move(int _idler, int _selector, int _pulley)
{
int _acc = 50;
// gets steps to be done and set direction
_idler = set_idler_direction(_idler);
_selector = set_selector_direction(_selector);
_pulley = set_pulley_direction(_pulley);
do
{
if (_idler > 0) { PORTD |= 0x40; }
if (_selector > 0) { PORTD |= 0x10;}
if (_pulley > 0) { PORTB |= 0x10; }
asm("nop");
if (_idler > 0) { PORTD &= ~0x40; _idler--; delayMicroseconds(1000); }
if (_selector > 0) { PORTD &= ~0x10; _selector--; delayMicroseconds(800); }
if (_pulley > 0) { PORTB &= ~0x10; _pulley--; delayMicroseconds(700); }
asm("nop");
if (_acc > 0) { delayMicroseconds(_acc*10); _acc = _acc - 1; }; // super pseudo acceleration control
} while (_selector != 0 || _idler != 0 || _pulley != 0);
}
void set_idler_dir_down()
{
shr16_set_dir(shr16_get_dir() & ~4);
//shr16_set_dir(shr16_get_dir() | 4);
}
void set_idler_dir_up()
{
shr16_set_dir(shr16_get_dir() | 4);
//shr16_set_dir(shr16_get_dir() & ~4);
}
int set_idler_direction(int _steps)
{
if (_steps < 0)
{
_steps = _steps * -1;
set_idler_dir_down();
}
else
{
set_idler_dir_up();
}
return _steps;
}
int set_selector_direction(int _steps)
{
if (_steps < 0)
{
_steps = _steps * -1;
shr16_set_dir(shr16_get_dir() & ~2);
}
else
{
shr16_set_dir(shr16_get_dir() | 2);
}
return _steps;
}
int set_pulley_direction(int _steps)
{
if (_steps < 0)
{
_steps = _steps * -1;
set_pulley_dir_pull();
}
else
{
set_pulley_dir_push();
}
return _steps;
}
void set_pulley_dir_push()
{
shr16_set_dir(shr16_get_dir() & ~1);
}
void set_pulley_dir_pull()
{
shr16_set_dir(shr16_get_dir() | 1);
}
bool checkOk()
{
bool _ret = false;
int _steps = 0;
int _endstop_hit = 0;
// filament in FINDA, let's try to unload it
set_pulley_dir_pull();
if (digitalRead(A1) == 1)
{
_steps = 3000;
_endstop_hit = 0;
do
{
do_pulley_step();
delayMicroseconds(3000);
if (digitalRead(A1) == 0) _endstop_hit++;
_steps--;
} while (_steps > 0 && _endstop_hit < 50);
}
if (digitalRead(A1) == 0)
{
// looks ok, load filament to FINDA
set_pulley_dir_push();
_steps = 3000;
_endstop_hit = 0;
do
{
do_pulley_step();
delayMicroseconds(3000);
if (digitalRead(A1) == 1) _endstop_hit++;
_steps--;
} while (_steps > 0 && _endstop_hit < 50);
if (_steps == 0)
{
// we ran out of steps, means something is again wrong, abort
_ret = false;
}
else
{
// looks ok !
// unload to PTFE tube
set_pulley_dir_pull();
for (int i = 600; i > 0; i--) // 570
{
do_pulley_step();
delayMicroseconds(3000);
}
_ret = true;
}
}
else
{
// something is wrong, abort
_ret = false;
}
return _ret;
}