Skip to content

Instantly share code, notes, and snippets.

@mountiealpha
Created October 22, 2019 16:00
Show Gist options
  • Select an option

  • Save mountiealpha/7aecbbd35a1e2ea2617fdfb8ee2bf62e to your computer and use it in GitHub Desktop.

Select an option

Save mountiealpha/7aecbbd35a1e2ea2617fdfb8ee2bf62e to your computer and use it in GitHub Desktop.
Generate G-code for trochoid toolpath on X-axis
// usage: cscript xtrochoid.js //Nologo > myFile.nc
// alter the below variables to taste!
// based on code from https://gist.github.com/r24y/48285ef648803d195740
//
// program will output trochoidal cut in +X direction
// creating D shapes from -120 to 105 degrees
// feed to MAXFEED outside of arc
var TOOLDIA = 0.375; // Tool diameter
var STEPOVER = 0.0375; // Stepover (try 10% of tool diameter)
var CUTFEED = 54.3; // Cutting feed
var MAXFEED = 150.; // Feed for positioning moves
var LL = [-0.06,-0.93]; // slot lower left corner
var UR = [24.2,-0.075]; // slot upper right corner
var DEC_PLACES=4; // number of decimal places (for rounding)
// CODE STARTS HERE
// Display settings
WScript.Echo("(" + WScript.ScriptName + " Settings:) ");
WScript.Echo("(TOOLDIA = " + TOOLDIA + ") ");
WScript.Echo("(STEPOVER = " + STEPOVER + ") ");
WScript.Echo("(CUTFEED = " + CUTFEED + ") ");
WScript.Echo("(LL = [" + LL[0] + "," + LL[1] + "]) ");
WScript.Echo("(UR = [" + UR[0] + "," + UR[1] + "]) ");
WScript.Echo("(DEC_PLACES = " + DEC_PLACES + ") ");
// Timestamp -- from https://stackoverflow.com/questions/10632346/how-to-format-a-date-in-mm-dd-yyyy-hhmmss-format-in-javascript
Number.prototype.padLeft = function(base,chr){
var len = (String(base || 10).length - String(this).length)+1;
return len > 0? new Array(len).join(chr || '0')+this : this;
}
// usage
//=> 3..padLeft() => '03'
//=> 3..padLeft(100,'-') => '--3'
var d = new Date,
dformat = [d.getFullYear(),
(d.getMonth()+1).padLeft(),
(d.getDate()).padLeft()].join('-')+' '+
[d.getHours().padLeft(),
d.getMinutes().padLeft(),
d.getSeconds().padLeft()].join(':');
WScript.Echo("(" + dformat + ")");
// WScript.Echo("RAD = " + RAD); // DEBUG
// WScript.Echo("ORIGIN = (" + ORIGIN[0].toFixed(DEC_PLACES) + "," + ORIGIN[1].toFixed(DEC_PLACES) + ")"); // DEBUG
// WScript.Echo("0°: (" + Math.cos(0).toFixed(DEC_PLACES) + ", " + Math.sin(0).toFixed(DEC_PLACES) + ")" ); // DEBUG
// WScript.Echo("90°: (" + Math.cos(90/360*2*Math.PI).toFixed(DEC_PLACES) + ", " + Math.sin(90/360*2*Math.PI).toFixed(DEC_PLACES) + ")" ); // DEBUG
// WScript.Echo("180°: (" + Math.cos(180/360*2*Math.PI).toFixed(DEC_PLACES) + ", " + Math.sin(180/360*2*Math.PI).toFixed(DEC_PLACES) + ")" ); // DEBUG
// WScript.Echo("270°: (" + Math.cos(270/360*2*Math.PI).toFixed(DEC_PLACES) + ", " + Math.sin(270/360*2*Math.PI).toFixed(DEC_PLACES) + ")" ); // DEBUG
var RAD = (UR[1]-LL[1]-TOOLDIA)/2;
var ORIGIN = [LL[0]-RAD,(LL[1]+UR[1])/2];
var theta, x, y, start, end;
start = -2*Math.PI/3; // -120 deg
end = 7*Math.PI/12; // 105 deg
for(;ORIGIN[0]<=(UR[0]+STEPOVER);ORIGIN[0]+=STEPOVER) {
// WScript.Echo("ORIGIN = (" + ORIGIN[0].toFixed(DEC_PLACES) + "," + ORIGIN[1].toFixed(DEC_PLACES) + ")"); // DEBUG
theta = start;
x = ORIGIN[0]+Math.cos(theta)*RAD;
y = ORIGIN[1]+Math.sin(theta)*RAD;
WScript.Echo(
"G01 X"+x.toFixed(DEC_PLACES).replace(/0+$/, "")
+" Y"+y.toFixed(DEC_PLACES).replace(/0+$/, "")
+" F"+MAXFEED.toFixed(DEC_PLACES).replace(/0+$/, "")+" "
);
theta = end;
x = ORIGIN[0]+Math.cos(theta)*RAD;
y = ORIGIN[1]+Math.sin(theta)*RAD;
WScript.Echo(
"G03 X"+x.toFixed(DEC_PLACES).replace(/0+$/, "")
+" Y"+y.toFixed(DEC_PLACES).replace(/0+$/, "")
+" I"+ORIGIN[0].toFixed(DEC_PLACES).replace(/0+$/, "")
+" J"+ORIGIN[1].toFixed(DEC_PLACES).replace(/0+$/, "")
+" F"+CUTFEED.toFixed(DEC_PLACES).replace(/0+$/, "")+" "
);
}
WScript.Echo("(End " + WScript.ScriptName + ") ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment