summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/devices/GenericExtruder.java
blob: 3a3fe01bcdef89c72211ed481efaf6f0e8e7d630 (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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
package org.reprap.devices;

import java.io.IOException;

import org.reprap.Preferences;
import org.reprap.Extruder;
import org.reprap.Printer;
import org.reprap.geometry.LayerRules;
import org.reprap.utilities.Debug;
import javax.media.j3d.Appearance;
import javax.vecmath.Color3f;
import javax.media.j3d.Material;

/**
 * @author jwiel
 *
 */
public abstract class GenericExtruder implements Extruder
{
	/**
	 * Flag to decide extrude speed
	 */
	protected boolean separating;
	
	/**
	 * How far we have extruded plus other things like temperature
	 */
	protected ExtruderState es;
	
	/**
	 * Flag to decide if the machine implements 4D extruding
	 * (i.e. it processes extrude lengths along with X, Y, and Z
	 * lengths in a 4D Bressenham DDA)
	 */
	protected boolean fiveD;
	
	/**
	 * Flag to record if we're going backwards
	 */
	//protected boolean reversing;
	
	/**
	 * The current extrude speed
	 */
	//protected double currentSpeed = 0;
	
	/**
	 * Offset of 0 degrees centigrade from absolute zero
	 */
	public static final double absZero = 273.15;
	
	/**
	 * 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 double extrusionSpeed;
	
	/**
	 * The time to run the extruder at the start
	 * -ve values supress
	 */
	protected double purgeTime;
	
	/**
	 * The PWM into stepper extruders
	 */
	protected double extrusionPWM;
	
	/**
	 * 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; 
	
	/**
	 * below this infill finely
	 */
	protected int lowerFineLayers;
	
	/**
	 * Above this infill finely
	 */
	protected int upperFineLayers;
	
	/**
	 * Use this for broad infill in the middle; if negative, always
	 * infill fine.
	 */
	protected double extrusionBroadWidth;
   	
	/**
	 * The number of seconds to cool between layers
	 */
	protected double coolingPeriod;
	
	/**
	 * The fastest speed of movement in XY when depositing
	 */
	protected double fastXYFeedrate;
	
	/**
	 * The speed from which that machine can do a standing start with this extruder
	 */
	protected double slowXYFeedrate;
	
	/**
	 * The fastest the machine can accelerate with this extruder
	 */
	protected double maxAcceleration;
	
	/**
	 * 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 the extruder 
	 */
	protected int myExtruderID;
	
	/**
	 * One physical extruder can have several of this class attached to it
	 * with different myExtruderIDs.  physicalExtruder is a unique integer
	 * for each actual extruder, derived from the old SNAP address of the extruder,
	 * which is necessarily unique.
	 */
	//protected int physicalExtruder;
	
	/**
	* prefix for our preferences.
	*/
	protected String prefName;
	
	/**
	 * Start polygons at random perimiter points 
	 */
	protected boolean randSt = false;

	/**
	 * Start polygons at incremented perimiter points 
	 */
	protected boolean incrementedSt = 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 double nozzleWipeDatumX;
	protected double nozzleWipeDatumY;
	
	/**
	 * X Distance to move nozzle over wiper
	 */
	protected double nozzleWipeStrokeX;
	
	/**
	 * Y Distance to move nozzle over wiper
	 */
	protected double nozzleWipeStrokeY;
	
	/**
	 * Number of wipe cycles per method call
	 */
	protected int nozzleWipeFreq;
	
	/**
	 * Number of seconds to run to re-start the nozzle before a wipe
	 */
	protected double nozzleClearTime;

	/**
	 * Number of seconds to wait after restarting the nozzle
	 */
	protected double nozzleWaitTime;
	
	/**
	 * The current coordinate to wipe at
	 */
	protected double wipeX;
	
	/**
	 * The number of ms to pulse the valve to open or close it
	 * -ve to supress
	 */
	protected double valvePulseTime;
	
	/**
	 * The number of milliseconds to wait before starting a border track
	 */
	protected double extrusionDelayForLayer = 0;
	
	/**
	 * The number of milliseconds to wait before starting a hatch track
	 */
	protected double extrusionDelayForPolygon = 0;
	
	/**
	 * The number of milliseconds to reverse at the end of a track
	 */
	protected double extrusionReverseDelay = -1;
	
	/**
	 * The number of milliseconds to wait before starting a border track
	 */
	protected double valveDelayForLayer = 0;
	
	/**
	 * The number of milliseconds to wait before starting a hatch track
	 */
	protected double valveDelayForPolygon = 0;
	
	/**
	 * The number of mm to stop extruding before the end of a track
	 */
	protected double extrusionOverRun; 
	
	/**
	 * The number of mm to stop extruding before the end of a track
	 */
	protected double valveOverRun; 
	
    /**
     * The smallest allowable free-movement height above the base
     */
	protected double minLiftedZ = 1;
	
	/**
	 * The number of outlines to plot
	 */
	protected int shells = 1;
	
	/**
	 * Stop the extrude motor between segments?
	 */
	protected boolean pauseBetweenSegments = true;
	
	/**
	* Are we currently extruding?
	*/
	//protected boolean isExtruding = false;
	
	private double extrusionFoundationWidth;
	private double extrusionLastFoundationWidth;
	private double separationFraction;
	
	private double arcCompensationFactor;
	private double arcShortSides;
	
	private double evenHatchDirection;
	private double oddHatchDirection;
	private String supportMaterial;
	private String inFillMaterial;
	
	protected double extrudeRatio = 1;
	
	/**
	* Our printer object.
	*/
	protected Printer printer = null;
	
	/**
	 * @param extruderId
	 */
	public GenericExtruder(int extruderId, Printer p)
	{
		printer = p;
		try
		{
			fiveD = Preferences.loadGlobalBool("FiveD");
		} catch (Exception e)
		{
			fiveD = false;
		}
		myExtruderID = extruderId;
		separating = false;
		es = new ExtruderState(refreshPreferences());
		es.setReverse(false);
	}

	/**
	 * Allow otthers to set our extrude length so that all logical extruders
	 * talking to one physical extruder can use the same length instance.
	 * @param e
	 */
	public void setExtrudeState(ExtruderState e)
	{
		es = e;
	}
	
	/**
	 * Zero the extruded length
	 *
	 */
	public void zeroExtrudedLength()
	{
		es.zero();
	}
	
	public int refreshPreferences()
	{
		prefName = "Extruder" + myExtruderID + "_";
		int result = -1;
		try
		{
			result = Preferences.loadGlobalInt(prefName + "Address");
			maxExtruderSpeed = Preferences.loadGlobalInt(prefName + "MaxSpeed(0..255)");
//			extrusionSpeed = Preferences.loadGlobalDouble(prefName + "ExtrusionSpeed(mm/minute)");
			purgeTime = Preferences.loadGlobalDouble(prefName + "Purge(ms)");
			extrusionPWM = Preferences.loadGlobalDouble(prefName + "ExtrusionPWM(0..1)");
			extrusionTemp = Preferences.loadGlobalDouble(prefName + "ExtrusionTemp(C)");
			extrusionSize = Preferences.loadGlobalDouble(prefName + "ExtrusionSize(mm)");
			extrusionHeight = Preferences.loadGlobalDouble(prefName + "ExtrusionHeight(mm)");
			extrusionInfillWidth = Preferences.loadGlobalDouble(prefName + "ExtrusionInfillWidth(mm)");
			lowerFineLayers = Preferences.loadGlobalInt(prefName + "LowerFineLayers(0...)");
			upperFineLayers = Preferences.loadGlobalInt(prefName + "UpperFineLayers(0...)");
			extrusionBroadWidth = Preferences.loadGlobalDouble(prefName + "ExtrusionBroadWidth(mm)");		
			coolingPeriod = Preferences.loadGlobalDouble(prefName + "CoolingPeriod(s)");
			fastXYFeedrate = Preferences.loadGlobalDouble(prefName + "FastXYFeedrate(mm/minute)");
			slowXYFeedrate = Preferences.loadGlobalDouble(prefName + "SlowXYFeedrate(mm/minute)");
			maxAcceleration = Preferences.loadGlobalDouble(prefName + "MaxAcceleration(mm/minute/minute)");
			t0 = Preferences.loadGlobalInt(prefName + "t0(0..255)");
			iSpeed = Preferences.loadGlobalDouble(prefName + "InfillSpeed(0..1)");
			oSpeed = Preferences.loadGlobalDouble(prefName + "OutlineSpeed(0..1)");
			asLength = Preferences.loadGlobalDouble(prefName + "AngleSpeedLength(mm)");
			asFactor = Preferences.loadGlobalDouble(prefName + "AngleSpeedFactor(0..1)");
			material = Preferences.loadGlobalString(prefName + "MaterialType(name)");
			supportMaterial = Preferences.loadGlobalString(prefName + "SupportMaterialType(name)");
			inFillMaterial = Preferences.loadGlobalString(prefName + "InFillMaterialType(name)");			
			offsetX = Preferences.loadGlobalDouble(prefName + "OffsetX(mm)");
			offsetY = Preferences.loadGlobalDouble(prefName + "OffsetY(mm)");
			offsetZ = Preferences.loadGlobalDouble(prefName + "OffsetZ(mm)");
			nozzleWipeEnabled = Preferences.loadGlobalBool(prefName + "NozzleWipeEnabled");
			nozzleWipeDatumX = Preferences.loadGlobalDouble(prefName + "NozzleWipeDatumX(mm)");
			nozzleWipeDatumY = Preferences.loadGlobalDouble(prefName + "NozzleWipeDatumY(mm)");
			nozzleWipeStrokeX = Preferences.loadGlobalDouble(prefName + "NozzleWipeStrokeX(mm)");
			nozzleWipeStrokeY = Preferences.loadGlobalDouble(prefName + "NozzleWipeStrokeY(mm)");
			nozzleWipeFreq = Preferences.loadGlobalInt(prefName + "NozzleWipeFreq");
			nozzleClearTime = Preferences.loadGlobalDouble(prefName + "NozzleClearTime(s)");
			nozzleWaitTime = Preferences.loadGlobalDouble(prefName + "NozzleWaitTime(s)");
			randSt = Preferences.loadGlobalBool(prefName + "RandomStart");
			incrementedSt = Preferences.loadGlobalBool(prefName + "IncrementedStart");
			shortLength = Preferences.loadGlobalDouble(prefName + "ShortLength(mm)");
			shortSpeed = Preferences.loadGlobalDouble(prefName + "ShortSpeed(0..1)");
			infillOverlap = Preferences.loadGlobalDouble(prefName + "InfillOverlap(mm)");
			extrusionDelayForLayer = Preferences.loadGlobalDouble(prefName + "ExtrusionDelayForLayer(ms)");
			extrusionDelayForPolygon = Preferences.loadGlobalDouble(prefName + "ExtrusionDelayForPolygon(ms)");
			extrusionOverRun = Preferences.loadGlobalDouble(prefName + "ExtrusionOverRun(mm)");
			valveDelayForLayer = Preferences.loadGlobalDouble(prefName + "ValveDelayForLayer(ms)");
			valveDelayForPolygon = Preferences.loadGlobalDouble(prefName + "ValveDelayForPolygon(ms)");
			extrusionReverseDelay = Preferences.loadGlobalDouble(prefName + "Reverse(ms)");
			valveOverRun = Preferences.loadGlobalDouble(prefName + "ValveOverRun(mm)");		
			minLiftedZ = Preferences.loadGlobalDouble(prefName + "MinimumZClearance(mm)");
			// NB - store as 2ms ticks to allow longer pulses
			valvePulseTime = 0.5*Preferences.loadGlobalDouble(prefName + "ValvePulseTime(ms)");
			shells = Preferences.loadGlobalInt(prefName + "NumberOfShells(0..N)");
			pauseBetweenSegments = Preferences.loadGlobalBool(prefName + "PauseBetweenSegments");
			extrusionFoundationWidth = Preferences.loadGlobalDouble(prefName + "ExtrusionFoundationWidth(mm)");
			extrusionLastFoundationWidth = Preferences.loadGlobalDouble(prefName + "ExtrusionLastFoundationWidth(mm)");
			separationFraction = Preferences.loadGlobalDouble(prefName + "SeparationFraction(0..1)");
			arcCompensationFactor = Preferences.loadGlobalDouble(prefName + "ArcCompensationFactor(0..)");
			arcShortSides = Preferences.loadGlobalDouble(prefName + "ArcShortSides(0..)");
			extrudeRatio = Preferences.loadGlobalDouble(prefName + "ExtrudeRatio(0..)");
			
			evenHatchDirection = Preferences.loadGlobalDouble(prefName + "EvenHatchDirection(degrees)");
			oddHatchDirection = Preferences.loadGlobalDouble(prefName + "OddHatchDirection(degrees)");			
			
			Color3f col = new Color3f((float)Preferences.loadGlobalDouble(prefName + "ColourR(0..1)"), 
					(float)Preferences.loadGlobalDouble(prefName + "ColourG(0..1)"), 
					(float)Preferences.loadGlobalDouble(prefName + "ColourB(0..1)"));
			materialColour = new Appearance();
			materialColour.setMaterial(new Material(col, black, col, black, 101f));
		} catch (Exception ex)
		{
			System.err.println("Refresh extruder preferences: " + ex.toString());
		}
		
		if(printer == null)
		{
			System.err.println("GenericExtruder(): printer is null!");
		} else
		{
			fastXYFeedrate = Math.min(printer.getFastXYFeedrate(), fastXYFeedrate);
			slowXYFeedrate = Math.min(printer.getSlowXYFeedrate(), slowXYFeedrate);
			maxAcceleration = Math.min(printer.getMaxXYAcceleration(), maxAcceleration);			
		}
		
		return result;
	}
	
	
	/* (non-Javadoc)
	 * @see org.reprap.Extruder#dispose()
	 */
	public void dispose() {
	}
	
	/**
	 * Wait while the motors move about
	 * @throws IOException
	 */
	public void waitTillNotBusy() throws IOException
	{
		if(printer == null)
			return;
		printer.waitTillNotBusy();
	}
	
	
	public void setPrinter(Printer p)
	{
		printer = p;
	}
	
	public Printer getPrinter()
	{
		return printer;
	}	

	/**
	 * Start the extruder motor at a given speed.  In the old scheme, 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.
	 * 
	 * In the new scheme speed is simply mm/sec.
	 * 
	 * @param speed The speed to drive the motor at (0-255)
	 * @throws IOException
	 */
	public void setExtrusion(double speed, boolean rev) throws IOException
	{
		if (speed > 0)
			es.setExtruding(true);
		else
			es.setExtruding(false);
			
		es.setSpeed(speed);
		es.setReverse(rev);
	}
	
	public void setTemperature(double temperature, boolean wait) throws Exception
	{
		es.setTargetTemperature(temperature);
	}
	
	public void startExtruding()
	{
		if (!es.isExtruding())
		{
			try
			{
				setExtrusion(getExtruderSpeed(), false);
			} catch (Exception e) {
				//hmm.
			}
			es.setExtruding(true);
		}
	}
	
	public void stopExtruding()
	{
		if (es.isExtruding())
		{
			try
			{
				setExtrusion(0, false);
			} catch (Exception e) {
				//hmm.
			}
			es.setExtruding(false);
		}
	}
	
	public void setMotor(boolean motorOn) throws IOException
	{
		if(getExtruderSpeed() < 0)
			return;
		
		if(motorOn)
		{
			setExtrusion(getExtruderSpeed(), false);
			es.setSpeed(getExtruderSpeed());
		} else
		{
			setExtrusion(0, false);
			es.setSpeed(0);
		}
		es.setReverse(false);
	}

	public void heatOn(boolean wait) throws Exception 
	{
		setTemperature(extrusionTemp, wait);
	}
	
	public void heatOff() throws Exception 
	{
		setTemperature(0, false);
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Extruder#getTemperatureTarget()
	 */
	public double getTemperatureTarget() {
		return es.targetTemperature();
	}

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

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

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

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

	
	/**
	 * 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() * getFastXYFeedrate();
	}
	
	/* (non-Javadoc)
	 * @see org.reprap.Extruder#isAvailable()
	 */
	public boolean isAvailable()
	{
		return true;
	}

    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getXYSpeed()
     */
    public double getFastXYFeedrate()
    {
    	return fastXYFeedrate;
    }
    
	/**
	 * @return slow XY movement feedrate in mm/minute
	 */
	public double getSlowXYFeedrate()
	{
		return slowXYFeedrate;
	}
	
	/**
	 * @return the fastest the machine can accelerate
	 */
	public double getMaxAcceleration()
	{
		return maxAcceleration;
	}
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtruderSpeed()
     */
    private double getRegularExtruderSpeed()
    {
    	try
    	{
    		return Preferences.loadGlobalDouble(prefName + "ExtrusionSpeed(mm/minute)");
    	} catch (Exception e)
    	{
    		System.err.println(e.toString());
    		return 200;  //Hack
    	}
    }
    
    private double getSeparationSpeed()
    {
    	try
    	{
    		return Preferences.loadGlobalDouble(prefName + "SeparationSpeed(mm/minute)");
    	} catch (Exception e)
    	{
    		System.err.println(e.toString());
    		return 200;  //Hack
    	}
    }
    
    public void setSeparating(boolean s)
    {
    	separating = s;
    }
    
    public double getExtruderSpeed()
    {
    	if(separating)
    		return getSeparationSpeed();
    	else
    		return getRegularExtruderSpeed();
    	
    }
    
    
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionSize()
     */
    public double getExtrusionSize()
    {
    	return extrusionSize;
    } 
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionHeight()
     */
    public double getExtrusionHeight()
    {
    	return extrusionHeight;
    } 
    
    /**
     * At the top and bottom return the fine width; in between
     * return the braod one.  If the braod one is negative, just do fine.
     */
    public double getExtrusionInfillWidth()
    {
    		return extrusionInfillWidth;
    } 
    
    public double getExtrusionBroadWidth()
    {
    		return extrusionBroadWidth;
    } 
    
	public int getLowerFineLayers()
	{
		return lowerFineLayers;
	}
	
	public int getUpperFineLayers()
	{
		return upperFineLayers;
	}
    
  
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getCoolingPeriod()
     */
    public double 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
     * TODO: should this give more information?
     */
    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 double getNozzleWipeDatumX()
    {
    	return nozzleWipeDatumX;
    }

    /**
     * @return the Y-cord for the nozzle wiper
     */
    public double getNozzleWipeDatumY()
    {
    	return nozzleWipeDatumY;
    }
    
    /**
     * @return the length of the nozzle movement over the wiper
     */
    public double getNozzleWipeStrokeX()
    {
    	return nozzleWipeStrokeX;
    }
    
    /**
     * @return the length of the nozzle movement over the wiper
     */
    public double getNozzleWipeStrokeY()
    {
    	return nozzleWipeStrokeY;
    }
    
    /**
     * @return the number of times the nozzle moves over the wiper
     */
    public int getNozzleWipeFreq()
    {
    	return nozzleWipeFreq;
    }
    
    /**
     * @return the time to extrude before wiping the nozzle
     */
    public double getNozzleClearTime()
    {
    	return nozzleClearTime;
    }
    
    /**
     * @return the time to wait after extruding before wiping the nozzle
     */
    public double getNozzleWaitTime()
    {
    	return nozzleWaitTime;
    }
    
    
    /**
     * Start polygons at a random location round their perimiter
     * @return
     */
    public boolean randomStart()
    {
    	return randSt;
    }

    /**
     * Start polygons at an incremented location round their perimiter
     * @return
     */
    public boolean incrementedStart()
    {
    	return incrementedSt;
    }
    
    /**
     * 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() * getFastXYFeedrate();
	}
    
    /**
     * 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;
    }
    
    /**
	 * Gets the number of milliseconds to wait before starting a border track
	 * @return
     */
    public double getExtrusionDelayForLayer()
    {
    	return extrusionDelayForLayer; 
    }
    
    /**
	 * Gets the number of milliseconds to wait before starting a hatch track
	 * @return
     */
    public double getExtrusionDelayForPolygon()
    {
    	return extrusionDelayForPolygon; 
    }
    
    
    /**
	 * Gets the number of milliseconds to reverse the extrude motor
	 * at the end of a track
	 * @return
     */
    public double getExtrusionReverseDelay()
    {
    	return extrusionReverseDelay;
    }
    /**
	 * Gets the number of milliseconds to wait before opening the valve
	 * for the first track of a layer
	 * @return
     */
    public double getValveDelayForLayer()
    {
    	return valveDelayForLayer;
    }
    
    /**
	 * Gets the number of milliseconds to wait before opening the valve
	 * for any other track
	 * @return
     */
    public double getValveDelayForPolygon()
    {
    	return valveDelayForPolygon;
    }
    
    /* (non-Javadoc)
     * @see org.reprap.Extruder#getExtrusionOverRun()
     */
    public double getExtrusionOverRun()
    {
    	return extrusionOverRun;
    } 
 
    /**
     * @return the valve overrun in millimeters (i.e. how many mm
     * before the end of a track to turn off the extrude motor)
     */
    public double getValveOverRun()
    {
    	return valveOverRun;
    }
    
    /**
     * The smallest allowable free-movement height above the base
     * @return
     */
    public double getMinLiftedZ()
    {
    	return minLiftedZ;
    }
    
    /**
     * The number of times to go round the outline (0 to supress)
     * @return
     */
    public int getShells()
    {
    	return shells;
    }
    
    /**
     * Stop the extrude motor between segments?
     * @return
     */
    public boolean getPauseBetweenSegments()
    {
    	return pauseBetweenSegments;
    }
    
	/**
	 * What stuff are we working with?
	 * @return
	 */
	public String getMaterial()
	{
		return material;
	}
	
	/**
	 * What stuff are we holding up with?
	 * @return
	 */
	public String getSupportMaterial()
	{
		return supportMaterial;
	}
	
	public int getSupportExtruder()
	{
		return getNumberFromMaterial(supportMaterial);
	}
	
	/**
	 * What stuff are we infilling with?
	 * @return
	 */
	public String getInfillMaterial()
	{
		return inFillMaterial;
	}
	
	public int getInfillExtruder()
	{
		return getNumberFromMaterial(inFillMaterial);
	}
	
	/**
	 * What are the dimensions for infill?
	 * @return
	 */
//	public String getBroadInfillMaterial()
//	{
//		return inFillMaterial;
//	}
	
    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;
			}
			throw new Exception("getNumberFromMaterial - can't find " + material);
		} catch (Exception ex)
		{
			Debug.d(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)));
    }
    
    public double getExtrusionFoundationWidth()
    {
    	return extrusionFoundationWidth;
    }

    public double getLastFoundationWidth(LayerRules lc)
    {
    	return extrusionLastFoundationWidth;
    }
    
	/**
	 * At the support layer before a layer is to be separated, how far up
	 * the normal Z movement do we go to make a bigger gap to form a weak join?
	 * @return
	 */
	public double getSeparationFraction()
	{
		return separationFraction;
	}
	
	/**
	 * The arc compensation factor.  
	 * See org.reprap.geometry.polygons.RrPolygon.arcCompensate(...)
	 * @return
	 */
	public double getArcCompensationFactor()
	{
		return arcCompensationFactor;
	}
	
	/**
	 * The arc short sides.  
	 * See org.reprap.geometry.polygons.RrPolygon.arcCompensate(...)
	 * @return
	 */
	public double getArcShortSides()
	{
		return arcShortSides;
	}
	
	/**
	 * The direction to hatch even-numbered layers in degrees anticlockwise
	 * from the X axis
	 * @return
	 */
	public double getEvenHatchDirection()
	{
		return evenHatchDirection;
	}

	/**
	 * The direction to hatch odd-numbered layers in degrees anticlockwise
	 * from the X axis
	 * @return
	 */
	public double getOddHatchDirection()
	{
		return oddHatchDirection;		
	}
	
	   /**
     * Find out what our current speed is
     * @return
     */
    public double getCurrentSpeed()
    {
    	return es.speed();
    }
    
    /**
     * Find out if we are currently in reverse
     * @return
     */
    public boolean getReversing()
    {
    	return es.reverse();
    }
    
    /**
     * Get how much extrudate is deposited in a given time (in milliseconds)
     * currentSpeed is in mm per minute
     * @param time (ms)
     * @return
     */
    public double getDistanceFromTime(double time)
    {
    	if(!es.isExtruding())
    		return 0;
    	return es.speed()*time/60000.0;
    }
    
    /**
     * Get how much extrudate is deposited for a given xy movement
     * currentSpeed is in mm per minute
     * @param xyDistance (mm)
     * @param xyFeedrate (mm/minute)
     * @return
     */
    public double getDistance(double distance)
    {
    	if(!es.isExtruding())
    		return 0;
    	return extrudeRatio*distance;
    }
	
    /**
     * Find out if we're working in 5D
     * @return
     */
    public boolean get5D()
    {
    	return fiveD;
    }
    /**
     * Find out how far we have extruded so far
     * @return
     */
    public ExtruderState getExtruderState()
    {
    	return es;
    }
    
    /**
     * Add some extruded length
     * @param l
     */
    public void addExtrudedLength(double l)
    {
    	es.add(l);
    }
    
    /**
     * Each logical extruder has a unique ID
     * @return
     */
    public int getID()
    {
    	return myExtruderID;
    }
    
    /**
     * Several logical extruders can share one physical extruder
     * This number is unique to each physical extruder
     * @return
     */
    public int getPhysicalExtruderNumber()
    {
    	return es.physicalExtruder();
    }
    
	/**
	 * Return the PWM for the motor.  -ve values mean
	 * this feature is unavailable
	 */
	public double getPWM()
	{
		return extrusionPWM;
	}
	
    /**
     * Time to purge the extruder
     * -ve values supress
     * @return
     */
    public double getPurgeTime()
    {
    	return purgeTime;
    }
    
}