summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/machines/SNAPReprap.java
blob: d97d420c469479a71cc67dc4fe7bde654e3c00bb (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
package org.reprap.machines;

import java.io.IOException;
import java.lang.Math;

import org.reprap.Preferences;
import org.reprap.ReprapException;
import org.reprap.comms.snap.SNAPAddress;
import org.reprap.devices.NullExtruder;
import org.reprap.devices.SNAPExtruder;
import org.reprap.devices.SNAPStepperMotor;
import org.reprap.devices.pseudo.LinePrinter;
import org.reprap.gui.CalibrateZAxis;
import org.reprap.Extruder;
import org.reprap.utilities.Debug;

/**
 * 
 * A Reprap printer is a 3-D cartesian printer with one or more
 * extruders
 *
 */
public class SNAPReprap extends GenericRepRap
{
	
	/**
	 * 
	 */
	//private Communicator communicator = org.reprap.Main.getCommunicator();
	
	/**
	* our line printer object.
	*/
	private LinePrinter layerPrinter;
	
	/**
	 * @param prefs
	 * @throws Exception
	 */
	public SNAPReprap() throws Exception
	{
		super();
		
		layerPrinter = new LinePrinter(motorX, motorY, extruders[extruder]);
		
		try {			
			currentX = convertToPositionZ(motorX.getPosition());
			currentY = convertToPositionZ(motorY.getPosition());
		} catch (Exception ex) {
			throw new Exception("Warning: X and/or Y controller not responding, cannot continue");
		}
		try {
			currentZ = convertToPositionZ(motorZ.getPosition());
		} catch (Exception ex) {
			System.err.println("Z axis not responding and will be ignored");
			excludeZ = true;
		}

	}
	
	public void loadMotors()
	{
		try
		{
			motorX = new SNAPStepperMotor(org.reprap.Main.getCommunicator(),
					new SNAPAddress(Preferences.loadGlobalInt("XAxisAddress")), 1);
			motorY = new SNAPStepperMotor(org.reprap.Main.getCommunicator(),
					new SNAPAddress(Preferences.loadGlobalInt("YAxisAddress")), 2);
			motorZ = new SNAPStepperMotor(org.reprap.Main.getCommunicator(),
					new SNAPAddress(Preferences.loadGlobalInt("ZAxisAddress")), 3);

			motorX.setPrinter(this);
			motorY.setPrinter(this);
			motorZ.setPrinter(this);	
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void loadExtruders()
	{
		extruders = new SNAPExtruder[extruderCount];
		
		super.loadExtruders();
	}
	
	public Extruder extruderFactory(int count)
	{
		try
		{
			String prefix = "Extruder" + count + "_";
			SNAPAddress addy = new SNAPAddress(Preferences.loadGlobalInt(prefix + "Address"));
			return new SNAPExtruder(org.reprap.Main.getCommunicator(), addy, count, this);
		}
		catch (Exception e)
		{
			return new NullExtruder(count, this);
		}
	}
	
	public void refreshPreferences()
	{
		super.refreshPreferences();
		
		motorX.refreshPreferences();
		motorY.refreshPreferences();
		motorZ.refreshPreferences();
	}
	
	public void startRun()
	{
		
	}
	
	/**
	 * Wait while the motors move about
	 * @throws IOException
	 */
	public void waitTillNotBusy() throws IOException
	{
		motorX.waitTillNotBusy();
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#calibrate()
	 */
	public void calibrate()
	{
	}
	
	/**
	 * Go to the purge point
	 */
	public void moveToPurge()
	{
		singleMove(dumpX, dumpY, currentZ, getExtruder().getFastXYFeedrate());
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#moveTo(double, double, double, boolean, boolean)
	 */
	public void moveTo(double x, double y, double z, double feedrate, boolean startUp, boolean endUp) throws ReprapException, IOException {
		
		if (isCancelled()) return;
		
		currentFeedrate = feedrate;
		
		int stepperX = convertToStepX(x);
		int stepperY = convertToStepY(y);
		int stepperZ = convertToStepZ(z);
		int currentStepperX = convertToStepX(currentX);
		int currentStepperY = convertToStepY(currentY);
		int currentStepperZ = convertToStepZ(currentZ);		
		
		if (currentStepperX == stepperX && 
				currentStepperY ==stepperY && 
				currentStepperZ == stepperZ && 
				!startUp)
			return;

		// We don't need to lift a whole layer up. Half a layer should do
		// and will dribble less. Remember the Z axis is kinda slow...
		double liftedZ = z + (extruders[extruder].getMinLiftedZ());
		int stepperLiftedZ = convertToStepZ(liftedZ);
		int targetZ;
		
		// Raise head slightly before move?
		if(startUp)
		{
			targetZ = stepperLiftedZ;
			currentZ = liftedZ;
		} else
		{
			targetZ = stepperZ;
			currentZ = z;
		}
		
		if (targetZ != currentStepperZ) {
			totalDistanceMoved += Math.abs(currentZ - liftedZ);
			int zSpeed = convertFeedrateToSpeedZ(getFastFeedrateZ());
			if (!excludeZ) motorZ.seekBlocking(zSpeed, targetZ);
			if (idleZ) motorZ.setIdle();
			currentStepperZ = targetZ;
		}
		
		int currentSpeedXY = convertFeedrateToSpeedXY(getFeedrate());
		layerPrinter.moveTo(stepperX, stepperY, currentSpeedXY, false, false);
		totalDistanceMoved += segmentLength(x - currentX, y - currentY);
		currentX = x;
		currentY = y;
		
		if(endUp)
		{
			targetZ = stepperLiftedZ;
			currentZ = liftedZ;
		} else
		{
			targetZ = stepperZ;
			currentZ = z;
		}
		
		// Move head back down to surface?
		if(targetZ != currentStepperZ)
		{
			totalDistanceMoved += Math.abs(currentZ - z);
			int zSpeed = convertFeedrateToSpeedZ(maxFeedrateZ);
			if (!excludeZ) motorZ.seekBlocking(zSpeed, targetZ);
			if (idleZ) motorZ.setIdle();
			currentStepperZ = targetZ;
		} 
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#printTo(double, double, double, boolean)
	 */
	public void printTo(double x, double y, double z, double feedrate, boolean stopExtruder, boolean closeValve) 
		throws ReprapException, IOException 
	{
		if (isCancelled()) return;
		EnsureNotEmpty();
		if (isCancelled()) return;
		EnsureHot();
		if (isCancelled()) return;
		
		currentFeedrate = feedrate;
		
		maybeReZero();

		int stepperX = convertToStepX(x);
		int stepperY = convertToStepY(y);
		int stepperZ = convertToStepZ(z);
		
		if ((stepperX != layerPrinter.getCurrentX() || stepperY != layerPrinter.getCurrentY()) && z != currentZ)
			throw new ReprapException("Reprap cannot print a line across 3 axes simultaneously");

//		if (previewer != null)
//			previewer.addSegment(convertToPositionX(layerPrinter.getCurrentX()),
//					convertToPositionY(layerPrinter.getCurrentY()), currentZ,
//					x, y, z);

		if (isCancelled()) return;
		
		
		if (z != currentZ) 
		{
			Debug.d("Printing a vertical extrusion.  Should we do that?");
			// Print a simple vertical extrusion
			double distance = Math.abs(currentZ - z);
			totalDistanceExtruded += distance;
			totalDistanceMoved += distance;
			extruders[extruder].setMotor(true);
			int zSpeed = convertFeedrateToSpeedZ(maxFeedrateZ);
			if (!excludeZ) motorZ.seekBlocking(zSpeed, stepperZ);
			extruders[extruder].setMotor(false);
			currentZ = z;
			return;
		}
		


		// Otherwise printing only in X/Y plane
		double deltaX = x - currentX;
		double deltaY = y - currentY;
		double distance = segmentLength(deltaX, deltaY);
		totalDistanceExtruded += distance;
		totalDistanceMoved += distance;
		if (segmentPauseCheckbox != null && distance > 0)
			if(segmentPauseCheckbox.isSelected())
				segmentPause();

		int currentSpeedXY = convertFeedrateToSpeedXY(getExtruder().getFastXYFeedrate());
		//System.out.println("close: " + closeValve + ", stop ex:" + stopExtruder);
		layerPrinter.printTo(stepperX, stepperY, currentSpeedXY, getExtruder().getExtruderSpeed(), stopExtruder, closeValve);
		currentX = x;
		currentY = y;
	}
	
	public void stopMotor() throws IOException
	{
		layerPrinter.stopMotor();
	}
	
	public void stopValve() throws IOException
	{
		layerPrinter.stopValve();
	}

	/* Move to zero stop on X axis.
	 * (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroX() 
	 */
	public void homeToZeroX() throws ReprapException, IOException {
		
		int fastSpeedXY = convertFeedrateToSpeedXY(getExtruder().getFastXYFeedrate());
		motorX.homeReset(fastSpeedXY);
		layerPrinter.zeroX();

		super.homeToZeroX();
	}
	
	/* Move to zero stop on Y axis.
	 * (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroY()
	 */
	public void homeToZeroY() throws ReprapException, IOException {
		
		int fastSpeedXY = convertFeedrateToSpeedXY(getExtruder().getFastXYFeedrate());
		motorY.homeReset(fastSpeedXY);
		layerPrinter.zeroY();

		super.homeToZeroX();
	}
	
	/* Move to zero stop on Z axis.
	 * (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroZ()
	 */
	public void homeToZeroZ() throws ReprapException, IOException {
		
		int fastSpeedZ = convertFeedrateToSpeedZ(getFastFeedrateZ());
		motorZ.homeReset(fastSpeedZ);

		super.homeToZeroZ();
	}
	
	public void home()
	{
		try
		{
			int fastSpeedXY = convertFeedrateToSpeedXY(getExtruder().getFastXYFeedrate());
			int fastSpeedZ = convertFeedrateToSpeedZ(getFastFeedrateZ());

			motorX.homeReset(fastSpeedXY);
			motorY.homeReset(fastSpeedXY);
			if (!excludeZ) motorZ.homeReset(fastSpeedZ);
		}
		catch (Exception e)
		{
			System.err.println("Error homing all axes.");
		}
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#terminate()
	 */
	public void terminate() throws Exception {
		motorX.setIdle();
		motorY.setIdle();
		if (!excludeZ) motorZ.setIdle();
		
		super.terminate();
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#dispose()
	 */
	public void dispose() {
		motorX.dispose();
		motorY.dispose();
		motorZ.dispose();
		
		super.dispose();

//		communicator.close();
//		communicator.dispose();
	}
	
	/**
	 * 
	 */
	private void EnsureNotEmpty() {
		if (!extruders[extruder].isEmpty()) return;
		
		while (extruders[extruder].isEmpty() && !isCancelled()) {
			//if (previewer != null)
				//previewer.
				setMessage("Extruder is out of feedstock.  Waiting for refill.");
				machineWait(1000);
		}
		//if (previewer != null) previewer.
		setMessage(null);
	}
	
	/**
	 * @throws ReprapException
	 * @throws IOException
	 */
	private void EnsureHot() throws ReprapException, IOException {
		if(extruders[extruder].getTemperatureTarget() <= Preferences.absoluteZero() + 1)
			return;
		
		double threshold = extruders[extruder].getTemperatureTarget() * 0.90;	// Changed from 0.95 by Vik.
		
		if (extruders[extruder].getTemperature() >= threshold)
			return;
		

		double x = currentX;
		double y = currentY;
		int tempReminder=0;
		temperatureReminder();
		Debug.d("Moving to heating zone");
		double oldFeedrate = getFeedrate();
		
		// Ensure the extruder is off
		
		extruders[extruder].setMotor(false);
				
		moveToHeatingZone();
		while(extruders[extruder].getTemperature() < threshold && !isCancelled()) {
			//if (previewer != null) previewer.
			setMessage("Waiting for extruder to reach working temperature (" + 
					Math.round(extruders[extruder].getTemperature()) + ")");
				machineWait(1000);
				// If it stays cold for 10s, remind it of its purpose.
				if (tempReminder++ >10) {
					tempReminder=0;
					temperatureReminder();
				}
		}
		Debug.d("Returning to previous position");
		moveTo(x, y, currentZ, currentFeedrate, true, false);
		
		currentFeedrate = oldFeedrate;
		
		//if (previewer != null) previewer.
		setMessage(null);
		
	}

	/** A bodge to fix the extruder's current tendency to forget what temperature
	 * it is supposed to be reaching.
	 * 
	 * Vik
	 */
	private void temperatureReminder() {
		if(extruders[extruder].getTemperatureTarget() < Preferences.absoluteZero())
			return;
		Debug.d("Reminding it of the temperature");
		try {
			extruders[extruder].setTemperature(extruders[extruder].getTemperatureTarget(), false);
			//setTemperature(Preferences.loadGlobalInt("ExtrusionTemp"));
		} catch (Exception e) {
			System.err.println("Error resetting temperature.");
		}
	}
	
	/**
	 * Moves the head to the predefined heating area
	 * @throws IOException
	 * @throws ReprapException
	 */
	private void moveToHeatingZone() throws ReprapException, IOException {
		//setFeedrate(getFastFeedrateXY());
		moveTo(1, 1, currentZ, getExtruder().getFastXYFeedrate(), true, false);
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#setZManual(double)
	 */
	public void setZManual(double zeroPoint) throws IOException {
		
		int zSpeed = convertFeedrateToSpeedZ(getFastFeedrateZ());
		CalibrateZAxis msg =
			new CalibrateZAxis(null, motorZ, scaleZ, zSpeed);
		msg.setVisible(true);
		try {
			synchronized(msg) {
				msg.wait();
			}
		} catch (Exception ex) {
		}
		msg.dispose();
		
		motorZ.setPosition(convertToStepZ(zeroPoint));
	}
	
	/**
	 * All machine dwells and delays are routed via this function, rather than 
	 * calling Thread.sleep - this allows them to generate the right G codes (G4) etc.
	 * 
	 * The RS232/USB etc comms system doesn't use this - it sets its own delays.
	 * @param milliseconds
	 */
	public void machineWait(double milliseconds)
	{
		if(milliseconds <= 0)
			return;
		try {
			Thread.sleep((long)milliseconds);
		} catch (InterruptedException e) {
		}		
	}
	
	public void waitWhileBufferNotEmpty()
	{
	}
	
	public void slowBuffer()
	{
	}
	
	public void speedBuffer()
	{
	}
	
	/**
	 * Load a GCode file to be made.
	 * @return the name of the file
	 */
	public String loadGCodeFileForMaking()
	{
		System.err.println("SNAP RepRap: attempt to load GCode file.");
		//super.loadGCodeFileForMaking();
		return null;
	}
	
	/**
	 * Set an output file
	 * @return
	 */
	public String setGCodeFileForOutput(String fileRoot)
	{
		System.err.println("SNAP RepRap: cannot generate GCode file.");
		return null;		
	}

	/**
	 * If a file replay is being done, do it and return true
	 * otherwise return false.
	 * @return
	 */
	public boolean filePlay()
	{
		return false;
	}
}