-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimulatorDraws.java
More file actions
430 lines (376 loc) · 12.9 KB
/
Copy pathSimulatorDraws.java
File metadata and controls
430 lines (376 loc) · 12.9 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.Arrays;
@SuppressWarnings("serial")
public class SimulatorDraws extends JFrame
{
private JTextField xField;
private JTextField yField;
private JTextField zField;
private JSlider xSlider;
private JSlider ySlider;
private JSlider zSlider;
private TopPanel topPanel;
private SidePanel sidePanel;
private double x=0; // The figure below details h locations
private double y=0; // | Z | Z is to the rear
private double z=0; // Y | |
private double hx=0;// | --- X
private double hy=0;// |/ \|
private double hz=0;// |\___/|
//Physical dimensions
private double diagonal=215;//From carriage pivot to effector pivot
private double railOffset=137;//From center of printer to center of carriage rail
//private double effectorOffset=19.9;//Distance from where diagonal attaches to center printer
//private double carriageOffset=19.5;//Distance from diagonal attaches to carriage to center of carriage rail
//Printable area user measured
private double buildX=170;
private double buildY=170;
private double buildZ=200;//Possibly calculated?
//Calculated dimensions
private double buildXMid=buildX/2;
private double buildYMid=buildY/2;
private double perimeter=2*(railOffset*Math.cos(Math.toRadians(30)));
private double diagonalSq=Math.pow(diagonal,2);
private double[] railX = {perimeter/2,-Math.sqrt(Math.pow(railOffset,2)-Math.pow(perimeter,2)/4)};
private double[] railY = {-perimeter/2,-Math.sqrt(Math.pow(railOffset,2)-Math.pow(perimeter,2)/4)};
private double[] railZ = {0,perimeter*Math.sqrt(3)/2-Math.sqrt(Math.pow(railOffset,2)-Math.pow(perimeter,2)/4)};
//Speed Constraints and locations
private int maxFeedrate = 200;
private int maxAcceleration = 9000;
private int printAcceleration = 3000;//for extruder also
private int timeStep=10;
private ArrayList<String> vertexList;
//Draw Extruded
private ArrayList<double[]> extruded;
public SimulatorDraws(){
ActionHandler ah=new ActionHandler();
ChangeHandler ch=new ChangeHandler();
JMenuBar jmb=new JMenuBar();
setJMenuBar(jmb);
JMenu fileMenu=new JMenu("File");
jmb.add(fileMenu);
JMenuItem fileOpenItem=new JMenuItem("Open...");
fileOpenItem.addActionListener(ah);
fileMenu.add(fileOpenItem);
JPanel mainPanel=new JPanel();
mainPanel.setLayout(new GridLayout(1,2));
add(mainPanel);
JPanel inputPanel=new JPanel();
inputPanel.setLayout(new GridLayout(4,1));
add(inputPanel,BorderLayout.NORTH);
JPanel xPanel=new JPanel();
inputPanel.add(xPanel);
xField=new JTextField(5);
xField.setText(""+x);
xPanel.add(new JLabel("X:"));
xPanel.add(xField);
xSlider=new JSlider((int)-diagonal,(int)diagonal,0);
xSlider.addChangeListener(ch);
xPanel.add(xSlider);
JPanel yPanel=new JPanel();
inputPanel.add(yPanel);
yField=new JTextField(5);
yField.setText(""+y);
yPanel.add(new JLabel("Y:"));
yPanel.add(yField);
ySlider=new JSlider((int)-diagonal,(int)diagonal,0);
ySlider.addChangeListener(ch);
yPanel.add(ySlider);
JPanel zPanel=new JPanel();
inputPanel.add(zPanel);
zField=new JTextField(5);
zField.setText(""+z);
zPanel.add(new JLabel("Z:"));
zPanel.add(zField);
zSlider=new JSlider(-100,100,0);
zSlider.addChangeListener(ch);
zPanel.add(zSlider);
topPanel=new TopPanel();
mainPanel.add(topPanel);
sidePanel=new SidePanel();
mainPanel.add(sidePanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800,600);
setVisible(true);
//new Timer().start();
}
private class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e){
ArrayList<String> vertexList=new ArrayList<String>();
JFileChooser jfc=new JFileChooser();
jfc.setCurrentDirectory(new File("."));
int result=jfc.showOpenDialog(SimulatorDraws.this);
if(result==JFileChooser.CANCEL_OPTION) return;
File f=jfc.getSelectedFile();
if(!f.exists()) return;
try{
BufferedReader in=new BufferedReader(new FileReader(f));
String line;
while((line=in.readLine())!=null){
if(line.startsWith("G1")) vertexList.add(line);
}
in.close();
}
catch(IOException ioe){
System.out.println(ioe);
}
setVertexList(vertexList);
extruded=new ArrayList<double[]>();
new Timer().start();
}
}
private double[][] vertexListToArray(){
int index = 0;
double previousX=0.0;
double previousY=0.0;
double previousZ=0.0;
double previousE=0.0;
double previousF=0.0;
double[][] point = new double[vertexList.size()][5];
for(String item: this.vertexList){
String[] line2 = item.split(" ");
point[index][0]=previousX;
point[index][1]=previousY;
point[index][2]=previousZ;
point[index][3]=previousE;
point[index][4]=previousF;
for(int x=0; x<line2.length; x++){
String value = line2[x];
switch (value.substring(0,1)) {
case "X": point[index][0]=previousX=Double.parseDouble(value.substring(1));
break;
case "Y": point[index][1]=previousY=Double.parseDouble(value.substring(1));
break;
case "Z": point[index][2]=previousZ=Double.parseDouble(value.substring(1));
break;
case "E": point[index][3]=previousE=Double.parseDouble(value.substring(1));
break;
case "F": point[index][4]=previousF=Double.parseDouble(value.substring(1));
break;
default : break;
}
}
index++;
}
return point;
}
private void setVertexList(ArrayList<String> vertexList){
this.vertexList=vertexList;
}
public double getBaseX(){
return Math.sqrt(Math.pow(railX[0]-x,2) + Math.pow(railX[1]-y,2));
}
public double getBaseY(){
return Math.sqrt(Math.pow(railY[0]-x,2) + Math.pow(railY[1]-y,2));
}
public double getBaseZ(){
return Math.sqrt(Math.pow(railZ[0]-x,2) + Math.pow(railZ[1]-y,2));
}
public double getHX(){
return Math.sqrt(this.diagonalSq - Math.pow(getBaseX(),2));
}
public double getHY(){
return Math.sqrt(this.diagonalSq - Math.pow(getBaseY(),2));
}
public double getHZ(){
return Math.sqrt(this.diagonalSq - Math.pow(getBaseZ(),2));
}
private class TopPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.gray);
int width=getWidth();
int height=getHeight();
if(extruded!=null)
{
for(int i=0;i<extruded.size()-1;i++)
{
double[] da=extruded.get(i);
int x1=(int)(width/2+width*da[0]/buildX);
int y1=(int)(height/2-height*da[1]/buildY);
int x2=(int)(width/2+width*da[3]/buildX);
int y2=(int)(height/2-height*da[4]/buildY);
g.drawLine(x1,y1,x2,y2);
}
}
g.setColor(Color.black);
if(width==0) return;
int x1=(int)(width/2+width*x/buildX);
int y1=(int)(height/2-height*y/buildY);
g.fillOval(x1-5,y1-5,10,10);
g.drawLine(x1,y1,50,height-100);
g.drawLine(x1,y1,width-50,height-100);
g.drawLine(x1,y1,width/2,50);
g.drawRect(0,0,width-1,height-1);
}
}
private class SidePanel extends JPanel
{
public void paintComponent(Graphics g)
{
/* System.out.println("hx"+getHX());
System.out.println("hy"+getHY());
System.out.println("hz"+getHZ());
System.out.println("basex"+getBaseX());
System.out.println("basey"+getBaseY());
System.out.println("basez"+getBaseZ());
System.out.println("perimeter"+perimeter);
System.out.println("railX"+Arrays.toString(railX));
System.out.println("railY"+Arrays.toString(railY));
System.out.println("railZ"+Arrays.toString(railZ));
*/
super.paintComponent(g);
int width=getWidth();
int height=getHeight();
if(width==0) return;
//pillars
g.setColor(Color.red);
g.drawLine(50,0,50,height);
g.drawLine(width/2,0,width/2,height);
g.drawLine(width-50,0,width-50,height);
if(extruded!=null)
{
for(int i=0;i<extruded.size()-1;i++)
{
double[] da=extruded.get(i);
int x1=(int)(width/2+width*da[0]/buildX);
int y1=(int)(height/2-height*da[2]/buildY);
int x2=(int)(width/2+width*da[3]/buildX);
int y2=(int)(height/2-height*da[5]/buildY);
g.drawLine(x1,y1,x2,y2);
}
}
int x1=(int)(width/2+width*x/buildX);
int y1=(int)(height/2-height*z/buildY);
//effector
g.setColor(Color.black);
g.fillOval(x1-5,y1-5,10,10);
//rods
g.drawLine(x1,y1,width-50,y1-(int)getHX());
g.drawLine(x1,y1,50,y1-(int)getHY());
g.drawLine(x1,y1,width/2,y1-(int)getHZ());
g.drawRect(0,0,width-1,height-1);
g.setColor(Color.gray);
g.fillRect(width-55,y1-(int)getHX(),10,10);
g.fillRect(45,y1-(int)getHY(),10,10);
g.fillRect(width/2-5,y1-(int)getHZ(),10,10);
}
}
private class ChangeHandler implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
if(e.getSource()==xSlider)
{
x=xSlider.getValue();
xField.setText(""+x);
topPanel.repaint();
sidePanel.repaint();
}
else if(e.getSource()==ySlider)
{
y=ySlider.getValue();
yField.setText(""+y);
topPanel.repaint();
sidePanel.repaint();
}
else if(e.getSource()==zSlider)
{
z=zSlider.getValue();
zField.setText(""+z);
topPanel.repaint();
sidePanel.repaint();
}
}
}
private class Timer extends Thread
{
public void run()
{
long currTime;
long prevTime;
double[][] point = vertexListToArray();
double rise=0;
double run=0;
double deltaZ=0;
double steps=0;
double feedRate=0;//in mm/min
double prevExtruded=0;
try{
prevTime=System.currentTimeMillis();
//get this point and next point to get length
for(int i=0;i<point.length;i++){
//get distance
run = point[i+1][0]-point[i][0];//change in x
rise = point[i+1][1]-point[i][1];//change in y
deltaZ = point[i+1][2]-point[i][2];//change in z
double distance = Math.sqrt(Math.pow(run,2) + Math.pow(rise,2));
distance = Math.sqrt(Math.pow(distance,2) + Math.pow(deltaZ,2));
//get feedrate in mm/minute, convert to mm/s
if((int)point[i][4]!=0){
feedRate = point[i+1][4]/60;
}
if(point[i][3]!=prevExtruded){
extruded.add(new double[]{point[i][0],point[i][1],point[i][2],
point[i+1][0],point[i+1][1],point[i+1][2]});
prevExtruded=point[i][3];
}
steps = (distance / (feedRate/1000));//how long it takes to do the move
System.out.println("feed"+feedRate*60);
System.out.println("distance"+distance);
System.out.println("steps"+steps);
System.out.println("x"+x+"y"+y);
System.out.println(point[i][0]+","+point[i][1]+","+point[i][2]+","+point[i][3]+","+point[i][4]);
System.out.println(point[i+1][0]+","+point[i+1][1]+","+point[i+1][2]+","+point[i+1][3]+","+point[i+1][4]);
int chop = (int)(steps/timeStep);//divide time into frames
int counter=1;
steps=chop;
x=point[i][0];
y=point[i][1];
z=point[i][2];
topPanel.repaint();
sidePanel.repaint();
while(counter<chop){
/* System.out.println("this"+point[i][0]+"next"+point[i+1][1]);
System.out.println("counter"+counter+"chop"+chop);
System.out.println("feed"+feedRate);
System.out.println("distance"+distance);
System.out.println("steps"+steps);
System.out.println("rise"+rise/steps+"run"+run/steps);*/
//cut move into steps
x=x+run/steps;
y=y+rise/steps;
z=z+deltaZ/steps;
//System.out.println("x"+x+"y"+y);
topPanel.repaint();
sidePanel.repaint();
currTime=System.currentTimeMillis();
int t=(int)(currTime-prevTime);
if(t<=timeStep) sleep(timeStep-t);
else System.out.println("lagging "+(t-timeStep)+"ms at interval ");
prevTime+=timeStep;
counter++;
//System.out.println("counter"+counter+"chop"+chop);
}
}
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
public static void main(String[] args)
{
new SimulatorDraws();
}
}