summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/gcode-host-old/src/org/reprap/devices/GenericExtruder.java
blob: bb7246ed4e92085066bee5dabec180e67e3d1555 (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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package org.reprap.devices;

import java.io.IOException;

import org.reprap.Device;
import org.reprap.Preferences;
import org.reprap.utilities.Debug;
import org.reprap.Extruder;
import org.reprap.comms.Address;
import org.reprap.comms.Communicator;
import org.reprap.comms.IncomingMessage;
import org.reprap.comms.OutgoingMessage;
import org.reprap.comms.IncomingMessage.InvalidPayloadException;
import org.reprap.comms.messages.OutgoingBlankMessage;
import org.reprap.comms.messages.OutgoingByteMessage;
import javax.media.j3d.Appearance;
import javax.vecmath.Color3f;
import javax.media.j3d.Material;

/**
 * @author jwiel
 *
 */
public abstract class GenericExtruder implements Extruder
{
	/**
	 * Offset of 0 degrees centigrade from absolute zero
	 */
	public static final double absZero = 273.15;
	
	/**
	 * The temperature to maintain 
	 */
	protected double requestedTemperature = 0;
	
	/**
	 * The temperature most recently read from the device 
	 */
	protected double currentTemperature = 0;
	
	/**
	 * Maximum motor speed (value between 0-255)
	 */
	protected int maxExtruderSpeed; 
	
	/**
	 * The actual extrusion speed
	 */
	protected int extrusionSpeed;
	
	/**
	 * The extrusion temperature
	 */
	protected double extrusionTemp; 
	
	/**
	 * The extrusion width in XY
	 */
	protected double extrusionSize;
	
	/**
	 * The extrusion height in Z
	 * TODO: Should this be a machine-wide constant? - AB
	 */
	protected double extrusionHeight; 
	                                
	/**
	 * The step between infill tracks
	 */
	protected double extrusionInfillWidth; 
	
	/**
	 * The number of mm to stop extruding before the end of a track
	 */
	protected double extrusionOverRun; 
	
	/**
	 * The number of milliseconds to wait before starting a track
	 */
	protected int extrusionDelay;
	
	/**
	 * The number of seconds to cool between layers
	 */
	protected int coolingPeriod;

	/**
	 * The base speed of movement in XY in mm/minute
	 */
	protected double xyFeedrate; 
	
	/**
	 * Zero torque speed 
	 */
	protected int t0;
	
	/**
	 * Infill speed [0,1]*maxSpeed
	 */
	protected double iSpeed;
	
	/**
	 * Outline speed [0,1]*maxSpeed
	 */
	protected double oSpeed;
	
	/**
	 * Length (mm) to speed up round corners
	 */
	protected double asLength;
	
	/**
	 * Factor by which to speed up round corners
	 */
	protected double asFactor;
	
	/**
	 * Line length below which to plot faster
	 */
	protected double shortLength;
	
	/**
	 *Factor for short line speeds
	 */
	protected double shortSpeed;
	
	/**
	 * The name of this extruder's material
	 */
	protected String material;
	
	/**
	 * Number of mm to overlap the hatching infill with the outline.  0 gives none; -ve will 
     * leave a gap between the two
	 */
	protected double infillOverlap = 0;
	
	/**
	 * Where to put the nozzle
	 */
	protected double offsetX, offsetY, offsetZ; 
	
	/**
	 * 
	 */
	protected long lastTemperatureUpdate = 0;
	
	/**
	 * Identifier of extruder
	 * TODO: which values mean what? 
	 */
	protected int myExtruderID;
	
	/**
	 * Start polygons at random perimiter points? 
	 */
	protected boolean randSt = false;
	
	/**
	 * Flag indicating if initialisation succeeded.  Usually this
	 * indicates if the extruder is present in the network. 
	 */
	protected boolean isCommsAvailable = false;
	
	/**
	 *  The colour black
	 */	
	protected static final Color3f black = new Color3f(0, 0, 0);
	
	/**
	 *  The colour of the material to use in the simulation windows 
	 */	
	protected Appearance materialColour;
	
	/**
	 * Enable wiping procedure for nozzle
	 */
	protected boolean nozzleWipeEnabled;
	
	/**
	 * Co-ordinates for the nozzle wiper
	 */
	protected int nozzleWipeDatumX;
	protected int nozzleWipeDatumY;
	
	/**
	 * Distance to move nozzle over wiper
	 */
	protected int nozzleWipeStroke;
	
	/**
	 * Number of wipe cycles per method call
	 */
	protected int nozzleWipeFreq;

	/**
	* name of our extruder
	*/
	String prefName;
	
	/**
	* are we currently extruding?
	*/
	boolean isExtruding;

	public GenericExtruder(Preferences prefs, int extruderId)
	{
		myExtruderID = extruderId;
		prefName = "Extruder" + extruderId + "_";
		
		maxExtruderSpeed = prefs.loadInt(prefName + "MaxSpeed(0..255)");
		extrusionSpeed = prefs.loadInt(prefName + "ExtrusionSpeed(0..255)");
		extrusionTemp = prefs.loadDouble(prefName + "ExtrusionTemp(C)");
		extrusionSize = prefs.loadDouble(prefName + "ExtrusionSize(mm)");
		extrusionHeight = prefs.loadDouble(prefName + "ExtrusionHeight(mm)");
		extrusionInfillWidth = prefs.loadDouble(prefName + "ExtrusionInfillWidth(mm)");
		extrusionOverRun = prefs.loadDouble(prefName + "ExtrusionOverRun(mm)");
		extrusionDelay = prefs.loadInt(prefName + "ExtrusionDelay(ms)");
		coolingPeriod = prefs.loadInt(prefName + "CoolingPeriod(s)");
		xyFeedrate = prefs.loadDouble(prefName + "XYFeedrate(mm/minute)");
		t0 = prefs.loadInt(prefName + "t0(0..255)");
		iSpeed = prefs.loadDouble(prefName + "InfillSpeed(0..1)");
		oSpeed = prefs.loadDouble(prefName + "OutlineSpeed(0..1)");
		asLength = prefs.loadDouble(prefName + "AngleSpeedLength(mm)");
		asFactor = prefs.loadDouble(prefName + "AngleSpeedFactor(0..1)");
		material = prefs.loadString(prefName + "MaterialType(name)");
		offsetX = prefs.loadDouble(prefName + "OffsetX(mm)");
		offsetY = prefs.loadDouble(prefName + "OffsetY(mm)");
		offsetZ = prefs.loadDouble(prefName + "OffsetZ(mm)");
		nozzleWipeEnabled = prefs.loadBool(prefName + "NozzleWipeEnabled");
		nozzleWipeDatumX = prefs.loadInt(prefName + "NozzleWipeDatumX(mm)");
		nozzleWipeDatumY = prefs.loadInt(prefName + "NozzleWipeDatumY(mm)");
		nozzleWipeStroke = prefs.loadInt(prefName + "NozzleWipeStroke(mm)");
		nozzleWipeFreq = prefs.loadInt(prefName + "NozzleWipeFreq");
		randSt = prefs.loadBool(prefName + "RandomStart");
		shortLength = prefs.loadDouble(prefName + "ShortLength(mm)");
		shortSpeed = prefs.loadDouble(prefName + "ShortSpeed(0..1)");
		infillOverlap = prefs.loadDouble(prefName + "InfillOverlap(mm)");
		
		Color3f col = new Color3f((float)prefs.loadDouble(prefName + "ColourR(0..1)"), 
				(float)prefs.loadDouble(prefName + "ColourG(0..1)"), 
				(float)prefs.loadDouble(prefName + "ColourB(0..1)"));
		materialColour = new Appearance();
		materialColour.setMaterial(new Material(col, black, col, black, 101f));
		
		isExtruding = false;
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Extruder#dispose()
	 */
	public void dispose() {
	}

	/**
	 * Turn the extruder on using the extrusionTemp property
	 * @throws Exception
	 */
	
	public void heatOn() throws Exception 
	{
		setTemperature(extrusionTemp);
	}
	
	public void heatOff() throws Exception 
	{
		setTemperature(0);
	}
	
	/**
	 * Start the extruder motor at a given speed.  This ranges from 0
	 * to 255 but is scaled by maxSpeed and t0, so that 255 corresponds to the
	 * highest permitted speed.  It is also scaled so that 0 would correspond
	 * with the lowest extrusion speed.
	 * @param speed The speed to drive the motor at (0-255)
	 * @throws IOException
	 */
	public void setExtrusion(int speed) throws IOException
	{
		if (speed > 0)
			isExtruding = true;
		else
			isExtruding = false;
			
		setExtrusion(speed, false);
	}
	
	public void startExtruding()
	{
		if (!isExtruding)
		{
			isExtruding = true;
			try
			{
				setExtrusion(getExtruderSpeed());
			} catch (Exception e) {
				//hmm.
			}
		}
	}
	
	
	public void stopExtruding()
	{
		if (isExtruding)
		{
			isExtruding = false;
			
			try
			{
				setExtrusion(0);
			} catch (Exception e) {
				//hmm.
			}
		}
	}

	
	public void setHeater(int heat, double maxTemp) throws IOException
	{
	}	
	
	/* (non-Javadoc)
	 * @see org.reprap.Extruder#getTemperatureTarget()
	 */
	public double getTemperatureTarget() {
		return requestedTemperature;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Extruder#getDefaultTemperature()
	 */
	public double getDefaultTemperature() {
		return extrusionTemp;
	}	
	
	/* (non-Javadoc)
	 * @see org.reprap.Extruder#getTemperature()
	 */
	public double getTemperature()
	{
		return currentTemperature;
	}

	/**
	 * The the outline speed and the infill speed [0,1]
	 */
	public double getInfillSpeedFactor()
	{
		return iSpeed;
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Extruder#getInfillFeedrate()
	 */
	public double getInfillFeedrate()
	{
		return getInfillSpeedFactor() * getXYFeedrate();
	}

	/* (non-Javadoc)
	 * @see org.reprap.Extruder#getOutlineSpeedFactor()
	 */
	public double getOutlineSpeedFactor()
	{
		return oSpeed;
	}

	/* (non-Javadoc)
	 * @see org.reprap.Extruder#getOutlineFeedrate()
	 */
	public double getOutlineFeedrate()
	{
		return getOutlineSpeedFactor() * getXYFeedrate();
	}


	/**
	 * The length in mm to speed up when going round corners
	 * (non-Javadoc)
	 * @see org.reprap.Extruder#getAngleSpeedUpLength()
	 */
	public double getAngleSpeedUpLength()
	{
		return asLength;
	}
	
	/**
	 * The factor by which to speed up when going round a corner.
	 * The formula is speed = baseSpeed*[1 - 0.5*(1 + ca)*getAngleSpeedFactor()]
	 * where ca is the cos of the angle between the lines.  So it goes fastest when
	 * the line doubles back on itself (returning 1), and slowest when it 
	 * continues straight (returning 1 - getAngleSpeedFactor()).
	 * (non-Javadoc)
	 * @see org.reprap.Extruder#getAngleSpeedFactor()
	 */
	public double getAngleSpeedFactor()
	{
		return asFactor;
	}	
	
	public double getAngleFeedrate()
	{
		return getAngleSpeedFactor() * getXYFeedrate();
	}

	/* (non-Javadoc)
	 * @see org.reprap.Extruder#isAvailable()
	 */
	public boolean isAvailable()
	{
		return isCommsAvailable;
	}

    /* (non-Javadoc)
     * @see org.reprap.Extruder#getXYSpeed()
     */
    public double getXYFeedrate()
    {
    	return xyFeedrate;
    }
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtruderSpeed()
     */
    public int getExtruderSpeed()
    {
    	return extrusionSpeed;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionSize()
     */
    public double getExtrusionSize()
    {
    	return extrusionSize;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionHeight()
     */
    public double getExtrusionHeight()
    {
    	return extrusionHeight;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionInfillWidth()
     */
    public double getExtrusionInfillWidth()
    {
    	return extrusionInfillWidth;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionOverRun()
     */
    public double getExtrusionOverRun()
    {
    	return extrusionOverRun;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionDelay()
     */
    public long getExtrusionDelay()
    {
    	return extrusionDelay;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getCoolingPeriod()
     */
    public int getCoolingPeriod()
    {
    	return coolingPeriod;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getOffsetX()
     */
    public double getOffsetX()
    {
    	return offsetX;
    }    
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getOffsetY()
     */
    public double getOffsetY()
    {
    	return offsetY;
    }
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getOffsetZ()
     */
    public double getOffsetZ()
    {
    	return offsetZ;
    }
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getColour()
     */    
    public Appearance getAppearance()
    {
    	return materialColour;
    }  
    
    /**
     * @return the name of the material
     */
    public String toString()
    {
    	return material;
    }
    
    /**
     * @return determine whether nozzle wipe method is enabled or not 
     */
    public boolean getNozzleWipeEnabled()
    {
    	return nozzleWipeEnabled;
    }    
    
    /**
     * @return the X-cord for the nozzle wiper
     */
    public int getNozzleWipeDatumX()
    {
    	return nozzleWipeDatumX;
    }

    /**
     * @return the Y-cord for the nozzle wiper
     */
    public int getNozzleWipeDatumY()
    {
    	return nozzleWipeDatumY;
    }
    
    /**
     * @return the length of the nozzle movement over the wiper
     */
    public int getNozzleWipeStroke()
    {
    	return nozzleWipeStroke;
    }
    
    /**
     * @return the number of times the nozzle moves over the wiper
     */
    public int getNozzleWipeFreq()
    {
    	return nozzleWipeFreq;
    }
    
    /**
     * Start polygons at a random location round their perimiter
     * @return
     */
    public boolean randomStart()
    {
    	return randSt;
    }
    
    /**
     * get short lengths which need to be plotted faster
     * set -ve to turn this off.
     * @return
     */
    public double getShortLength()
    {
    	return shortLength; 
    }
     
    /**
     * Factor (between 0 and 1) to use to set the speed for
     * short lines.
     * @return
     */
    public double getShortLineSpeedFactor()
    {
    	return shortSpeed; 
    }

	/**
	 * Feedrate for short lines in mm/minute
	 * @return
	 */
	public double getShortLineFeedrate()
	{
		return getShortLineSpeedFactor() * getXYFeedrate();
	}
    
    /**
     * Number of mm to overlap the hatching infill with the outline.  0 gives none; -ve will 
     * leave a gap between the two
     * @return
     */
    public double getInfillOverlap()
    {
    	return infillOverlap;
    }

    
    public static int getNumberFromMaterial(String material)
    {
    	String[] names;
		try
		{
			names = Preferences.allMaterials();
			for(int i = 0; i < names.length; i++)
			{
				if(names[i].equals(material))
					return i;
			}

		} catch (Exception ex)
		{
			System.err.println(ex.toString());
		}
		return -1;
    }
    
    public static Appearance getAppearanceFromNumber(int n)
    {
    	String prefName = "Extruder" + n + "_";
    	Color3f col = null;
		try
		{
			col = new Color3f((float)Preferences.loadGlobalDouble(prefName + "ColourR(0..1)"), 
				(float)Preferences.loadGlobalDouble(prefName + "ColourG(0..1)"), 
				(float)Preferences.loadGlobalDouble(prefName + "ColourB(0..1)"));
		} catch (Exception ex)
		{
			System.err.println(ex.toString());
		}
		Appearance a = new Appearance();
		a.setMaterial(new Material(col, black, col, black, 101f));
		return a;
    }
    
    public static Appearance getAppearanceFromMaterial(String material)
    {
    	return(getAppearanceFromNumber(getNumberFromMaterial(material)));
    }

}