Action.java 3.53 KB


public class Action {
	public static final long DELAI_DEFAULT = 1000;
	
	private boolean endAction = true;
	
	private int deltaL = 0;
	private int deltaR = 0;
	
	private long delaiL = DELAI_DEFAULT; // in us
	private long delaiR = DELAI_DEFAULT; // in us
	
	public Action() {}
	public Action(int deltaL, int deltaR) {
		this.deltaL = deltaL;
		this.deltaR = deltaR;
		this.endAction = false;
	}
	public Action(int deltaL, int deltaR, long delaiL, long delaiR) {
		this.deltaL = deltaL;
		this.deltaR = deltaR;
		this.delaiL = delaiL;
		this.delaiR = delaiR;
		this.endAction = false;
	}

	public void addDeltaL(int amount){this.deltaL += amount; endAction = false;}
	public void addDeltaR(int amount){this.deltaR += amount; endAction = false;}
	public void setDeltaL(int amount){this.deltaL = amount; endAction = false;}
	public void setDeltaR(int amount){this.deltaR = amount; endAction = false;}
	public int getDeltaL(){return this.deltaL;}
	public int getDeltaR(){return this.deltaR;}
	
	public void setDelaiL(long delai){this.delaiL = delai; endAction = false;}
	public void setDelaiR(long delai){this.delaiR = delai; endAction = false;}
	public long getDelaiL(){return this.delaiL;}
	public long getDelaiR(){return this.delaiR;}
	
	public boolean isEmpty() {return endAction;}
	
	public void calculateDelais(long microseconds) {
		// left
		double ticksL = (double) microseconds / (double) deltaL;
		delaiL = (long) (ticksL / 2d); // 1 step = 1 "on state" + 1 "off state"
		
		// right
		double ticksR = (double) microseconds / (double) deltaR;
		delaiR = (long) (ticksR / 2d);
		
		// tests
		if(delaiL < 50 || delaiR < 50)
			System.out.println("Warning : delais are very small ! (< 50 us)");
	}
	
	@Override
	public String toString() {
		if(endAction)
			return "End sequence";
		if(deltaL == 0 && deltaR == 0)
			return "Pause for " + String.valueOf((double) delaiL / 1000d) + " miliseconds";
		if(deltaL == 0) {
			if(deltaR > 0)
				return "Turning right forward for " + String.valueOf(deltaR) + " steps during " + String.valueOf((double) (deltaR * delaiR) / 1000d) + " miliseconds";
			else
				return "Turning right backward for " + String.valueOf(-deltaR) + " steps during " + String.valueOf((double) (-deltaR * delaiR) / 1000d) + " miliseconds";
		} if(deltaR == 0) {
			if(deltaL > 0)
				return "Turning left forward for " + String.valueOf(deltaL) + " steps during " + String.valueOf((double) (deltaL * delaiL) / 1000d) + " miliseconds";
			else
				return "Turning left backward for " + String.valueOf(-deltaL) + " steps during " + String.valueOf((double) (-deltaL * delaiL) / 1000d) + " miliseconds";
		} if(deltaR == deltaL) {
			if(deltaL > 0)
				return "Going forward for " + String.valueOf(deltaL) + " steps during " + String.valueOf((double) (deltaL * delaiL) / 1000d) + " miliseconds";
			else
				return "Going backward for " + String.valueOf(-deltaL) + " steps during " + String.valueOf((double) (-deltaL * delaiL) / 1000d) + " miliseconds";
		} if(deltaR == -deltaL) {
			if(deltaL > 0)
				return "Turning on the spot clockwise for " + String.valueOf(deltaL) + " steps during "
					+ String.valueOf((double) (deltaL * delaiL) / 1000d) + " miliseconds";
			else
				return "Turning on the spot anti-clockwise for " + String.valueOf(deltaR) + " steps during "
					+ String.valueOf((double) (deltaR * delaiR) / 1000d) + " miliseconds";
		}
		return "Custom curve dL:" + String.valueOf(deltaL) + "steps tL:" + String.valueOf((double) (deltaL * delaiL) / 1000d) + "ms | dR"
			+ String.valueOf(deltaR) + "steps tR:" + String.valueOf((double) (deltaR * delaiR) / 1000d) + "ms";
	}
}