Skip to content

Instantly share code, notes, and snippets.

@ryaninvents
Created April 7, 2015 14:29
Show Gist options
  • Save ryaninvents/48285ef648803d195740 to your computer and use it in GitHub Desktop.
Save ryaninvents/48285ef648803d195740 to your computer and use it in GitHub Desktop.
G-Code spiral generator
// usage: node testgen.js > myFile.g
// alter the below variables to taste!
var ORIGIN = [0,0];
var EXTENT = 90;
var NUM_LOOPS = 20;
var POINTS_PER_LOOP = 100;
var SPEED=20000;
var MAX_HEIGHT=250;
var i, theta, r, h;
console.log("G4 P5000");
console.log("G28");
console.log("G1 F"+SPEED);
for(i=0;i<NUM_LOOPS;i+=(1/POINTS_PER_LOOP)){
theta = 2*i*Math.PI;
r = EXTENT*(1-i/NUM_LOOPS);
h = MAX_HEIGHT*i/NUM_LOOPS;
console.log("G1 X"+(ORIGIN[0]+Math.cos(theta)*r)
+" Y"+(ORIGIN[1]+Math.sin(theta)*r)
+" Z"+h);
}
console.log("G28");
@mountiealpha
Copy link

Further updates:

  • CW/CCW option
  • Output settings used for code generation
// usage: cscript spiralgen.js //Nologo > myFile.nc
// alter the below variables to taste!
// original from https://gist.github.com/r24y/48285ef648803d195740
 
var ORIGIN = [0,0]; // center of spiral
var EXTENT = 1.3; // OD to start at
var NUM_LOOPS = 14; // number of loops in spiral
// Note: stepover = EXTENT / NUM_LOOPS
var POINTS_PER_LOOP = 60; // how close an approximation? more points --> closer
var MAX_HEIGHT = 0; // Total Z travel
var DEC_PLACES = 4; // number of decimal places (for rounding)
var CW = true; // Clockwise / Counterclockwise
var i, theta, r, h, x, y;

// Display settings
WScript.Echo("(spiralgen.js Settings:) ");
WScript.Echo("(ORIGIN = [" + ORIGIN[0] + "," + ORIGIN[1] + "]) ");
WScript.Echo("(EXTENT = " + EXTENT + ") ");
WScript.Echo("(NUM_LOOPS = " + NUM_LOOPS + ") ");
WScript.Echo("(POINTS_PER_LOOP = " + POINTS_PER_LOOP + ") ");
WScript.Echo("(MAX_HEIGHT = " + MAX_HEIGHT + ") ");
WScript.Echo("(DEC_PLACES = " + DEC_PLACES + ") ");
WScript.Echo("(CW = " + CW + ") ");

// Output G-Code
for(i=0;i<NUM_LOOPS;i+=(1/POINTS_PER_LOOP)) {
  theta = 2*i*Math.PI*(CW?-1:1);
  r = EXTENT*(1-i/NUM_LOOPS);
  h = MAX_HEIGHT*i/NUM_LOOPS;
  x = ORIGIN[0]+Math.cos(theta)*r;
  y = ORIGIN[1]+Math.sin(theta)*r;
  WScript.Echo("G01 X"+x.toFixed(DEC_PLACES)
      +" Y"+y.toFixed(DEC_PLACES)
      +" Z"+h+" ");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment