/
GCode.php
342 lines (297 loc) · 8.65 KB
/
GCode.php
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* GCode is the common name for the most widely used numerical control programming language.
* It is used mainly in computer-aided manufacturing to control automated machine tools.
*
* The first four general codes apart from thou shall spawn PHP everywhere ;)
*
* Please use library with CARE as this is very experimental!
*
*
* G00 Rapid positioning
* G01 Linear interpolation
* G02 Circular interpolation, clockwise
* G03 Circular interpolation, counterclockwise
*
* Add the following to the config file in the [FILTER] section of your machine's config
* then LinuxCnc can read the string output of the PHP script!
*
* PROGRAM_EXTENSION = .php PHP Script
* php = php
*
* NOTES
* http://linuxcnc.org/docs/html/gcode.html
* http://www.tormach.com/g40_g41_g42.html
*
* http://www.practicalmachinist.com/vb/cnc-machining/newbie-g-code-tool-compensation-help-124118/
* G41 = Cutter Comp left; that is, the cutter WILL ALWAYS BE CUTTING TO THE LEFT OF THE PART. This is 'climb' milling, and is the preferred method.
* G42 = Cutter Comp right; that is, the cutter WILL ALWAYS CUT TO THE RIGHT OF THE PART. This is 'conventional' milling, and generally doesn't give as good of a finish, and re-cutting chips is a problem.
*
* @license http://www.gnu.org/licenses/lgpl-3.0.en.html
* @author David Stevens <mail.davro@gmail.com>
*/
class GCode
{
/**
* GCode string to current output
*
* @var string
*/
protected $code;
/**
* The Tool Size Diameter
*
* @var integer
*/
protected $toolSizeDiameter = 0;
/**
* The Tool Size Radius
*
* @var integer
*/
protected $toolSizeRadius = 0;
/**
* The Feed Rate
*
* @var integer
*/
protected $feedRate = 0;
/**
* The spindle speed
*
* @var integer
*/
protected $spindleSpeed = 0;
/**
* The spindle start position eg Z10
*
* @var integer
*/
protected $spindleAxisStartPosition = 10;
/**
* GCODE reference
*
* http://linuxcnc.org/docs/html/gcode.html
*
* @var array
*/
protected $gcodeReference = [
// Motion (X Y Z A B C U V W apply to all motions)
'G0' => ['code' => 'motion', 'description' => 'Rapid Motion', 'parameters' => []],
'G1' => ['code' => 'motion', 'description' => 'Coordinated motion ("Straight feed")', 'parameters' => []],
'G2' => ['code' => 'motion', 'description' => 'Coordinated helical motion ("Arc feed") CW', 'parameters' => ['I','J','K','R','P']],
'G3' => ['code' => 'motion', 'description' => 'Coordinated helical motion ("Arc feed") CCW', 'parameters' => ['I','J','K','R','P']],
'G4' => ['code' => 'motion', 'description' => 'Dwell (no motion for P seconds)', 'parameters' => ['P']],
'G5' => ['code' => 'motion', 'description' => 'Cubic spline', 'parameters' => ['I','J','P','Q']],
'G5.1' => ['code' => 'motion', 'description' => 'Quadratic spline', 'parameters' => ['I','J']],
'G5.2' => ['code' => 'motion', 'description' => 'NURBS, add control point ', 'parameters' => ['P','L']],
'G5.3' => ['code' => 'motion', 'description' => 'NURBS, execute ', 'parameters' => []],
// Canned cycles (X Y Z or U V W apply to canned cycles, depending on active plane)
'G81' => ['code' => 'canned-cycles', 'description' => '', 'parameters' => []],
// Distance Mode
'G90' => ['code' => 'distance-mode', 'description' => '', 'parameters' => []],
// Feed Rate Mode
'G93' => ['code' => 'feed-rate-mode', 'description' => '', 'parameters' => []],
'G94' => ['code' => 'feed-rate-mode', 'description' => '', 'parameters' => []],
'G95' => ['code' => 'feed-rate-mode', 'description' => '', 'parameters' => []],
];
/**
* Class constructor
*/
public function __construct()
{
}
/**
* Setter for changing the unit type
*
* @param string $value
*/
public function setUnitType($value)
{
if ($value == 'metric') {
$this->setCode('G21 (Metric mm)');
} else {
$this->setCode('G20 (Imperial inch)');
}
}
public function setToolSize($value)
{
$this->toolSizeDiameter = $value;
$this->toolSizeRadius = $value/2;
}
/**
* Setter for changing the feed rate
*
* @param type $value
*/
public function setFeedRate($value)
{
$this->feedRate = $value;
}
/**
* Getter for retrieving the header GCodes
*
* @return string
*/
public function getHeader()
{
$output = "(GCode fabricated on " . date('r') . " http://davro.net)\n";
$output.= "G90 (absolute mode)\n";
// Feed Rate Depending on the setting of the Feed Mode toggle the rate may be in
// units-per-minute or units-per-rev of the spindle. The units are those defined by the G20/G21 mode.
$output.= "F{$this->feedRate} (Feed Rate)\n";
$output.= "S{$this->spindleSpeed} (Spindle Speed)\n";
return $output;
}
/**
* Getter for retrieving the footer GCodes
*
* @return string
*/
public function getFooter()
{
$output = "M2\n";
return $output;
}
/**
* Setter for adding a line to the GCode
*
* @param string $code The GCode to add
*/
public function setCode($code)
{
$this->code.= $code . "\n";
return;
}
/**
* Output HeaderCode MainCode and FooterCode in a single string
*
* @return string
*/
public function __toString()
{
$output = $this->getHeader();
$output.= $this->code;
$output.= $this->getFooter();
return $output;
}
/**
* Draw a circle on a XY plane
* With a certain motion clockwise or anticlockwise for conventional or climb milling
*
* TODO Plane selection
* G17 XY Plane
* G18 XZ Plane
* G19 YZ Plane
*
* Motion control
* G2 Clockwise Arcs
* G3 Counter-Clockwise Arcs
*
* @param float $x
* @param float $y
* @param float $z
* @param float $radius
* @param string $motion
* @param string $plane
* @return boolean
*/
public function drawCircle($x, $y, $z, $radius, $motion = 'G2', $plane = 'G17')
{
if ($plane == 'G17') {
$of = $x - $radius;
$this->setCode("(circle)");
$this->setCode("G0 X{$of} Y{$y}");
$this->setCode("G1 Z{$z} (axis spindle start point)");
// TODO Tool size ...
// TODO Cutting spindle movement ...
$this->setCode("{$plane} {$motion} X{$of} Y{$y} I{$radius} J0.00 Z{$z}");
$this->setCode("G0 Z{$this->spindleAxisStartPosition} (axis spindle start position)");
$this->setCode("(/circle)\n");
}
if ($plane == 'G18') {
// G18 is disabled
return;
}
if ($plane == 'G19') {
// G19 is disabled
return;
}
return $this;
}
/**
* Draw a box on a XY plane
*
* @param float $x1
* @param float $y1
* @param float $z1
* @param float $x2
* @param float $y2
* @param float $z2
* @return \GCode
*/
public function drawBox($x1, $y1, $z1, $x2, $y2, $z2)
{
$this->setCode("(box)");
$this->setCode("G0 X{$x1} Y{$y1} Z{$this->spindleAxisStartPosition} (move to spindle axis start position)");
$this->setCode("G1 Z{$z1}");
$this->setCode("G1 X{$x1} Y{$y1} Z{$z1}");
$this->setCode("G1 Y{$y2}");
$this->setCode("G1 X{$x2}");
$this->setCode("G1 Y{$y1}");
$this->setCode("G1 X{$x1}");
$this->setCode("G0 Z{$z2}");
$this->setCode("(/box)\n");
return $this;
}
/**
* Draw a line on a XY plane
*
* @param float $xStart
* @param float $yStart
* @param float $zStart
* @param float $xEnd
* @param float $yEnd
* @param float $zEnd
* @return \GCode
*/
public function drawLine($xStart, $yStart, $zStart, $xEnd, $yEnd, $zEnd)
{
$this->setCode("(line)");
$this->setCode("G0 X{$xStart} Y{$yStart} Z{$this->spindleAxisStartPosition}");
$this->setCode("G1 Z{$zStart}");
// testing tool setup tool metric M8 diameter 4mm
$this->setCode("T1 M08");
$this->setCode("G41 D4");
$this->setCode("G1 X{$xStart} Y{$yStart} Z{$zStart}");
$this->setCode("G1 X{$xEnd} Y{$yEnd} Z{$zEnd}");
$this->setCode("G0 Z{$this->spindleAxisStartPosition}");
$this->setCode("G0 X{$xStart} Y{$yStart}");
$this->setCode("(/line)\n");
return $this;
}
/**
* G5 creates a cubic B-spline in the XY plane with the X and Y axes only.
* P and Q must both be specified for every G5 command.
*
* @param float $xStart
* @param float $yStart
* @param float $xFromEnd
* @param float $yFromEnd
* @param float $xEnd
* @param float $yEnd
* @return \GCode
*/
public function drawCubicSpline($xStart, $yStart, $xFromEnd, $yFromEnd, $xEnd, $yEnd)
{
$this->setCode("(cubic spline)");
$this->setCode("G0 X{$xStart} Y{$yStart} Z{$this->spindleAxisStartPosition}");
// $this->setCode("G1 Z{$zStart}");
// G5 Cubic spline
$this->setCode("G5 I{$xStart} J{$yStart} P{$xFromEnd} Q-{$yFromEnd} X{$xEnd} Y{$yEnd}");
$this->setCode("G0 Z{$this->spindleAxisStartPosition}");
$this->setCode("G0 X{$xStart} Y{$yStart}");
$this->setCode("(/cubic spline)");
return $this;
}
}