This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.cpp
3000 lines (2884 loc) · 108 KB
/
Commands.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
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
/*
This file is part of Repetier-Firmware.
Repetier-Firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Repetier-Firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Repetier-Firmware. If not, see <http://www.gnu.org/licenses/>.
This firmware is a nearly complete rewrite of the sprinter firmware
by kliment (https://github.com/kliment/Sprinter)
which based on Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
*/
#include "Repetier.h"
const int8_t sensitive_pins[] PROGMEM = SENSITIVE_PINS; // Sensitive pin list for M42
int Commands::lowestRAMValue = MAX_RAM;
int Commands::lowestRAMValueSend = MAX_RAM;
void Commands::commandLoop() {
//while(true) {
#ifdef DEBUG_PRINT
debugWaitLoop = 1;
#endif
if(!Printer::isBlockingReceive()) {
GCode::readFromSerial();
GCode *code = GCode::peekCurrentCommand();
//UI_SLOW; // do longer timed user interface action
UI_MEDIUM; // do check encoder
if(code) {
#if SDSUPPORT
if(sd.savetosd) {
if(!(code->hasM() && code->M == 29)) // still writing to file
sd.writeCommand(code);
else
sd.finishWrite();
#if ECHO_ON_EXECUTE
code->echoCommand();
#endif
} else
#endif
Commands::executeGCode(code);
code->popCurrentCommand();
}
} else {
GCode::keepAlive(Paused);
UI_MEDIUM;
}
Printer::defaultLoopActions();
//}
}
void Commands::checkForPeriodicalActions(bool allowNewMoves) {
Printer::handleInterruptEvent();
EVENT_PERIODICAL;
#if defined(DOOR_PIN) && DOOR_PIN > -1
if(Printer::updateDoorOpen()) {
#if defined(SUPPORT_LASER) && SUPPORT_LASER
if(Printer::mode == PRINTER_MODE_LASER) {
LaserDriver::changeIntensity(0);
}
#endif
}
#endif
if(!executePeriodical) return; // gets true every 100ms
executePeriodical = 0;
EVENT_TIMER_100MS;
Extruder::manageTemperatures();
if(--counter500ms == 0) {
if(manageMonitor)
writeMonitor();
counter500ms = 5;
EVENT_TIMER_500MS;
}
// If called from queueDelta etc. it is an error to start a new move since it
// would invalidate old computation resulting in unpredicted behavior.
// lcd controller can start new moves, so we disallow it if called from within
// a move command.
UI_SLOW(allowNewMoves);
}
/** \brief Waits until movement cache is empty.
Some commands expect no movement, before they can execute. This function
waits, until the steppers are stopped. In the meanwhile it buffers incoming
commands and manages temperatures.
*/
void Commands::waitUntilEndOfAllMoves() {
#ifdef DEBUG_PRINT
debugWaitLoop = 8;
#endif
while(PrintLine::hasLines()) {
//GCode::readFromSerial();
checkForPeriodicalActions(false);
GCode::keepAlive(Processing);
UI_MEDIUM;
}
}
void Commands::waitUntilEndOfAllBuffers() {
GCode *code = NULL;
#ifdef DEBUG_PRINT
debugWaitLoop = 9;
#endif
while(PrintLine::hasLines() || (code != NULL)) {
//GCode::readFromSerial();
code = GCode::peekCurrentCommand();
UI_MEDIUM; // do check encoder
if(code) {
#if SDSUPPORT
if(sd.savetosd) {
if(!(code->hasM() && code->M == 29)) // still writing to file
sd.writeCommand(code);
else
sd.finishWrite();
#if ECHO_ON_EXECUTE
code->echoCommand();
#endif
} else
#endif
Commands::executeGCode(code);
code->popCurrentCommand();
}
Commands::checkForPeriodicalActions(false); // only called from memory
UI_MEDIUM;
}
}
void Commands::printCurrentPosition() {
float x, y, z;
Printer::realPosition(x, y, z);
x += Printer::coordinateOffset[X_AXIS];
y += Printer::coordinateOffset[Y_AXIS];
z += Printer::coordinateOffset[Z_AXIS];
Com::printF(Com::tXColon, x * (Printer::unitIsInches ? 0.03937 : 1), 2);
Com::printF(Com::tSpaceYColon, y * (Printer::unitIsInches ? 0.03937 : 1), 2);
Com::printF(Com::tSpaceZColon, z * (Printer::unitIsInches ? 0.03937 : 1), 3);
Com::printFLN(Com::tSpaceEColon, Printer::currentPositionSteps[E_AXIS] * Printer::invAxisStepsPerMM[E_AXIS] * (Printer::unitIsInches ? 0.03937 : 1), 4);
#ifdef DEBUG_POS
Com::printF(PSTR("OffX:"), Printer::offsetX); // to debug offset handling
Com::printF(PSTR(" OffY:"), Printer::offsetY);
Com::printF(PSTR(" OffZ:"), Printer::offsetZ);
Com::printF(PSTR(" OffZ2:"), Printer::offsetZ2);
Com::printF(PSTR(" XS:"), Printer::currentPositionSteps[X_AXIS]);
Com::printF(PSTR(" YS:"), Printer::currentPositionSteps[Y_AXIS]);
Com::printFLN(PSTR(" ZS:"), Printer::currentPositionSteps[Z_AXIS]);
#endif
}
void Commands::printTemperatures(bool showRaw) {
int error;
#if NUM_EXTRUDER > 0
float temp = Extruder::current->tempControl.currentTemperatureC;
#if HEATED_BED_SENSOR_TYPE == 0
Com::printF(Com::tTColon, temp);
Com::printF(Com::tSpaceSlash, Extruder::current->tempControl.targetTemperatureC, 0);
#else
Com::printF(Com::tTColon, temp);
Com::printF(Com::tSpaceSlash, Extruder::current->tempControl.targetTemperatureC, 0);
#if HAVE_HEATED_BED
Com::printF(Com::tSpaceBColon, Extruder::getHeatedBedTemperature());
Com::printF(Com::tSpaceSlash, heatedBedController.targetTemperatureC, 0);
if((error = heatedBedController.errorState()) > 0) {
Com::printF(PSTR(" DB:"), error);
}
if(showRaw) {
Com::printF(Com::tSpaceRaw, (int)NUM_EXTRUDER);
Com::printF(Com::tColon, (1023 << (2 - ANALOG_REDUCE_BITS)) - heatedBedController.currentTemperature);
}
Com::printF(Com::tSpaceBAtColon, (pwm_pos[heatedBedController.pwmIndex])); // Show output of auto tune when tuning!
#endif
#endif
Com::printF(Com::tSpaceAtColon, (autotuneIndex == 255 ? pwm_pos[Extruder::current->id] : pwm_pos[autotuneIndex])); // Show output of auto tune when tuning!
#if NUM_EXTRUDER > 1 && MIXING_EXTRUDER == 0
for(uint8_t i = 0; i < NUM_EXTRUDER; i++) {
Com::printF(Com::tSpaceT, (int)i);
Com::printF(Com::tColon, extruder[i].tempControl.currentTemperatureC);
Com::printF(Com::tSpaceSlash, extruder[i].tempControl.targetTemperatureC, 0);
Com::printF(Com::tSpaceAt, (int)i);
Com::printF(Com::tColon, (pwm_pos[extruder[i].tempControl.pwmIndex])); // Show output of auto tune when tuning!
if((error = extruder[i].tempControl.errorState()) > 0) {
Com::printF(PSTR(" D"), (int)i);
Com::printF(Com::tColon, error);
}
if(showRaw) {
Com::printF(Com::tSpaceRaw, (int)i);
Com::printF(Com::tColon, (1023 << (2 - ANALOG_REDUCE_BITS)) - extruder[i].tempControl.currentTemperature);
}
}
#elif NUM_EXTRUDER == 1 || MIXING_EXTRUDER
if((error = extruder[0].tempControl.errorState()) > 0) {
Com::printF(PSTR(" D0:"), error);
}
if(showRaw) {
Com::printF(Com::tSpaceRaw, (int)0);
Com::printF(Com::tColon, (1023 << (2 - ANALOG_REDUCE_BITS)) - extruder[0].tempControl.currentTemperature);
}
#endif
#ifdef FAKE_CHAMBER
Com::printF(PSTR(" C:"), extruder[0].tempControl.currentTemperatureC);
Com::printF(Com::tSpaceSlash, extruder[0].tempControl.targetTemperatureC, 0);
Com::printF(PSTR(" @:"), (pwm_pos[extruder[0].tempControl.pwmIndex]));
#endif
Com::println();
#endif
}
void Commands::changeFeedrateMultiply(int factor) {
if(factor < 25) factor = 25;
if(factor > 500) factor = 500;
Printer::feedrate *= (float)factor / (float)Printer::feedrateMultiply;
Printer::feedrateMultiply = factor;
Com::printFLN(Com::tSpeedMultiply, factor);
}
void Commands::changeFlowrateMultiply(int factor) {
if(factor < 25) factor = 25;
if(factor > 200) factor = 200;
Printer::extrudeMultiply = factor;
if(Extruder::current->diameter <= 0)
Printer::extrusionFactor = 0.01f * static_cast<float>(factor);
else
Printer::extrusionFactor = 0.01f * static_cast<float>(factor) * 4.0f / (Extruder::current->diameter * Extruder::current->diameter * 3.141592654f);
Com::printFLN(Com::tFlowMultiply, factor);
}
#if FEATURE_FAN_CONTROL
uint8_t fanKickstart;
#endif
#if FEATURE_FAN2_CONTROL
uint8_t fan2Kickstart;
#endif
void Commands::setFanSpeed(int speed, bool immediately) {
#if FAN_PIN >- 1 && FEATURE_FAN_CONTROL
if(Printer::fanSpeed == speed)
return;
speed = constrain(speed, 0, 255);
Printer::setMenuMode(MENU_MODE_FAN_RUNNING, speed != 0);
Printer::fanSpeed = speed;
if(PrintLine::linesCount == 0 || immediately) {
if(Printer::mode == PRINTER_MODE_FFF) {
for(fast8_t i = 0; i < PRINTLINE_CACHE_SIZE; i++)
PrintLine::lines[i].secondSpeed = speed; // fill all printline buffers with new fan speed value
}
Printer::setFanSpeedDirectly(speed);
}
Com::printFLN(Com::tFanspeed, speed); // send only new values to break update loops!
#endif
}
void Commands::setFan2Speed(int speed) {
#if FAN2_PIN >- 1 && FEATURE_FAN2_CONTROL
speed = constrain(speed, 0, 255);
Printer::setFan2SpeedDirectly(speed);
Com::printFLN(Com::tFan2speed, speed); // send only new values to break update loops!
#endif
}
void Commands::reportPrinterUsage() {
#if EEPROM_MODE != 0
float dist = Printer::filamentPrinted * 0.001 + HAL::eprGetFloat(EPR_PRINTING_DISTANCE);
Com::printF(Com::tPrintedFilament, dist, 2);
Com::printF(Com::tSpacem);
bool alloff = true;
#if NUM_EXTRUDER > 0
for(uint8_t i = 0; i < NUM_EXTRUDER; i++)
if(tempController[i]->targetTemperatureC > 15) alloff = false;
#endif
int32_t seconds = (alloff ? 0 : (HAL::timeInMilliseconds() - Printer::msecondsPrinting) / 1000) + HAL::eprGetInt32(EPR_PRINTING_TIME);
int32_t tmp = seconds / 86400;
seconds -= tmp * 86400;
Com::printF(Com::tPrintingTime, tmp);
tmp = seconds / 3600;
Com::printF(Com::tSpaceDaysSpace, tmp);
seconds -= tmp * 3600;
tmp = seconds / 60;
Com::printF(Com::tSpaceHoursSpace, tmp);
Com::printFLN(Com::tSpaceMin);
#endif
}
#if STEPPER_CURRENT_CONTROL == CURRENT_CONTROL_DIGIPOT
// Digipot methods for controling current and microstepping
#if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
int digitalPotWrite(int address, uint16_t value) { // From Arduino DigitalPotControl example
if(value > 255)
value = 255;
WRITE(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
HAL::spiSend(address); // send in the address and value via SPI:
HAL::spiSend(value);
WRITE(DIGIPOTSS_PIN, HIGH); // take the SS pin high to de-select the chip:
//delay(10);
}
void setMotorCurrent(uint8_t driver, uint16_t current) {
if(driver > 4) return;
const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
digitalPotWrite(digipot_ch[driver], current);
}
void setMotorCurrentPercent( uint8_t channel, float level) {
uint16_t raw_level = ( level * 255 / 100 );
setMotorCurrent(channel, raw_level);
}
#endif
void motorCurrentControlInit() { //Initialize Digipot Motor Current
#if DIGIPOTSS_PIN && DIGIPOTSS_PIN > -1
HAL::spiInit(0); //SPI.begin();
SET_OUTPUT(DIGIPOTSS_PIN);
#ifdef MOTOR_CURRENT_PERCENT
const float digipot_motor_current[] = MOTOR_CURRENT_PERCENT;
for(int i = 0; i <= 4; i++)
//digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
setMotorCurrentPercent(i, digipot_motor_current[i]);
#else
const uint8_t digipot_motor_current[] = MOTOR_CURRENT;
for(int i = 0; i <= 4; i++)
//digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
setMotorCurrent(i, digipot_motor_current[i]);
#endif
#endif
}
#endif
#if STEPPER_CURRENT_CONTROL == CURRENT_CONTROL_LTC2600
void setMotorCurrent( uint8_t channel, unsigned short level ) {
if(channel >= LTC2600_NUM_CHANNELS) return;
const uint8_t ltc_channels[] = LTC2600_CHANNELS;
if(channel > LTC2600_NUM_CHANNELS) return;
uint8_t address = ltc_channels[channel];
fast8_t i;
// NOTE: Do not increase the current endlessly. In case the engine reaches its current saturation, the engine and the driver can heat up and loss power.
// When the saturation is reached, more current causes more heating and more power loss.
// In case of engines with lower quality, the saturation current may be reached before the nominal current.
// configure the pins
WRITE( LTC2600_CS_PIN, HIGH );
SET_OUTPUT( LTC2600_CS_PIN );
WRITE( LTC2600_SCK_PIN, LOW );
SET_OUTPUT( LTC2600_SCK_PIN );
WRITE( LTC2600_SDI_PIN, LOW );
SET_OUTPUT( LTC2600_SDI_PIN );
// enable the command interface of the LTC2600
WRITE( LTC2600_CS_PIN, LOW );
// transfer command and address
for( i = 7; i >= 0; i-- ) {
WRITE( LTC2600_SDI_PIN, address & (0x01 << i));
WRITE( LTC2600_SCK_PIN, 1 );
WRITE( LTC2600_SCK_PIN, 0 );
}
// transfer the data word
for( i = 15; i >= 0; i-- ) {
WRITE( LTC2600_SDI_PIN, level & (0x01 << i));
WRITE( LTC2600_SCK_PIN, 1 );
WRITE( LTC2600_SCK_PIN, 0 );
}
// disable the ommand interface of the LTC2600 -
// this carries out the specified command
WRITE( LTC2600_CS_PIN, HIGH );
} // setLTC2600
void setMotorCurrentPercent( uint8_t channel, float level) {
if(level > 100.0f) level = 100.0f;
uint16_t raw_level = static_cast<uint16_t>( (long)level * 65535L / 100L );
setMotorCurrent(channel, raw_level);
}
void motorCurrentControlInit() { //Initialize LTC2600 Motor Current
uint8_t i;
#ifdef MOTOR_CURRENT_PERCENT
const float digipot_motor_current[] = MOTOR_CURRENT_PERCENT;
for(int i = 0; i < LTC2600_NUM_CHANNELS; i++)
//digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
setMotorCurrentPercent(i, digipot_motor_current[i]);
#else
const unsigned int ltc_current[] = MOTOR_CURRENT;
for(i = 0; i < LTC2600_NUM_CHANNELS; i++) {
setMotorCurrent(i, ltc_current[i] );
}
#endif
}
#endif
#if STEPPER_CURRENT_CONTROL == CURRENT_CONTROL_ALLIGATOR
void setMotorCurrent(uint8_t channel, unsigned short value) {
if(channel >= 7) // max channel (X,Y,Z,E0,E1,E2,E3)
return;
if(value > 255)
value = 255;
uint8_t externalDac_buf[2] = {0x10, 0x00};
if(channel > 3)
externalDac_buf[0] |= (7 - channel << 6);
else
externalDac_buf[0] |= (3 - channel << 6);
externalDac_buf[0] |= (value >> 4);
externalDac_buf[1] |= (value << 4);
// All SPI chip-select HIGH
WRITE(DAC0_SYNC, HIGH);
WRITE(DAC1_SYNC, HIGH);
WRITE(SPI_EEPROM1_CS, HIGH);
WRITE(SPI_EEPROM2_CS, HIGH);
WRITE(SPI_FLASH_CS, HIGH);
WRITE(SDSS, HIGH);
if(channel > 3) { // DAC Piggy E1,E2,E3
WRITE(DAC1_SYNC, LOW);
HAL::delayMicroseconds(2);
WRITE(DAC1_SYNC, HIGH);
HAL::delayMicroseconds(2);
WRITE(DAC1_SYNC, LOW);
} else { // DAC onboard X,Y,Z,E0
WRITE(DAC0_SYNC, LOW);
HAL::delayMicroseconds(2);
WRITE(DAC0_SYNC, HIGH);
HAL::delayMicroseconds(2);
WRITE(DAC0_SYNC, LOW);
}
HAL::delayMicroseconds(2);
HAL::spiSend(SPI_CHAN_DAC, externalDac_buf, 2);
}
void setMotorCurrentPercent( uint8_t channel, float level) {
uint16_t raw_level = ( level * 255 / 100 );
setMotorCurrent(channel, raw_level);
}
void motorCurrentControlInit() { //Initialize Motor Current
uint8_t externalDac_buf[2] = {0x20, 0x00};//all off
// All SPI chip-select HIGH
WRITE(DAC0_SYNC, HIGH);
WRITE(DAC1_SYNC, HIGH);
WRITE(SPI_EEPROM1_CS, HIGH);
WRITE(SPI_EEPROM2_CS, HIGH);
WRITE(SPI_FLASH_CS, HIGH);
WRITE(SDSS, HIGH);
// init onboard DAC
WRITE(DAC0_SYNC, LOW);
HAL::delayMicroseconds(2);
WRITE(DAC0_SYNC, HIGH);
HAL::delayMicroseconds(2);
WRITE(DAC0_SYNC, LOW);
HAL::spiSend(SPI_CHAN_DAC, externalDac_buf, 2);
WRITE(DAC0_SYNC, HIGH);
#if NUM_EXTRUDER > 1
// init Piggy DAC
WRITE(DAC1_SYNC, LOW);
HAL::delayMicroseconds(2);
WRITE(DAC1_SYNC, HIGH);
HAL::delayMicroseconds(2);
WRITE(DAC1_SYNC, LOW);
HAL::spiSend(SPI_CHAN_DAC, externalDac_buf, 2);
WRITE(DAC1_SYNC, HIGH);
#endif
#ifdef MOTOR_CURRENT_PERCENT
const float digipot_motor_current[] = MOTOR_CURRENT_PERCENT;
for(int i = 0; i < NUM_EXTRUDER + 3; i++)
setMotorCurrentPercent(i, digipot_motor_current[i]);
#else
const uint8_t digipot_motor_current[] = MOTOR_CURRENT;
for(uint8_t i = 0; i < NUM_EXTRUDER + 3; i++)
setMotorCurrent(i, digipot_motor_current[i]);
#endif
}
#endif
#if STEPPER_CURRENT_CONTROL == CURRENT_CONTROL_TMC2130
TMC2130Stepper* tmcDriverByIndex(uint8_t index) {
switch(index) {
#if TMC2130_ON_X
case 0: // X Axis
return Printer::tmc_driver_x;
break;
#endif
#if TMC2130_ON_Y
case 1: // Y Axis
return Printer::tmc_driver_y;
break;
#endif
#if TMC2130_ON_Z
case 2: // Z Axis
return Printer::tmc_driver_z;
break;
#endif
#if TMC2130_ON_EXT0
case 3: // E0 Axis
return Printer::tmc_driver_e0;
break;
#endif
#if TMC2130_ON_EXT1
case 4: // E1 Axis
return Printer::tmc_driver_e1;
break;
#endif
#if TMC2130_ON_EXT2
case 5: // E2 Axis
return Printer::tmc_driver_e2;
break;
#endif
default:
return NULL;
break;
}
}
void setMotorCurrent( uint8_t driver, uint16_t level ) {
TMC2130Stepper* tmc_driver = tmcDriverByIndex(driver);
if(tmc_driver) {
#if MOTHERBOARD == 310
tmc_driver->rms_current(level, 0.5, 0.22);
#else
tmc_driver->rms_current(level);
#endif
}
}
void setMotorCurrentPercent( uint8_t channel, float level ) {
const uint16_t tmc_motor_current[] = MOTOR_CURRENT;
uint16_t raw_level = ( level * tmc_motor_current[channel] / 100 );
setMotorCurrent(channel, raw_level);
}
void motorCurrentControlInit() {
const uint16_t tmc_motor_current[] = MOTOR_CURRENT;
for(uint8_t i = 0; i < (sizeof(tmc_motor_current) / sizeof(uint16_t)); i++) {
setMotorCurrent(i, tmc_motor_current[i]);
}
}
void motorCurrentReadings() {
Com::printF(Com::tTrinamicMotorCurrent);
#if TMC2130_ON_X
Com::printF(Com::tSpaceXColon, (uint32_t)(Printer::tmc_driver_x->rms_current()));
#endif
#if TMC2130_ON_Y
Com::printF(Com::tSpaceYColon, (uint32_t)(Printer::tmc_driver_y->rms_current()));
#endif
#if TMC2130_ON_Z
Com::printF(Com::tSpaceZColon, (uint32_t)(Printer::tmc_driver_z->rms_current()));
#endif
#if TMC2130_ON_EXT0
Com::printF(Com::tSpaceEColon, (uint32_t)(Printer::tmc_driver_e0->rms_current()));
#endif
#if TMC2130_ON_EXT1
Com::printF(PSTR(" E1:"), (uint32_t)(Printer::tmc_driver_e1->rms_current()));
#endif
#if TMC2130_ON_EXT2
Com::printF(PSTR(" E2:"), (uint32_t)(Printer::tmc_driver_e2->rms_current()));
#endif
Com::printFLN(Com::tSpace);
}
#endif // CURRENT_CONTROL_TMC2130
#if STEPPER_CURRENT_CONTROL == CURRENT_CONTROL_MCP4728
uint8_t _intVref[] = {MCP4728_VREF, MCP4728_VREF, MCP4728_VREF, MCP4728_VREF};
uint8_t _gain[] = {MCP4728_GAIN, MCP4728_GAIN, MCP4728_GAIN, MCP4728_GAIN};
uint8_t _powerDown[] = {0, 0, 0, 0};
int16_t dac_motor_current[] = {0, 0, 0, 0};
uint8_t _intVrefEp[] = {MCP4728_VREF, MCP4728_VREF, MCP4728_VREF, MCP4728_VREF};
uint8_t _gainEp[] = {MCP4728_GAIN, MCP4728_GAIN, MCP4728_GAIN, MCP4728_GAIN};
uint8_t _powerDownEp[] = {0, 0, 0, 0};
int16_t _valuesEp[] = {0, 0, 0, 0};
uint8_t dac_stepper_channel[] = MCP4728_STEPPER_ORDER;
int dacSimpleCommand(uint8_t simple_command) {
HAL::i2cStartWait(MCP4728_GENERALCALL_ADDRESS + I2C_WRITE);
HAL::i2cWrite(simple_command);
HAL::i2cStop();
}
void dacReadStatus() {
HAL::delayMilliseconds(500);
HAL::i2cStartWait(MCP4728_I2C_ADDRESS | I2C_READ);
for (int i = 0; i < 8; i++) { // 2 sets of 4 Channels (1 EEPROM, 1 Runtime)
uint8_t deviceID = HAL::i2cReadAck();
uint8_t hiByte = HAL::i2cReadAck();
uint8_t loByte = ((i < 7) ? HAL::i2cReadAck() : HAL::i2cReadNak());
uint8_t isEEPROM = (deviceID & 0B00001000) >> 3;
uint8_t channel = (deviceID & 0B00110000) >> 4;
if (isEEPROM == 1) {
_intVrefEp[channel] = (hiByte & 0B10000000) >> 7;
_gainEp[channel] = (hiByte & 0B00010000) >> 4;
_powerDownEp[channel] = (hiByte & 0B01100000) >> 5;
_valuesEp[channel] = word((hiByte & 0B00001111), loByte);
} else {
_intVref[channel] = (hiByte & 0B10000000) >> 7;
_gain[channel] = (hiByte & 0B00010000) >> 4;
_powerDown[channel] = (hiByte & 0B01100000) >> 5;
dac_motor_current[channel] = word((hiByte & 0B00001111), loByte);
}
}
HAL::i2cStop();
}
void dacAnalogUpdate(bool saveEEPROM = false) {
uint8_t dac_write_cmd = MCP4728_CMD_SEQ_WRITE;
HAL::i2cStartWait(MCP4728_I2C_ADDRESS + I2C_WRITE);
if (saveEEPROM) HAL::i2cWrite(dac_write_cmd);
for (int i = 0; i < MCP4728_NUM_CHANNELS; i++) {
uint16_t level = dac_motor_current[i];
uint8_t highbyte = ( _intVref[i] << 7 | _gain[i] << 4 | (uint8_t)((level) >> 8) );
uint8_t lowbyte = ( (uint8_t) ((level) & 0xff) );
dac_write_cmd = MCP4728_CMD_MULTI_WRITE | (i << 1);
if (!saveEEPROM) HAL::i2cWrite(dac_write_cmd);
HAL::i2cWrite(highbyte);
HAL::i2cWrite(lowbyte);
}
HAL::i2cStop();
// Instruct the MCP4728 to reflect our updated value(s) on its DAC Outputs
dacSimpleCommand((uint8_t)MCP4728_CMD_GC_UPDATE); // MCP4728 General Command Software Update (Update all DAC Outputs to reflect settings)
// if (saveEEPROM) dacReadStatus(); // Not necessary, just a read-back sanity check.
}
void dacCommitEeprom() {
dacAnalogUpdate(true);
dacReadStatus(); // Refresh EEPROM Values with values actually stored in EEPROM. .
}
void dacPrintSet(int dacChannelSettings[], const char* dacChannelPrefixes[]) {
for (int i = 0; i < MCP4728_NUM_CHANNELS; i++) {
uint8_t dac_channel = dac_stepper_channel[i]; // DAC Channel is a mapped lookup.
Com::printF(dacChannelPrefixes[i], ((float)dacChannelSettings[dac_channel] * 100 / MCP4728_VOUT_MAX));
Com::printF(Com::tSpaceRaw);
Com::printFLN(Com::tColon, dacChannelSettings[dac_channel]);
}
}
void dacPrintValues() {
const char* dacChannelPrefixes[] = {Com::tSpaceXColon, Com::tSpaceYColon, Com::tSpaceZColon, Com::tSpaceEColon};
Com::printFLN(Com::tMCPEpromSettings);
dacPrintSet(_valuesEp, dacChannelPrefixes); // Once for the EEPROM set
Com::printFLN(Com::tMCPCurrentSettings);
dacPrintSet(dac_motor_current, dacChannelPrefixes); // And another for the RUNTIME set
}
void setMotorCurrent( uint8_t xyz_channel, uint16_t level ) {
if (xyz_channel >= MCP4728_NUM_CHANNELS) return;
uint8_t stepper_channel = dac_stepper_channel[xyz_channel];
dac_motor_current[stepper_channel] = level < MCP4728_VOUT_MAX ? level : MCP4728_VOUT_MAX;
dacAnalogUpdate();
}
void setMotorCurrentPercent( uint8_t channel, float level) {
uint16_t raw_level = ( level * MCP4728_VOUT_MAX / 100 );
setMotorCurrent(channel, raw_level);
}
void motorCurrentControlInit() { //Initialize MCP4728 Motor Current
HAL::i2cInit(400000); // Initialize the i2c bus.
dacSimpleCommand((uint8_t)MCP4728_CMD_GC_RESET); // MCP4728 General Command Reset
dacReadStatus(); // Load Values from EEPROM.
for(int i = 0; i < MCP4728_NUM_CHANNELS; i++) {
setMotorCurrent(dac_stepper_channel[i], _valuesEp[i] ); // This is not strictly necessary, but serves as a good sanity check to ensure we're all on the same page.
}
}
#endif
#if defined(DRV_TMC2130)
void microstepMode(uint8_t driver, uint16_t stepping_mode) {
TMC2130Stepper* tmc_driver;
if(tmc_driver = tmcDriverByIndex(driver))
tmc_driver->microsteps(stepping_mode);
}
void microstepInit() {
const uint16_t microstep_modes[] = MICROSTEP_MODES;
for(uint8_t i = 0; i < (sizeof(microstep_modes) / sizeof(uint16_t)); i++) {
microstepMode(i, microstep_modes[i]);
}
}
void microstepReadings() {
Com::printF(Com::tTrinamicMicrostepMode);
#if TMC2130_ON_X
Com::printF(Com::tSpaceXColon, (uint32_t)(Printer::tmc_driver_x->microsteps()));
#endif
#if TMC2130_ON_Y
Com::printF(Com::tSpaceYColon, (uint32_t)(Printer::tmc_driver_y->microsteps()));
#endif
#if TMC2130_ON_Z
Com::printF(Com::tSpaceZColon, (uint32_t)(Printer::tmc_driver_z->microsteps()));
#endif
#if TMC2130_ON_EXT0
Com::printF(Com::tSpaceEColon, (uint32_t)(Printer::tmc_driver_e0->microsteps()));
#endif
#if TMC2130_ON_EXT1
Com::printF(PSTR(" E1:"), (uint32_t)(Printer::tmc_driver_e1->microsteps()));
#endif
#if TMC2130_ON_EXT2
Com::printF(PSTR(" E1:"), (uint32_t)(Printer::tmc_driver_e2->microsteps()));
#endif
Com::printFLN(Com::tSpace);
}
#else
#if defined(X_MS1_PIN) && X_MS1_PIN > -1
void microstepMS(uint8_t driver, int8_t ms1, int8_t ms2) {
if(ms1 > -1) switch(driver) {
case 0:
#if X_MS1_PIN > -1
WRITE( X_MS1_PIN, ms1);
#endif
break;
case 1:
#if Y_MS1_PIN > -1
WRITE( Y_MS1_PIN, ms1);
#endif
break;
case 2:
#if Z_MS1_PIN > -1
WRITE( Z_MS1_PIN, ms1);
#endif
break;
case 3:
#if E0_MS1_PIN > -1
WRITE(E0_MS1_PIN, ms1);
#endif
break;
case 4:
#if E1_MS1_PIN > -1
WRITE(E1_MS1_PIN, ms1);
#endif
break;
}
if(ms2 > -1) switch(driver) {
case 0:
#if X_MS2_PIN > -1
WRITE( X_MS2_PIN, ms2);
#endif
break;
case 1:
#if Y_MS2_PIN > -1
WRITE( Y_MS2_PIN, ms2);
#endif
break;
case 2:
#if Z_MS2_PIN > -1
WRITE( Z_MS2_PIN, ms2);
#endif
break;
case 3:
#if E0_MS2_PIN > -1
WRITE(E0_MS2_PIN, ms2);
#endif
break;
case 4:
#if E1_MS2_PIN > -1
WRITE(E1_MS2_PIN, ms2);
#endif
break;
}
}
void microstepMode(uint8_t driver, uint8_t stepping_mode) {
switch(stepping_mode) {
case 1:
microstepMS(driver, MICROSTEP1);
break;
case 2:
microstepMS(driver, MICROSTEP2);
break;
case 4:
microstepMS(driver, MICROSTEP4);
break;
case 8:
microstepMS(driver, MICROSTEP8);
break;
case 16:
microstepMS(driver, MICROSTEP16);
break;
case 32:
microstepMS(driver, MICROSTEP32);
break;
}
}
void microstepReadings() {
Com::printFLN(Com::tMS1MS2Pins);
#if X_MS1_PIN > -1 && X_MS2_PIN > -1
Com::printF(Com::tXColon, READ(X_MS1_PIN));
Com::printFLN(Com::tComma, READ(X_MS2_PIN));
#elif X_MS1_PIN > -1
Com::printFLN(Com::tXColon, READ(X_MS1_PIN));
#endif
#if Y_MS1_PIN > -1 && Y_MS2_PIN > -1
Com::printF(Com::tYColon, READ(Y_MS1_PIN));
Com::printFLN(Com::tComma, READ(Y_MS2_PIN));
#elif Y_MS1_PIN > -1
Com::printFLN(Com::tYColon, READ(Y_MS1_PIN));
#endif
#if Z_MS1_PIN > -1 && Z_MS2_PIN > -1
Com::printF(Com::tZColon, READ(Z_MS1_PIN));
Com::printFLN(Com::tComma, READ(Z_MS2_PIN));
#elif Z_MS1_PIN > -1
Com::printFLN(Com::tZColon, READ(Z_MS1_PIN));
#endif
#if E0_MS1_PIN > -1 && E0_MS2_PIN > -1
Com::printF(Com::tE0Colon, READ(E0_MS1_PIN));
Com::printFLN(Com::tComma, READ(E0_MS2_PIN));
#elif E0_MS1_PIN > -1
Com::printFLN(Com::tE0Colon, READ(E0_MS1_PIN));
#endif
#if E1_MS1_PIN > -1 && E1_MS2_PIN > -1
Com::printF(Com::tE1Colon, READ(E1_MS1_PIN));
Com::printFLN(Com::tComma, READ(E1_MS2_PIN));
#elif E1_MS1_PIN > -1
Com::printFLN(Com::tE1Colon, READ(E1_MS1_PIN));
#endif
}
#endif
void microstepInit() {
#if defined(X_MS1_PIN) && X_MS1_PIN > -1
const uint8_t microstep_modes[] = MICROSTEP_MODES;
#if X_MS1_PIN > -1
SET_OUTPUT(X_MS1_PIN);
#endif
#if Y_MS1_PIN > -1
SET_OUTPUT(Y_MS1_PIN);
#endif
#if Z_MS1_PIN > -1
SET_OUTPUT(Z_MS1_PIN);
#endif
#if E0_MS1_PIN > -1
SET_OUTPUT(E0_MS1_PIN);
#endif
#if E1_MS1_PIN > -1
SET_OUTPUT(E1_MS1_PIN);
#endif
#if X_MS2_PIN > -1
SET_OUTPUT(X_MS2_PIN);
#endif
#if Y_MS2_PIN > -1
SET_OUTPUT(Y_MS2_PIN);
#endif
#if Z_MS2_PIN > -1
SET_OUTPUT(Z_MS2_PIN);
#endif
#if E0_MS2_PIN > -1
SET_OUTPUT(E0_MS2_PIN);
#endif
#if E1_MS2_PIN > -1
SET_OUTPUT(E1_MS2_PIN);
#endif
for(int i = 0; i <= 4; i++) microstepMode(i, microstep_modes[i]);
#endif
}
#endif
/**
\brief Execute the Arc command stored in com.
*/
#if ARC_SUPPORT
void Commands::processArc(GCode *com) {
float position[Z_AXIS_ARRAY];
Printer::realPosition(position[X_AXIS], position[Y_AXIS], position[Z_AXIS]);
if(!Printer::setDestinationStepsFromGCode(com)) return; // For X Y Z E F
float offset[2] = {Printer::convertToMM(com->hasI() ? com->I : 0), Printer::convertToMM(com->hasJ() ? com->J : 0)};
float target[E_AXIS_ARRAY] = {Printer::realXPosition(), Printer::realYPosition(), Printer::realZPosition(), Printer::destinationSteps[E_AXIS]*Printer::invAxisStepsPerMM[E_AXIS]};
float r;
if (com->hasR()) {
/*
We need to calculate the center of the circle that has the designated radius and passes
through both the current position and the target position. This method calculates the following
set of equations where [x,y] is the vector from current to target position, d == magnitude of
that vector, h == hypotenuse of the triangle formed by the radius of the circle, the distance to
the center of the travel vector. A vector perpendicular to the travel vector [-y,x] is scaled to the
length of h [-y/d*h, x/d*h] and added to the center of the travel vector [x/2,y/2] to form the new point
[i,j] at [x/2-y/d*h, y/2+x/d*h] which will be the center of our arc.
d^2 == x^2 + y^2
h^2 == r^2 - (d/2)^2
i == x/2 - y/d*h
j == y/2 + x/d*h
O <- [i,j]
- |
r - |
- |
- | h
- |
[0,0] -> C -----------------+--------------- T <- [x,y]
| <------ d/2 ---->|
C - Current position
T - Target position
O - center of circle that pass through both C and T
d - distance from C to T
r - designated radius
h - distance from center of CT to O
Expanding the equations:
d -> sqrt(x^2 + y^2)
h -> sqrt(4 * r^2 - x^2 - y^2)/2
i -> (x - (y * sqrt(4 * r^2 - x^2 - y^2)) / sqrt(x^2 + y^2)) / 2
j -> (y + (x * sqrt(4 * r^2 - x^2 - y^2)) / sqrt(x^2 + y^2)) / 2
Which can be written:
i -> (x - (y * sqrt(4 * r^2 - x^2 - y^2))/sqrt(x^2 + y^2))/2
j -> (y + (x * sqrt(4 * r^2 - x^2 - y^2))/sqrt(x^2 + y^2))/2
Which we for size and speed reasons optimize to:
h_x2_div_d = sqrt(4 * r^2 - x^2 - y^2)/sqrt(x^2 + y^2)
i = (x - (y * h_x2_div_d))/2
j = (y + (x * h_x2_div_d))/2
*/
r = Printer::convertToMM(com->R);
// Calculate the change in position along each selected axis
double x = target[X_AXIS] - position[X_AXIS];
double y = target[Y_AXIS] - position[Y_AXIS];
double h_x2_div_d = -sqrt(4 * r * r - x * x - y * y) / hypot(x, y); // == -(h * 2 / d)
// If r is smaller than d, the arc is now traversing the complex plane beyond the reach of any
// real CNC, and thus - for practical reasons - we will terminate promptly:
if(isnan(h_x2_div_d)) {
Com::printErrorFLN(Com::tInvalidArc);
return;
}
// Invert the sign of h_x2_div_d if the circle is counter clockwise (see sketch below)
if (com->G == 3) {
h_x2_div_d = -h_x2_div_d;
}
/* The counter clockwise circle lies to the left of the target direction. When offset is positive,
the left hand circle will be generated - when it is negative the right hand circle is generated.
T <-- Target position
^
Clockwise circles with this center | Clockwise circles with this center will have
will have > 180 deg of angular travel | < 180 deg of angular travel, which is a good thing!
\ | /
center of arc when h_x2_div_d is positive -> x <----- | -----> x <- center of arc when h_x2_div_d is negative
|
|
C <-- Current position */
// Negative R is g-code-alias for "I want a circle with more than 180 degrees of travel" (go figure!),
// even though it is advised against ever generating such circles in a single line of g-code. By
// inverting the sign of h_x2_div_d the center of the circles is placed on the opposite side of the line of
// travel and thus we get the inadvisable long arcs as prescribed.
if (r < 0) {
h_x2_div_d = -h_x2_div_d;
r = -r; // Finished with r. Set to positive for mc_arc
}
// Complete the operation by calculating the actual center of the arc
offset[0] = 0.5 * (x - (y * h_x2_div_d));
offset[1] = 0.5 * (y + (x * h_x2_div_d));
} else { // Offset mode specific computations
r = hypot(offset[0], offset[1]); // Compute arc radius for arc
}
// Set clockwise/counter-clockwise sign for arc computations
uint8_t isclockwise = com->G == 2;