-
Notifications
You must be signed in to change notification settings - Fork 26
/
gcode.cpp
executable file
·1439 lines (1278 loc) · 39.9 KB
/
gcode.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
#include "common.h"
#include "gcode.h"
#include "timer.h"
#include "temp.h"
#include "eprom.h"
#include "gcodesave.h"
#if defined(ESP32) || defined(ESP8266)
// ==========================
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <FS.h> // Include the SPIFFS library
// ==========================
#elif ESP32
#include <WiFi.h>
#include "SPIFFS.h"
// ==========================
#endif
//#include <WiFiClient.h>
File fme;
extern IPAddress ip ;
#endif
int32_t linecount, lineprocess;
#define MLOOP
#include<stdint.h>
#include<Arduino.h>
/// crude crc macro
#define crc(a, b) (a ^ b)
decfloat read_digit;
int sdcardok = 0;
int waitforline = 0;
// g_str can hold upto 1024 char on ARM and 128 on avr
// to receive laser raster bitmap
char g_str[g_str_len];
uint8_t okxyz;
int g_str_c = 0;
int g_str_l = 0;
GCODE_COMMAND next_target;
uint16_t last_field = 0;
/// list of powers of ten, used for dividing down decimal numbers for sending, and also for our crude floating point algorithm
extern void wifi_loop();
static float decfloat_to_float(void)
{
float r = read_digit.mantissa;
uint8_t e = read_digit.exponent;
/* uint32_t powers=1;
for (e=1; e<read_digit.exponent;e++) powers*=10;
// e=1 means we've seen a decimal point but no digits after it, and e=2 means we've seen a decimal point with one digit so it's too high by one if not zero
*/
if (e) r = (r /*+ powers[e-1] / 2*/) / POWERS(e - 1);
// if (e) r = (r /*+ powers[e-1] / 2*/) * POWERS(e - 1);
MLOOP
return read_digit.sign ? -r : r;
}
void changefilament(float l)
{
#ifdef CHANGEFILAMENT
waitbufferempty();
float backupE = ce01;
float backupX = cx1;
float backupY = cy1;
float backupZ = ocz1;
addmove(50, 0, 0, 0, -2, 0, 1); // retract
addmove(50, 0, 0, 30, 0, 0, 1); // move up
addmove(50, 0, 0, 0, -l, 0, 1); // unload filament
waitbufferempty();
checkendstop = 1;
//zprintf(PSTR("change filemant, then push endstop\n"));
while (1) {
docheckendstop(0);
if (endstopstatus < 0) break;
domotionloop
}
checkendstop = 0;
addmove(5, 0, 0, 0, l + 10, 0, 1); // load filament
addmove(50, 0, 0, -30, 0, 0, 1);
waitbufferempty();
ce01 = backupE;
cx1 = backupX;
cy1 = backupY;
ocz1 = backupZ;
#endif
}
bool waitexecute=false;
int reset_command() {
// reset variables
//if (ok)zprintf(PSTR("ok\n")); // response quick !!
next_target.seen_X = next_target.seen_Y = next_target.seen_Z = \
next_target.seen_E = next_target.seen_F = next_target.seen_S = \
next_target.seen_P = next_target.seen_T = \
next_target.seen_G = next_target.seen_M = \
next_target.read_string = g_str_c = 0;
MLOOP
#ifdef ARC_SUPPORT
next_target.seen_R = next_target.seen_I = next_target.seen_J = 0;
#endif
last_field = 0;
read_digit.sign = read_digit.mantissa = read_digit.exponent = 0;
if (next_target.option_all_relative) {
next_target.target.axis[nX] = next_target.target.axis[nY] = next_target.target.axis[nZ] = 0;
}
if (next_target.option_all_relative || next_target.option_e_relative) {
next_target.target.axis[nE] = 0;
}
return 2;
}
int tryexecute(){
if (!waitexecute)return 0;
if (nextbuff(head) == tail) { // pending operation if buffer is full
return 0;
}
if ((head!=tail) || (cmhead !=cmtail)){ // still have moves, some gcodes need to wait until trully empty
#ifndef laser_pin
if (next_target.seen_M && next_target.M==3) return 0;// M3 need buffer to be empty for cnc
#endif
if (next_target.seen_M && next_target.G==109) return 0;// G92 need buffer to be empty
if (next_target.seen_G && next_target.G==92) return 0;// G92 need buffer to be empty
}
MLOOP
okxyz = next_target.seen_X || next_target.seen_Y || next_target.seen_Z || next_target.seen_E || next_target.seen_F;
uint8_t ok = next_target.seen_G || next_target.seen_M || next_target.seen_T || okxyz;
if (ok){
process_gcode_command();
zprintf(PSTR("ok\n")); // response quick !!
reset_command();
}
waitexecute=false;
return 1;
}
void update_pos(void) {
next_target.target.axis[nX] = cx1;
next_target.target.axis[nY] = cy1;
next_target.target.axis[nZ] = ocz1;
next_target.target.axis[nE] = ce01;
}
uint8_t gcode_parse_char(uint8_t c)
{
uint8_t checksum_char = c;
//serialwr(c);
// uppercase
if (c >= 'a' && c <= 'z' && !next_target.read_string)
c &= ~32;
// An asterisk is a quasi-EOL and always ends all fields.
if (c == '*') {
//next_target.read_string = 0;
}
// Skip comments and strings.
if (
next_target.read_string == 0
) {
// Check if the field has ended. Either by a new field, space or EOL.
if (last_field && (c < '0' || c > '9') && c != '.') {
switch (last_field) {
case 'G':
next_target.G = read_digit.mantissa;
break;
case 'M':
next_target.M = read_digit.mantissa;
if (next_target.M == 117) next_target.read_string = 1;
break;
case 'X':
next_target.target.axis[nX] = decfloat_to_float();
break;
case 'Y':
next_target.target.axis[nY] = decfloat_to_float();
break;
case 'Z':
next_target.target.axis[nZ] = decfloat_to_float();
break;
#ifdef ARC_SUPPORT
case 'I':
next_target.I = decfloat_to_float();
break;
case 'J':
next_target.J = decfloat_to_float();
break;
case 'R':
next_target.R = decfloat_to_float();
break;
#endif
case 'E':
next_target.target.axis[nE] = decfloat_to_float();
break;
case 'F':
// just use raw integer, we need move distance and n_steps to convert it to a useful value, so wait until we have those to convert it
next_target.target.F = decfloat_to_float() / 60;
MLOOP
break;
case 'S':
next_target.S = decfloat_to_float();
break;
case 'P':
next_target.P = decfloat_to_float();
break;
case '*':
//next_target.checksum_read = decfloat_to_float();
break;
case 'T':
//next_target.T = read_digit.mantissa;
break;
case 'N':
//next_target.N = decfloat_to_float();
break;
}
}
// new field?
if ((c >= 'A' && c <= 'Z') || c == '*') {
last_field = c;
read_digit.sign = read_digit.mantissa = read_digit.exponent = 0;
}
// process character
// Can't do ranges in switch..case, so process actual digits here.
// Do it early, as there are many more digits than characters expected.
if (c >= '0' && c <= '9') {
if (read_digit.exponent < DECFLOAT_EXP_MAX + 1) {
// this is simply mantissa = (mantissa * 10) + atoi(c) in different clothes
read_digit.mantissa = (read_digit.mantissa * 10) + (c - '0');
if (read_digit.exponent)
read_digit.exponent++;
}
} else {
switch (c) {
// Each currently known command is either G or M, so preserve
// previous G/M unless a new one has appeared.
// FIXME: same for T command
case 'G':
next_target.seen_G = 1;
//next_target.seen_M = 0;
//next_target.M = 0;
break;
case 'M':
next_target.seen_M = 1;
//next_target.seen_G = 0;
//next_target.G = 0;
break;
#ifdef ARC_SUPPORT
case 'I':
next_target.seen_I = 1;
break;
case 'J':
next_target.seen_J = 1;
break;
case 'R':
next_target.seen_R = 1;
break;
#endif
case 'X':
next_target.seen_X = 1;
break;
case 'Y':
next_target.seen_Y = 1;
break;
case 'Z':
next_target.seen_Z = 1;
break;
case 'E':
next_target.seen_E = 1;
break;
case 'F':
next_target.seen_F = 1;
break;
case 'S':
next_target.seen_S = 1;
break;
case 'P':
next_target.seen_P = 1;
break;
case 'T':
next_target.seen_T = 1;
break;
case 'N':
//next_target.seen_N = 1;
break;
case '*':
//next_target.seen_checksum = 0;//1;
break;
// comments
case '[':
next_target.read_string = 1; // Reset by ')' or EOL
g_str_l = 0;
if (next_target.seen_P && next_target.seen_G) g_str_c = next_target.P; else g_str_c = 0;
if (next_target.G == 7)str_wait();
break;
case ';':
next_target.read_string = 1; // Reset by EOL.
break;
// now for some numeracy
case '-':
read_digit.sign = 1;
// force sign to be at start of number, so 1-2 = -2 instead of -12
read_digit.exponent = 0;
read_digit.mantissa = 0;
break;
case '.':
if (read_digit.exponent == 0)
read_digit.exponent = 1;
break;
#ifdef DEBUG
case ' ':
case '\t':
case 10:
case 13:
// ignore
break;
#endif
default:
#ifdef DEBUG
// invalid
//zprintf(PSTR("?%d\n"), fi(c));
#endif
break;
}
}
} //else if ( next_target.seen_parens_comment == 1 && c == ')')
else {
// store string in g_str from gcode example M206 P450 [ryan widi]
if (c == ']' || c == 10 || c == 13) {
g_str[g_str_c] = 0;
next_target.read_string = 0;
} else {
if (g_str_c < g_str_len - 1) {
g_str[g_str_c] = c;
g_str_c++;
//g_str_c=g_str_c&8191;
g_str_l++;
}
}
//next_target.seen_parens_comment = 0; // recognize stuff after a (comment)
}
// end of line
if ((c == 10) || (c == 13)) {
waitexecute=true;
}
return 0;
}
// implement minimalis code to match teacup
float lastE;
int overridetemp = 0;
void printposition()
{
zprintf(PSTR("X:%f Y:%f Z:%f E:%f\n"),
ff(info_x), ff(info_y),
ff(info_z), ff(ce01));
}
void printbufflen()
{
zprintf(PSTR("Buf:%d\n"), fi(bufflen));
}
void pausemachine()
{
PAUSE = !PAUSE;
if (PAUSE)zprintf(PSTR("Pause\n"));
else zprintf(PSTR("Resume\n"));
}
#ifdef IR_OLED_MENU
#include "ir_oled.h"
#endif
bool stopping=false;
void stopmachine(){
stopping=true;
}
void stopmachine2() {
// soft stop
//PAUSE = 1;
//waitbufferempty(false);
extern void doHardStop();
doHardStop();
enduncompress(true);
PAUSE=0;
stopping=false;
set_pwm(0);
#ifdef laser_pin
LASER(!LASERON);
#endif
}
//#define queue_wait() needbuffer()
#define queue_wait()
void delay_ms(uint32_t d)
{
while (d) {
d--;
somedelay(1000);
}
}
void temp_wait(void)
{
#ifdef heater_pin
wait_for_temp = 1;
uint32_t c = millis();
while (wait_for_temp && !temp_achieved()) {
domotionloop
wifi_loop();
//report each second
if (millis() - c > 1000) {
c = millis();
zprintf(PSTR("T:%f\n"), ff(Input));
//zprintf(PSTR("Heating\n"));
}
}
wait_for_temp = 0;
#endif
}
int lastB = 0;
void str_wait()
{
//uint32_t c = millis();
while (lastB > 5) {
domotionloop
MEMORY_BARRIER()
//delayMicroseconds(10);
}
}
//int32_t mvc = 0;
typedef struct {
float pX, pY;
uint8_t bit;
} tlaserdata;
bool collectLaser = false;
bool runLaser = false;
int laseridx = 0;
int laseridxrun = 0;
tlaserdata laserdata[200];
tlaserdata laserdatarun[200];
void runlasernow() {
// if still running, lets wait
while (runLaser) {
motionloop();
}
laseridxrun = 0;
runLaser = 1;
memcpy(&laserdatarun, &laserdata, sizeof(laserdata));
}
void addlaserxy(float x, float y, uint8_t bit)
{
laserdata[laseridx].pX = x;
laserdata[laseridx].pY = y;
laserdata[laseridx].bit = bit;
laseridx++;
if (laseridx > 199) {
runlasernow();
}
}
void testLaser(void) {
for (int j = 3000; j--;) {
LASER(LASERON)
domotionloop
}
// after some delay turn off laser
LASER(!LASERON);
}
static void enqueue(GCODE_COMMAND *) __attribute__ ((always_inline));
float F0 = 5000;
float F1 = 2000;
uint8_t S1 = 255;
inline void enqueue(GCODE_COMMAND *t, int g0 = 1)
{
if (t->seen_F) {
if (g0)F0 = t->target.F; else F1 = t->target.F;
}
amove(g0 ? F0 : F1, t->seen_X ? t->target.axis[nX] : cx1
, t->seen_Y ? t->target.axis[nY] : cy1
, t->seen_Z ? t->target.axis[nZ] : ocz1
, t->seen_E ? t->target.axis[nE] : ce01
, g0, 0);
}
#ifdef ARC_SUPPORT
inline void enqueuearc(GCODE_COMMAND *t, float I, float J, int cw)
{
if (t->seen_F) {
F1 = t->target.F;
}
draw_arc(F1, t->seen_X ? t->target.axis[nX] : cx1
, t->seen_Y ? t->target.axis[nY] : cy1
, t->seen_Z ? t->target.axis[nZ] : ocz1
, t->seen_E ? t->target.axis[nE] : ce01
, I, J, cw);
}
#endif
int lastG = 0;
int probex1, probey1;
int probemode = 0;
void printmeshleveling() {
#ifdef MESHLEVEL
for (int j = 0; j <= YCount; j++) {
for (int i = 0; i <= XCount; i++) {
if (i)zprintf(PSTR("\t"));
if (j == 0 && i > 0)zprintf(PSTR("%d"), fi(i));
else if (i == 0 && j > 0)zprintf(PSTR("%d"), fi(j));
else if (i == 0 && j == 0)zprintf(PSTR("*"));
else zprintf(PSTR("%d"), fi(ZValues[i][j]));
}
zprintf(PSTR("\n"));
}
zprintf(PSTR("\n\n"));
#endif
}
void loadmeshleveling() {
#ifdef MESHLEVEL
#if defined(ESP8266) || defined(ESP32)
#define path "/mesh.dat"
if (SPIFFS.exists(path)) {
fme = SPIFFS.open(path, "r");
fme.read((uint8_t *)&XCount, sizeof XCount);
fme.read((uint8_t *)&YCount, sizeof YCount);
fme.read((uint8_t *) & (ZValues[0][0]), sizeof ZValues);
zprintf(PSTR("%d bytes\nMESH\n"), fi(fme.size()));
zprintf(PSTR("%dx%d\n"), fi(XCount), fi(YCount));
fme.close();
/*for (int j = 1; j <= XCount; j++) {
for (int i = 1; i <= YCount; i++) {
zprintf(PSTR("%d,"),fi(ZValues[j][i]));
}
zprintf(PSTR("\n"));
}
zprintf(PSTR("\n"));
*/
} else {
zprintf(PSTR("File not found mesh.dat\n"));
return;
}
#endif
MESHLEVELING = 1;
printmeshleveling();
#endif
}
int lastS = 0;
void process_gcode_command()
{
uint32_t backup_f;
// convert relative to absolute
if (next_target.option_all_relative) {
next_target.target.axis[nX] += cx1;//startpoint.axis[nX];
next_target.target.axis[nY] += cy1;//startpoint.axis[nY];
next_target.target.axis[nZ] += ocz1;//startpoint.axis[nZ];
next_target.target.axis[nE] += ce01;//startpoint.axis[nZ];
}
// E relative movement.
// Matches Sprinter's behaviour as of March 2012.
if (next_target.option_all_relative || next_target.option_e_relative)
next_target.target.e_relative = 1;
else
next_target.target.e_relative = 0;
if (next_target.seen_T) {
//? --- T: Select Tool ---
//?
//? Example: T1
//?
//? Select extruder number 1 to build with. Extruder numbering starts at 0.
//next_tool = next_target.T;
}
// check if buffer is near full
/*
int bl=bufflen();
float spd=1;
if (bl>NUMBUFFER/2) {
spd=(float)NUMBUFFER/(bl*4+2);
}
*/
if (!next_target.seen_M) {
if (!next_target.seen_G) {
if (lastG > 1)return;
if (!okxyz)return;
next_target.G = lastG;
}
lastG = next_target.G;
uint8_t axisSelected = 0;
//zprintf(PSTR("Gcode G%d \n"),fi(next_target.G));
switch (next_target.G) {
case 0:
//? G0: Rapid Linear Motion
//?
//? Example: G0 X12
//?
//? In this case move rapidly to X = 12 mm. In fact, the RepRap firmware uses exactly the same code for rapid as it uses for controlled moves (see G1 below), as - for the RepRap machine - this is just as efficient as not doing so. (The distinction comes from some old machine tools that used to move faster if the axes were not driven in a straight line. For them G0 allowed any movement in space to get to the destination as fast as possible.)
//?
laserOn = 0;
constantlaserVal = 0;
enqueue(&next_target, 1);
break;
case 1:
//? --- G1: Linear Motion at Feed Rate ---
//?
//? Example: G1 X90.6 Y13.8 E22.4
//?
//? Go in a straight line from the current (X, Y) point to the point (90.6, 13.8), extruding material as the move happens from the current extruded length to a length of 22.4 mm.
//?
//next_target.target.axis[nE]=0;
// auto retraction change
// thread S parameter as value of the laser, in 3D printer, donot use S in G1 !!
if (next_target.seen_S) {
S1 = next_target.S;
}
laserOn = S1 > 0;
//zprintf(PSTR("int %d\n"), fi(S1));
constantlaserVal = S1;
enqueue(&next_target, 0);
if (laserOn) {
}
break;
// G2 - Arc Clockwise
// G3 - Arc anti Clockwise
case 2:
case 3:
#ifdef ARC_SUPPORT
temp_wait();
if (!next_target.seen_I) next_target.I = 0;
if (!next_target.seen_J) next_target.J = 0;
//if (DEBUG_ECHO && (debug_flags & DEBUG_ECHO))
#define isCW (next_target.G == 2)
if (next_target.seen_R) {
float r = next_target.R;
float x = next_target.seen_X ? next_target.target.axis[nX] - cx1 : 0;
float y = next_target.seen_Y ? next_target.target.axis[nY] - cy1 : 0;
float h_x2_div_d = 4 * r * r - x * x - y * y;
if (h_x2_div_d < 0) {
break;
//FAIL(STATUS_ARC_RADIUS_ERROR);
//return (gc.status_code);
}
// Finish computing h_x2_div_d.
h_x2_div_d = -sqrt(h_x2_div_d) / hypot(x, y); // == -(h * 2 / d)
// Invert the sign of h_x2_div_d if the circle is counter clockwise (see sketch below)
if (!isCW) {
h_x2_div_d = -h_x2_div_d;
}
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
next_target.I = 0.5 * (x - (y * h_x2_div_d));
next_target.J = 0.5 * (y + (x * h_x2_div_d));
}
enqueuearc(&next_target, next_target.I, next_target.J, isCW);
#endif
break;
case 4:
break;
#ifdef output_enable
case 5:
reset_eeprom();
reload_eeprom();
case 6:
cx1 = 0;
cy1 = 0;
ocz1 = 0;
ce01 = 0;
/*
amove(1, 100, 100, 100, 0);
amove(100, 10, 0, 0, 0);
amove(100, 10, 10, 0, 0);
amove(100, 0, 10, 0, 0);
amove(100, 0, 0, 0, 0);
amove(100, 10, 0, 0, 0);
amove(100, 10, 10, 0, 0);
amove(100, 0, 10, 0, 0);
amove(100, 0, 0, 0, 0);
amove(100, 10, 0, 0, 0);
amove(100, 10, 10, 0, 0);
amove(100, 0, 10, 0, 0);
amove(100, 0, 0, 0, 0);
amove(100, 10, 0, 0, 0);
amove(100, 10, 10, 0, 0);
amove(100, 0, 10, 0, 0);
amove(100, 0, 0, 0, 0);
*/
break;
#endif
case 7:
// WE NEED TO REIMPLEMENT THE G7 COMMAND
break;
case 28:
#ifdef PLASMA_MODE
// Gcode G28 Z40 mean
// Probe at this point then ready to cut
// Z 40 mean distance between float Z idle and hit the limit switch is 40mm
if (next_target.seen_Z) {
//MESHLEVELING = 0;
//addmove(4000, next_target.target.axis[nX], next_target.target.axis[nY], ocz1, ce01, 1, 0);
extern float pointProbing(float floatdis);
float zz = pointProbing(next_target.target.axis[nZ]);
//zprintf(PSTR("%f\n"), ff(zz));
}
#else
homing();
update_pos();
printposition();
#endif
break;
#ifdef MESHLEVEL
case 29:
MESHLEVELING = next_target.seen_S;
//zprintf(PSTR("A.L "));
if (MESHLEVELING)zprintf(PSTR("on\n")); else zprintf(PSTR("off\n"));
break;
// Probing
// mesh bed probing from current position to width , height and number of data
// G30 Snumdata Xwidth Yheight
// G30 S4 X100 Y100
//
// single probe
// G30 Xpos Ypos
// G30 (current position)
case 30:
if (next_target.seen_S) {
MESHLEVELING = 0;
int w = next_target.S;
probex1 = cx1;
probey1 = cy1;
//float probez1 = ocz1;
// move up before probing
//addmove(8000, probex1 , probey1 , ocz1 + 15, ce01, 0, 0);
int ww = next_target.target.axis[nX];
int hh = next_target.target.axis[nY];
XCount = floor(ww / w) + 2; // 150/200 = 0 + 2 = 2
YCount = floor(hh / w) + 2; // 50/200 = 0 + 2 = 2
int dx = ww / (XCount - 1); // 150/1 = 150
int dy = hh / (YCount - 1);; // 50/1 = 50
int zmin = 10000;
for (int j = 0; j < YCount; j++) { // 0,1
ZValues[0][j + 1] = (probey1 + j * dy); // [
}
for (int j = 0; j < XCount; j++) {
ZValues[j + 1][0] = (probex1 + j * dx);
for (int ii = 0; ii < YCount; ii++) {
int i = ii;
if (j & 1 == 1)i = YCount - 1 - ii;
int lz, zz, good, gz;
good = 0;
gz = 0;
lz = 10 * pointProbing();
while (good < 3) {
addmove(8000, probex1 + j * dx, probey1 + i * dy, ocz1, ce01, 0, 0);
zz = 10 * pointProbing();
if (abs(zz - lz) < 3) {
good++;
gz += zz;
}
lz = zz;
}
ZValues[j + 1][i + 1] = gz / good; // average good z
}
#ifdef WIFISERVER
wifi_loop();
#endif
}
zmin = ZValues[1][1];
// normalize the data
for (int j = 0; j <= XCount; j++) {
for (int i = 0; i <= YCount; i++) {
if (i && j)ZValues[j][i] -= zmin;
}
}
// activate leveling
// back to zero position and adjust
addmove(8000, probex1 , probey1 , ocz1, ce01, 0, 0);
//addmove(8000, probex1 , probey1 , ocz1+ZValues[1][1], ce01, 0, 0);
waitbufferempty();
ocz1 = 0;
cz1 = 0;
printposition();
MESHLEVELING = 1;
#if defined(ESP32) || defined(ESP8266)
fme = SPIFFS.open("/mesh.dat", "w");
fme.write((uint8_t *)&XCount, sizeof XCount);
fme.write((uint8_t *)&YCount, sizeof YCount);
fme.write((uint8_t *) & (ZValues[0][0]), sizeof ZValues);
fme.close();
#endif
loadmeshleveling();
} else {
MESHLEVELING = 0;
if (!next_target.seen_X)next_target.target.axis[nX] = cx1;
if (!next_target.seen_Y)next_target.target.axis[nY] = cy1;
//zprintf(PSTR("PR X=%f Y=%f :"), ff(next_target.target.axis[nX]), ff(next_target.target.axis[nY]));
addmove(4000, next_target.target.axis[nX], next_target.target.axis[nY], ocz1, ce01, 1, 0);
float zz = pointProbing();
zprintf(PSTR("%f\n"), ff(zz));
}
break;
// manually store probing data
case 31:
// load meshleveling
loadmeshleveling();
break;
case 32:
// load meshleveling
int ii;
ii = next_target.target.axis[nY];
int jj;
jj = next_target.target.axis[nX];
ZValues[jj][ii] = next_target.target.axis[nZ];
printmeshleveling();
break;
case 33:
#if defined(ESP32) || defined(ESP8266)
int zmin;
zmin = ZValues[1][1];
// normalize the data
for (int j = 0; j <= XCount; j++) {
for (int i = 0; i <= YCount; i++) {
if (i && j)ZValues[j][i] -= zmin;
}
}
fme = SPIFFS.open("/mesh.dat", "w");
fme.write((uint8_t *)&XCount, sizeof XCount);
fme.write((uint8_t *)&YCount, sizeof YCount);
fme.write((uint8_t *) & (ZValues[0][0]), sizeof ZValues);
fme.close();
printmeshleveling();
#endif
break;
#endif
case 90:
//? --- G90: Set to Absolute Positioning ---
//?
//? Example: G90
//?
//? All coordinates from now on are absolute relative to the origin
//? of the machine. This is the RepRap default.
//?
//? If you ever want to switch back and forth between relative and
//? absolute movement keep in mind, X, Y and Z follow the machine's
//? coordinate system while E doesn't change it's position in the
//? coordinate system on relative movements.
//?
// No wait_queue() needed.
next_target.option_all_relative = 0;
break;
case 91:
//? --- G91: Set to Relative Positioning ---
//?
//? Example: G91
//?
//? All coordinates from now on are relative to the last position.
//?
// No wait_queue() needed.
next_target.option_all_relative = 1;
break;
case 92:
//? --- G92: Set Position ---
//?
//? Example: G92 X10 E90
//?
//? Allows programming of absolute zero point, by reseting the current position to the values specified. This would set the machine's X coordinate to 10, and the extrude coordinate to 90. No physical motion will occur.
//?
//waitbufferempty();
queue_wait();
float lx;
lx = cx1;
float ly;
ly = cy1;
if (next_target.seen_X) {
cx1 = next_target.target.axis[nX];
axisSelected = 1;
};
if (next_target.seen_Y) {
cy1 = next_target.target.axis[nY];
axisSelected = 1;
};
if (next_target.seen_Z) {
cz1 = ocz1 = next_target.target.axis[nZ];
axisSelected = 1;
};
if (next_target.seen_E) {
lastE = ce01 = next_target.target.axis[nE];
axisSelected = 1;
};
if (axisSelected == 0) {
cx1 = next_target.target.axis[nX] =
cy1 = next_target.target.axis[nY] =
cz1 = ocz1 = next_target.target.axis[nZ] =
ce01 = next_target.target.axis[nE] = 0;
}
init_pos();
/*if (MESHLEVELING) {
lx -= cx1;
ly -= cy1;
// normalize the data
for (int j = 0; j < XCount; j++) {
ZValues[j + 1][0] -= lx;
}
for (int j = 0; j < YCount; j++) {
ZValues[0][j + 1] -= ly;
}
// activate leveling
}*/
break;
// unknown gcode: spit an error
default:
//zprintf(PSTR("E:G%d\nok\n"), next_target.G);
return;