-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leapfrog-Firmware.ino
2229 lines (1972 loc) · 59.7 KB
/
Leapfrog-Firmware.ino
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
/* -*- c++ -*- */
/*
Reprap firmware based on Sprinter and grbl.
Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
This firmware is a mashup between Sprinter and grbl.
(https://github.com/kliment/Sprinter)
(https://github.com/simen/grbl/tree)
It has preliminary support for Matthew Roberts advance algorithm
http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
*/
#include "Marlin.h"
#include "planner.h"
#include "stepper.h"
#include "temperature.h"
#include "EEPROMwrite.h"
#include "language.h"
#include "pins_arduino.h"
#define VERSION_STRING "1.0.0"
//Implemented Codes
//-------------------G
// G0 -> G1
// G1 - Coordinated Movement X Y Z E
// G4 - Dwell S<seconds> or P<milliseconds>
// G10 - retract filament according to settings of M207
// G11 - retract recover filament according to settings of M208
// G28 - Home all Axis
// G29 - Test if Zprobe solenoid is working
// G32 - level printing bed
// G40 - Print amount of steps missed since last reset
// G90 - Use Absolute Coordinates
// G91 - Use Relative Coordinates
// G92 - Set current position to cordinates given
// M Codes
// M104 - Set extruder target temp
// M105 - Read current temp
// M106 - Fan on
// M107 - Fan off
// M109 - Wait for extruder current temp to reach target temp.
// M140 - Set bed target temp
// M190 - Wait for bed current temp to reach target temp.
// M114 - Display current position
//Custom M Codes
// M17 - Enable/Power all stepper motors
// M18 - Disable all stepper motors; same as M84
// M42 - Change pin status via gcode
// M50 - Set Extruder 2 Offset. Does NOT reset with firmware reset M502. Works in DualX mode.
// M80 - Turn on Power Supply
// M81 - Turn off Power Supply
// M82 - Set E codes absolute (default)
// M83 - Set E codes relative while in Absolute Coordinates (G90) mode
// M84 - Disable steppers until next move,
// or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
// M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
// M92 - Set axis_steps_per_unit - same syntax as G92
// M114 - Output current position to serial port
// M119 - Output Endstop status to serial port
// M200 - Set filament diameter
// M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
// M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
// M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
// M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
// M206 - set additional homeing offset
// M207 - set T0 maximum temperature. Syntax: M207 S<desired max temp> T<desired tool>
// M220 - S<factor in percent> set speed factor override percentage (M220 S110)
// M221 - S<factor in percent> set extrude factor override percentage (M221 S75)
// M301 - Set PID parameters P I and D
// M302 - Allow cold extrudes
// M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
// M304 - Set Bed PID parameters P I and D
// M400 - Finish all moves
// M500 - stores parameters in EEPROM
// M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
// M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
// M503 - print the current settings (from memory not from eeprom)
// M601 - Set calibration offsets (X, Y, Z)
// M605 - Synchronous mode - Syncs all control (movement and heating) of T0 with T1. Use toolswitch T0/T1, M605 S1, or G28 to disable.
// M606 - Set parking position
// M999 - Restart after being stopped by error
//Stepper Movement Variables
/* ======== TO DO: ADD NOMENCLATURE OF IMPORTANT VARIABLES ================ */
//===========================================================================
//=============================imported variables============================
//===========================================================================
//===========================================================================
//=============================public variables=============================
//===========================================================================
float homing_feedrate[] = HOMING_FEEDRATE;
bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
volatile int feedmultiply = 100; //100->1 1000->2
int saved_feedmultiply;
volatile bool feedmultiplychanged = false;
volatile int extrudemultiply = 100; //100->1 1000->2
float bed_width_correction = 0.0;
float parking_offset = DEFAULT_PARK_OFFSET;
float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
float add_homeing[3] = { 0.0,0.0,0.0 };
float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
#ifdef DUAL_X
float parking_pos[2] = { X_MIN_POS + DEFAULT_PARK_OFFSET, X_MAX_POS - DEFAULT_PARK_OFFSET }; // Only X-axis, TODO: Store in EEPROM
#endif
uint8_t active_extruder = 0;
unsigned char FanSpeed = 0;
bool door_status = false;
int swap_dir = 1; // Used to swap homing direction for X-axis. 1 = normal, -1 = opposite direction
#if SAFE_MOVES
bool position_known[3] = { false, false, false }; // If current position is unknown, a homing sequence is required (only for X, Y, Z)
#endif
// Extruder offset, only in XY plane
float extruder_offset[2][EXTRUDERS] = {
#if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
#endif
};
// Head PCB and hotend type detection (for up to 2 independent heads):
bool old_head_pcb[2] = { true, true };
bool low_temp_hotend[2] = { true, true };
//===========================================================================
//=============================private variables=============================
//===========================================================================
const char axis_codes[NUM_AXIS] = { 'X', 'Y', 'Z', 'E' };
static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
static float offset = 0.0;
static bool home_all_axis = true;
static float feedrate = 1500.0, next_feedrate, saved_feedrate;
static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
static bool relative_mode = false; //Determines Absolute or Relative Coordinates
static bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
static float yoffset = 0.0; // Used for tool-changes to correct for y offset (x is managed differently)
static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
static bool fromsd[BUFSIZE];
static int bufindr = 0;
static int bufindw = 0;
static int buflen = 0;
static char serial_char;
static int serial_count = 0;
static boolean comment_mode = false;
static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
//Inactivity shutdown variables
static unsigned long previous_millis_cmd = 0;
static unsigned long max_inactive_time = 0;
static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME * 1000l;
static bool break_heating_wait = false;
static unsigned long starttime = 0;
static unsigned long stoptime = 0;
static uint8_t tmp_extruder;
static uint8_t target_extruder;
static unsigned long readTimeout = 0;
static unsigned long stopAt = 0;
static bool includePwm = false;
#ifdef DUAL_X
static bool is_parked[2] = { false, false };
static float dual_x_offset = 0.0; // Requested x-offset
static float actual_dual_x_offset = 0.0; // Calculated x-offset
static float temp_offset = 0.0; // Requested temperature difference between left and right in sync mode
static bool parking_disabled = false;
static float last_known_x[2] = { X_MAX_POS, X_MIN_POS };
static int dual_x_mode = 1; // 0 = No parking, 1 = auto parking, 2 = sync, 3 = sync mirrored. Defaults to 1
#endif
static float target_temp; // Target temperature when heating. Stored here because may be used twice in DUAL_X mode
extern int heater_0_maxtemp;
extern int heater_1_maxtemp;
extern float heater_0_offset;
extern float heater_1_offset;
int toolnum;
bool Stopped = false;
//===========================================================================
//=============================ROUTINES=============================
//===========================================================================
bool setTargetedHotend(int code);
void serial_echopair_P(const char *s_P, float v)
{
serialprintPGM(s_P); SERIAL_ECHO(v);
}
void serial_echopair_P(const char *s_P, double v)
{
serialprintPGM(s_P); SERIAL_ECHO(v);
}
void serial_echopair_P(const char *s_P, unsigned long v)
{
serialprintPGM(s_P); SERIAL_ECHO(v);
}
extern "C" {
extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;
int freeMemory() {
int free_memory;
if ((int)__brkval == 0)
free_memory = ((int)&free_memory) - ((int)&__bss_end);
else
free_memory = ((int)&free_memory) - ((int)__brkval);
return free_memory;
}
}
//adds an command to the main command buffer
//thats really done in a non-safe way.
//needs overworking someday
void enquecommand(const char *cmd)
{
if (buflen < BUFSIZE)
{
//this is dangerous if a mixing of serial and this happsens
strcpy(&(cmdbuffer[bufindw][0]), cmd);
SERIAL_ECHO_START;
SERIAL_ECHOPGM("enqueing \"");
SERIAL_ECHO(cmdbuffer[bufindw]);
SERIAL_ECHOLNPGM("\"");
bufindw = (bufindw + 1) % BUFSIZE;
buflen += 1;
}
}
void setup_killpin()
{
#if( KILL_PIN>-1 )
pinMode(KILL_PIN, INPUT);
WRITE(KILL_PIN, HIGH);
#endif
}
void setup_doorpin()
{
#if defined(DOOR_PIN) && DOOR_PIN > -1
pinMode(DOOR_PIN, INPUT);
WRITE(DOOR_PIN, LOW);
door_status = READ(DOOR_PIN);
#endif
}
void setup_filament_pins()
{
#if defined(FILAMENT_E0_PIN) && FILAMENT_E0_PIN > -1
pinMode(FILAMENT_E0_PIN, INPUT);
WRITE(FILAMENT_E0_PIN, HIGH);
#endif
#if defined(FILAMENT_E1_PIN) && FILAMENT_E1_PIN > -1
pinMode(FILAMENT_E1_PIN, INPUT);
WRITE(FILAMENT_E1_PIN, HIGH);
#endif
}
// Output hotend type information
// Only call after setup_head_pcb_detect() has run
static void print_head_hotend_type()
{
for (uint8_t e = 0; e < EXTRUDERS; e++)
{
SERIAL_PROTOCOLPGM(MSG_M115_SENSOR_TYPE);
SERIAL_PROTOCOL_F(e, 10);
if (old_head_pcb[e]) { SERIAL_PROTOCOLPGM(MSG_M115_SENSOR_TYPE_THERMISTOR); }
else { SERIAL_PROTOCOLPGM(MSG_M115_SENSOR_TYPE_PT100); }
SERIAL_PROTOCOLPGM(MSG_M115_HOTEND_TYPE);
SERIAL_PROTOCOL_F(e, 10);
if(low_temp_hotend[e]) { SERIAL_PROTOCOLPGM(MSG_M115_HOTEND_TYPE_LOW_TEMP); }
else { SERIAL_PROTOCOLPGM(MSG_M115_HOTEND_TYPE_HIGH_TEMP); }
}
}
static void setup_head_pcb_detect()
{
// Set the head PCB detection pins as inputs and enable pullups (20-50k):
pinMode(HEAD_PCB_OLD_0_PIN, INPUT_PULLUP);
pinMode(LOW_TEMP_HOTEND_0_PIN, INPUT_PULLUP);
#if EXTRUDERS > 1
pinMode(HEAD_PCB_OLD_1_PIN, INPUT_PULLUP);
pinMode(LOW_TEMP_HOTEND_1_PIN, INPUT_PULLUP);
#endif
// Wait for detection lines to be properly settled:
delay(50);
// Determine the head PCB and hotend scenario (see pins.h):
old_head_pcb[0] = digitalRead(HEAD_PCB_OLD_0_PIN); // HIGH iff old head PCB is connected (pulled high due to pull-up)
low_temp_hotend[0] = digitalRead(LOW_TEMP_HOTEND_0_PIN); // HIGH iff a low temp hotend is connected
if(!low_temp_hotend[0]){ heater_0_maxtemp = HEATER_HIGH_TEMP_MAX; }
#if EXTRUDERS > 1
old_head_pcb[1] = digitalRead(HEAD_PCB_OLD_1_PIN); // HIGH iff old head PCB is connected (pulled high due to pull-up)
low_temp_hotend[1] = digitalRead(LOW_TEMP_HOTEND_1_PIN); // HIGH iff a low temp hotend is connected
if(!low_temp_hotend[1]){ heater_1_maxtemp = HEATER_HIGH_TEMP_MAX; }
#endif
// Check for illegal hotends / head PCB combinations:
if (old_head_pcb[0] && !low_temp_hotend[0]) {
SERIAL_ERROR_START
SERIAL_ERRORPGM(MSG_ERR_HEAD_PCB_HOTEND_COMBO);
SERIAL_ERRORLN(0);
//kill();
}
#if EXTRUDERS > 1
if (old_head_pcb[1] && !low_temp_hotend[1]) {
SERIAL_ERROR_START
SERIAL_ERRORPGM(MSG_ERR_HEAD_PCB_HOTEND_COMBO);
SERIAL_ERRORLN(1);
//kill();
}
if (old_head_pcb[0] ^ old_head_pcb[1]) { // Combination of one old and one new head PCB
SERIAL_ERROR_START
SERIAL_ERRORPGM(MSG_ERR_HEAD_PCB_OLD_NEW);
SERIAL_ERRORLN(1);
//kill();
}
#endif
// DEBUG:
print_head_hotend_type();
SERIAL_PROTOCOLLN("");
}
void setup_powerhold()
{
#ifdef SUICIDE_PIN
#if (SUICIDE_PIN> -1)
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, HIGH);
#endif
#endif
}
void suicide()
{
#ifdef SUICIDE_PIN
#if (SUICIDE_PIN> -1)
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, LOW);
#endif
#endif
}
void setup()
{
setup_killpin();
setup_powerhold();
MYSERIAL.begin(BAUDRATE);
SERIAL_ECHO_START;
// Check startup - does nothing if bootloader sets MCUSR to 0
byte mcu = MCUSR;
if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
MCUSR = 0;
SERIAL_ECHOPGM(MSG_MARLIN);
SERIAL_ECHOLNPGM(VERSION_STRING);
#ifdef STRING_VERSION_CONFIG_H
#ifdef STRING_CONFIG_H_AUTHOR
SERIAL_ECHO_START;
SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
SERIAL_ECHOPGM(MSG_AUTHOR);
SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
#endif
#endif
SERIAL_ECHO_START;
SERIAL_ECHOPGM(MSG_FREE_MEMORY);
SERIAL_ECHO(freeMemory());
SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
for (int8_t i = 0; i < BUFSIZE; i++)
{
fromsd[i] = false;
}
EEPROM_RetrieveSettings(); // loads data from EEPROM if available
for (int8_t i = 0; i < NUM_AXIS; i++)
{
axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
}
// Detect head PCB version and hotend type, before starting temperature loop:
setup_head_pcb_detect();
// Now we have the EEPROM, set the parking positions
updateParkingPos();
tp_init(); // Initialize temperature loop
plan_init(); // Initialize planner;
st_init(); // Initialize stepper;
setup_doorpin();
setup_filament_pins();
SERIAL_PROTOCOLLNPGM("start");
}
void readCommand()
{
if (buflen < (BUFSIZE - 1))
get_command();
}
void loop()
{
readCommand();
if (buflen)
{
process_commands();
buflen = (buflen - 1);
bufindr = (bufindr + 1) % BUFSIZE;
}
//check heater every n milliseconds
manage_heater();
manage_inactivity();
checkHitEndstops();
}
void get_command()
{
while (MYSERIAL.available() > 0 && buflen < BUFSIZE)
{
serial_char = MYSERIAL.read();
if (serial_char == '\n' ||
serial_char == '\r' ||
(serial_char == ':' && comment_mode == false) ||
serial_count >= (MAX_CMD_SIZE - 1))
{
if (!serial_count)
{ //if empty line
comment_mode = false; //for new command
return;
}
cmdbuffer[bufindw][serial_count] = 0; //terminate string
if (!comment_mode)
{
comment_mode = false; //for new command
fromsd[bufindw] = false;
if (strstr(cmdbuffer[bufindw], "N") != NULL)
{
strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
if (gcode_N != gcode_LastN + 1 && (strstr(cmdbuffer[bufindw], "M110") == NULL))
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
SERIAL_ERRORLN(gcode_LastN);
//Serial.println(gcode_N);
FlushSerialRequestResend();
serial_count = 0;
return;
}
if (strstr(cmdbuffer[bufindw], "*") != NULL)
{
byte checksum = 0;
byte count = 0;
while (cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
strchr_pointer = strchr(cmdbuffer[bufindw], '*');
if ((int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum)
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
SERIAL_ERRORLN(gcode_LastN);
FlushSerialRequestResend();
serial_count = 0;
return;
}
//if no errors, continue parsing
}
else
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
SERIAL_ERRORLN(gcode_LastN);
FlushSerialRequestResend();
serial_count = 0;
return;
}
gcode_LastN = gcode_N;
//if no errors, continue parsing
}
else // if we don't receive 'N' but still see '*'
{
if ((strstr(cmdbuffer[bufindw], "*") != NULL))
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
SERIAL_ERRORLN(gcode_LastN);
serial_count = 0;
return;
}
}
if ((strstr(cmdbuffer[bufindw], "G") != NULL))
{
strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
switch ((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL))))
{
case 0:
case 1:
case 2:
case 3:
if (Stopped == true) {
SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
}
break;
default:
break;
}
}
if (strstr(cmdbuffer[bufindw], "M108") != NULL)
{
// Don't add the command to the buffer and pretend this never happened.
break_heating_wait= true;
// It never happened, so we don't send OK
//SERIAL_PROTOCOLLNPGM(MSG_OK);
}
else
{
bufindw = (bufindw + 1) % BUFSIZE;
buflen += 1;
}
}
serial_count = 0; //clear buffer
}
else
{
if (serial_char == ';') comment_mode = true;
if (!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
}
}
}
float code_value()
{
return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
}
long code_value_long()
{
return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
}
bool code_seen(char code_string[]) //Return True if the string was found
{
return (strstr(cmdbuffer[bufindr], code_string) != NULL);
}
bool code_seen(char code)
{
strchr_pointer = strchr(cmdbuffer[bufindr], code);
return (strchr_pointer != NULL); //Return True if a character was found
}
#define DEFINE_PGM_READ_ANY(type, reader) \
static inline type pgm_read_any(const type *p) \
{ return pgm_read_##reader##_near(p); }
DEFINE_PGM_READ_ANY(float, float);
DEFINE_PGM_READ_ANY(signed char, byte);
#define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
static const PROGMEM type array##_P[3] = \
{ X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
static inline type array(int axis) \
{ return pgm_read_any(&array##_P[axis]); }
XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
//TODO: Get rid of the approach and work solely with base_min_pos and base_max_pos (and incorporate bed_width_correction into base_base_pos)
static inline float bed_width() { return base_max_pos(X_AXIS) - base_min_pos(X_AXIS) + bed_width_correction; }
static void axis_is_at_home(int axis)
{
#ifdef DUAL_X
if (axis == X_AXIS)
{
// TODO: Make more dynamic. It now assumes X0 homes right, X1 homes left.
if (syncmode_enabled)
{
current_position[axis] = X_MIN_POS + add_homeing[axis];
min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
if (inverted_x0_mode)
max_pos[axis] = base_min_pos(X_AXIS) + bed_width() / 2.0 + add_homeing[axis] + extruder_offset[X_AXIS][1] / 2.0;
else
max_pos[axis] = base_min_pos(X_AXIS) + bed_width() + add_homeing[axis] - actual_dual_x_offset;
}
else if (active_extruder == 1)
{
current_position[axis] = base_min_pos(axis) + add_homeing[axis];
min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
max_pos[axis] = min(base_min_pos(X_AXIS) + bed_width(), last_known_x[0]) + extruder_offset[X_AXIS][1];
}
else
{
current_position[axis] = base_min_pos(X_AXIS) + bed_width() + add_homeing[axis];
min_pos[axis] = max(base_min_pos(axis), last_known_x[1]) - extruder_offset[X_AXIS][1];
max_pos[axis] = base_min_pos(X_AXIS) + bed_width() + add_homeing[axis];
}
}
else
{
current_position[axis] = base_home_pos(axis) + add_homeing[axis];
min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
}
#else
current_position[axis] = base_home_pos(axis) + add_homeing[axis];
min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
#endif
#if SAFE_MOVES
position_known[axis] = true;
#endif
}
static void homeaxis(int axis)
{
#define HOMEAXIS_DO(LETTER) \
((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
#define HOMEAXIS_DO_X \
((X0_MIN_PIN > -1 && X_HOME_DIR == -1) || (X0_MAX_PIN > -1 && X_HOME_DIR == 1))
if (axis == X_AXIS ? HOMEAXIS_DO_X :
axis == Y_AXIS ? HOMEAXIS_DO(Y) :
axis == Z_AXIS ? HOMEAXIS_DO(Z) :
0)
{
// For X: homing will only be performed for active_extruder. Determine whether direction needs to be swapped
//TODO: Save seperate X0 and X1 homing dir in config
#ifdef DUAL_X
swap_dir = axis == X_AXIS && active_extruder == 1 && X1_HOME_DIR == -1 ? -1 : 1;
if (axis == X_AXIS) is_parked[active_extruder] = false;
#else
swap_dir = 1;
#endif
current_position[axis] = 0;
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
destination[axis] = 1.5 * max_length(axis) * home_dir(axis) * swap_dir;
feedrate = homing_feedrate[axis];
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate / 60, active_extruder);
st_synchronize();
current_position[axis] = 0;
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
destination[axis] = -home_retract_mm(axis) * home_dir(axis) * swap_dir;
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate / 60, active_extruder);
st_synchronize();
destination[axis] = 2 * home_retract_mm(axis) * home_dir(axis) * swap_dir;
feedrate = homing_feedrate[axis] / 2;
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate / 60, active_extruder);
st_synchronize();
axis_is_at_home(axis);
destination[axis] = current_position[axis];
feedrate = 0.0;
endstops_hit_on_purpose();
#ifdef DUAL_X
last_known_x[active_extruder] = current_position[X_AXIS];
#endif
}
}
#define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
// Planner shorthand inline functions
inline void line_to_current_position() {
plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate / 60, active_extruder);
}
inline void line_to_z(float zPosition) {
plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate / 60, active_extruder);
}
inline void line_to_destination(float mm_m) {
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], mm_m / 60, active_extruder);
}
inline void line_to_destination() {
line_to_destination(feedrate);
}
inline void sync_plan_position() {
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
}
inline void set_current_to_destination() { memcpy(current_position, destination, sizeof(current_position)); }
inline void set_destination_to_current() { memcpy(destination, current_position, sizeof(destination)); }
void process_commands()
{
unsigned long codenum; //throw away variable
char *starpos = NULL;
bool relative_mode_backup;
float maxAdjust;
int probeIterations;
if (code_seen('G'))
{
switch ((int)code_value())
{
case 0: // G0 -> G1
case 1: // G1
if (Stopped == false)
{
get_coordinates(); // For X Y Z E F
prepare_move();
#ifdef FILAMENT_DETECTION
checkFilamentError();
#endif
//ClearToSend();
}
break;
case 4: // G4 dwell
codenum = 0;
if (code_seen('P')) codenum = code_value(); // milliseconds to wait
if (code_seen('S')) codenum = code_value() * 1000; // seconds to wait
st_synchronize();
codenum += millis(); // keep track of when we started waiting
previous_millis_cmd = millis();
while (millis() < codenum) {
manage_heater();
manage_inactivity();
}
break;
case 28: //G28 Home all Axis one at a timer
{
saved_feedrate = feedrate;
saved_feedmultiply = feedmultiply;
feedmultiply = 100;
previous_millis_cmd = millis();
enable_endstops(true, true, true);
set_destination_to_current();
feedrate = 0.0;
bool homeX = code_seen(axis_codes[X_AXIS]),
homeY = code_seen(axis_codes[Y_AXIS]),
homeZ = code_seen(axis_codes[Z_AXIS]);
home_all_axis = !(homeX || homeY || homeZ) || (homeX && homeY && homeZ);
// Wait before we do anything (like switching off syncmode)
st_synchronize();
if (home_all_axis || homeX)
{
#ifdef DUAL_X
homeDualX();
#else
HOMEAXIS(X);
#endif
}
// Home Y
if (home_all_axis || homeY) HOMEAXIS(Y);
// Home Z
if (home_all_axis || homeZ) HOMEAXIS(Z);
// Set the X position and add M206
if (code_seen(axis_codes[X_AXIS])) {
float v = code_value();
if (v) current_position[X_AXIS] = v + add_homeing[0];
}
// Set the Y position and add M206
if (code_seen(axis_codes[Y_AXIS])) {
float v = code_value();
if (v) current_position[Y_AXIS] = v + add_homeing[1];
}
// Set the Z position and add M206
if (code_seen(axis_codes[Z_AXIS])) {
float v = code_value();
if (v) current_position[Z_AXIS] = v + add_homeing[2];
}
sync_plan_position();
enable_endstops(ENDSTOPS_DURING_PRINT_X, ENDSTOPS_DURING_PRINT_Y, ENDSTOPS_DURING_PRINT_Z);
feedrate = saved_feedrate;
feedmultiply = saved_feedmultiply;
previous_millis_cmd = millis();
endstops_hit_on_purpose(); // clear endstop hit flags
break;
}
case 90: // G90
relative_mode = false;
break;
case 91: // G91
relative_mode = true;
break;
case 92: // G92
if (!code_seen(axis_codes[E_AXIS]))
st_synchronize();
for (int8_t i = 0; i < NUM_AXIS; i++)
{
if (code_seen(axis_codes[i]))
{
if (i == E_AXIS)
{
current_position[i] = (float)code_value();
plan_set_e_position(current_position[E_AXIS]);
}
else
{
current_position[i] = code_value() + add_homeing[i];
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
}
}
}
break;
}
}
else if (code_seen('M'))
{
switch ((int)code_value())
{
case 42: //M42 -Change pin status via gcode
if (code_seen('S'))
{
int pin_status = code_value();
if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
{
int pin_number = code_value();
for (int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
{
if (sensitive_pins[i] == pin_number)
{
pin_number = -1;
break;
}
}
if (pin_number > -1)
{
pinMode(pin_number, OUTPUT);
digitalWrite(pin_number, pin_status);
analogWrite(pin_number, pin_status);
}
}
}
break;
case 605: // M605
{
#ifndef DUAL_X
SERIAL_ECHO_START;
SERIAL_ECHOLN(MSG_NO_DUALX);
#else
bool temp_sync = true;
if (code_seen('S'))
dual_x_mode = code_value();
else
dual_x_mode = 1;
if (code_seen('X'))
dual_x_offset = code_value();
else
dual_x_offset = 0;
if (code_seen('R'))
temp_offset = code_value();
else
temp_offset = 0;
if (code_seen('U') && code_value() == 0)
temp_sync = false;
if (dual_x_mode == 3)
beginSyncMode(0.0, true, temp_sync);
else if (dual_x_mode == 2)
beginSyncMode(dual_x_offset, false, temp_sync);
else if (dual_x_mode == 1)
{
endSyncMode();
parking_disabled = false;
}
else
{
endSyncMode();
parking_disabled = true;
}
#endif
}
break;
case 606: // M606
{
#ifndef DUAL_X
SERIAL_ECHO_START;
SERIAL_ECHOLN(MSG_NO_DUALX);
#else
if (code_seen('P')) parking_offset = code_value();
updateParkingPos();
#endif
}
break;
case 198:
outputRaw();
break;
case 199:
//TODO: Remove this
// Dump status about X
dumpstatus();
break;
case 104: // M104
if (setTargetedHotend(104))
{
break;
}
if (code_seen('S')) {
target_temp = code_value();
setTargetHotend(target_temp, target_extruder);
#ifdef DUAL_X