forked from markwal/GPX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpx.c
5559 lines (4717 loc) · 195 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 <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "gpx.h"
#define A 0
#define B 1
#define SHOW(FN) if(gpx->flag.logMessages) {FN;}
#define VERBOSE(FN) if(gpx->flag.verboseMode && gpx->flag.logMessages) {FN;}
#define CALL(FN) if((rval = FN) != SUCCESS) return rval
// 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
1,
};
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
2,
};
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
3,
};
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
4,
};
// 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
5,
};
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
6,
};
// 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
7,
};
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
8,
};
// 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
9,
};
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
10,
};
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
11,
};
#define MACHINE_IS(m) strcasecmp(machine, m) == 0
int gpx_set_machine(Gpx *gpx, char *machine)
{
// only load/clobber the on-board machine definition if the one specified is different
if(MACHINE_IS("c3")) {
if(gpx->machine.type != 1) {
gpx->machine = cupcake_G3;
VERBOSE( fputs("Loading machine definition: Cupcake Gen3 XYZ, Mk5/6 + Gen4 Extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m c3" EOL, gpx->log) );
}
}
else if(MACHINE_IS("c4")) {
if(gpx->machine.type != 2) {
gpx->machine = cupcake_G4;
VERBOSE( fputs("Loading machine definition: Cupcake Gen4 XYZ, Mk5/6 + Gen4 Extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m c4" EOL, gpx->log) );
}
}
else if(MACHINE_IS("cp4")) {
if(gpx->machine.type != 3) {
gpx->machine = cupcake_P4;
VERBOSE( fputs("Loading machine definition: Cupcake Pololu XYZ, Mk5/6 + Gen4 Extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m cp4" EOL, gpx->log) );
}
}
else if(MACHINE_IS("cpp")) {
if(gpx->machine.type != 4) {
gpx->machine = cupcake_PP;
VERBOSE( fputs("Loading machine definition: Cupcake Pololu XYZ, Mk5/6 + Pololu Extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m cpp" EOL, gpx->log) );
}
}
else if(MACHINE_IS("t6")) {
if(gpx->machine.type != 5) {
gpx->machine = thing_o_matic_7;
VERBOSE( fputs("Loading machine definition: TOM Mk6 - single extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m t6" EOL, gpx->log) );
}
}
else if(MACHINE_IS("t7")) {
if(gpx->machine.type != 5) {
gpx->machine = thing_o_matic_7;
VERBOSE( fputs("Loading machine definition: TOM Mk7 - single extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m t7" EOL, gpx->log) );
}
}
else if(MACHINE_IS("t7d")) {
if(gpx->machine.type != 6) {
gpx->machine = thing_o_matic_7D;
VERBOSE( fputs("Loading machine definition: TOM Mk7 - dual extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m t7d" EOL, gpx->log) );
}
}
else if(MACHINE_IS("r1")) {
if(gpx->machine.type != 7) {
gpx->machine = replicator_1;
VERBOSE( fputs("Loading machine definition: Replicator 1 - single extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m r1" EOL, gpx->log) );
}
}
else if(MACHINE_IS("r1d")) {
if(gpx->machine.type != 8) {
gpx->machine = replicator_1D;
VERBOSE( fputs("Loading machine definition: Replicator 1 - dual extruder" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m r1d" EOL, gpx->log) );
}
}
else if(MACHINE_IS("r2")) {
if(gpx->machine.type != 9) {
gpx->machine = replicator_2;
VERBOSE( fputs("Loading machine definition: Replicator 2" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m r2" EOL, gpx->log) );
}
}
else if(MACHINE_IS("r2h")) {
if(gpx->machine.type != 10) {
gpx->machine = replicator_2H;
VERBOSE( fputs("Loading machine definition: Replicator 2 with HBP" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m r2h" EOL, gpx->log) );
}
}
else if(MACHINE_IS("r2x")) {
if(gpx->machine.type != 11) {
gpx->machine = replicator_2X;
VERBOSE( fputs("Loading machine definition: Replicator 2X" EOL, gpx->log) );
}
else {
VERBOSE( fputs("Ignoring duplicate machine definition: -m r2x" EOL, gpx->log) );
}
}
else {
return ERROR;
}
// update known position mask
gpx->axis.mask = gpx->machine.extruder_count == 1 ? (XYZ_BIT_MASK | A_IS_SET) : AXES_BIT_MASK;;
return SUCCESS;
}
// PRIVATE FUNCTION PROTOTYPES
static double get_home_feedrate(Gpx *gpx, int flag);
static int pause_at_zpos(Gpx *gpx, float z_positon);
// initialization of global variables
void gpx_initialize(Gpx *gpx, int firstTime)
{
int i;
gpx->buffer.ptr = gpx->buffer.out;
// we default to using pipes
// initialise machine
if(firstTime) gpx->machine = replicator_2;
// initialise command
gpx->command.x = 0.0;
gpx->command.y = 0.0;
gpx->command.z = 0.0;
gpx->command.a = 0.0;
gpx->command.b = 0.0;
gpx->command.e = 0.0;
gpx->command.f = 0.0;
gpx->command.p = 0.0;
gpx->command.r = 0.0;
gpx->command.s = 0.0;
gpx->command.g = 0.0;
gpx->command.m = 0.0;
gpx->command.t = 0.0;
gpx->command.comment = "";
gpx->command.flag = 0;
// initialize target position
gpx->target.position.x = 0.0;
gpx->target.position.y = 0.0;
gpx->target.position.z = 0.0;
gpx->target.position.a = 0.0;
gpx->target.position.b = 0.0;
gpx->target.extruder = 0;
// initialize current position
gpx->current.position.x = 0.0;
gpx->current.position.y = 0.0;
gpx->current.position.z = 0.0;
gpx->current.position.a = 0.0;
gpx->current.position.b = 0.0;
gpx->current.feedrate = get_home_feedrate(gpx, XYZ_BIT_MASK);
gpx->current.extruder = 0;
gpx->current.offset = 0;
gpx->current.percent = 0;
gpx->axis.positionKnown = 0;
gpx->axis.mask = gpx->machine.extruder_count == 1 ? (XYZ_BIT_MASK | A_IS_SET) : AXES_BIT_MASK;;
// initialize the accumulated rounding error
gpx->excess.a = 0.0;
gpx->excess.b = 0.0;
// initialize the G10 offsets
for(i = 0; i < 7; i++) {
gpx->offset[i].x = 0.0;
gpx->offset[i].y = 0.0;
gpx->offset[i].z = 0.0;
}
// initialize the command line offset
if(firstTime) {
gpx->user.offset.x = 0.0;
gpx->user.offset.y = 0.0;
gpx->user.offset.z = 0.0;
gpx->user.scale = 1.0;
}
for(i = 0; i < 2; i++) {
gpx->tool[i].motor_enabled = 0;
#if ENABLE_SIMULATED_RPM
gpx->tool[i].rpm = 0;
#endif
gpx->tool[i].nozzle_temperature = 0;
gpx->tool[i].build_platform_temperature = 0;
gpx->override[i].actual_filament_diameter = 0;
gpx->override[i].filament_scale = 1.0;
gpx->override[i].packing_density = 1.0;
gpx->override[i].standby_temperature = 0;
gpx->override[i].active_temperature = 0;
gpx->override[i].build_platform_temperature = 0;
}
if(firstTime) {
gpx->filament[0].colour = "_null_";
gpx->filament[0].diameter = 0.0;
gpx->filament[0].temperature = 0;
gpx->filament[0].LED = 0;
gpx->filamentLength = 1;
}
if(firstTime) {
gpx->commandAtIndex = 0;
gpx->commandAtLength = 0;
}
gpx->commandAtZ = 0.0;
// SETTINGS
if(firstTime) {
gpx->sdCardPath = NULL;
gpx->buildName = "GPX " GPX_VERSION;
}
gpx->flag.relativeCoordinates = 0;
gpx->flag.extruderIsRelative = 0;
if(firstTime) {
gpx->flag.reprapFlavor = 1; // reprap flavor is enabled by default
gpx->flag.dittoPrinting = 0;
gpx->flag.buildProgress = 0;
gpx->flag.verboseMode = 0;
gpx->flag.logMessages = 1; // logging is enabled by default
gpx->flag.rewrite5D = 0;
}
// STATE
gpx->flag.programState = 0;
gpx->flag.doPauseAtZPos = 0;
gpx->flag.pausePending = 0;
gpx->flag.macrosEnabled = 0;
if(firstTime) {
gpx->flag.loadMacros = 1;
gpx->flag.runMacros = 1;
}
gpx->flag.framingEnabled = 0;
gpx->longestDDA = 0;
gpx->layerHeight = 0.34;
gpx->lineNumber = 1;
// STATISTICS
gpx->accumulated.a = 0.0;
gpx->accumulated.b = 0.0;
gpx->accumulated.time = 0.0;
gpx->accumulated.bytes = 0;
if(firstTime) {
gpx->total.length = 0.0;
gpx->total.time = 0.0;
gpx->total.bytes = 0;
}
// CALLBACK
gpx->callbackHandler = NULL;
gpx->callbackData = NULL;
// LOGGING
if(firstTime) gpx->log = stderr;
}
// PRINT STATE
#define start_program() gpx->flag.programState = RUNNING_STATE
#define end_program() gpx->flag.programState = ENDED_STATE
#define program_is_ready() gpx->flag.programState < RUNNING_STATE
#define program_is_running() gpx->flag.programState < ENDED_STATE
// IO FUNCTIONS
static void write_8(Gpx *gpx, unsigned char value)
{
*gpx->buffer.ptr++ = value;
}
static unsigned char read_8(Gpx *gpx)
{
return *gpx->buffer.ptr++;
}
static void write_16(Gpx *gpx, unsigned short value)
{
union {
unsigned short s;
unsigned char b[2];
} u;
u.s = value;
*gpx->buffer.ptr++ = u.b[0];
*gpx->buffer.ptr++ = u.b[1];
}
static unsigned short read_16(Gpx *gpx)
{
union {
unsigned short s;
unsigned char b[2];
} u;
u.b[0] = *gpx->buffer.ptr++;
u.b[1] = *gpx->buffer.ptr++;
return u.s;
}
static void write_32(Gpx *gpx, unsigned int value)
{
union {
unsigned int i;
unsigned char b[4];
} u;
u.i = value;
*gpx->buffer.ptr++ = u.b[0];
*gpx->buffer.ptr++ = u.b[1];
*gpx->buffer.ptr++ = u.b[2];
*gpx->buffer.ptr++ = u.b[3];
}
static unsigned int read_32(Gpx *gpx)
{
union {
unsigned int i;
unsigned char b[4];
} u;
u.b[0] = *gpx->buffer.ptr++;
u.b[1] = *gpx->buffer.ptr++;
u.b[2] = *gpx->buffer.ptr++;
u.b[3] = *gpx->buffer.ptr++;
return u.i;
}
static void write_float(Gpx *gpx, float value)
{
union {
float f;
unsigned char b[4];
} u;
u.f = value;
*gpx->buffer.ptr++ = u.b[0];
*gpx->buffer.ptr++ = u.b[1];
*gpx->buffer.ptr++ = u.b[2];
*gpx->buffer.ptr++ = u.b[3];
}
static float read_float(Gpx *gpx)
{
union {
float f;
unsigned char b[4];
} u;
u.b[0] = *gpx->buffer.ptr++;
u.b[1] = *gpx->buffer.ptr++;
u.b[2] = *gpx->buffer.ptr++;
u.b[3] = *gpx->buffer.ptr++;
return u.f;
}
static long write_bytes(Gpx *gpx, char *data, long length)
{
long l = length;
while(l--) {
*gpx->buffer.ptr++ = *data++;
}
return length;
}
static long read_bytes(Gpx *gpx, char *data, long length)
{
long l = length;
while(l--) {
*data++ = *gpx->buffer.ptr++;
}
return length;
}
static long write_string(Gpx *gpx, char *string, long length)
{
long l = length;
while(l--) {
*gpx->buffer.ptr++ = *string++;
}
*gpx->buffer.ptr++ = '\0';
return length;
}
// FRAMING
static unsigned char calculate_crc(unsigned char *addr, long len)
{
unsigned char data, crc = 0;
while(len--) {
data = *addr++;
// 8-bit iButton/Maxim/Dallas CRC loop unrolled
crc = crc ^ data;
// 1
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
// 2
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
// 3
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
// 4
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
// 5
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
// 6
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
// 7
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
// 8
if (crc & 0x01) crc = (crc >> 1) ^ 0x8C;
else crc >>= 1;
}
return crc;
}
static void begin_frame(Gpx *gpx)
{
gpx->buffer.ptr = gpx->buffer.out;
if(gpx->flag.framingEnabled) {
gpx->buffer.out[0] = 0xD5; // synchronization byte
gpx->buffer.ptr += 2;
}
}
static int end_frame(Gpx *gpx)
{
if(gpx->flag.framingEnabled) {
unsigned char *start = (unsigned char *)gpx->buffer.out + 2;
unsigned char *end = (unsigned char *)gpx->buffer.ptr;
size_t payload_length = end - start;
gpx->buffer.out[1] = (unsigned char)payload_length;
*gpx->buffer.ptr++ = calculate_crc(start, payload_length);
}
size_t length = gpx->buffer.ptr - gpx->buffer.out;
gpx->accumulated.bytes += length;
if(gpx->callbackHandler) return gpx->callbackHandler(gpx, gpx->callbackData, gpx->buffer.out, length);
return SUCCESS;
}
// 5D VECTOR FUNCTIONS
// compute the filament scaling factor
static void set_filament_scale(Gpx *gpx, unsigned extruder_id, double filament_diameter)
{
double actual_radius = filament_diameter / 2;
double nominal_radius = gpx->machine.nominal_filament_diameter / 2;
gpx->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, result = 0.0;
if(flag & X_IS_SET) {
result = fabs(vector->x);
}
if(flag & Y_IS_SET) {
length = fabs(vector->y);
if(result < length) result = length;
}
if(flag & Z_IS_SET) {
length = fabs(vector->z);
if(result < length) result = length;
}
if(flag & A_IS_SET) {
length = fabs(vector->a);
if(result < length) result = length;
}
if(flag & B_IS_SET) {
length = fabs(vector->b);
if(result < length) result = length;
}
return result;
}
// calculate the dda for the longest axis for the current machine definition
static int get_longest_dda(Gpx *gpx)
{
// calculate once
int longestDDA = gpx->longestDDA;
if(longestDDA == 0) {
longestDDA = (int)(60 * 1000000.0 / (gpx->machine.x.max_feedrate * gpx->machine.x.steps_per_mm));
int axisDDA = (int)(60 * 1000000.0 / (gpx->machine.y.max_feedrate * gpx->machine.y.steps_per_mm));
if(longestDDA < axisDDA) longestDDA = axisDDA;
axisDDA = (int)(60 * 1000000.0 / (gpx->machine.z.max_feedrate * gpx->machine.z.steps_per_mm));
if(longestDDA < axisDDA) longestDDA = axisDDA;
gpx->longestDDA = longestDDA;
}
return longestDDA;
}
// return the maximum home feedrate
static double get_home_feedrate(Gpx *gpx, int flag) {
double feedrate = 0.0;
if(flag & X_IS_SET) {
feedrate = gpx->machine.x.home_feedrate;
}
if(flag & Y_IS_SET && feedrate < gpx->machine.y.home_feedrate) {
feedrate = gpx->machine.y.home_feedrate;
}
if(flag & Z_IS_SET && feedrate < gpx->machine.z.home_feedrate) {
feedrate = gpx->machine.z.home_feedrate;
}
return feedrate;
}
// return the maximum safe feedrate
static double get_safe_feedrate(Gpx *gpx, int flag, Ptr5d delta) {
double feedrate = gpx->current.feedrate;
if(feedrate == 0.0) {
feedrate = gpx->machine.x.max_feedrate;
if(feedrate < gpx->machine.y.max_feedrate) {
feedrate = gpx->machine.y.max_feedrate;
}
if(feedrate < gpx->machine.z.max_feedrate) {
feedrate = gpx->machine.z.max_feedrate;
}
if(feedrate < gpx->machine.a.max_feedrate) {
feedrate = gpx->machine.a.max_feedrate;
}
if(feedrate < gpx->machine.b.max_feedrate) {
feedrate = gpx->machine.b.max_feedrate;
}
}
double distance = magnitude(flag & XYZ_BIT_MASK, delta);
if(flag & X_IS_SET && (feedrate * delta->x / distance) > gpx->machine.x.max_feedrate) {
feedrate = gpx->machine.x.max_feedrate * distance / delta->x;
}
if(flag & Y_IS_SET && (feedrate * delta->y / distance) > gpx->machine.y.max_feedrate) {
feedrate = gpx->machine.y.max_feedrate * distance / delta->y;
}
if(flag & Z_IS_SET && (feedrate * delta->z / distance) > gpx->machine.z.max_feedrate) {
feedrate = gpx->machine.z.max_feedrate * distance / delta->z;
}
if(distance == 0) {
if(flag & A_IS_SET && feedrate > gpx->machine.a.max_feedrate) {
feedrate = gpx->machine.a.max_feedrate;
}
if(flag & B_IS_SET && feedrate > gpx->machine.b.max_feedrate) {
feedrate = gpx->machine.b.max_feedrate;
}
}
else {
if(flag & A_IS_SET && (feedrate * delta->a / distance) > gpx->machine.a.max_feedrate) {
feedrate = gpx->machine.a.max_feedrate * distance / delta->a;
}
if(flag & B_IS_SET && (feedrate * delta->b / distance) > gpx->machine.b.max_feedrate) {
feedrate = gpx->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(Gpx *gpx, Ptr5d mm, Ptr2d excess)
{
double value;
Point5d result;
result.x = round(mm->x * gpx->machine.x.steps_per_mm);
result.y = round(mm->y * gpx->machine.y.steps_per_mm);
result.z = round(mm->z * gpx->machine.z.steps_per_mm);
if(excess) {
// accumulate rounding remainder
value = (mm->a * gpx->machine.a.steps_per_mm) + excess->a;
result.a = round(value);
// changes to excess
excess->a = value - result.a;
value = (mm->b * gpx->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 * gpx->machine.a.steps_per_mm);
result.b = round(mm->b * gpx->machine.b.steps_per_mm);
}
return result;
}
static Point5d delta_mm(Gpx *gpx)
{
Point5d deltaMM;
// compute the relative distance traveled along each axis and convert to steps
if(gpx->command.flag & X_IS_SET) deltaMM.x = gpx->target.position.x - gpx->current.position.x; else deltaMM.x = 0;
if(gpx->command.flag & Y_IS_SET) deltaMM.y = gpx->target.position.y - gpx->current.position.y; else deltaMM.y = 0;
if(gpx->command.flag & Z_IS_SET) deltaMM.z = gpx->target.position.z - gpx->current.position.z; else deltaMM.z = 0;
if(gpx->command.flag & A_IS_SET) deltaMM.a = gpx->target.position.a - gpx->current.position.a; else deltaMM.a = 0;
if(gpx->command.flag & B_IS_SET) deltaMM.b = gpx->target.position.b - gpx->current.position.b; else deltaMM.b = 0;
return deltaMM;
}
static Point5d delta_steps(Gpx *gpx,Point5d deltaMM)
{
Point5d deltaSteps;
// compute the relative distance traveled along each axis and convert to steps
if(gpx->command.flag & X_IS_SET) deltaSteps.x = round(fabs(deltaMM.x) * gpx->machine.x.steps_per_mm); else deltaSteps.x = 0;
if(gpx->command.flag & Y_IS_SET) deltaSteps.y = round(fabs(deltaMM.y) * gpx->machine.y.steps_per_mm); else deltaSteps.y = 0;
if(gpx->command.flag & Z_IS_SET) deltaSteps.z = round(fabs(deltaMM.z) * gpx->machine.z.steps_per_mm); else deltaSteps.z = 0;
if(gpx->command.flag & A_IS_SET) deltaSteps.a = round(fabs(deltaMM.a) * gpx->machine.a.steps_per_mm); else deltaSteps.a = 0;
if(gpx->command.flag & B_IS_SET) deltaSteps.b = round(fabs(deltaMM.b) * gpx->machine.b.steps_per_mm); else deltaSteps.b = 0;
return deltaSteps;
}
// X3G QUERIES
#define COMMAND_OFFSET 2
#define EXTRUDER_ID_OFFSET 3
#define QUERY_COMMAND_OFFSET 4
#define EEPROM_LENGTH_OFFSET 8
// 00 - Get version
static int get_version(Gpx *gpx)
{
begin_frame(gpx);
write_8(gpx, 0);
// uint16: host version
write_16(gpx, HOST_VERSION);
return end_frame(gpx);
}
/* 01 - Initialize firmware to boot state
This is treated as a NOOP in the Sailfish firmware. */
static int initialize_firmware(Gpx *gpx)
{
begin_frame(gpx);
write_8(gpx, 1);
return end_frame(gpx);
}
// 02 - Get available buffer size
static int get_buffer_size(Gpx *gpx)
{
begin_frame(gpx);
write_8(gpx, 2);
return end_frame(gpx);
}
// 03 - Clear buffer (same as 07 and 17)
static int clear_buffer(Gpx *gpx)
{
begin_frame(gpx);
write_8(gpx, 3);
return end_frame(gpx);
}
// 07 - Abort immediately
static int abort_immediately(Gpx *gpx)
{
begin_frame(gpx);
write_8(gpx, 7);
return end_frame(gpx);
}
// 08 - Pause/Resume
static int pause_resume(Gpx *gpx)
{
begin_frame(gpx);
write_8(gpx, 8);
return end_frame(gpx);
}
// 10 - Extruder Query Commands
// Query 00 - Query firmware version information
static int get_extruder_version(Gpx *gpx, unsigned extruder_id)
{
begin_frame(gpx);
write_8(gpx, 10);
// uint8: ID of the extruder to query
write_8(gpx, extruder_id);
// uint8: Query command to send to the extruder
write_8(gpx, 0);
// uint8: Length of the extruder command payload (N)
write_8(gpx, 2);
// uint16: host version
write_16(gpx, HOST_VERSION);
return end_frame(gpx);
}