G-code 6.05 KB
Newer Older
Neil Gershenfeld's avatar
Neil Gershenfeld committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
   })
}())