summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/gcode-host-old/src/org/reprap/machines/GenericCartesianPrinter.java
blob: 235564a4f5597fb9474dda2902687578ffc983af (plain)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package org.reprap.machines;

import javax.media.j3d.*;
import java.io.IOException;

import org.reprap.Attributes;
import org.reprap.CartesianPrinter;
import org.reprap.Preferences;
import org.reprap.ReprapException;
import org.reprap.gui.Previewer;
import org.reprap.devices.NullExtruder;
import org.reprap.Extruder;
import org.reprap.utilities.Debug;

public abstract class GenericCartesianPrinter implements CartesianPrinter
{
	/**
	 * This is our previewer window
	 */
	protected Previewer previewer = null;

	/**
	 * How far have we moved, in mm.
	 */
	protected double totalDistanceMoved = 0.0;
	
	/**
	 * What distnace did we extrude, in mm.
	 */
	protected double totalDistanceExtruded = 0.0;
	
	/**
	 * Scale for each axis in steps/mm.
	 */
	protected double scaleX, scaleY, scaleZ;
	
	/**
	 * Current X, Y and Z position of the extruder 
	 */
	protected double currentX, currentY, currentZ;
	
	/**
	 * Maximum feedrate for X, Y, and Z axes
	 */
	protected double maxFeedrateX, maxFeedrateY, maxFeedrateZ;
	
	/**
	* Current feedrate for the machine.
	*/
	protected double currentFeedrate;
	
	/**
	* Feedrate for fast XY moves on the machine.
	*/
	protected double fastFeedrateXY;
	
	/**
	 * Number of extruders on the 3D printer
	 */
	protected int extruderCount;
	
	/**
	 * Array containing the extruders on the 3D printer
	 */
	protected Extruder extruders[];

	/**
	 * Current extruder?
	 */
	protected int extruder;
	
	/**
	 * When did we start printing?
	 */
	protected long startTime;
	
	/**
	 * Do we idle the z axis?
	 */
	protected boolean idleZ;
	
	/**
	 * 
	 */
	private double overRun;
	
	/**
	 * 
	 */
	private long delay;
	
	public GenericCartesianPrinter(Preferences config) throws Exception
	{
		startTime = System.currentTimeMillis();
		
		//load axis prefs
		int axes = config.loadInt("AxisCount");
		if (axes != 3)
			throw new Exception("A Cartesian Bot must contain 3 axes");
			
		//load extruder prefs
		extruderCount = config.loadInt("NumberOfExtruders");
		if (extruderCount < 1)
			throw new Exception("A Reprap printer must contain at least one extruder");
		
		//load our actual extruders.
		extruders = new NullExtruder[extruderCount];
		loadExtruders(config);
			
		// TODO This should be from calibration
		scaleX = config.loadDouble("XAxisScale(steps/mm)");
		scaleY = config.loadDouble("YAxisScale(steps/mm)");
		scaleZ = config.loadDouble("ZAxisScale(steps/mm)");
		
		// Load our maximum feedrate variables
		maxFeedrateX = config.loadDouble("MaximumFeedrateX(mm/minute)");
		maxFeedrateY = config.loadDouble("MaximumFeedrateY(mm/minute)");
		maxFeedrateZ = config.loadDouble("MaximumFeedrateZ(mm/minute)");
		
		//set our fastest feedrate.
		fastFeedrateXY = Math.min(maxFeedrateX, maxFeedrateY);
		
		//init our stuff.
		currentX = 0;
		currentY = 0;
		currentZ = 0;
	}
	
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#initialise()
	 */
	public void initialise() throws Exception
	{
		if (previewer != null)
			previewer.reset();
				
		Debug.d("Selecting material 0");
		selectExtruder(0);
		
		Debug.d("Homing machine");
		home();

		Debug.d("Setting temperature");
		getExtruder().heatOn();
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#calibrate()
	 */
	public void calibrate() {
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#calibrate()
	 */
	public void dispose() {
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#terminate()
	 */
	public void terminate() throws Exception {
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#isCancelled()
	 */
	public boolean isCancelled() {
		if (previewer != null)
			return previewer.isCancelled();
		return false;
	}

	
	public void loadExtruders(Preferences config)
	{
		for(int i = 0; i < extruderCount; i++)
			extruders[i] = extruderFactory(config, i);

		extruder = 0;
	}
	
	public Extruder extruderFactory(Preferences prefs, int count)
	{
		return new NullExtruder(prefs, count);
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#selectMaterial(int)
	 */
	public void selectExtruder(int materialIndex)
	{
		if (isCancelled())
			return;

		if(materialIndex < 0 || materialIndex >= extruderCount)
			System.err.println("Selected material (" + materialIndex + ") is out of range.");
		else
			extruder = materialIndex;

		//todo: move back to cartesian snap
		//layerPrinter.changeExtruder(extruders[extruder]);

//		if (previewer != null)
//			previewer.setExtruder(extruders[extruder]);

		if (isCancelled())
			return;
		// TODO Select new material
		// TODO Load new x/y/z offsets for the new extruder
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#selectMaterial(int)
	 */
	public void selectExtruder(Attributes att) {
		for(int i = 0; i < extruderCount; i++)
		{
			if(att.getMaterial().equals(extruders[i].toString()))
			{
				selectExtruder(i);
				return;
			}
		}
		System.err.println("selectExtruder() - extruder not found for: " + att.getMaterial());
	}
	
	/**
	 * FIXME: Why don't these use round()? - AB.
	 * @param n
	 * @return
	 */
	protected int convertToStepX(double n) {
		return (int)((n + extruders[extruder].getOffsetX()) * scaleX);
	}

	/**
	 * @param n
	 * @return
	 */
	protected int convertToStepY(double n) {
		return (int)((n + extruders[extruder].getOffsetY()) * scaleY);
	}

	/**
	 * @param n
	 * @return
	 */
	protected int convertToStepZ(double n) {
		return (int)((n + extruders[extruder].getOffsetZ()) * scaleZ);
	}

	/**
	 * @param n
	 * @return
	 */
	protected double convertToPositionX(int n) {
		return n / scaleX - extruders[extruder].getOffsetX();
	}

	/**
	 * @param n
	 * @return
	 */
	protected double convertToPositionY(int n) {
		return n / scaleY - extruders[extruder].getOffsetY();
	}

	/**
	 * @param n
	 * @return
	 */
	protected double convertToPositionZ(int n) {
		return n / scaleZ - extruders[extruder].getOffsetZ();
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#getX()
	 */
	public double getX() {
		return currentX;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#getY()
	 */
	public double getY() {
		return currentY;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#getZ()
	 */
	public double getZ() {
		return currentZ;
	}
	
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#getTotalDistanceMoved()
	 */
	public double getTotalDistanceMoved() {
		return totalDistanceMoved;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#getTotalDistanceExtruded()
	 */
	public double getTotalDistanceExtruded() {
		return totalDistanceExtruded;
	}
	
	/**
	 * @param x
	 * @param y
	 * @return segment length in millimeters
	 */
	public double segmentLength(double x, double y) {
		return Math.sqrt(x*x + y*y);
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#getTotalElapsedTime()
	 */
	public double getTotalElapsedTime() {
		long now = System.currentTimeMillis();
		return (now - startTime) / 1000.0;
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#getExtruder(String)
	 */
	public Extruder getExtruder(String name)
	{
		for(int i = 0; i < extruderCount; i++)
			if(name.equals(extruders[i].toString()))
				return extruders[i];
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#getExtruder()
	 */
	public Extruder getExtruder()
	{
		return extruders[extruder];
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#getExtruder()
	 */
	public Extruder[] getExtruders()
	{
		return extruders;
	}
	
	/**
	 * Extrude for the given time in milliseconds, so that polymer is flowing
	 * before we try to move the extruder.
	 */
	public void printStartDelay(long msDelay) {
		try
		{
			extruders[extruder].setExtrusion(extruders[extruder].getExtruderSpeed());
			Thread.sleep(msDelay);
//			extruders[extruder].setExtrusion(0);  // What's this for?  - AB
		} catch(Exception e)
		{
			// If anything goes wrong, we'll let someone else catch it.
		}
	}
	
	/**
	 * @param startX
	 * @param startY
	 * @param startZ
	 * @param endX
	 * @param endY
	 * @param endZ
	 * @throws ReprapException
	 * @throws IOException
	 */
	public void printSegment(double startX, double startY, double startZ, 
			double endX, double endY, double endZ, boolean turnOff) throws ReprapException, IOException {
		moveTo(startX, startY, startZ, true, true);
		printTo(endX, endY, endZ, turnOff);
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#moveTo(double, double, double, boolean, boolean)
	 */
	public void moveTo(double x, double y, double z, boolean startUp, boolean endUp) throws ReprapException, IOException
	{
		if (isCancelled()) return;

		totalDistanceMoved += segmentLength(x - currentX, y - currentY);
		
		//TODO - next bit needs to take account of startUp and endUp
		if (z != currentZ)
			totalDistanceMoved += Math.abs(currentZ - z);

		currentX = x;
		currentY = y;
		currentZ = z;
	}
	
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#printTo(double, double, double)
	 */
	public void printTo(double x, double y, double z, boolean turnOff) throws ReprapException, IOException
	{		
		if (previewer != null)
			previewer.addSegment(currentX, currentY, currentZ, x, y, z);
		
		if (isCancelled())
			return;

		double distance = segmentLength(x - currentX, y - currentY);
		if (z != currentZ)
			distance += Math.abs(currentZ - z);
			
		totalDistanceExtruded += distance;
		totalDistanceMoved += distance;
		
		currentX = x;
		currentY = y;
		currentZ = z;
	}
	
	/**
	 * @param enable
	 * @throws IOException
	 */
	public void setCooling(boolean enable) throws IOException {
		extruders[extruder].setCooler(enable);
	}
		
	/* (non-Javadoc)
	 * @see org.reprap.Printer#setLowerShell(javax.media.j3d.Shape3D)
	 */
	public void setLowerShell(BranchGroup ls)
	{
		previewer.setLowerShell(ls);
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#setPreviewer(org.reprap.gui.Previewer)
	 */
	public void setPreviewer(Previewer previewer) {
		this.previewer = previewer;
	}
	
	/**
	 * Moves nozzle back and forth over wiper
	 */
	public void wipeNozzle() throws ReprapException, IOException {
		
		if (getExtruder().getNozzleWipeEnabled() == false)
			return;
		else
		{
			Debug.d("Wiping nozzle");
			
			int freq = getExtruder().getNozzleWipeFreq();
			int datumX = getExtruder().getNozzleWipeDatumX();
			int datumY = getExtruder().getNozzleWipeDatumY();
			int stroke = getExtruder().getNozzleWipeStroke();
			
			setFeedrate(getFastFeedrateXY());
			
			// Moves nozzle over wiper
			for (int w=0; w < freq; w++)
			{
				moveTo(50, datumY-(stroke/2), currentZ, false, false);
				moveTo(datumX, datumY-(stroke/2), currentZ, false, false);
				moveTo(datumX, datumY+(stroke/2), currentZ, false, false);
				moveTo(50, datumY+(stroke/2), currentZ, false, false);
			
			}	
		}
	}
	
	
	public void setFeedrate(double feedrate)
	{
		currentFeedrate = feedrate;
	}
		
	public double getFeedrate()
	{
		return currentFeedrate;
	}
	
	public double getFastFeedrateXY()
	{
		return fastFeedrateXY;
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#setZManual()
	 */
	public void setZManual() throws Exception {
		setZManual(0.0);
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#setZManual(double)
	 */
	public void setZManual(double zeroPoint) throws Exception {
	}	
	
	//	public double getExtrusionSize() {
	//		return extrusionSize;
	//	}

	//	public double getExtrusionHeight() {
	//		return extrusionHeight;
	//	}

	//	public double getInfillWidth() {
	//		return infillWidth;
	//	}

		/**
		 * Get the length before the end of a track to turn the extruder off
		 * to allow for the delay in the stream stopping.
		 */
	//	public double getOverRun() { return overRun; }

		/**
		 * Get the number of milliseconds to wait between turning an 
		 * extruder on and starting to move it.
		 */
	//	public long getDelay() { return delay; }
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroX()
	 */
	public void homeToZeroX() throws ReprapException, IOException {
		currentX = 0.0;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroY()
	 */
	public void homeToZeroY() throws ReprapException, IOException {
		currentY = 0.0;
	}
	
	public void homeToZeroZ() throws ReprapException, IOException {
		currentZ = 0.0;
	}
	
	public void home(){
		currentX = currentY = currentZ = 0.0;
	}
	
	public double getMaxFeedrateX()
	{
		return maxFeedrateX;
	}

	public double getMaxFeedrateY()
	{
		return maxFeedrateY;
	}

	public double getMaxFeedrateZ()
	{
		return maxFeedrateZ;
	}

}