npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gcode

v0.4.0

Published

Parse and interpret G-code files

Downloads

59

Readme

node-gcode

GCode interpreter and simulator for node.js

Most of the function of this interpreter is derived from the NIST G-code standard.

Installation

Install from npm

npm install gcode

Parsing a G-code File

gcode = require('gcode')
gcode.parseFile('example.nc', function(err, result) {
    console.log(JSON.stringify(result));
})

The data returned by the parseFile callback is a list of G-code blocks, where each block is an object with a num property (the G-code line number) and a words property (the list of G-code words in that block) Each G-code word is a list of two items, the word letter (G, M, X,Y,Z, etc.) and the word argument. Word arguments are typically numbers, but the parser supports full expressions, including parameter values, so in the event that an expression or parameter value is provided, an expression-tree-like object is returned that must be evaluated. Currently, this is left as an exercise for the reader.

G17
G1 X1 Y2 Z3 F120
G1 Z0
G1 X-0.125 F240
G0 X[3+5]

The output of the above example might look like this (note the last line that includes 3+5 in the X-axis word):

[{"N":null,"words":[["G",17]]},
 {"N":null,"words":[["G",1],["X",1],["Y",2],["Z",3],["F",120]]},
 {"N":null,"words":[["G",1],["Z",0]]},
 {"N":null,"words":[["G",1],["Z",-0.125],["F",240]]},
 {"N":null,"words":[["G",0],["X",{"left":3,"right":5,"op":"+"}]]}]

Parsing a G-code String

gcode = require('gcode');
gcode.parseString('G0 X1 Y2 Z3', function(err, result) {
   console.log(JSON.stringify(result)); 
});

See parseFile above for the output format. Strings work exactly the same way. Strings can contain any number of codes, separated by newlines per the standard. The output of the above example would be:

[{"N":null,"words":[["G",0], ['X',1],['Y',2],['Z',3]]}]

G-code Interpreters

Writing a custom interpreter for G-code is easy with the Interpreter object provided by the gcode library. To create your own interpreter, use the following example:

Interpreter = require('gcode').Interpreter;

var MyGCodeRunner = function() {
    this.units = 'imperial';
    Interpreter.call(this);
}
util.inherits(MyGCodeRunner, Interpreter)

MyGCodeRunner.prototype.G0 = function(args) {
    console.log("Got a G0 code!");
    console.log(args);
}

MyGCodeRunner.prototype.G20 = function(args) {
    console.log("Switching to inches.");
    this.units = 'imperial';
}

MyGCodeRunner.prototype.G21 = function(args) {
    console.log("Switching to millimeters.");
    this.units = 'metric';
}

runner = new MyGCodeRunner();

runner.interpretFile('example.nc');

Any handlers attached to the interpreter whose names correspond to G or M codes are called in turn as those codes are parsed from the incoming file stream.

G-codes that contain decimal points (G38.2, G59.1, etc.) are represented as handler functions by replacing the decimal point in the code with an underscore. Thus, the handler for G38.2 would be:

MyGCodeRunner.prototype.G38_2 = function(args) {
    console.log("Initiating a straight probe: " + args);
}

There is a special handler for any unhandled codes, which is just an underscore. If you define this method on your interpeter, it will be called for every unknown G or M code that is encountered. Because it is for handling unknown G/M codes, it takes two arguments, the command, followed by the remaining words in the code:

MyGCodeRunner.prototype._ = function(cmd, args) {
    console.log('Got an unknown G/M code: ',cmd);
    console.log('With arguments: ', args);
}