Skip to content

Commit 1ad4c91

Browse files
committed
Support for parsing tool number from "M6 Tx" or "Tx" commands (closes #1)
1 parent 917efe2 commit 1ad4c91

File tree

6 files changed

+212
-30
lines changed

6 files changed

+212
-30
lines changed

README.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,25 @@ const gcode = new Toolpath({
2222
feedrate: 'G94', // G93: Inverse time mode, G94: Units per minute, G95: Units per rev
2323
program: 'M0', // M0, M1, M2, M30
2424
spindle: 'M5', // M3, M4, M5
25-
coolant: 'M9' // M7, M8, M9
25+
coolant: 'M9', // M7, M8, M9
26+
tool: 0
2627
},
2728
// @param {object} modal The modal object.
2829
// @param {object} v1 A 3D vector of the start point.
2930
// @param {object} v2 A 3D vector of the end point.
3031
addLine: (modal, v1, v2) => {
31-
var motion = modal.motion;
32-
toolpaths.push({ motion: motion, v1: v1, v2: v2 });
32+
const motion = modal.motion;
33+
const tool = modal.tool;
34+
toolpaths.push({ motion: motion, tool: tool, v1: v1, v2: v2 });
3335
},
3436
// @param {object} modal The modal object.
3537
// @param {object} v1 A 3D vector of the start point.
3638
// @param {object} v2 A 3D vector of the end point.
3739
// @param {object} v0 A 3D vector of the fixed point.
3840
addArcCurve: (modal, v1, v2, v0) => {
39-
var motion = modal.motion;
40-
toolpaths.push({ motion: motion, v1: v1, v2: v2, v0: v0 });
41+
const motion = modal.motion;
42+
const tool = modal.tool;
43+
toolpaths.push({ motion: motion, tool: tool, v1: v1, v2: v2, v0: v0 });
4144
}
4245
});
4346

@@ -64,7 +67,7 @@ Run this example with babel-node:
6467
import Toolpath from 'gcode-toolpath';
6568

6669
const GCODE = [
67-
'N1 G17 G20 G90 G94 G54',
70+
'N1 T2 G17 G20 G90 G94 G54',
6871
'N2 G0 Z0.25',
6972
'N3 X-0.5 Y0.',
7073
'N4 Z0.1',
@@ -84,15 +87,17 @@ const gcode = new Toolpath({
8487
// @param {object} v2 A 3D vector of the end point.
8588
addLine: (modal, v1, v2) => {
8689
const motion = modal.motion;
87-
toolpaths.push({ motion: motion, v1: v1, v2: v2 });
90+
const tool = modal.tool;
91+
toolpaths.push({ motion: motion, tool: tool, v1: v1, v2: v2 });
8892
},
8993
// @param {object} modal The modal object.
9094
// @param {object} v1 A 3D vector of the start point.
9195
// @param {object} v2 A 3D vector of the end point.
9296
// @param {object} v0 A 3D vector of the fixed point.
9397
addArcCurve: (modal, v1, v2, v0) => {
9498
const motion = modal.motion;
95-
toolpaths.push({ motion: motion, v1: v1, v2: v2, v0: v0 });
99+
const tool = modal.tool;
100+
toolpaths.push({ motion: motion, tool: tool, v1: v1, v2: v2, v0: v0 });
96101
}
97102
});
98103

@@ -111,38 +116,48 @@ gcode
111116
and you will see the output as below:
112117
```js
113118
[ { motion: 'G0',
119+
tool: 2,
114120
v1: { x: 0, y: 0, z: 0 },
115121
v2: { x: 0, y: 0, z: 6.35 } },
116122
{ motion: 'G0',
123+
tool: 2,
117124
v1: { x: 0, y: 0, z: 6.35 },
118-
v2: { x: -12.7, y: 0, z: 0 } },
125+
v2: { x: -12.7, y: 0, z: 6.35 } },
119126
{ motion: 'G0',
120-
v1: { x: -12.7, y: 0, z: 0 },
121-
v2: { x: 0, y: 0, z: 2.54 } },
127+
tool: 2,
128+
v1: { x: -12.7, y: 0, z: 6.35 },
129+
v2: { x: -12.7, y: 0, z: 2.54 } },
122130
{ motion: 'G1',
123-
v1: { x: 0, y: 0, z: 2.54 },
124-
v2: { x: 0, y: 0, z: 0 } },
131+
tool: 2,
132+
v1: { x: -12.7, y: 0, z: 2.54 },
133+
v2: { x: -12.7, y: 0, z: 0 } },
125134
{ motion: 'G2',
126-
v1: { x: 0, y: 0, z: 0 },
135+
tool: 2,
136+
v1: { x: -12.7, y: 0, z: 0 },
127137
v2: { x: 0, y: 12.7, z: 0 },
128-
v0: { x: 12.7, y: 0, z: 0 } },
138+
v0: { x: 0, y: 0, z: 0 } },
129139
{ motion: 'G2',
140+
tool: 2,
130141
v1: { x: 0, y: 12.7, z: 0 },
131142
v2: { x: 12.7, y: 0, z: 0 },
132143
v0: { x: 0, y: 0, z: 0 } },
133144
{ motion: 'G2',
145+
tool: 2,
134146
v1: { x: 12.7, y: 0, z: 0 },
135147
v2: { x: 0, y: -12.7, z: 0 },
136148
v0: { x: 0, y: 0, z: 0 } },
137149
{ motion: 'G2',
150+
tool: 2,
138151
v1: { x: 0, y: -12.7, z: 0 },
139152
v2: { x: -12.7, y: 0, z: 0 },
140153
v0: { x: 0, y: 0, z: 0 } },
141154
{ motion: 'G1',
155+
tool: 2,
142156
v1: { x: -12.7, y: 0, z: 0 },
143-
v2: { x: 0, y: 0, z: 2.54 } },
157+
v2: { x: -12.7, y: 0, z: 2.54 } },
144158
{ motion: 'G0',
145-
v1: { x: 0, y: 0, z: 2.54 },
159+
tool: 2,
160+
v1: { x: -12.7, y: 0, z: 2.54 },
146161
v2: { x: 0, y: 0, z: 6.35 } } ]
147162
```
148163

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"main": "lib/index.js",
2727
"dependencies": {
28-
"gcode-interpreter": "^2.0.0"
28+
"gcode-interpreter": "^2.0.1"
2929
},
3030
"devDependencies": {
3131
"babel-cli": "^6.26.0",

src/Toolpath.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ class Toolpath {
6767

6868
// Coolant State
6969
// M7, M8, M9
70-
coolant: 'M9' // 'M7', 'M8', 'M7,M8', or 'M9'
70+
coolant: 'M9', // 'M7', 'M8', 'M7,M8', or 'M9'
71+
72+
tool: 0
7173
};
7274

7375
handlers = {
@@ -515,7 +517,10 @@ class Toolpath {
515517
}
516518
},
517519
// M6: Tool Change
518-
'M6': () => {
520+
'M6': (params) => {
521+
if (params && params.T !== undefined) {
522+
this.setModal({ tool: params.T });
523+
}
519524
},
520525
// Coolant Control
521526
// M7: Turn mist coolant on
@@ -545,6 +550,11 @@ class Toolpath {
545550
if (this.modal.coolant !== 'M9') {
546551
this.setModal({ coolant: 'M9' });
547552
}
553+
},
554+
'T': (tool) => {
555+
if (tool !== undefined) {
556+
this.setModal({ tool: tool });
557+
}
548558
}
549559
};
550560

test/fixtures/linear.nc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ G55
99
M9
1010
G0 X1.1185 Y0.5562
1111
Z0.0394
12+
M6 T2
1213
Z-0.0231
1314
G1 Z-0.0365 F19.7

test/fixtures/t2laser.nc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
( Generated by T2Laser )
2+
( Image2Gcode for Grbl )
3+
( Start Point: Center )
4+
( Frame Mode : Abs. )
5+
( X Maximum : 84.4 )
6+
( Y Maximum : 99.9 )
7+
( Laser Max : 1000 )
8+
( Feed Rate : 750 )
9+
( Resolution : 0.1 )
10+
( Image Name : text4272.png )
11+
M6 T1
12+
T1
13+
G21
14+
G90
15+
F750
16+
M05
17+
G00X0Y0F750
18+
G92X42.2Y49.95
19+
G90
20+
G00 X0 Y0
21+
T2
22+
G01
23+
M03 S0
24+
X5.2 Y0.2 M03 S0
25+
X5.3 Y0.1 M03 S1000
26+
X5.4 Y0 M03 S0
27+
X5.5 Y0 M03 S0
28+
X5.4 Y0.1 M03 S1000
29+
X5.2 Y0.3 M03 S1000
30+
X5.1 Y0.4 M03 S0
31+
X5.2 Y0.4 M03 S0
32+
X5.3 Y0.3 M03 S1000
33+
X5.5 Y0.1 M03 S1000
34+
X5.6 Y0 M03 S0
35+
X5.7 Y0 M03 S0
36+
X5.6 Y0.1 M03 S1000
37+
X5.2 Y0.5 M03 S1000
38+
X5.1 Y0.6 M03 S0
39+
X5.2 Y0.6 M03 S0
40+
X5.3 Y0.5 M03 S1000
41+
X5.7 Y0.1 M03 S1000
42+
X5.8 Y0 M03 S0
43+
X5.9 Y0 M03 S0
44+
X5.8 Y0.1 M03 S1000
45+
X5.2 Y0.7 M03 S1000
46+
X5.1 Y0.8 M03 S0
47+
X5.2 Y0.8 M03 S0
48+
X5.3 Y0.7 M03 S1000
49+
X5.9 Y0.1 M03 S1000
50+
X6 Y0 M03 S0
51+
X6.1 Y0 M03 S0
52+
X6 Y0.1 M03 S1000
53+
X5.2 Y0.9 M03 S1000

test/index.js

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ describe('G-code Toolpath', () => {
112112
},
113113
{
114114
"motion": "G0",
115-
"v1": {
116-
"x": 28.4099,
117-
"y": 14.12748,
118-
"z": 0
119-
},
120-
"v2": {
121-
"x": 28.4099,
122-
"y": 14.12748,
123-
"z": 1.0007599999999999
124-
}
115+
"v1": {
116+
"x": 28.4099,
117+
"y": 14.12748,
118+
"z": 0
119+
},
120+
"v2": {
121+
"x": 28.4099,
122+
"y": 14.12748,
123+
"z": 1.0007599999999999
124+
}
125125
},
126126
{
127127
"motion": "G0",
@@ -571,4 +571,107 @@ describe('G-code Toolpath', () => {
571571
});
572572
});
573573

574+
describe('Tool Change & Tool Select: M6/T', () => {
575+
it('should change the modal state: t2laser.nc', (done) => {
576+
const motions = [];
577+
const toolpath = new Toolpath({
578+
addLine: (modal, v1, v2) => {
579+
motions.push({
580+
motion: modal.motion,
581+
tool: modal.tool
582+
});
583+
},
584+
addArcCurve: (modal, v1, v2, v0) => {
585+
motions.push({
586+
motion: modal.motion,
587+
tool: modal.tool
588+
});
589+
}
590+
});
591+
const expectedMotions = [
592+
{ "motion": "G0", tool: 1 },
593+
{ "motion": "G0", tool: 1 },
594+
{ "motion": "G1", tool: 2 },
595+
{ "motion": "G1", tool: 2 },
596+
{ "motion": "G1", tool: 2 },
597+
{ "motion": "G1", tool: 2 },
598+
{ "motion": "G1", tool: 2 },
599+
{ "motion": "G1", tool: 2 },
600+
{ "motion": "G1", tool: 2 },
601+
{ "motion": "G1", tool: 2 },
602+
{ "motion": "G1", tool: 2 },
603+
{ "motion": "G1", tool: 2 },
604+
{ "motion": "G1", tool: 2 },
605+
{ "motion": "G1", tool: 2 },
606+
{ "motion": "G1", tool: 2 },
607+
{ "motion": "G1", tool: 2 },
608+
{ "motion": "G1", tool: 2 },
609+
{ "motion": "G1", tool: 2 },
610+
{ "motion": "G1", tool: 2 },
611+
{ "motion": "G1", tool: 2 },
612+
{ "motion": "G1", tool: 2 },
613+
{ "motion": "G1", tool: 2 },
614+
{ "motion": "G1", tool: 2 },
615+
{ "motion": "G1", tool: 2 },
616+
{ "motion": "G1", tool: 2 },
617+
{ "motion": "G1", tool: 2 },
618+
{ "motion": "G1", tool: 2 },
619+
{ "motion": "G1", tool: 2 },
620+
{ "motion": "G1", tool: 2 },
621+
{ "motion": "G1", tool: 2 },
622+
{ "motion": "G1", tool: 2 },
623+
{ "motion": "G1", tool: 2 },
624+
{ "motion": "G1", tool: 2 }
625+
];
626+
627+
toolpath.loadFromFileSync('test/fixtures/t2laser.nc');
628+
expect(motions).to.deep.equal(expectedMotions);
629+
done();
630+
});
631+
632+
it('should change the modal state: linear.nc', (done) => {
633+
const expectedMotions = [
634+
{
635+
"motion": "G0",
636+
"tool": 0
637+
},
638+
{
639+
"motion": "G0",
640+
"tool": 4
641+
},
642+
{
643+
"motion": "G0",
644+
"tool": 4
645+
},
646+
{
647+
"motion": "G0",
648+
"tool": 2
649+
},
650+
{
651+
"motion": "G1",
652+
"tool": 2
653+
}
654+
];
655+
const motions = [];
656+
const toolpath = new Toolpath({
657+
addLine: (modal, v1, v2) => {
658+
motions.push({
659+
motion: modal.motion,
660+
tool: modal.tool
661+
});
662+
},
663+
addArcCurve: (modal, v1, v2, v0) => {
664+
motions.push({
665+
motion: modal.motion,
666+
tool: modal.tool
667+
});
668+
}
669+
});
670+
671+
toolpath.loadFromFileSync('test/fixtures/linear.nc');
672+
expect(motions).to.deep.equal(expectedMotions);
673+
done();
674+
});
675+
});
676+
574677
});

0 commit comments

Comments
 (0)