From 4d7c55ae52d904a7d28dac622e5f10e12b9c09e5 Mon Sep 17 00:00:00 2001
From: Neil Gershenfeld <gersh@cba.mit.edu>
Date: Fri, 26 May 2017 13:05:32 -0400
Subject: [PATCH] start G-codes

---
 modules/index.html              |   2 +
 modules/toolpath/formats/G-code | 223 ++++++++++++++++++++++++++++++++
 2 files changed, 225 insertions(+)
 create mode 100644 modules/toolpath/formats/G-code

diff --git a/modules/index.html b/modules/index.html
index 4eac9d6..4a7b75a 100644
--- a/modules/index.html
+++ b/modules/index.html
@@ -81,6 +81,8 @@
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:handler('modules/string/format')">format</a><br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:handler('modules/string/variables')">variables</a><br>
 <i>toolpath</i><br>
+<i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;formats</i><br>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:handler('modules/toolpath/formats/G-code')">G-code</a><br>
 <i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;machines</i><br>
 <i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Roland</i><br>
 <i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;milling</i><br>
diff --git a/modules/toolpath/formats/G-code b/modules/toolpath/formats/G-code
new file mode 100644
index 0000000..d677a04
--- /dev/null
+++ b/modules/toolpath/formats/G-code
@@ -0,0 +1,223 @@
+//
+// G-code
+//
+// Neil Gershenfeld 
+// (c) Massachusetts Institute of Technology 2017
+// 
+// This work may be reproduced, modified, distributed, performed, and 
+// displayed for any purpose, but must acknowledge the mods
+// project. Copyright is retained and must be preserved. The work is 
+// provided as is; no warranty is provided, and users accept all 
+// liability.
+//
+// closure
+//
+(function(){
+//
+// module globals
+//
+var mod = {}
+//
+// name
+//
+var name = 'G-code'
+//
+// initialization
+//
+var init = function() {
+   mod.cutspeed.value = 20
+   mod.plungespeed.value = 20
+   mod.jogspeed.value = 75
+   mod.jogheight.value = 5
+   mod.spindlespeed.value = 10000
+   mod.unitsin.checked = true   
+   }
+//
+// inputs
+//
+var inputs = {
+   toolpath:{type:'object',
+      event:function(evt){
+         mod.name = evt.detail.name
+         mod.path = evt.detail.path
+         mod.dpi = evt.detail.dpi
+         mod.width = evt.detail.width
+         mod.height = evt.detail.height
+         make_path()
+         }}}
+//
+// outputs
+//
+var outputs = {
+   file:{type:'object',
+      event:function(str){
+         obj = {}
+         obj.name = mod.name+".sbp"
+         obj.contents = str
+         mods.output(mod,'file',obj)
+         }}}
+//
+// interface
+//
+var interface = function(div){
+   mod.div = div
+   //
+   // cut speed
+   //
+   div.appendChild(document.createTextNode('cut speed: '))
+   var input = document.createElement('input')
+      input.type = 'text'
+      input.size = 6
+      div.appendChild(input)
+      mod.cutspeed = input
+   div.appendChild(document.createTextNode(' (mm/s)'))
+   div.appendChild(document.createElement('br'))
+   //
+   // plunge speed
+   //
+   div.appendChild(document.createTextNode('plunge speed: '))
+   var input = document.createElement('input')
+      input.type = 'text'
+      input.size = 6
+      div.appendChild(input)
+      mod.plungespeed = input
+   div.appendChild(document.createTextNode(' (mm/s)'))
+   div.appendChild(document.createElement('br'))
+   //
+   // jog speed
+   //
+   div.appendChild(document.createTextNode('jog speed: '))
+   var input = document.createElement('input')
+      input.type = 'text'
+      input.size = 6
+      div.appendChild(input)
+      mod.jogspeed = input
+   div.appendChild(document.createTextNode(' (mm/s)'))
+   div.appendChild(document.createElement('br'))
+   //
+   // jog height
+   //
+   div.appendChild(document.createTextNode('jog height: '))
+   var input = document.createElement('input')
+      input.type = 'text'
+      input.size = 6
+      div.appendChild(input)
+      mod.jogheight = input
+   div.appendChild(document.createTextNode(' (mm)'))
+   div.appendChild(document.createElement('br'))
+   //
+   // spindle speed
+   //
+   div.appendChild(document.createTextNode('spindle speed: '))
+   var input = document.createElement('input')
+      input.type = 'text'
+      input.size = 6
+      div.appendChild(input)
+      mod.spindlespeed = input
+   div.appendChild(document.createTextNode(' (RPM)'))
+   div.appendChild(document.createElement('br'))
+   //
+   // file units
+   //
+   div.appendChild(document.createTextNode('file units:'))
+   var input = document.createElement('input')
+      input.type = 'radio'
+      input.name = mod.div.id+'units'
+      input.id = mod.div.id+'unitsin'
+      div.appendChild(input)
+      mod.unitsin = input
+   div.appendChild(document.createTextNode('in'))
+   var input = document.createElement('input')
+      input.type = 'radio'
+      input.name = mod.div.id+'units'
+      input.id = mod.div.id+'unitsmm'
+      div.appendChild(input)
+      mod.unitsmm = input
+   div.appendChild(document.createTextNode('mm'))
+   }
+//
+// local functions
+//
+function make_path() {
+   var dx = units*mod.width/mod.dpi
+   var nx = mod.width
+   var scale = dx/(nx-1)
+   var jog_speed = parseFloat(mod.jogspeed.value)/25.4
+   var cut_speed = parseFloat(mod.cutspeed.value)/25.4
+   var plunge_speed = parseFloat(mod.plungespeed.value)/25.4
+   var jog_height = parseFloat(mod.jogheight.value)/25.4
+   var spindle_speed = parseFloat(mod.spindlespeed.value)
+   var tool = parseInt(mod.tool.value)
+   str = "%\n" // tape start
+   // xy plane, inch mode, cancel diameter compensation, cancel length offset
+   // coordinate system 1, cancel motion, non-incremental motion, feed/minute mode
+   str += "G17\n"
+   str += "G20\n"
+   str += "G40\n"
+   str += "G49\n"
+   str += "G54\n"
+   str += "G80\n"
+   str += "G90\n"
+   str += "G94\n"
+   str += "T"+tool+"M06\n" // tool selection, tool change
+   str += "F"+cut_speed.toFixed(4)+"\n" // feed rate
+   str += "S"+spindle_speed+"\n" // spindle speed
+   if (mod.coolant.checked)
+      str += "M08\n" // coolant on
+   str += "G00Z"+jog_height.toFixed(4)+"\n" // move up before starting spindle
+   str += "M03\n" // spindle on clockwise
+   str += "G04 P1\n" // give spindle 1 second to spin up
+   //
+   // follow segments
+   //
+   for (var seg = 0; seg < mod.path.length; ++seg) {
+      //
+      // move up to starting point
+      //
+      x = scale*mod.path[seg][0][0]
+      y = scale*mod.path[seg][0][1]
+      str += "Z"+jog_height.toFixed(4)+"\n"
+      str += "G00X"+x.toFixed(4)+"Y"+y.toFixed(4)+"Z"+jog_height.toFixed(4)+"\n"
+      //
+      // move down
+      //
+      z = scale*mod.path[seg][0][2]
+      str += "G01Z"+z.toFixed(4)+" F"+plunge_speed.toFixed(4)+"\n"
+      str += "F"+cut_speed.toFixed(4)+"\n" //restore xy feed rate
+      for (var pt = 1; pt < mod.path[seg].length; ++pt) {
+         //
+         // move to next point
+         //
+         x = scale*mod.path[seg][pt][0]
+         y = scale*mod.path[seg][pt][1]
+         z = scale*mod.path[seg][pt][2]
+         str += "G01X"+x.toFixed(4)+"Y"+y.toFixed(4)+"Z"+z.toFixed(4)+"\n"
+         }
+      }
+   //
+   // finish
+   //
+   str += "G00Z"+jog_height.toFixed(4)+"\n" // move up before stopping spindle
+   str += "M05\n" // spindle stop
+   if (mod.coolant.checked)
+      str += "M09\n" // coolant off
+   str += "M30\n" // program end and reset
+   str += "%\n" // tape end
+   //
+   // output file
+   //
+   str += "MZ,"+jog_height.toFixed(4)+"\r\n"
+   outputs.file.event(str)
+   }
+//
+// return values
+//
+return ({
+   name:name,
+   init:init,
+   inputs:inputs,
+   outputs:outputs,
+   interface:interface
+   })
+}())
+
-- 
GitLab