forked from markwal/GPX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpx.c
3575 lines (3152 loc) · 130 KB
/
gpx.c
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
//
// gpx.c
//
// Created by WHPThomas <me(at)henri(dot)net> on 1/04/13.
//
// Copyright (c) 2013 WHPThomas, All rights reserved.
//
// gpx references ReplicatorG sources from /src/replicatorg/drivers
// which are part of the ReplicatorG project - http://www.replicat.org
// Copyright (c) 2008 Zach Smith
// and Makerbot4GSailfish.java Copyright (C) 2012 Jetty / Dan Newman
//
// 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 2 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, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#ifdef _WIN32
# include "getopt.h"
#else
# include <unistd.h>
#endif
#include "gpx.h"
#include "ini.h"
#define A 0
#define B 1
// Machine definitions
// Axis - max_feedrate, home_feedrate, steps_per_mm, endstop;
// Extruder - max_feedrate, steps_per_mm, motor_steps, has_heated_build_platform;
static Machine cupcake_G3 = {
{9600, 500, 11.767463, ENDSTOP_IS_MIN}, // x axis
{9600, 500, 11.767463, ENDSTOP_IS_MIN}, // y axis
{450, 450, 320, ENDSTOP_IS_MIN}, // z axis
{7200, 50.235478806907409, 400, 1}, // a extruder
{7200, 50.235478806907409, 400, 0}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
static Machine cupcake_G4 = {
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // x axis
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // y axis
{450, 450, 1280, ENDSTOP_IS_MIN}, // z axis
{7200, 50.235478806907409, 400, 1}, // a extruder
{7200, 50.235478806907409, 400, 0}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
static Machine cupcake_P4 = {
{9600, 500, 94.13970462, ENDSTOP_IS_MIN}, // x axis
{9600, 500, 94.13970462, ENDSTOP_IS_MIN}, // y axis
{450, 450, 2560, ENDSTOP_IS_MIN}, // z axis
{7200, 50.235478806907409, 400, 1}, // a extruder
{7200, 50.235478806907409, 400, 0}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
static Machine cupcake_PP = {
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // x axis
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // y axis
{450, 450, 1280, ENDSTOP_IS_MIN}, // z axis
{7200, 100.470957613814818, 400, 1}, // a extruder
{7200, 100.470957613814818, 400, 0}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
// Axis - max_feedrate, home_feedrate, steps_per_mm, endstop;
// Extruder - max_feedrate, steps_per_mm, motor_steps, has_heated_build_platform;
static Machine thing_o_matic_7 = {
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // x axis
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // y axis
{1000, 500, 200, ENDSTOP_IS_MAX}, // z axis
{1600, 50.235478806907409, 1600, 1}, // a extruder
{1600, 50.235478806907409, 1600, 0}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
static Machine thing_o_matic_7D = {
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // x axis
{9600, 500, 47.069852, ENDSTOP_IS_MIN}, // y axis
{1000, 500, 200, ENDSTOP_IS_MAX}, // z axis
{1600, 50.235478806907409, 1600, 0}, // a extruder
{1600, 50.235478806907409, 1600, 1}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
2, // extruder count
20, // timeout
};
// Axis - max_feedrate, home_feedrate, steps_per_mm, endstop;
// Extruder - max_feedrate, steps_per_mm, motor_steps, has_heated_build_platform;
static Machine replicator_1 = {
{18000, 2500, 94.139704, ENDSTOP_IS_MAX}, // x axis
{18000, 2500, 94.139704, ENDSTOP_IS_MAX}, // y axis
{1170, 1100, 400, ENDSTOP_IS_MIN}, // z axis
{1600, 96.275201870333662468889989185642, 3200, 1}, // a extruder
{1600, 96.275201870333662468889989185642, 3200, 0}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
static Machine replicator_1D = {
{18000, 2500, 94.139704, ENDSTOP_IS_MAX}, // x axis
{18000, 2500, 94.139704, ENDSTOP_IS_MAX}, // y axis
{1170, 1100, 400, ENDSTOP_IS_MIN}, // z axis
{1600, 96.275201870333662468889989185642, 3200, 1}, // a extruder
{1600, 96.275201870333662468889989185642, 3200, 0}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
2, // extruder count
20, // timeout
};
// Axis - max_feedrate, home_feedrate, steps_per_mm, endstop;
// Extruder - max_feedrate, steps_per_mm, motor_steps, has_heated_build_platform;
static Machine replicator_2 = {
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // x axis
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // y axis
{1170, 1100, 400, ENDSTOP_IS_MIN}, // z axis
{1600, 96.275201870333662468889989185642, 3200, 0}, // a extruder
{1600, 96.275201870333662468889989185642, 3200, 0}, // b extruder
1.75, // nominal filament diameter
0.97, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
static Machine replicator_2H = {
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // x axis
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // y axis
{1170, 1100, 400, ENDSTOP_IS_MIN}, // z axis
{1600, 96.275201870333662468889989185642, 3200, 1}, // a extruder
{1600, 96.275201870333662468889989185642, 3200, 0}, // b extruder
1.75, // nominal filament diameter
0.97, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
static Machine replicator_2X = {
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // x axis
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // y axis
{1170, 1100, 400, ENDSTOP_IS_MIN}, // z axis
{1600, 96.275201870333662468889989185642, 3200, 1}, // a extruder
{1600, 96.275201870333662468889989185642, 3200, 1}, // b extruder
1.75, // nominal filament diameter
0.85, // nominal packing density
0.4, // nozzle diameter
2, // extruder count
20, // timeout
};
// The default machine definition is the Replicator 2
Machine machine = {
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // x axis
{18000, 2500, 88.573186, ENDSTOP_IS_MAX}, // y axis
{1170, 1100, 400, ENDSTOP_IS_MIN}, // z axis
{1600, 96.275201870333662468889989185642, 3200, 0}, // a extruder
{1600, 96.275201870333662468889989185642, 3200, 0}, // b extruder
1.75, // nominal filament diameter
0.97, // nominal packing density
0.4, // nozzle diameter
1, // extruder count
20, // timeout
};
// PRIVATE FUNCTION PROTOTYPES
static double get_home_feedrate(int flag);
static void pause_at_zpos(float z_positon);
// GLOBAL VARIABLES
Command command; // the gcode command line
Point5d currentPosition; // the current position of the extruder in 5D space
Point5d targetPosition; // the target poaition the extruder will move to (including G10 offsets)
Point2d excess; // the accumulated rounding error in mm to step conversion
int selectedExtruder; // the current extruder selection (on the virtual tool carosel)
int currentExtruder; // the currently selectd extruder being used by the bot
double currentFeedrate; // the current feed rate
int currentOffset; // current G10 offset
Point3d offset[7]; // G10 offsets
Point3d userOffset; // command line offset
Tool tool[2]; // tool state
Override override[2]; // gcode override
int isRelative; // signals relitive or absolute coordinates
int extruderIsRelative; // signals relitive or absolute coordinates for extruder
int positionKnown; // is the current extruder position known
int programState; // gcode program state used to trigger start and end code sequences
int reprapFlavor; // reprap gcode flavor
int dittoPrinting; // enable ditto printing
int buildProgress; // override build percent
int verboseMode;
unsigned lineNumber; // the current line number in the gcode file
static char buffer[BUFFER_MAX + 1]; // the statically allocated parse-in-place buffer
Filament filament[FILAMENT_MAX];
int filamentLength;
CommandAt commandAt[COMMAND_AT_MAX];
int commandAtIndex;
int commandAtLength;
int macrosEnabled; // M73 P1 or ;@body encountered signalling body start
int pausePending; // signals a pause is pending before the macro script has started
int rewrite5D; // calculate 5D E values rather than scaling them
double layer_height;
FILE *in; // the gcode input file stream
FILE *out; // the x3g output file stream
FILE *out2; // secondary output path
char *sdCardPath;
// cleanup code in case we encounter an error that causes the program to exit
static void exit_handler(void)
{
// close open files
if(in != stdin) {
fclose(in);
if(out != stdout) {
if(ferror(out)) {
perror("while writing to output file");
}
fclose(out);
}
if(out2) {
fclose(out2);
}
}
}
// initialization of global variables
static void initialize_globals(void)
{
int i;
// we default to using pipes
in = stdin;
out = stdout;
out2 = NULL;
sdCardPath = NULL;
// register cleanup function
atexit(exit_handler);
command.flag = 0;
// initialize current position to zero
currentPosition.x = 0.0;
currentPosition.y = 0.0;
currentPosition.z = 0.0;
currentPosition.a = 0.0;
currentPosition.b = 0.0;
command.e = 0.0;
command.f = 0.0;
command.p = 0.0;
command.r = 0.0;
command.s = 0.0;
command.comment = "";
excess.a = 0.0;
excess.b = 0.0;
currentFeedrate = get_home_feedrate(XYZ_BIT_MASK);
currentOffset = 0;
for(i = 0; i < 7; i++) {
offset[i].x = 0.0;
offset[i].y = 0.0;
offset[i].z = 0.0;
}
userOffset.x = 0.0;
userOffset.y = 0.0;
userOffset.z = 0.0;
selectedExtruder = 0;
currentExtruder = 0;
for(i = 0; i < 2; i++) {
tool[i].motor_enabled = 0;
#if ENABLE_SIMULATED_RPM
tool[i].rpm = 0;
#endif
tool[i].nozzle_temperature = 0;
tool[i].build_platform_temperature = 0;
override[i].actual_filament_diameter = 0;
override[i].filament_scale = 1.0;
override[i].packing_density = 1.0;
override[i].standby_temperature = 0;
override[i].active_temperature = 0;
override[i].build_platform_temperature = 0;
}
isRelative = 0;
extruderIsRelative = 0;
positionKnown = 0;
programState = 0;
reprapFlavor = 1; // default is reprap flavor
dittoPrinting = 0;
buildProgress = 0;
verboseMode = 0;
lineNumber = 1;
filament[0].colour = "_null_";
filament[0].diameter = 0.0;
filament[0].temperature = 0;
filament[0].LED = 0;
filamentLength = 1;
commandAtIndex = 0;
commandAtLength = 0;
macrosEnabled = 0;
pausePending = 0;
rewrite5D = 0;
layer_height = 0.34;
}
// STATE
#define start_program() programState = RUNNING_STATE
#define end_program() programState = ENDED_STATE
#define program_is_ready() programState < RUNNING_STATE
#define program_is_running() programState < ENDED_STATE
// IO FUNCTIONS
static void write_8(unsigned char value)
{
if(fputc(value, out) == EOF) exit(1);
if(out2) {
if(fputc(value, out2) == EOF) exit(1);
}
}
static void write_16(unsigned short value)
{
union {
unsigned short s;
unsigned char b[2];
} u;
u.s = value;
if(fputc(u.b[0], out) == EOF) exit(1);
if(fputc(u.b[1], out) == EOF) exit(1);
if(out2) {
if(fputc(u.b[0], out2) == EOF) exit(1);
if(fputc(u.b[1], out2) == EOF) exit(1);
}
}
static void write_32(unsigned int value)
{
union {
unsigned int i;
unsigned char b[4];
} u;
u.i = value;
if(fputc(u.b[0], out) == EOF) exit(1);
if(fputc(u.b[1], out) == EOF) exit(1);
if(fputc(u.b[2], out) == EOF) exit(1);
if(fputc(u.b[3], out) == EOF) exit(1);
if(out2) {
if(fputc(u.b[0], out2) == EOF) exit(1);
if(fputc(u.b[1], out2) == EOF) exit(1);
if(fputc(u.b[2], out2) == EOF) exit(1);
if(fputc(u.b[3], out2) == EOF) exit(1);
}
}
static void write_float(float value)
{
union {
float f;
unsigned char b[4];
} u;
u.f = value;
if(fputc(u.b[0], out) == EOF) exit(1);
if(fputc(u.b[1], out) == EOF) exit(1);
if(fputc(u.b[2], out) == EOF) exit(1);
if(fputc(u.b[3], out) == EOF) exit(1);
if(out2) {
if(fputc(u.b[0], out2) == EOF) exit(1);
if(fputc(u.b[1], out2) == EOF) exit(1);
if(fputc(u.b[2], out2) == EOF) exit(1);
if(fputc(u.b[3], out2) == EOF) exit(1);
}
}
static size_t write_string(char *string, long length)
{
size_t bytes_sent = fwrite(string, 1, length, out);
if(fputc('\0', out) == EOF) exit(1);
if(out2) {
bytes_sent = fwrite(string, 1, length, out2);
if(fputc('\0', out2) == EOF) exit(1);
}
return bytes_sent;
}
// COMMAND @ ZPOS FUNCTIONS
// find an existing filament definition
static int find_filament(char *filament_id)
{
int i, index = -1;
// a brute force search is generally fastest for low n
for(i = 0; i < filamentLength; i++) {
if(strcmp(filament_id, filament[i].colour) == 0) {
index = i;
break;
}
}
return index;
}
// add a new filament definition
static int add_filament(char *filament_id, double diameter, unsigned temperature, unsigned LED)
{
int index = find_filament(filament_id);
if(index < 0) {
if(filamentLength < FILAMENT_MAX) {
index = filamentLength++;
filament[index].colour = strdup(filament_id);
filament[index].diameter = diameter;
filament[index].temperature = temperature;
filament[index].LED = LED;
}
else {
fprintf(stderr, "(line %u) Buffer overflow: too many @filament definitions (maximum = %i)" EOL, lineNumber, FILAMENT_MAX - 1);
index = 0;
}
}
return index;
}
// append a new command at z function
static void add_command_at(double z, char *filament_id, unsigned temperature)
{
static double previous_z = 0.0;
int index = filament_id ? find_filament(filament_id) : 0;
if(index < 0) {
fprintf(stderr, "(line %u) Semantic error: @pause macro with undefined filament name '%s', use a @filament macro to define it" EOL, lineNumber, filament_id);
index = 0;
}
// insert command
if(commandAtLength < COMMAND_AT_MAX) {
if(z <= previous_z) {
int i = commandAtLength;
// make a space
while(i > 0 && z <= commandAt[i - 1].z) {
commandAt[i] = commandAt[i - 1];
i--;
}
commandAt[i].z = z;
commandAt[i].filament_index = index;
commandAt[i].temperature = temperature;
previous_z = commandAt[commandAtLength].z;
}
// append command
else {
commandAt[commandAtLength].z = z;
commandAt[commandAtLength].filament_index = index;
commandAt[commandAtLength].temperature = temperature;
previous_z = z;
}
// nonzero temperature signals a tmperature change, not a pause @ zPos
if(temperature == 0 && commandAtLength == 0) {
if(macrosEnabled) {
pause_at_zpos(z);
}
else {
pausePending = 1;
}
}
commandAtLength++;
}
else {
fprintf(stderr, "(line %u) Buffer overflow: too many @pause definitions (maximum = %i)" EOL, lineNumber, COMMAND_AT_MAX);
}
}
// 5D VECTOR FUNCTIONS
// compute the filament scaling factor
static void set_filament_scale(unsigned extruder_id, double filament_diameter)
{
double actual_radius = filament_diameter / 2;
double nominal_radius = machine.nominal_filament_diameter / 2;
override[extruder_id].filament_scale = (nominal_radius * nominal_radius) / (actual_radius * actual_radius);
}
// return the magnitude (length) of the 5D vector
static double magnitude(int flag, Ptr5d vector)
{
double acc = 0.0;
if(flag & X_IS_SET) {
acc = vector->x * vector->x;
}
if(flag & Y_IS_SET) {
acc += vector->y * vector->y;
}
if(flag & Z_IS_SET) {
acc += vector->z * vector->z;
}
if(flag & A_IS_SET) {
acc += vector->a * vector->a;
}
if(flag & B_IS_SET) {
acc += vector->b * vector->b;
}
return sqrt(acc);
}
// return the largest axis in the vector
static double largest_axis(int flag, Ptr5d vector)
{
double length, rval = 0.0;
if(flag & X_IS_SET) {
rval = fabs(vector->x);
}
if(flag & Y_IS_SET) {
length = fabs(vector->y);
if(rval < length) rval = length;
}
if(flag & Z_IS_SET) {
length = fabs(vector->z);
if(rval < length) rval = length;
}
if(flag & A_IS_SET) {
length = fabs(vector->a);
if(rval < length) rval = length;
}
if(flag & B_IS_SET) {
length = fabs(vector->b);
if(rval < length) rval = length;
}
return rval;
}
// calculate the dda for the longest axis for the current machine definition
static int get_longest_dda()
{
// calculate once
static int longestDDA = 0;
if(longestDDA == 0) {
longestDDA = (int)(60 * 1000000.0 / (machine.x.max_feedrate * machine.x.steps_per_mm));
int axisDDA = (int)(60 * 1000000.0 / (machine.y.max_feedrate * machine.y.steps_per_mm));
if(longestDDA < axisDDA) longestDDA = axisDDA;
axisDDA = (int)(60 * 1000000.0 / (machine.z.max_feedrate * machine.z.steps_per_mm));
if(longestDDA < axisDDA) longestDDA = axisDDA;
}
return longestDDA;
}
// return the maximum home feedrate
static double get_home_feedrate(int flag) {
double feedrate = 0.0;
if(flag & X_IS_SET) {
feedrate = machine.x.home_feedrate;
}
if(flag & Y_IS_SET && feedrate < machine.y.home_feedrate) {
feedrate = machine.y.home_feedrate;
}
if(flag & Z_IS_SET && feedrate < machine.z.home_feedrate) {
feedrate = machine.z.home_feedrate;
}
return feedrate;
}
// return the maximum safe feedrate
static double get_safe_feedrate(int flag, Ptr5d delta) {
double feedrate = currentFeedrate;
if(feedrate == 0.0) {
feedrate = machine.x.max_feedrate;
if(feedrate < machine.y.max_feedrate) {
feedrate = machine.y.max_feedrate;
}
if(feedrate < machine.z.max_feedrate) {
feedrate = machine.z.max_feedrate;
}
if(feedrate < machine.a.max_feedrate) {
feedrate = machine.a.max_feedrate;
}
if(feedrate < machine.b.max_feedrate) {
feedrate = machine.b.max_feedrate;
}
}
double distance = magnitude(flag & XYZ_BIT_MASK, delta);
if(flag & X_IS_SET && (feedrate * delta->x / distance) > machine.x.max_feedrate) {
feedrate = machine.x.max_feedrate * distance / delta->x;
}
if(flag & Y_IS_SET && (feedrate * delta->y / distance) > machine.y.max_feedrate) {
feedrate = machine.y.max_feedrate * distance / delta->y;
}
if(flag & Z_IS_SET && (feedrate * delta->z / distance) > machine.z.max_feedrate) {
feedrate = machine.z.max_feedrate * distance / delta->z;
}
if(distance == 0) {
if(flag & A_IS_SET && feedrate > machine.a.max_feedrate) {
feedrate = machine.a.max_feedrate;
}
if(flag & B_IS_SET && feedrate > machine.b.max_feedrate) {
feedrate = machine.b.max_feedrate;
}
}
else {
if(flag & A_IS_SET && (feedrate * delta->a / distance) > machine.a.max_feedrate) {
feedrate = machine.a.max_feedrate * distance / delta->a;
}
if(flag & B_IS_SET && (feedrate * delta->b / distance) > machine.b.max_feedrate) {
feedrate = machine.b.max_feedrate * distance / delta->b;
}
}
return feedrate;
}
// convert mm to steps using the current machine definition
// IMPORTANT: this command changes the global excess value which accumulates the rounding remainder
static Point5d mm_to_steps(Ptr5d mm, Ptr2d excess)
{
double value;
Point5d result;
result.x = round(mm->x * machine.x.steps_per_mm);
result.y = round(mm->y * machine.y.steps_per_mm);
result.z = round(mm->z * machine.z.steps_per_mm);
if(excess) {
// accumulate rounding remainder
value = (mm->a * machine.a.steps_per_mm) + excess->a;
result.a = round(value);
// changes to excess
excess->a = value - result.a;
value = (mm->b * machine.b.steps_per_mm) + excess->b;
result.b = round(value);
// changes to excess
excess->b = value - result.b;
}
else {
result.a = round(mm->a * machine.a.steps_per_mm);
result.b = round(mm->b * machine.b.steps_per_mm);
}
return result;
}
// X3G COMMANDS
// 131 - Find axes minimums
// 132 - Find axes maximums
static void home_axes(unsigned direction)
{
Point5d unitVector;
int xyz_flag = command.flag & XYZ_BIT_MASK;
double feedrate = command.flag & F_IS_SET ? currentFeedrate : get_home_feedrate(command.flag);
double longestAxis = 0.0;
assert(direction <= 1);
// compute the slowest feedrate
if(xyz_flag & X_IS_SET) {
if(machine.x.home_feedrate < feedrate) {
feedrate = machine.x.home_feedrate;
}
unitVector.x = 1;
longestAxis = machine.x.steps_per_mm;
// confirm machine compatibility
if(direction != machine.x.endstop) {
fprintf(stderr, "(line %u) Semantic warning: X axis homing to %s endstop" EOL, lineNumber, direction ? "maximum" : "minimum");
}
}
if(xyz_flag & Y_IS_SET) {
if(machine.y.home_feedrate < feedrate) {
feedrate = machine.y.home_feedrate;
}
unitVector.y = 1;
if(longestAxis < machine.y.steps_per_mm) {
longestAxis = machine.y.steps_per_mm;
}
if(direction != machine.y.endstop) {
fprintf(stderr, "(line %u) Semantic warning: Y axis homing to %s endstop" EOL, lineNumber, direction ? "maximum" : "minimum");
}
}
if(xyz_flag & Z_IS_SET) {
if(machine.z.home_feedrate < feedrate) {
feedrate = machine.z.home_feedrate;
}
unitVector.z = 1;
if(longestAxis < machine.z.steps_per_mm) {
longestAxis = machine.z.steps_per_mm;
}
if(direction != machine.z.endstop) {
fprintf(stderr, "(line %u) Semantic warning: Z axis homing to %s endstop" EOL, lineNumber, direction ? "maximum" : "minimum");
}
}
// unit vector distance in mm
double distance = magnitude(xyz_flag, &unitVector);
// move duration in microseconds = distance / feedrate * 60,000,000
double microseconds = distance / feedrate * 60000000.0;
// time between steps for longest axis = microseconds / longestStep
unsigned step_delay = (unsigned)round(microseconds / longestAxis);
write_8(direction == ENDSTOP_IS_MIN ? 131 :132);
// uint8: Axes bitfield. Axes whose bits are set will be moved.
write_8(xyz_flag);
// uint32: Feedrate, in microseconds between steps on the max delta. (DDA)
write_32(step_delay);
// uint16: Timeout, in seconds.
write_16(machine.timeout);
}
// 133 - delay
static void delay(unsigned milliseconds)
{
write_8(133);
// uint32: delay, in milliseconds
write_32(milliseconds);
}
// 134 - Change extruder offset
// This is important to use on dual-head Replicators, because the machine needs to know
// the current toolhead in order to apply a calibration offset.
static void change_extruder_offset(unsigned extruder_id)
{
assert(extruder_id < machine.extruder_count);
write_8(134);
// uint8: ID of the extruder to switch to
write_8(extruder_id);
}
// 135 - Wait for extruder ready
static void wait_for_extruder(unsigned extruder_id, unsigned timeout)
{
assert(extruder_id < machine.extruder_count);
write_8(135);
// uint8: ID of the extruder to wait for
write_8(extruder_id);
// uint16: delay between query packets sent to the extruder, in ms (nominally 100 ms)
write_16(100);
// uint16: Timeout before continuing without extruder ready, in seconds (nominally 1 minute)
write_16(timeout);
}
// 136 - extruder action command
// Action 03 - Set extruder target temperature
static void set_nozzle_temperature(unsigned extruder_id, unsigned temperature)
{
assert(extruder_id < machine.extruder_count);
write_8(136);
// uint8: ID of the extruder to query
write_8(extruder_id);
// uint8: Action command to send to the extruder
write_8(3);
// uint8: Length of the extruder command payload (N)
write_8(2);
// int16: Desired target temperature, in Celsius
write_16(temperature);
}
// Action 12 - Enable / Disable fan
static void set_fan(unsigned extruder_id, unsigned state)
{
assert(extruder_id < machine.extruder_count);
write_8(136);
// uint8: ID of the extruder to query
write_8(extruder_id);
// uint8: Action command to send to the extruder
write_8(12);
// uint8: Length of the extruder command payload (N)
write_8(1);
// uint8: 1 to enable, 0 to disable
write_8(state);
}
// Action 13 - Enable / Disable extra output (blower fan)
static void set_valve(unsigned extruder_id, unsigned state)
{
assert(extruder_id < machine.extruder_count);
write_8(136);
// uint8: ID of the extruder to query
write_8(extruder_id);
// uint8: Action command to send to the extruder
write_8(13);
// uint8: Length of the extruder command payload (N)
write_8(1);
// uint8: 1 to enable, 0 to disable
write_8(state);
}
// Action 31 - Set build platform target temperature
static void set_build_platform_temperature(unsigned extruder_id, unsigned temperature)
{
assert(extruder_id < machine.extruder_count);
write_8(136);
// uint8: ID of the extruder to query
write_8(extruder_id);
// uint8: Action command to send to the extruder
write_8(31);
// uint8: Length of the extruder command payload (N)
write_8(2);
// int16: Desired target temperature, in Celsius
write_16(temperature);
}
// 137 - Enable / Disable axes steppers
static void set_steppers(unsigned axes, unsigned state)
{
unsigned bitfield = axes & AXES_BIT_MASK;
if(state) {
bitfield |= 0x80;
}
write_8(137);
// uint8: Bitfield codifying the command (see below)
write_8(bitfield);
}
// 139 - Queue absolute point
static void queue_absolute_point()
{
long longestDDA = get_longest_dda();
Point5d steps = mm_to_steps(&targetPosition, &excess);
write_8(139);
// int32: X coordinate, in steps
write_32((int)steps.x);
// int32: Y coordinate, in steps
write_32((int)steps.y);
// int32: Z coordinate, in steps
write_32((int)steps.z);
// int32: A coordinate, in steps
write_32(-(int)steps.a);
// int32: B coordinate, in steps
write_32(-(int)steps.b);
// uint32: Feedrate, in microseconds between steps on the max delta. (DDA)
write_32((int)longestDDA);
}
// 140 - Set extended position
static void set_position()
{
Point5d steps = mm_to_steps(¤tPosition, NULL);
write_8(140);
// int32: X position, in steps
write_32((int)steps.x);
// int32: Y position, in steps
write_32((int)steps.y);
// int32: Z position, in steps
write_32((int)steps.z);
// int32: A position, in steps
write_32((int)steps.a);
// int32: B position, in steps
write_32((int)steps.b);
}
// 141 - Wait for build platform ready
static void wait_for_build_platform(unsigned extruder_id, int timeout)
{
assert(extruder_id < machine.extruder_count);
write_8(141);
// uint8: ID of the extruder platform to wait for
write_8(extruder_id);