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-utils

v1.1.0

Published

Simple G-code parser and interpreter for Node and browser

Downloads

17

Readme

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

<script src="https://unpkg.com/gcode-utils@latest/dist/bundle.umd.js"></script>

G-code parser

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;
    }
}

Example

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: {} }
//]

G-code interpreter

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

Example


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",
//     }
//   }
// ]

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:


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;
}

License

This project is licensed under the MIT License - see the LICENSE file for details

TODO

See the TODO list.