Skip to content

Instantly share code, notes, and snippets.

@ryansturmer
Created February 24, 2015 14:58
Show Gist options
  • Save ryansturmer/56fdc5ebc6ea1d598681 to your computer and use it in GitHub Desktop.
Save ryansturmer/56fdc5ebc6ea1d598681 to your computer and use it in GitHub Desktop.
util = require('util');
Interpreter = require('gcode').Interpreter;
var GCodeAnalyzer = function() {
Interpreter.call(this);
this.x = 0.0;
this.y = 0.0;
this.z = 0.0;
this.min_dist = 100000000000;
this.line_count = 1;
this.circle_count = 0;
this.stats = {0:0, 1:0, 10:0, 100:0}
}
util.inherits(GCodeAnalyzer, Interpreter);
GCodeAnalyzer.prototype.setPosition = function(x,y,z) {
var new_x = (x === undefined) ? this.x : x;
var new_y = (y === undefined) ? this.y : y;
var new_z = (z === undefined) ? this.z : z;
var dx = new_x - this.x;
var dy = new_y - this.y;
var dz = new_z - this.z;
dist = Math.sqrt(dx*dx + dy*dy + dz*dz);
if(dist === 0) {
this.stats[0] += 1;
} else if(dist < 0.0001) {
this.stats[1] += 1;
} else if(dist < 0.001) {
this.stats[10] += 1;
} else if(dist < 0.010) {
this.stats[100] += 1;
}
this.x = new_x;
this.y = new_y;
this.z = new_z;
}
GCodeAnalyzer.prototype.G0 = function(args) {
this.setPosition(args.X, args.Y, args.Z);
}
GCodeAnalyzer.prototype.G1 = function(args) {
this.setPosition(args.X, args.Y, args.Z);
}
GCodeAnalyzer.prototype.G2 = function(args) {
this.circle_count += 1;
}
var filename = process.argv[2];
analyzer = new GCodeAnalyzer();
analyzer.interpretFile(filename, function(err, lines) {
console.log("Total circles in file: " + this.circle_count);
console.log(" Zero length moves: " + this.stats[0]);
console.log(" Moves < 0.1mil: " + this.stats[1]);
console.log(" Moves < 1.0mil: " + this.stats[10]);
console.log(" Moves < 10.0mil: " + this.stats[100]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment