Simple zero dependencies JavaScript G-code parser and interpreter for Node JS and browser.
npm install gcode-utils
or via CDN
<script src="https://unpkg.com/gcode-utils@latest/dist/bundle.umd.js"></script>
Parses a G-code string, strips away the comments and returns an array of commands in the form:
interface Command {
/** The name of the command e.g. 'G0' */
command: string;
/** An object containing the parsed command parameters */
params:
{
[parameter: string]: number;
}
}
import { GcodeParser } from "gcode-utils";
const gcode = ["G90;a comment", "G00 X1.1 Z1.1", "T1 M08"].join("\n");
const parsedGcode = GcodeParser.parseGcode(gcode);
//[
// { command: 'G90', params: {} },
// { command: 'G00', params: { X: 1.1, Z: 1.1 } },
// { command: 'T1', params: {} },
// { command: 'M08', params: {} }
//]
The main tool of the interpreter is the processor. You can create a processor in the following way:
import {GcodeInterpreter} from "gcode-utils";
const gcode = ["G90;a comment", "G00 X1.1 Z1.1", "M08"].join("\n");
const processor = GcodeInterpreter.createProcessor();
Currently the only firmware supported is GRBL.
The processor, via the processGcode
function, can then take a G-code string and return a list of operations.
Currently the processor can interpret the following G-code commands:
G0, G1, G90, G91, G28, G28.1, G92
const gcode = ["G90;a comment", "G0 X10 Y10 F1000", "G91", "G0 X1 Y1", "G40"].join("\n");
const operations = processor.processGcode(gcode);
// [
// {
// operation: "RAPID_MOVE",
// props: {
// from: {x: 0, y: 0, z: 0},
// to: { x: 10, y: 10, z: 0},
// speed: 1000
// }
// },
// {
// operation: "RAPID_MOVE",
// props: {
// from: {x: 10, y: 10, z: 0},
// to: { x: 11, y: 10, z: 0},
// speed: 1000
// }
// },
// {
// operation: "UNKNOWN",
// props: {
// command: "G40",
// }
// }
// ]
In order to correctly interpret the G-code, the processor uses a virtual machine with its own internal state. You can access the current state of the virtual machine of the interpreter with processor.state
. The machine state has the following interface:
interface MachineState {
/** The current tool position */
position: Position;
/** The current home position (for GRBL firmware) */
home: Position;
/** The current distance mode: 'ABSOLUTE' or 'RELATIVE' */
distanceMode: 'ABSOLUTE' | 'RELATIVE';
/** The current travel mode: 'G0' or 'G1' */
travelMode: 'G0' | 'G1';
/** The current feedrate */
feedRate: number;
/** The current position of the extrusion axis (for 3D printing) */
extrusion: number;
/** The current power of the laser: between 0 and 1 (for laser cutting) */
laserPower: number;
/** The spindel speed in revolutions per minute (for CNC) */
spindleSpeed: number;
}
This project is licensed under the MIT License - see the LICENSE file for details
See the TODO list.