summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/gcode-host-old/src/org/reprap/machines/CartesianGCode.java
blob: c67d0852f007ac1ccda6f2943f70c923a20ff05a (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
package org.reprap.machines;

/*
 * TODO: To do's:
 * 
 * TODO: fixup warmup segments GCode (forgets to turn on extruder) 
 * TODO: fixup all the RR: println commands 
 * TODO: find a better place for the code. You cannot even detect a layer change without hacking now. 
 * TODO: read Zach's GCode examples to check if I messed up. 
 * TODO: make GCodeWriter a subclass of NullCartesian, so I don't have to fix code all over the place.
 */

import org.reprap.Attributes;
import org.reprap.CartesianPrinter;
import org.reprap.Preferences;
import org.reprap.ReprapException;
import org.reprap.gui.Previewer;
import org.reprap.Extruder;
import org.reprap.comms.GCodeWriter;
import org.reprap.utilities.Debug;
import org.reprap.devices.GCodeExtruder;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 *
 */
public class CartesianGCode extends GenericCartesianPrinter {
	
	/**
	* our class to send gcode instructions
	*/
	GCodeWriter gcode;
	
	/**
	* what is our current feedrate?
	*/
	double currentFeedrate = 0.0;
	
	/**
	 * @param prefs
	 * @throws Exception
	 */
	public CartesianGCode(Preferences config) throws Exception {
		
		super(config);

		String portname = config.loadString("Port(name)");
		boolean useSerialPort = config.loadBool("GCodeUseSerial");

		gcode = new GCodeWriter();
		if (useSerialPort)
			gcode.openSerialConnection(portname);
		else
			gcode.openFile();
			
		loadExtruders(config);
	}
	
	public void loadExtruders(Preferences config)
	{
		extruders = new GCodeExtruder[extruderCount];
		
		super.loadExtruders(config);
	}
	
	public Extruder extruderFactory(Preferences prefs, int count)
	{
		return new GCodeExtruder(gcode, prefs, count);
	}

	/* (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;

		double gx = round(x, 1);
		double gy = round(y, 1);
		double gz = round(z, 4);
		double zFeedrate = round(getMaxFeedrateZ(), 4);
		double xyFeedrate = round(getFeedrate(), 1);

		double dx = currentX - x;
		double dy = currentY - y;
		double dz = currentZ - z;
		
		String code;

		if (dx == 0.0 && dy == 0.0 && dz == 0.0)
			return;
		
		double liftedZ = round(currentZ + (extruders[extruder].getExtrusionHeight()/2), 4);

		//go up first?
		if (startUp)
		{
			code = "G1 Z" + liftedZ;

			if (zFeedrate != currentFeedrate)
			{
				code += "F" + zFeedrate;
				currentFeedrate = zFeedrate;
			}

			gcode.queue(code);
			currentZ = liftedZ;
			dz = 0;
		}
		
		//our real command
		code = "";
		if (currentFeedrate != xyFeedrate)
			code = "G1 ";
		
		if (dx != 0)
			code += "X" + gx;
		if (dy != 0)
			code += "Y" + gy;
		if (currentFeedrate != xyFeedrate)
		{
			code += "F" + xyFeedrate;
			currentFeedrate = xyFeedrate;
		}
		
		gcode.queue(code);

		if (dz != 0)
		{
			code = "G1 ";
			code += "Z" + gz;
			
			if (zFeedrate != currentFeedrate)
			{
				code += "F" + zFeedrate;
				currentFeedrate = zFeedrate;
			}

			gcode.queue(code);
		}
		
		//go back down?
		if (!endUp && z != currentZ)
		{
			code = "G1 Z" + gz;
			
			if (zFeedrate != currentFeedrate)
			{
				code += "F" + zFeedrate;
				currentFeedrate = zFeedrate;
			}

			gcode.queue(code);
		}
		
		super.moveTo(x, y, z, startUp, endUp);
	}

	/* (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;
			
		getExtruder().startExtruding();		

		double distance = segmentLength(x - currentX, y - currentY);
		if (z != currentZ)
			distance += Math.abs(currentZ - z);
		totalDistanceExtruded += distance;
		totalDistanceMoved += distance;

		double gx = round(x, 1);
		double gy = round(y, 1);
		double gz = round(z, 4);
		double feed = round(getFeedrate(), 1);

		double dx = currentX - x;
		double dy = currentY - y;
		double dz = currentZ - z;

		if (dx == 0.0 && dy == 0.0 && dz == 0.0)
			return;

		String code = "";
		
		if (feed != currentFeedrate)
			code += "G1 ";
		
		if (dx != 0)
			code += "X" + gx;
		if (dy != 0)
			code += "Y" + gy;
		if (dz != 0)
			code += "Z" + gz;
		if (feed != currentFeedrate)
		{
			code += "F" + feed;
			currentFeedrate = feed;
		}
		
		gcode.queue(code);
		
		if(turnOff)
			getExtruder().stopExtruding();

		currentX = x;
		currentY = y;
		currentZ = z;
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Printer#getSpeedZ()
	 */
	public int getSpeedZ() {
		return 200;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#setSpeedZ(int)
	 */
	public void setSpeedZ(int speed) {
		// TODO: MiniMug prints this, but I don't know what to do with it
		Debug.d("RR: set speed Z: " + speed);
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#dispose()
	 */
	public void dispose() {
		// TODO: fix this to be more flexible
		
		try
		{
			// Fan off
			getExtruder().setCooler(false);

			// Extruder off
			getExtruder().setExtrusion(0);

			// heater off
			getExtruder().heatOff();
		} catch(Exception e){
			//oops
		}
		
		//write/close our file/serial port
		gcode.finish();
	}


	/* (non-Javadoc)
	 * @see org.reprap.Printer#initialise()
	 */
	public void initialise() {
		
		// TODO: Fix this to be more flexible - howso?
		
		//take us to fun, safe metric land.
		gcode.queue("G21");
		
		// Set absolute positioning, which is what we use.
		gcode.queue("G90");
		
		try	{
			super.initialise();
		} catch (Exception E) {
			Debug.d("Initialization error.");
		}
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#printStartDelay(long)
	 */
	public void printStartDelay(long msDelay) {
		// This would extrude for the given interval to ensure polymer flow.
		getExtruder().startExtruding();
		
		delay(msDelay);
	}

	public void home() {
		super.home();
		
		double xyFeedrate = round(getFastFeedrateXY(), 4);
		
		gcode.queue("G1 X-999 Y-999 F" + xyFeedrate);
		currentFeedrate = xyFeedrate;
		
		//gcode.queue("G0 Z-999");
		gcode.queue("G92");
	}
	
	public void delay(long millis)
	{
		gcode.queue("G4 P" + millis);
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroX()
	 */
	public void homeToZeroX() throws ReprapException, IOException {
		super.homeToZeroX();
		
		double feedrate = round(getMaxFeedrateX(), 4);
		gcode.queue("G1 X-999 F" + feedrate);
		currentFeedrate = feedrate;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroY()
	 */
	public void homeToZeroY() throws ReprapException, IOException {
		super.homeToZeroY();

		double feedrate = round(getMaxFeedrateY(), 4);
		gcode.queue("G1 Y-999 F" + feedrate);
		currentFeedrate = feedrate;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Printer#homeToZeroY()
	 */
	public void homeToZeroZ() throws ReprapException, IOException {
		super.homeToZeroZ();

		double feedrate = round(getMaxFeedrateZ(), 4);
		gcode.queue("G1 Z-999 F" + feedrate);
		currentFeedrate = feedrate;
	}
	
	public double round(double c, double d)
	{
		double power = Math.pow(10.0, d);
		
		return Math.round(c*power)/power;
	}
}