CNCLathe threading lib

by Pavlo

HTML

<canvas id=star width=300 height=0></canvas>
<div id="p_scents">

  <p>
    <label for="p_len">NumPitches
      <input type="text" id="len" size="5" name="p_scnt" value="5" placeholder="" />
    </label>
    <label for="p_pitch">Pitch
      <input type="text" id="pitch" size="5" name="p_scnt" value="3.7698" placeholder="" />
    </label>
    <label for="p_depth">Depth
      <input type="text" id="depth" size="5" name="p_scnt" value="3.26" placeholder="" />
    </label>
    <label for="p_scnts">DepthStep
      <input type="text" id="depthstep" value="0.1" size="4" name="p_scnt" placeholder="" />
    </label>
    <label for="p_feed">Feed
      <input type="text" id="feed" value="20" size="4" name="p_scnt" placeholder="" />
    </label>
  </p>
  <p>
    <label for="p_bx">Backlash X
      <input type="text" id="backlashx" value="0.15" size="4" name="p_scnt" placeholder="" />
    </label>
    <label for="p_by">Backlash Y
      <input type="text" id="backlashy" value="0.2" size="4" name="p_scnt" placeholder="" />
    </label>
    </p>
  <button id="draw">Generate G-Code</button>
  <pre id="output"></pre>

JavaScript

function out()
{
    var args = Array.prototype.slice.call(arguments, 0);
    document.getElementById('output').innerHTML += args.join("") + "\n";
}

function draw()
{
// http://robinsonia.com/wp/?p=161
    var args = Array.prototype.slice.call(arguments, 0);
    document.getElementById('output').innerHTML = "";

    
        var pitch = Number(document.getElementById("pitch").value);
        var numpitches = Number(document.getElementById("len").value);
        var len = numpitches*pitch;
        var feed = Number(document.getElementById("feed").value);
        var backlashx = Number(document.getElementById("backlashx").value);
        var backlashy = Number(document.getElementById("backlashy").value);
       var depth = Number(document.getElementById("depth").value);
      var depthstep = Number(document.getElementById("depthstep").value);

        out("; depth ", depth, " depthstep ",depthstep);

        var NumDepthStep = depth/depthstep;
        var increment = depth/NumDepthStep;
        curE = Number(0);
				out("M302 25");
				out("G92 X0 Y0 E0");
           for(var y=0;y<NumDepthStep;y++)
           { 
							out("G1 F",feed);
        			out("; ---------path-------");
        			out("; goto y");
              curE++;
							out("G1 Y",(y-1)*increment);
							out("G1 Y",y*increment," E",curE);
 
 							curE+=len/pitch;
        			out("; cut path y");
							out("G1 X",-len," E",curE);
              curE++;
							out("G1 E",curE);
        
							// backlash
        			out("; back+backlash y");
        			out("G1 Y",-0.5-backlashy);
        			out("G1 Y",0);

							out("; back+backlash x");
							out("G1 F",feed*2);
        			out("G1 X",backlashx );
							out("G1 X",0);
							out("G1 F",feed);
           }
}

draw();

document.getElementById("draw").onclick = draw;