summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/devices/pseudo/LinePrinter.java
blob: d8a4b0a63f4fe65f77ab78fe2ca6a912977aa61e (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
package org.reprap.devices.pseudo;

import java.io.IOException;

import org.reprap.devices.GenericExtruder;
import org.reprap.devices.GenericStepperMotor;

/**
 * This is pseudo device that provides an apparent single device
 * for plotting lines.
 */
public class LinePrinter {

	private GenericStepperMotor motorX;
	private GenericStepperMotor motorY;
	private GenericExtruder extruder;

	private boolean initialisedXY = false;
	private int currentX, currentY;
	
	public LinePrinter(GenericStepperMotor motorX, GenericStepperMotor motorY, GenericExtruder extruder) {
		this.motorX = motorX;
		this.motorY = motorY;
		this.extruder = extruder;
	}
	
	public void initialiseXY() throws IOException {
		if (!initialisedXY) {
			currentX = motorX.getPosition();
			currentY = motorY.getPosition();
			initialisedXY = true;
		}
	}

	/**
	 * Move to a 2-space point in a direct line.  At the moment this is just the pure 2D Bresenham algorithm.
	 * It would be good to generalise this to a 3D DDA.
	 */
	public void moveTo(int endX, int endY, int movementSpeed) throws IOException {
		initialiseXY();

		if (currentX == endX && currentY == endY)
			return;
		
		GenericStepperMotor master, slave;

		int x1, y0, y1;
		
		// Whichever is the greater distance will be the master
		// From an algorithmic point of view, we'll just consider
		// the master to be X and the slave to be Y, which eliminates
		// the need for mapping quadrants.
		if (Math.abs(endX - currentX) > Math.abs(endY - currentY)) {
			master = motorX;
			slave = motorY;
			x1 = endX;
			y0 = currentY;
			y1 = endY;
		} else {
			master = motorY;
			slave = motorX;
			x1 = endY;
			y0 = currentX;
			y1 = endX;
		}
				
		master.setSync(GenericStepperMotor.SYNC_NONE);
		if (y0 < y1)
			slave.setSync(GenericStepperMotor.SYNC_INC);
		else
			slave.setSync(GenericStepperMotor.SYNC_DEC);

		int deltaY = Math.abs(y1 - y0); 
		//int deltaX = Math.abs(x1 - x0); 
				
		master.dda(movementSpeed, x1, deltaY);
		
		slave.setSync(GenericStepperMotor.SYNC_NONE);

		currentX = endX;
		currentY = endY;
	}

	public void printTo(int endX, int endY, int movementSpeed, int extruderSpeed) throws IOException {
		extruder.setExtrusion(extruderSpeed);
		moveTo(endX, endY, movementSpeed);
		extruder.setExtrusion(0);
	}
	
	public void printLine(int startX, int startY, int endX, int endY, int movementSpeed, int extruderSpeed) throws IOException {
		moveTo(startX, startY, movementSpeed);
		printTo(endX, endY, movementSpeed, extruderSpeed);
	}

	/**
	 * @return Returns the currentX.
	 */
	public int getCurrentX() {
		return currentX;
	}
	/**
	 * @return Returns the currentY.
	 */
	public int getCurrentY() {
		return currentY;
	}
}