summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/machines/Reprap.java
blob: 8ed04c64c68a66637545987d834f24e869f16919 (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
package org.reprap.machines;

import java.io.IOException;
import java.util.Properties;

import org.reprap.CartesianPrinter;
import org.reprap.ReprapException;
import org.reprap.comms.Communicator;
import org.reprap.comms.port.Port;
import org.reprap.comms.snap.SNAPAddress;
import org.reprap.comms.snap.SNAPCommunicator;
import org.reprap.devices.GenericExtruder;
import org.reprap.devices.GenericStepperMotor;
import org.reprap.devices.pseudo.LinePrinter;
import org.reprap.gui.Previewer;

/**
 * 
 * A Reprap printer is a 3-D cartesian printer with one or more
 * extruders
 *
 */
public class Reprap implements CartesianPrinter {
	private final int localNodeNumber = 0;

	private Communicator communicator;
	private Previewer previewer = null;

	private GenericStepperMotor motorX;
	private GenericStepperMotor motorY;
	private GenericStepperMotor motorZ;

	double totalDistanceMoved = 0.0;
	double totalDistanceExtruded = 0.0;

	private LinePrinter layer;
	
	double scaleX, scaleY, scaleZ;
	
	double currentX, currentY, currentZ;
	
	private int speed = 236;
	private int speedExtruder = 200;
	
	private GenericExtruder extruder;  ///< Only one supported for now

	final boolean dummyZ = true;  ///< Don't perform Z operations.  Should be removed later.
	
	public Reprap(Properties config, Port port) throws Exception {
		int axes = Integer.parseInt(config.getProperty("AxisCount"));
		if (axes != 3)
			throw new Exception("A Reprap printer must contain 3 axes");
		int extruders = Integer.parseInt(config.getProperty("ExtruderCount"));
		if (extruders < 1)
			throw new Exception("A Reprap printer must contain at least one extruder");
		
		SNAPAddress myAddress = new SNAPAddress(localNodeNumber);
		communicator = new SNAPCommunicator(port, myAddress);
		
		motorX = new GenericStepperMotor(communicator,
				new SNAPAddress(config.getProperty("Axis1Address")),
				Integer.parseInt(config.getProperty("Axis1Torque")));
		motorY = new GenericStepperMotor(communicator,
				new SNAPAddress(config.getProperty("Axis2Address")),
				Integer.parseInt(config.getProperty("Axis2Torque")));
		motorZ = new GenericStepperMotor(communicator,
				new SNAPAddress(config.getProperty("Axis3Address")),
				Integer.parseInt(config.getProperty("Axis3Torque")));
		
		extruder = new GenericExtruder(communicator,
				new SNAPAddress(config.getProperty("Extruder1Address")),
				Integer.parseInt(config.getProperty("Extruder1Beta")),
				Integer.parseInt(config.getProperty("Extruder1Rz")));

		layer = new LinePrinter(motorX, motorY, extruder);

		// TODO This should be from calibration
		// Assume 400 steps per turn, 1.5mm travel per turn
		scaleX = scaleY = scaleZ = 400.0 / 1.5;
		
		currentX = convertToPositionZ(motorX.getPosition());
		currentY = convertToPositionZ(motorY.getPosition());
		if (!dummyZ) {
			currentZ = convertToPositionZ(motorZ.getPosition());
		}
	}
	
	public void calibrate() {
	}

	public void printSegment(double startX, double startY, double startZ, double endX, double endY, double endZ) throws ReprapException, IOException {
		moveTo(startX, startY, startZ);
		printTo(endX, endY, endZ);
	}

	public void moveTo(double x, double y, double z) throws ReprapException, IOException {
		if (isCancelled()) return;

		layer.moveTo(convertToStepX(x), convertToStepY(y), speed);
		totalDistanceMoved += segmentLength(x - currentX, y - currentY);

		if (z != currentZ) {
			totalDistanceMoved += Math.abs(currentZ - z);
			if (!dummyZ) motorZ.seekBlocking(speed, convertToStepZ(z));
		}
		currentX = x;
		currentY = y;
		currentZ = z;
	}

	public void printTo(double x, double y, double z) throws ReprapException, IOException {
		if (isCancelled()) return;
		EnsureNotEmpty();
		if (isCancelled()) return;
		EnsureHot();
		if (isCancelled()) return;

		if ((x != convertToPositionX(layer.getCurrentX()) || y != convertToPositionY(layer.getCurrentY())) && z != currentZ)
			throw new ReprapException("Reprap cannot print a line across 3 axes simultaneously");

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

		if (isCancelled()) return;
		
		
		if (x == convertToPositionX(layer.getCurrentX()) && y == convertToPositionY(layer.getCurrentY()) && z != currentZ) {
			// Print a simple vertical extrusion
			// TODO extrusion speed should be based on actual head speed
			// which depends on the angle of the line
			double distance = Math.abs(currentZ - z);
			totalDistanceExtruded += distance;
			totalDistanceMoved += distance;
			extruder.setExtrusion(speedExtruder);
			if (!dummyZ) motorZ.seekBlocking(speed, convertToStepZ(z));
			extruder.setExtrusion(0);
			currentZ = z;
			return;
		}

		// Otherwise printing only in X/Y plane
		double distance = segmentLength(x - currentX, y - currentY);
		totalDistanceExtruded += distance;
		totalDistanceMoved += distance;
		layer.printTo(convertToStepX(x), convertToStepY(y), speed, speedExtruder);
		currentX = x;
		currentY = y;
	}

	public void selectMaterial(int materialIndex) {
		if (isCancelled()) return;

		if (previewer != null)
			previewer.setMaterial(materialIndex);

		if (isCancelled()) return;
		// TODO Select new material
	}

	protected int convertToStepX(double n) {
		return (int)(n * scaleX);
	}

	protected int convertToStepY(double n) {
		return (int)(n * scaleY);
	}

	protected int convertToStepZ(double n) {
		return (int)(n * scaleZ);
	}

	protected double convertToPositionX(int n) {
		return n / scaleX;
	}

	protected double convertToPositionY(int n) {
		return n / scaleY;
	}

	protected double convertToPositionZ(int n) {
		return n / scaleZ;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#terminate()
	 */
	public void terminate() throws Exception {
		motorX.setIdle();
		motorY.setIdle();
		motorX.setIdle();
		extruder.setExtrusion(0);
		extruder.setTemperature(0);
	}
	
	public void dispose() {
		motorX.dispose();
		motorY.dispose();
		motorZ.dispose();
		extruder.dispose();
		communicator.close();
		communicator.dispose();
	}

	/**
	 * @return Returns the speed.
	 */
	public int getSpeed() {
		return speed;
	}
	/**
	 * @param speed The speed to set.
	 */
	public void setSpeed(int speed) {
		this.speed = speed;
	}
	/**
	 * @return Returns the speedExtruder.
	 */
	public int getExtruderSpeed() {
		return speedExtruder;
	}
	/**
	 * @param speedExtruder The speedExtruder to set.
	 */
	public void setExtruderSpeed(int speedExtruder) {
		this.speedExtruder = speedExtruder;
	}
	
	public void setPreviewer(Previewer previewer) {
		this.previewer = previewer;
	}

	public void setTemperature(int temperature) throws Exception {
		extruder.setTemperature(temperature);
	}

	private void EnsureNotEmpty() {
		if (!extruder.isEmpty()) return;
		
		while (extruder.isEmpty() && !isCancelled()) {
			if (previewer != null)
				previewer.setMessage("Extruder is out of feedstock.  Waiting for refill.");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
		if (previewer != null) previewer.setMessage(null);
	}
	
	private void EnsureHot() {
		double threshold = extruder.getTemperatureTarget() * 0.95;
		
		if (extruder.getTemperature() >= threshold)
			return;

		while(extruder.getTemperature() < threshold && !isCancelled()) {
			if (previewer != null) previewer.setMessage("Waiting for extruder to reach working temperature (" + Math.round(extruder.getTemperature()) + ")");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
		if (previewer != null) previewer.setMessage(null);
		
	}

	public boolean isCancelled() {
		if (previewer == null)
			return false;
		return previewer.isCancelled();
	}
	
	public void initialise() {
		if (previewer != null)
			previewer.reset();
	}

	public double getX() {
		return currentX;
	}

	public double getY() {
		return currentY;
	}

	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;
	}
	
	public double segmentLength(double x, double y) {
		return Math.sqrt(x*x + y*y);
	}
}