-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgCode.pde
213 lines (169 loc) · 6.95 KB
/
gCode.pde
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
//parse gCode
//some of this code adapted from:
// Arduino G-code Interpreter
// v1.0 by Mike Ellery - initial software ([email protected])
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks ([email protected])
// v1.2 by Chris Meighan - cleanup / G2&G3 support ([email protected])
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. ([email protected])
//look for the command if it exists.
boolean hasCommand(char key, String codeLine) {
for ( int i = 0; i < codeLine.length(); i++ ) {
if ( codeLine.charAt(i) == int( key ) ) {
return true;
}
}
return false;
}
//look for the number that appears after the char key and return it
// eg, "G1 Y0.027777778 F20" would return 0.027777778 for the key 'Y'
float searchString(char key, String gCodeLine ) {
String coords = new String();
//look thru each char in the gcode line
for ( int c = 0; c < gCodeLine.length(); c++ ) {
//get all digits and decimal points after key, until the next space or character
if ( gCodeLine.charAt( c ) == int( key ) ) {
c++;
while ( c < gCodeLine.length () ) {
int currentChar = int(gCodeLine.charAt(c));
//only keep recording if the character is a . or 0-9
if ( !( currentChar == 46 || ( currentChar <= 57 && currentChar >= 48 )))
break;
coords += gCodeLine.charAt(c);
c++;
}
return float( coords );
}
}
return float( coords );
}
//parse a line of Gcode to get the end coordinates in XYZ
float[] getCoords( String gCodeLine, float[] currentPos ) {
float[] newPos = currentPos;
//the character / means delete block... used for comments and stuff.
//Partkam seems to use ( for comments
if ( gCodeLine.charAt( 0 ) == int( '/' ) || gCodeLine.charAt( 0 ) == int( '(' ) ) {
println("Comment: " + gCodeLine);
return currentPos;
}
//is there a modal change in this line?
if ( hasCommand('G', gCodeLine) ) {
newPos[3] = searchString('G', gCodeLine );
}
int gMode = int(newPos[3]);
//did we get a gcode?
if ( hasCommand('X', gCodeLine) ) {
newPos[0] = searchString('X', gCodeLine );
}
if ( hasCommand('Y', gCodeLine) ) {
newPos[1] = searchString('Y', gCodeLine );
}
if ( hasCommand('Z', gCodeLine) ) {
newPos[2] = searchString('Z', gCodeLine );
}
//linear interpolation: fast or slow
if ( gMode == 0 || gMode == 1 ) {
}
//clockwise/counterclockwise circular interpolation
else if ( gMode == 2 || gMode == 3 ) {
/*
float[] cent; //centerpoint in x/y/z
// Centre coordinates are always relative
cent[0] = searchString('I', gCodeLine) + newPos[0];
cent[1] = searchString('J', gCodeLine) + newPos[1];
float angleA, angleB, angle, radius, length, aX, aY, bX, bY;
aX = (newPos[0] - cent[0]);
aY = (newPos[1] - cent[1]);
bX = (fp.x - cent.x);
bY = (fp.y - cent.y);
if (code == 2) { // Clockwise
angleA = atan2(bY, bX);
angleB = atan2(aY, aX);
}
else { // Counterclockwise
angleA = atan2(aY, aX);
angleB = atan2(bY, bX);
}
// Make sure angleB is always greater than angleA
// and if not add 2PI so that it is (this also takes
// care of the special case of angleA == angleB,
// ie we want a complete circle)
if (angleB <= angleA) angleB += 2 * M_PI;
angle = angleB - angleA;
radius = sqrt(aX * aX + aY * aY);
length = radius * angle;
int steps, s, step;
steps = (int) ceil(length / curve_section);
FloatPoint newPoint;
for (s = 1; s <= steps; s++) {
step = (code == 3) ? s : steps - s; // Work backwards for CW
newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));
newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
set_target(newPoint.x, newPoint.y, fp.z);
// Need to calculate rate for each section of curve
if (feedrate > 0)
feedrate_micros = calculate_feedrate_delay(feedrate);
else
feedrate_micros = getMaxSpeed();
// Make step
dda_move(feedrate_micros);
}
*/
}
return newPos;
}
void buildPreview( String[] gCode ) {
model = new UVertexList();
int lineNum = 0;
for ( int i = 0; i < gCode.length; i++ ) {
//parse each line. if there's a new x, y, or z value, update it
//otherwise carry over the previously set value
gCodePos = getCoords( gCode[ lineNum ], gCodePos );
lineNum++;
model.add( gCodePos[0], gCodePos[1], gCodePos[2] );
}
//move the render so it's centered on the origin
model.calcBounds();
model.center();
}
void renderPreview() {
if ( model.n > 0) {
//generate the gcode view offscreen
pushMatrix();
model.setDimensions(renderWd);
//nav.doTransforms();
renderWindow.beginDraw();
renderWindow.translate( renderWd/2, renderHt/2, nav.trans.z );
renderWindow.rotateX( nav.rot.x );
renderWindow.rotateY( nav.rot.y );
renderWindow.rotateZ( nav.rot.z );
renderWindow.background(255);
renderWindow.beginShape(LINES);
for ( int i = 1; i < model.n; i++ ) {
//make sure x and y are w/in the render bounds
renderWindow.vertex( model.v[i-1].x, model.v[i-1].y, model.v[i-1].z );
renderWindow.vertex( model.v[i].x, model.v[i].y, model.v[i].z );
}
renderWindow.endShape();
renderWindow.endDraw();
popMatrix();
//put the gcode view on screen
}
image(renderWindow, renderX, renderY );
}
public void mouseDragged() {
//if the the mouse is over the render window
if ( mouseX > renderX &&
mouseX < renderX + renderWd &&
mouseY > renderY &&
mouseY < renderY + renderHt ) {
nav.mouseDragged();
}
}
void mouseWheel(int delta) {
if ( mouseX > renderX &&
mouseX < renderX + renderWd &&
mouseY > renderY &&
mouseY < renderY + renderHt ) {
nav.trans.z -= delta;
}
}