G-code utilities
Simple zero dependencies JavaScript G-code parser and interpreter for Node JS and browser.
Installation
npm install gcode-utils
or via CDN
G-code parser
Parses a G-code string, strips away the comments and returns an array of commands in the form:
Example
; const gcode = "G90;a comment" "G00 X1.1 Z1.1" "T1 M08"; const parsedGcode = GcodeParser; //[// { command: 'G90', params: {} },// { command: 'G00', params: { X: 1.1, Z: 1.1 } },// { command: 'T1', params: {} },// { command: 'M08', params: {} }//]
G-code interpreter
The main tool of the interpreter is the processor. You can create a processor in the following way:
; const gcode = "G90;a comment" "G00 X1.1 Z1.1" "M08"; const processor = GcodeInterpreter;
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
Example
const gcode = "G90;a comment" "G0 X10 Y10 F1000" "G91" "G0 X1 Y1" "G40"; const operations = processor; // [// {// 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",// }// }// ]
Machine state
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:
License
This project is licensed under the MIT License - see the LICENSE file for details
TODO
See the TODO list.