-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcode-generator.js
201 lines (140 loc) · 5.56 KB
/
gcode-generator.js
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
'use strict';
import colors from 'colors';
import vector from './vector.js';
function generate(reflections, photowall, eye) {
const program = Array.from(generateModule(reflections)).join('\n');
console.log(colors.green(`created gcode (.nc) file`));
return program;
}
function* generateModule(reflections) {
const tool = 20;
const tool_diameter = 6.0;
yield `O1001 (TOP)`
yield `(SETUP)`;
yield `(G94 FEED PER MINUTE)`;
yield `(G17 USE XY PLANE)`;
yield `(G49 CANCEL TOOL OFFSET)`;
yield `(G40 CANCEL CUTTER RADIUS COMPENSATION)`;
yield `(G80 CANCEL CANNED CYCLES)`;
yield `G90 G94 G17 G49 G40 G80;`;
yield `(USE METRIC)`;
yield `G21;`;
//yield `(GO TO REFERENCE POSITION)`;
//yield `G28 G91 X0. Y0. Z0.`;
yield `(USE ABSOLUTE COORDINATES)`;
yield `G90;`;
yield `(CHANGE TO TOOL ${tool})`;
yield `T${tool} M06;`;
yield `(START SPINDLE 20K RPM)`; //TODO: A lot more. 22K max
yield `S20000 M03;`;
yield `(SET FEEDRATES)`;
yield `G01 F10000;`;
yield `G00 F10000;`;
yield `(USE G55 WORK ZERO)`;
yield `G55;`;
yield `(TURN SMOOTHING ON)`;
yield `G5.1 Q1;`; //TODO: Check if this is really right
yield `(TOOL LENGTH COMPENSATION AND MOVE TOOL TO Z FROM DATUM)`;
yield `G43 H${tool} Z300.0;`;
yield `(GO TO STARTING POSITION)`;
yield `G00 X0. Y0. Z300. B0. C0.;`;
yield *indent('', generatePositions(reflections, tool, tool_diameter));
yield `(GO BACK TO STARTING POSITION)`;
yield `G00 X0. Y0. Z300. B0. C0.;`;
yield `(TURN OFF COOLANT (M08 TURNS ON))`;
yield `M09;`;
yield `(USE ABSOLUTE POSITION)`;
yield `G90;`;
yield `(CANCEL TOOL OFFSET)`;
yield `G49;`;
yield `(TURN OFF SMOOTHING`;
yield `G5.1 Q0;`;
yield `(CANCEL ROTATED COORDINATE SYSTEM)`;
yield `G69;`;
//yield `(GO TO REFERENCE POSITION)`;
//yield `G28 G91 X0. Y0. B0. C0.;`;
yield `(REWIND COMMANDS AND END PROGRAM)`;
yield `M30;`;
yield ``; // Need ampty line at end
}
function* generatePositions(reflections, tool, tool_diameter) {
const clearence_height = 30.0
// beware since this scale is depending on the scale that the 3d generator has
// that come from the settings
const scale = 1000.0;
const adjusted_mirrors = reflections
.map(reflection => reflection.mirror)
.sort( (a,b) => {
if (a.pos.x != b.pos.y) {
return a.pos.x - b.pos.x;
}
return b.pos.y - a.pos.y;
})
.map(mirror => {
// scale from m to to mm
const adjusted_pos = mirror.pos.scale(scale);
return {
pos: adjusted_pos,
normal: mirror.normal,
id: mirror.id,
width: mirror.width * scale,
height: mirror.height * scale
};
});
const precision = 3;
const globalUp = vector(0,1,0);
const globalRight = vector(1,0,0);
const globalDown = vector(0,0,1);
var i = 0;
for (let mirror of adjusted_mirrors) {
i++;
yield `(START CIRCLE ${mirror.id}, ${i}/${adjusted_mirrors.length})`;
const feature_plane_method = 'points';
if (feature_plane_method === 'points') {
const right = globalUp.cross(mirror.normal).normalized().scale(mirror.width/2);
const up = globalRight.cross(mirror.normal).normalized().scale(mirror.height/2).negated();
const p0 = vector(0,0,0).toFixed(precision); // offset in feature plane from p1
const p1 = mirror.pos.toFixed(precision); // first point. origin
const p2 = mirror.pos.add(right).toFixed(precision); // second point defines x-axis
const p3 = mirror.pos.add(up).toFixed(precision); // third point defines y-axis
// THE Q0 ORIGIN IS IN THE NEW PLANES COORDINATE SYSTEM RELATIVE TO Q1.
// Amount of shift from the first point to the origin of a feature coordinate system
yield `G68.2 P2 Q0 X${p0.x} Y${p0.y} Z${p0.z} R0.;`;
// First point. (origin of a feature coordinate system)
yield `G68.2 P2 Q1 X${p1.x} Y${p1.y} Z${p1.z};`;
yield `G68.2 P2 Q2 X${p2.x} Y${p2.y} Z${p2.z};`;
yield `G68.2 P2 Q3 X${p3.x} Y${p3.y} Z${p3.z};`;
} else if (feature_plane_method === 'vectors') {
const n = mirror.normal.normalized().toFixed(precision);
const r = globalUp.cross(mirror.normal).normalized().toFixed(precision);
const p = mirror.pos.toFixed(precision);
yield `G68.2 P3 Q1 X${p.x} Y${p.y} Z${p.z} I${r.x} J${r.y} K${r.z};`;
yield `G68.2 P3 Q2 I${n.x} J${n.y} K${n.z};`;
}
yield `G53.6 H${tool}; (${i}/${adjusted_mirrors.length} ORIENT TOOL)`; // (G53.1 can be used but does not rotate around tool tip)
//yield `G00 X0. Y0. Z${clearence_height.toFixed(precision)} (RAPID TO CLEARENCE HEIGHT)`;
//yield `G43 H${tool} X0 Y0 Z0 (TOOL LENGTH COMPENSATION)`;
// RAPID INTO CENTER
//yield `G00 X${(mirror.width/2).toFixed(precision)} Y${(mirror.height/2).toFixed(precision)} Z${clearence_height.toFixed(precision)}; (RAPID INTO CENTER)`;
yield `G00 X${(0).toFixed(precision)} Y${(0).toFixed(precision)} Z${clearence_height.toFixed(precision)}; (RAPID INTO CENTER)`;
// FEED TO CUTTING DEPTH
yield `G01 Z${(0).toFixed(precision)}; (${i}/${adjusted_mirrors.length} FEED TO CUTTING DEPTH)`;
// FEED TO START OF ARC
yield `G01 Y${(mirror.height/2 - tool_diameter/2).toFixed(precision)}; (${i}/${adjusted_mirrors.length} FEED TO START OF ARC)`;
// CW CIRCULAR MOTION (IJ = DIST TO CENTER XY)
yield `G02 I0. J${((mirror.height/2 - tool_diameter / 2)*-1 ).toFixed(precision)}; (${i}/${adjusted_mirrors.length} CW CIRCULAR MOTION)`;
// RAPID TO CLEARENCE HEIGHT
yield `G00 Z${clearence_height.toFixed(precision)}; (${i}/${adjusted_mirrors.length} RAPID TO CLEARENCE HEIGHT)`;
yield `G69; (CANCEL COORDINATE PLANE)`;
yield `(END CIRCLE)`;
yield ``;
}
}
function* indent(indentation, generator) {
for (let line of generator) {
yield `${indentation}${line}`;
}
}
export {
generate,
}