-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgcode_process.c
1502 lines (1409 loc) · 46.5 KB
/
gcode_process.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
/** \file
\brief Work out what to do with received G-Code commands
*/
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include "bebopr.h"
#include "gcode_process.h"
#include "gcode_parse.h"
#include "debug.h"
#include "temp.h"
#include "heater.h"
#include "home.h"
#include "traject.h"
#include "pruss_stepper.h"
#include "heater.h"
#include "mendel.h"
#include "limit_switches.h"
#include "pepper.h"
/// the current tool
static uint8_t tool;
/// the tool to be changed when we get an M6
static uint8_t next_tool;
// 20111017 modmaker - use nanometers instead of steps for position
/// variable that holds the idea of 'current position' for the gcode interpreter.
/// the actual machine position will probably lag!
static TARGET gcode_current_pos;
// Home Position holds the offset set by G92, it is used to convert the
// gcode coordinates to machine / PRUSS coordinates.
static TARGET gcode_home_pos;
static double gcode_current_feed;
/*
* Local copy of channel tags to prevent a lookup with each access.
*/
static channel_tag heater_extruder = NULL;
static channel_tag heater_bed = NULL;
static channel_tag temp_extruder = NULL;
static channel_tag temp_bed = NULL;
static channel_tag pwm_extruder = NULL;
static channel_tag pwm_fan = NULL;
static channel_tag pwm_bed = NULL;
static int extruder_temp_wait = 0;
static int bed_temp_wait = 0;
/*
private functions
*/
static void wait_for_slow_signals( move5D* move)
{
if (DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
if (move) {
printf( "\nMOVE[ %lu] - defer move(s) until temperature(s) are stable\n",
move->serno);
} else {
printf( "\nnon-MOVE - defer processing until temperature(s) are stable\n");
}
}
while ( (extruder_temp_wait && !heater_temp_reached( heater_extruder)) ||
(bed_temp_wait && !heater_temp_reached( heater_bed)) )
{
usleep( 100000);
}
extruder_temp_wait = 0;
bed_temp_wait = 0;
if (DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
printf( "Resume after waiting for temperatures to stabilize\n");
}
}
/*
* Generate unique serial numbers for moves
*/
static inline long unsigned int get_serno( void)
{
static long unsigned int serno = 0;
return ++serno;
}
/*
* Calculate traject for move to 'target' position and store result in 'move'
*
* Move from 'gcode_current_pos' to 'target', using 'gcode_home_pos' as offset
* with feed from 'target'.
*
* Calling this routine should have no side effects!!!
* (exception: global serial number increment in traject.c)
*/
static void move_calc( unsigned long seqno, const TARGET* target, TARGET* current_pos, move5D* move)
{
move->serno = seqno;
if (DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
printf( "\nMOVE[ %lu] move_calc() for TARGET=( %d, %d, %d, %d, %u)\n",
move->serno, target->X, target->Y, target->Z, target->E, target->F);
}
/*
* The traject code requires SI coordinates.
* The integer TARGET position coordinates are in nm !
*/
traject5D traject = {
.s0x = POS2SI( gcode_home_pos.X + current_pos->X),
.s0y = POS2SI( gcode_home_pos.Y + current_pos->Y),
.s0z = POS2SI( gcode_home_pos.Z + current_pos->Z),
.s0e = POS2SI( gcode_home_pos.E + current_pos->E),
.s1x = POS2SI( gcode_home_pos.X + target->X),
.s1y = POS2SI( gcode_home_pos.Y + target->Y),
.s1z = POS2SI( gcode_home_pos.Z + target->Z),
.s1e = POS2SI( gcode_home_pos.E + target->E),
.feed = (double)target->F,
};
/*
* Calculate the trajectory details (e.g. velocities)) and store these in 'move'
*/
traject_calc_all_axes( &traject, move);
}
/*
* Execute actual move to new position as specified in 'move'
*
* Synchronize with slow signals.
*
*/
static void move_execute( move5D* move)
{
if (extruder_temp_wait || bed_temp_wait) {
if (DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
printf( "\nMOVE[ %lu] move_execute() - waiting for slow signals)\n",
move->serno);
}
wait_for_slow_signals( move);
}
if (DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
printf( "MOVE[ %lu] move_execute() from ( %1.6lf, %1.6lf, %1.6lf, %1.6lf) [m] with feed %1.3lf [mm/min]\n"
"MOVE ............ over ( %1.6lf, %1.6lf, %1.6lf, %1.6lf) [m]\n"
"MOVE ........ velocity ( %1.6lf, %1.6lf, %1.6lf, %1.6lf) [m/s]\n",
move->serno, move->s0x, move->s0y, move->s0z, move->s0e,
move->feed, move->dx, move->dy, move->dz, move->de,
move->vx, move->vy, move->vz, move->ve);
}
/* actually make the move */
traject_move_all_axes( move);
}
/*
* make a move to new 'target' position, at the end of this move 'target'
* should reflect the actual position.
*/
static void enqueue_pos( const TARGET* target)
{
if (target) {
move5D move;
move_calc( get_serno(), target, &gcode_current_pos, &move);
move_execute( &move);
}
}
/************************************************************************//**
\brief Processes command stored in global \ref next_target.
This is where we work out what to actually do with each command we
receive. All data has already been scaled to integers in gcode_parse.
If you want to add support for a new G or M code, this is the place.
*//*************************************************************************/
void dump_position_info( void)
{
printf( "current: X=%1.6lf, Y=%1.6lf, Z=%1.6lf, E=%1.6lf, F=%1.6lf\n",
POS2MM( gcode_current_pos.X), POS2MM( gcode_current_pos.Y),
POS2MM( gcode_current_pos.Z), POS2MM( gcode_current_pos.E),
gcode_current_feed);
printf( "origin: X=%1.6lf, Y=%1.6lf, Z=%1.6lf, E=%1.6lf\n",
POS2MM( gcode_home_pos.X), POS2MM( gcode_home_pos.Y),
POS2MM( gcode_home_pos.Z), POS2MM( gcode_home_pos.E));
pruss_dump_position();
}
static void clip_move( axis_e axis, int32_t* pnext_target, int32_t current_pos, int32_t home_pos)
{
double limit;
if (*pnext_target >= current_pos) {
// forward move or no move
if (config_max_soft_limit( axis, &limit)) {
int32_t pos_limit = MM2POS( limit);
if (home_pos + current_pos > pos_limit) {
pos_limit = home_pos + current_pos;
}
if (home_pos + *pnext_target > pos_limit) {
printf( "WARNING: Clipping target.%c (%d) to %d due to upper soft limit= %d (home= %d)\n",
axisName( axis), *pnext_target, pos_limit, MM2POS( limit), home_pos);
*pnext_target = pos_limit - home_pos;
}
}
} else {
// backward move
if (config_min_soft_limit( axis, &limit)) {
int32_t pos_limit = MM2POS( limit);
if (home_pos + current_pos < pos_limit) {
pos_limit = home_pos + current_pos;
}
if (home_pos + *pnext_target < pos_limit) {
printf( "WARNING: Clipping target.%c (%d) to %d due to lower soft limit= %d (home= %d)\n",
axisName( axis), *pnext_target, pos_limit, MM2POS( limit), home_pos);
*pnext_target = pos_limit - home_pos;
}
}
}
}
static void process_non_move_command( GCODE_COMMAND* target)
{
uint32_t backup_f;
if (target->seen_F) {
gcode_current_feed = target->target.F;
} else {
target->target.F = gcode_current_feed;
}
// convert relative to absolute
if (target->option_relative) {
target->target.X += gcode_current_pos.X;
target->target.Y += gcode_current_pos.Y;
target->target.Z += gcode_current_pos.Z;
target->target.E += gcode_current_pos.E;
}
// The GCode documentation was taken from http://reprap.org/wiki/Gcode .
if (target->seen_T) {
//? ==== T: Select Tool ====
//?
//? Example: T1
//?
//? Select extruder number 1 to build with. Extruder numbering starts at 0.
next_tool = target->T;
}
// if we didn't see an axis word, set it to gcode_current_pos. this fixes incorrect moves after homing TODO: fix homing ???
//TODO: fix this ???
if (target->seen_X == 0)
target->target.X = gcode_current_pos.X;
if (target->seen_Y == 0)
target->target.Y = gcode_current_pos.Y;
if (target->seen_Z == 0)
target->target.Z = gcode_current_pos.Z;
if (target->seen_E == 0)
target->target.E = gcode_current_pos.E;
if (target->seen_G) {
uint8_t axisSelected = 0;
switch (target->G) {
// G0 - rapid, unsynchronised motion
// since it would be a major hassle to force the dda to not synchronise, just provide a fast feedrate and hope it's close enough to what host expects
case 0:
//? ==== G0: Rapid move ====
//?
//? 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.)
case 1:
{
#if 1
printf( "\n\n\nOOPS\n\nThis shouldn't happen!!!!\n\n\n");
#else
//? ==== G1: Controlled move ====
//?
//? 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.
/*
* Implement soft axis limits:
*
* The soft axis limits define a safe operating zone.
* Coordinates are clipped in such a way that no moves are generate that would move
* from the inside to the outside of the safe operating zone. All moves from outside
* the safe operating zone directed towards the inside of the zone are allowed!
*/
if (target->seen_X) {
clip_move( x_axis, &target->target.X, gcode_current_pos.X, gcode_home_pos.X);
}
if (target->seen_Y) {
clip_move( y_axis, &target->target.Y, gcode_current_pos.Y, gcode_home_pos.Y);
}
if (target->seen_Z) {
clip_move( z_axis, &target->target.Z, gcode_current_pos.Z, gcode_home_pos.Z);
}
if (target->G == 0) {
backup_f = target->target.F;
target->target.F = 100000; // will be limited by the limitations of the individual axes
enqueue_pos( &target->target);
target->target.F = backup_f;
} else {
// synchronised motion
enqueue_pos( &target->target);
}
/* update our sense of position */
gcode_current_pos.X = target->target.X;
gcode_current_pos.Y = target->target.Y;
gcode_current_pos.Z = target->target.Z;
gcode_current_pos.E = target->target.E;
gcode_current_feed = target->target.F;
#endif
break;
}
// G2 - Arc Clockwise
// unimplemented
// G3 - Arc Counter-clockwise
// unimplemented
// G4 - Dwell
case 4:
//? ==== G4: Dwell ====
//?
//? Example: G4 P200
//?
//? In this case sit still doing nothing for 200 milliseconds. During delays the state of the machine (for example the temperatures of its extruders) will still be preserved and controlled.
//?
traject_wait_for_completion();
usleep( 1000* target->P);
break;
// G20 - inches as units
case 20:
//? ==== G20: Set Units to Inches ====
//?
//? Example: G20
//?
//? Units from now on are in inches.
//?
target->option_inches = 1;
break;
// G21 - mm as units
case 21:
//? ==== G21: Set Units to Millimeters ====
//?
//? Example: G21
//?
//? Units from now on are in millimeters. (This is the RepRap default.)
//?
target->option_inches = 0;
break;
// G30 - go home via point
case 30:
//? ==== G30: Go home via point ====
//?
//? Undocumented.
enqueue_pos( &target->target);
// no break here, G30 is move and then go home
// G28 - Move to Origin
case 28:
//? ==== G28: Move to Origin ====
//?
// 20110817 modmaker - Changed implementation according to info found in
// these docs: http://linuxcnc.org/docs/html/gcode.html,
// http://reprap.org/wiki/MCodeReference and http://reprap.org/wiki/GCodes .
// G28 generates a rapid traversal to the origin (or a preset position).
// Implementation: G0 like move with as destination the origin (x,y,z=0,0,0).
// The (absolute) origin is set at startup (current position) or by executing
// a calibration command (G161/G162) for one or more axes.
if (target->seen_X) {
target->target.X = 0;
axisSelected = 1;
}
if (target->seen_Y) {
target->target.Y = 0;
axisSelected = 1;
}
if (target->seen_Z) {
target->target.Z = 0;
axisSelected = 1;
}
if (axisSelected != 1) {
target->target.X = 0;
target->target.Y = 0;
target->target.Z = 0;
}
backup_f = target->target.F;
target->target.F = 99999; // let the software clip this to the maximum allowed rate
enqueue_pos( &target->target);
target->target.F = backup_f;
break;
// G90 - absolute positioning
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.)
target->option_relative = 0;
break;
// G91 - relative positioning
case 91:
//? ==== G91: Set to Relative Positioning ====
//?
//? Example: G91
//?
//? All coordinates from now on are relative to the last position.
target->option_relative = 1;
break;
// G92 - set home
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.
traject_wait_for_completion();
if (target->seen_X) {
gcode_home_pos.X += gcode_current_pos.X - target->target.X;
gcode_current_pos.X = target->target.X;
axisSelected = 1;
}
if (target->seen_Y) {
gcode_home_pos.Y += gcode_current_pos.Y - target->target.Y;
gcode_current_pos.Y = target->target.Y;
axisSelected = 1;
}
if (target->seen_Z) {
gcode_home_pos.Z += gcode_current_pos.Z - target->target.Z;
gcode_current_pos.Z = target->target.Z;
axisSelected = 1;
}
// Have to handle E-axis specially because it may be a relative-only axis
if (target->seen_E) {
if (!config_e_axis_is_always_relative() && target->target.E == 0) {
// slicers use this te adjust the origin to prevent running
// out of E range, adjust the PRUSS internal origin too.
pruss_queue_adjust_origin( 4, gcode_home_pos.E + gcode_current_pos.E);
// gcode_home_pos can overflow too, so clear it! NOTE: the E-axis
// now doesn't behave like a normal (absolute) axis anymore!
gcode_home_pos.E = 0;
} else {
gcode_home_pos.E += gcode_current_pos.E - target->target.E;
}
gcode_current_pos.E = target->target.E;
axisSelected = 1;
}
if (axisSelected == 0) {
gcode_home_pos.X += gcode_current_pos.X;
gcode_current_pos.X = target->target.X = 0;
gcode_home_pos.Y += gcode_current_pos.Y;
gcode_current_pos.Y = target->target.Y = 0;
gcode_home_pos.Z += gcode_current_pos.Z;
gcode_current_pos.Z = target->target.Z = 0;
gcode_home_pos.E += gcode_current_pos.E;
gcode_current_pos.E = target->target.E = 0;
}
break;
#define FOR_ONE_AXIS( axis_lc, axis_uc, axis_i, code) \
do { \
axis_xyz = axis_lc##_axis; \
pruss_axis_xyz = axis_i; \
next_target_seen_xyz = target->seen_##axis_uc; \
current_pos_xyz = gcode_current_pos.axis_uc; \
home_pos_xyz = gcode_home_pos.axis_uc; \
code \
gcode_current_pos.axis_uc = current_pos_xyz; \
gcode_home_pos.axis_uc = home_pos_xyz; \
} while (0)
#define FOR_EACH_AXIS_IN_XYZ( code) \
do { \
uint32_t feed = target->target.F; \
axis_e axis_xyz; \
int pruss_axis_xyz; \
int next_target_seen_xyz; \
int32_t current_pos_xyz; \
int32_t home_pos_xyz; \
/* X */ \
FOR_ONE_AXIS( x, X, 1, code); \
FOR_ONE_AXIS( y, Y, 2, code); \
FOR_ONE_AXIS( z, Z, 3, code); \
} while (0)
#define MIN_LIMIT_SWITCH 1
#define MAX_LIMIT_SWITCH 0
// G161 - Home negative
case 161:
// G162 - Home positive
case 162:
{
//? ==== G161: Home negative ====
//? ==== G162: Home positive ====
//?
//? Find the corresponding limit of the specified axes by searching for the limit switch.
// reference 'home' position to (then) current position
// Also used feed override for homing
double factor = traject_set_speed_override( 0.0); // get old value
traject_set_speed_override( factor); // restore
target->target.F *= factor;
fprintf( stdout, "Homing feed = %u (factor=%1.3lf)\n", target->target.F, factor);
// NOTE: G161/G162 clears any G92 offset !
double pos;
if (DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
fprintf( stderr, "G16%c: X(%d)=%d, Y(%d)=%d, Z(%d)=%d, E(%d)=%d, F(%d)=%d\n",
(target->G == 162) ? '2' : '1',
target->seen_X, target->target.X,
target->seen_Y, target->target.Y,
target->seen_Z, target->target.Z,
target->seen_E, target->target.E,
target->seen_F, target->target.F );
}
FOR_EACH_AXIS_IN_XYZ(
if (next_target_seen_xyz) {
int limit_switch = (target->G == 161) ? MIN_LIMIT_SWITCH : MAX_LIMIT_SWITCH;
/*
* Temporarily use machine coordinates during homing
*/
current_pos_xyz += home_pos_xyz;
home_axis_to_limit_switch( axis_xyz, ¤t_pos_xyz, feed, limit_switch);
current_pos_xyz -= home_pos_xyz;
/*
* If the limit switch is also a calibration switch, set the origin
*/
int is_reference_switch;
if (limit_switch == MIN_LIMIT_SWITCH) {
is_reference_switch = config_min_switch_pos( axis_xyz, &pos);
} else {
is_reference_switch = config_max_switch_pos( axis_xyz, &pos);
}
if (is_reference_switch) {
home_pos_xyz = 0;
current_pos_xyz = SI2POS( pos);
pruss_queue_set_position( pruss_axis_xyz, home_pos_xyz + current_pos_xyz);
}
} );
break;
}
// G244 - Resume halted PRUSS
case 244: {
int i = 1;
if (!target->seen_S || target->S > 1) {
i = target->S; // number of iterations
}
do {
pruss_stepper_resume();
if (i > 1) {
while (!pruss_stepper_halted()) {
usleep( 100);
}
}
} while (--i > 0);
break;
}
// G255 - Dump PRUSS state
case 255:
// === G255: Dump PRUSS state ====
// The (optional) parameter S0, will disable waiting
// for the current command to complete, before dumping.
if (!target->seen_S || target->S != 0) {
traject_wait_for_completion();
}
pruss_stepper_dump_state();
break;
// unknown gcode: spit an error
default:
printf("E: Bad G-code %d", target->G);
// newline is sent from gcode_parse after we return
return;
}
#ifdef DEBUG
if (DEBUG_POSITION && (debug_flags & DEBUG_POSITION)) {
dump_position_info();
# if 0
traject_status_print();
# endif
}
#endif
}
else if (target->seen_M) {
switch (target->M) {
// M0- machine stop
case 0:
// M2- program end
case 2:
//? ==== M2: program end ====
//?
//? Undocumented.
traject_wait_for_completion();
// no break- we fall through to M112 below
// M112- immediate stop
case 112:
//? ==== M112: Emergency Stop ====
//?
//? Example: M112
//?
//? Any moves in progress are immediately terminated, then RepRap shuts down. All motors and heaters are turned off.
//? It can be started again by pressing the reset button on the master microcontroller. See also M0.
traject_abort();
x_disable();
y_disable();
z_disable();
e_disable();
power_off();
for (;;) {
usleep( 1000);
}
break;
// M6- tool change
case 6:
//? ==== M6: tool change ====
//?
//? Undocumented.
tool = next_tool;
break;
// M82- set extruder to absolute mode
case 82: {
int old_mode = config_set_e_axis_mode( 0);
if (old_mode != 0 && DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
fprintf( stderr, "M82: switching to absolute extruder coordinates\n");
}
break;
}
// M83- set extruder to relative mode
case 83: {
int old_mode = config_set_e_axis_mode( 1);
if (old_mode == 0 && DEBUG_GCODE_PROCESS && (debug_flags & DEBUG_GCODE_PROCESS)) {
fprintf( stderr, "M83: switching to relative extruder coordinates\n");
}
break;
}
// M84- stop idle hold
case 84:
x_disable();
y_disable();
z_disable();
e_disable();
break;
// M3/M101- extruder on
case 3:
case 101:
//? ==== M101: extruder on ====
//?
//? Undocumented.
#ifdef DC_EXTRUDER
heater_set(DC_EXTRUDER, DC_EXTRUDER_PWM);
#elif E_STARTSTOP_STEPS > 0
do {
// backup feedrate, move E very quickly then restore feedrate
backup_f = gcode_current_feed;
gcode_current_feed = MAXIMUM_FEEDRATE_E;
SpecialMoveE( E_STARTSTOP_STEPS, MAXIMUM_FEEDRATE_E);
gcode_current_feed = backup_f;
} while (0);
#endif
break;
// M102- extruder reverse
// M5/M103- extruder off
case 5:
case 103:
//? ==== M103: extruder off ====
//?
//? Undocumented.
#ifdef DC_EXTRUDER
heater_set(DC_EXTRUDER, 0);
#elif E_STARTSTOP_STEPS > 0
do {
// backup feedrate, move E very quickly then restore feedrate
backup_f = gcode_current_feed;
gcode_current_feed = MAXIMUM_FEEDRATE_E;
SpecialMoveE( E_STARTSTOP_STEPS, MAXIMUM_FEEDRATE_E);
gcode_current_feed = backup_f;
} while (0);
#endif
break;
// M104- set temperature
case 104:
//? ==== M104: Set Extruder Temperature (Fast) ====
case 140:
//? ==== M140: Set heated bed temperature (Fast) ====
case 109:
//? ==== M109: Set Extruder Temperature (Wait) ====
case 190:
//? ==== M190: Set Bed Temperature (Wait) ====
{
channel_tag heater;
if (target->M == 140 || target->M == 190) {
heater = heater_bed;
} else {
if (target->seen_P && target->P == 1) {
heater = heater_bed;
} else {
heater = heater_extruder;
}
}
if (target->seen_S) {
heater_set_setpoint( heater, target->S);
// if setpoint is not null, turn power on
if (target->S > 0) {
power_on();
heater_enable( heater_extruder, 1);
} else {
heater_enable( heater_extruder, 0);
}
}
if (target->M == 109 || target->M == 190) {
if (heater == heater_bed) {
bed_temp_wait = 1;
} else {
extruder_temp_wait = 1;
}
}
break;
}
// M105- get temperature
case 105: {
//? ==== M105: Get Extruder Temperature ====
//?
//? Example: M105
//?
//? Request the temperature of the current extruder and the build base in degrees Celsius. The temperatures are returned to the host computer. For example, the line sent to the host in response to this command looks like
//?
//? <tt>ok T:201 B:117</tt>
//?
//? Teacup supports an optional P parameter as a sensor index to address.
double celsius;
# ifdef ENFORCE_ORDER
// wait for all moves to complete
traject_wait_for_completion();
# endif
if (target->seen_P) {
channel_tag temp_source;
switch (target->P) {
case 0: temp_source = heater_extruder; break;
case 1: temp_source = heater_bed; break;
default: temp_source = NULL;
}
if (heater_get_celsius( temp_source, &celsius) == 0) {
printf( "\nT:%1.1lf", celsius);
}
} else {
heater_get_celsius( heater_extruder, &celsius);
printf( "\nT:%1.1lf", celsius);
if (heater_bed != NULL) {
heater_get_celsius( heater_bed, &celsius);
printf( " B:%1.1lf", celsius);
}
}
break;
}
// M7 - cooling mist on
case 7:
traject_wait_for_completion();
pwm_set_output( pwm_fan, 100);
break;
// M106 - pwm device control
case 106: {
channel_tag pwm_device = pwm_fan; // unless overridden by P setting
int pwm_value = 100; // unless overridden by S setting
//? ==== M106: output control ====
//?
//? Example: M106
//?
//? General purpose output control, backwards compatible
//? with simple FAN control (without P and/or S).
//? Sets PWM value for the device selected with P
//? to output value as specified by S.
//? S value 0..255 maps to 0..100% PWM output.
// wait for all moves to complete
traject_wait_for_completion();
if (target->seen_P) {
switch (target->P) {
case 0: pwm_device = pwm_extruder; break;
case 1: pwm_device = pwm_bed; break;
case 2: pwm_device = pwm_fan; break;
}
}
if (target->seen_S) {
if (target->S >= 255) {
pwm_value = 100;
} else {
pwm_value = (100 * target->S) / 256;
}
}
pwm_set_output( pwm_device, pwm_value);
break;
}
// M107- fan off
case 9:
case 107:
//? ==== M107: Fan Off ====
//?
//? Example: M107
//?
//? Turn off the cooling fan (if any).
// wait for all moves to complete
traject_wait_for_completion();
pwm_set_output( pwm_fan, 0);
break;
// M110- set line number
case 110:
//? ==== M110: Set Current Line Number ====
//?
//? Example: N123 M110
//?
//? Set the current line number to 123. Thus the expected next line after this command will be 124.
//? This is a no-op in Teacup.
break;
// M111- set debug level
#ifdef DEBUG
case 111:
//? ==== M111: Get/Set Debug Level ====
//?
//? Example: M111 S6
//?
//? Set the level of debugging information transmitted back to the host to level 6. The level is the OR of three bits:
//?
//? <Pre>
//? #define DEBUG_PID 1
//? #define DEBUG_DDA 2
//? #define DEBUG_POSITION 4
//? </pre>
//?
//? This command is only available in DEBUG builds of Teacup.
if (target->seen_S) {
debug_flags = target->S;
printf( "New debug_flags setting: 0x%04x (%u)\n", debug_flags, debug_flags);
} else {
printf( "Active debug_flags setting: 0x%04x (%u)\n", debug_flags, debug_flags);
}
break;
#endif
// M113- extruder PWM
case 113: {
//? ==== M113: Set (extruder) PWM ====
//?
//? Example: M113 S0.125
//?
//? Set the (raw) extruder heater output to the specified value: 0.0-1.0 gives 0-100% duty cycle.
//? Should only be used when there is no heater control loop configured for this output!!!
if (target->seen_S) {
pwm_set_output( pwm_extruder, target->S);
}
break;
}
// M114- report XYZEF to host
case 114:
//? ==== M114: Get Current Position ====
//?
//? Example: M114
//?
//? This causes the RepRap machine to report its current X, Y, Z and E coordinates to the host.
//?
//? For example, the machine returns a string such as:
//?
//? <tt>ok C: X:0.00 Y:0.00 Z:0.00 E:0.00</tt>
# ifdef ENFORCE_ORDER
// wait for all moves to complete
traject_wait_for_completion();
# endif
printf( "current: X=%1.6lf, Y=%1.6lf, Z=%1.6lf, E=%1.6lf, F=%1.6lf\n",
POS2MM( gcode_current_pos.X), POS2MM( gcode_current_pos.Y),
POS2MM( gcode_current_pos.Z), POS2MM( gcode_current_pos.E),
gcode_current_feed);
// newline is sent from gcode_parse after we return
break;
// M115- capabilities string
case 115:
//? ==== M115: Get Firmware Version and Capabilities ====
//?
//? Example: M115
//?
//? Request the Firmware Version and Capabilities of the current microcontroller
//? The details are returned to the host computer as key:value pairs separated by spaces and terminated with a linefeed.
//?
//? sample data from firmware:
//? FIRMWARE_NAME:Teacup FIRMWARE_URL:http%%3A//github.com/triffid/Teacup_Firmware/ PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1 TEMP_SENSOR_COUNT:1 HEATER_COUNT:1
printf( "FIRMWARE_NAME: BeBoPr FIRMWARE_URL:https//github.com/modmaker/BeBoPr/ PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:%d TEMP_SENSOR_COUNT:%d HEATER_COUNT:%d", 1, 2, 2);
// newline is sent from gcode_parse after we return
break;
// M116 - Wait for all temperatures and other slowly-changing variables to arrive at their set values.
case 116: {
//? ==== M116: Wait ====
//?
//? Example: M116
//?
//? Wait for ''all'' temperatures and other slowly-changing variables to arrive at their set values. See also M109.
double setpoint;
// wait for all moves to complete
traject_wait_for_completion();
// wait for all (active) heaters to stabilize
if (heater_get_setpoint( heater_extruder, &setpoint) == 0) {
if (setpoint > 0.0) {
extruder_temp_wait = 1;
}
}
if (heater_get_setpoint( heater_bed, &setpoint) == 0) {
if (setpoint > 0.0) {
bed_temp_wait = 1;
}
}
wait_for_slow_signals( NULL);
break;
}
case 130:
//? ==== M130: heater P factor ====
case 131:
//? ==== M131: heater I factor ====
case 132:
//? ==== M132: heater D factor ====
case 133:
//? ==== M133: heater I limit ====
//? P0: set for extruder
//? P1: set for bed
//? Snnn.nn: factor to set
if (target->seen_S) {
pid_settings pid;
channel_tag channel;
if (target->seen_P) {
switch (target->P) {
case 0: channel = heater_extruder; break;
case 1: channel = heater_bed; break;
default: channel = NULL;
}
} else {
channel = heater_extruder;
}
heater_get_pid_values( channel, &pid);
switch (target->M) {
case 130: // M130- heater P factor
pid.P = target->S;
break;
case 131: // M131- heater I factor
pid.I = target->S;
break;
case 132: // M132- heater D factor
pid.D = target->S;
break;
case 133: // M133- heater I limit
pid.I_limit = target->S;
break;
}
heater_set_pid_values( channel, &pid);
}
break;
// M134- save PID settings to eeprom
case 134:
//? ==== M134: save PID settings to eeprom ====
//? Undocumented.
heater_save_settings();
break;
// M135- set heater output
case 135:
//? ==== M135: set heater output ====
//? Undocumented.
if (target->seen_S) {
channel_tag heater;
switch (target->P) {
case 0: heater = heater_extruder; break;
case 1: heater = heater_bed; break;
default: heater = NULL;
}
heater_set_raw_pwm( heater, target->S);
power_on();
}
break;