Skip to content

Commit 38b3080

Browse files
authored
Merge pull request #2 from MitchBradley/g92
G92 temporary offset support
2 parents 2e3605d + 0eb6b03 commit 38b3080

3 files changed

Lines changed: 180 additions & 16 deletions

File tree

src/Toolpath.js

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ const translatePosition = (position, newPosition, relative) => {
1616
};
1717

1818
class Toolpath {
19+
g92offset = {
20+
x: 0,
21+
y: 0,
22+
z: 0
23+
};
24+
25+
offsetG92 = (pos) => {
26+
return {
27+
x: pos.x + this.g92offset.x,
28+
y: pos.y + this.g92offset.y,
29+
z: pos.z + this.g92offset.z
30+
};
31+
}
32+
offsetAddLine = (start, end) => {
33+
this.fn.addLine(this.modal, this.offsetG92(start), this.offsetG92(end));
34+
}
35+
offsetAddArcCurve = (start, end, center) => {
36+
this.fn.addArcCurve(this.modal, this.offsetG92(start), this.offsetG92(end), this.offsetG92(center));
37+
}
38+
1939
position = {
2040
x: 0,
2141
y: 0,
@@ -92,7 +112,7 @@ class Toolpath {
92112
};
93113
const targetPosition = { x: v2.x, y: v2.y, z: v2.z };
94114

95-
this.fn.addLine(this.modal, v1, v2);
115+
this.offsetAddLine(v1, v2);
96116

97117
// Update position
98118
this.setPosition(targetPosition.x, targetPosition.y, targetPosition.z);
@@ -128,7 +148,7 @@ class Toolpath {
128148
};
129149
const targetPosition = { x: v2.x, y: v2.y, z: v2.z };
130150

131-
this.fn.addLine(this.modal, v1, v2);
151+
this.offsetAddLine(v1, v2);
132152

133153
// Update position
134154
this.setPosition(targetPosition.x, targetPosition.y, targetPosition.z);
@@ -212,7 +232,7 @@ class Toolpath {
212232
v0.y = v1.y + offsetY;
213233
}
214234

215-
this.fn.addArcCurve(this.modal, v1, v2, v0);
235+
this.offsetAddArcCurve(v1, v2, v0);
216236

217237
// Update position
218238
this.setPosition(targetPosition.x, targetPosition.y, targetPosition.z);
@@ -278,7 +298,7 @@ class Toolpath {
278298
v0.y = v1.y + offsetY;
279299
}
280300

281-
this.fn.addArcCurve(this.modal, v1, v2, v0);
301+
this.offsetAddArcCurve(v1, v2, v0);
282302

283303
// Update position
284304
this.setPosition(targetPosition.x, targetPosition.y, targetPosition.z);
@@ -429,21 +449,44 @@ class Toolpath {
429449
// This would set the machine's X coordinate to 10. No physical motion will occur.
430450
// A G92 without coordinates will reset all axes to zero.
431451
'G92': (params) => {
432-
const v2 = {
433-
x: this.translateX(params.X, false),
434-
y: this.translateY(params.Y, false),
435-
z: this.translateZ(params.Z, false)
436-
};
437-
438452
// A G92 without coordinates will reset all axes to zero.
439453
if ((params.X === undefined) && (params.Y === undefined) && (params.Z === undefined)) {
440-
v2.x = 0;
441-
v2.y = 0;
442-
v2.z = 0;
454+
this.position.x += this.g92offset.x;
455+
this.g92offset.x = 0;
456+
this.position.y += this.g92offset.y;
457+
this.g92offset.y = 0;
458+
this.position.z += this.g92offset.z;
459+
this.g92offset.z = 0;
460+
} else {
461+
// The calls to translateX/Y/Z() below are necessary for inch/mm conversion
462+
// params.X/Y/Z must be interpreted as absolute positions, hence the "false"
463+
if (params.X !== undefined) {
464+
const xmm = this.translateX(params.X, false);
465+
this.g92offset.x += this.position.x - xmm;
466+
this.position.x = xmm;
467+
}
468+
if (params.Y !== undefined) {
469+
const ymm = this.translateY(params.Y, false);
470+
this.g92offset.y += this.position.y - ymm;
471+
this.position.y = ymm;
472+
}
473+
if (params.Z !== undefined) {
474+
const zmm = this.translateX(params.Z, false);
475+
this.g92offset.z += this.position.z - zmm;
476+
this.position.z = zmm;
477+
}
443478
}
444-
445-
// Update position
446-
this.setPosition(v2.x, v2.y, v2.z);
479+
},
480+
// G92.1: Cancel G92 offsets
481+
// Parameters
482+
// none
483+
'G92.1': (params) => {
484+
this.position.x += this.g92offset.x;
485+
this.g92offset.x = 0;
486+
this.position.y += this.g92offset.y;
487+
this.g92offset.y = 0;
488+
this.position.z += this.g92offset.z;
489+
this.g92offset.z = 0;
447490
},
448491
// G93: Inverse Time Mode
449492
// In inverse time feed rate mode, an F word means the move should be completed in
@@ -571,6 +614,9 @@ class Toolpath {
571614
addLine = noop,
572615
addArcCurve = noop
573616
} = { ...options };
617+
this.g92offset.x = 0;
618+
this.g92offset.y = 0;
619+
this.g92offset.z = 0;
574620

575621
// Position
576622
if (position) {

test/fixtures/g92offset.nc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(Test for G92 temporary offsets)
2+
(The motions should be performed in absolute coordinates)
3+
4+
(Start with no offsets - rel=abs)
5+
6+
(Go from 0,0,0 to 1,2,3 with rel=abs)
7+
g0 x1 y2 z3
8+
9+
(Offset the current point so abs 1,2,3 becomes rel 0,0,0)
10+
g92 x0 y0 z0
11+
12+
(Go from rel 0,0,0 = abs 1,2,3 to rel 1,2,3 = abs 2,4,6)
13+
g0 x1 y2 z3
14+
15+
(Cancel the offsets so the current point rel 1,2,3 = abs 2,4,6 becomes rel 2,4,6)
16+
g92.1
17+
18+
(Go from 2,4,6 to 0,0,0 with rel=abs)
19+
g0 x0 y0 z0
20+
21+
(Change just one offset so rel=0,0,1)
22+
g92 z1
23+
24+
(Go from rel 0,0,1 = abs 0,0,0 to rel 0,0,0 = abs 0,0,-1)
25+
g0 z0
26+
27+
(Cancel the offsets using the alternate syntax - now rel is 0,0,-1)
28+
g92
29+
30+
(Go from 0,0,-1 to 0,0,0 with rel=abs)
31+
g0 z0

test/index.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,4 +769,91 @@ describe('G-code Toolpath', () => {
769769
});
770770
});
771771

772+
describe('Temporary Offset: G92', () => {
773+
it('should generate tool paths with correct endpoints.', (done) => {
774+
const expectedMotions = [
775+
{
776+
"motion": "G0",
777+
"v1": {
778+
"x": 0,
779+
"y": 0,
780+
"z": 0
781+
},
782+
"v2": {
783+
"x": 1,
784+
"y": 2,
785+
"z": 3
786+
}
787+
},
788+
{
789+
"motion": "G0",
790+
"v1": {
791+
"x": 1,
792+
"y": 2,
793+
"z": 3
794+
},
795+
"v2": {
796+
"x": 2,
797+
"y": 4,
798+
"z": 6
799+
}
800+
},
801+
{
802+
"motion": "G0",
803+
"v1": {
804+
"x": 2,
805+
"y": 4,
806+
"z": 6
807+
},
808+
"v2": {
809+
"x": 0,
810+
"y": 0,
811+
"z": 0
812+
}
813+
},
814+
{
815+
"motion": "G0",
816+
"v1": {
817+
"x": 0,
818+
"y": 0,
819+
"z": 0
820+
},
821+
"v2": {
822+
"x": 0,
823+
"y": 0,
824+
"z": -1
825+
}
826+
},
827+
{
828+
"motion": "G0",
829+
"v1": {
830+
"x": 0,
831+
"y": 0,
832+
"z": -1
833+
},
834+
"v2": {
835+
"x": 0,
836+
"y": 0,
837+
"z": 0
838+
}
839+
},
840+
];
841+
const motions = [];
842+
const toolpath = new Toolpath({
843+
modal: {},
844+
addLine: (modal, v1, v2) => {
845+
motions.push({
846+
motion: modal.motion,
847+
v1: v1,
848+
v2: v2
849+
});
850+
}
851+
});
852+
853+
toolpath.loadFromFileSync('test/fixtures/g92offset.nc');
854+
expect(motions).to.deep.equal(expectedMotions);
855+
done();
856+
});
857+
858+
});
772859
});

0 commit comments

Comments
 (0)