Blame view

PercTeacher/Sources/Action.java 3.53 KB
c5abccad   pfrison   PercTeacher inter...
1
2
3
4
5
6
7
  
  
  public class Action {
  	public static final long DELAI_DEFAULT = 1000;
  	
  	private boolean endAction = true;
  	
04949080   pfrison   PercTeacher stabl...
8
9
  	private int deltaL = 0;
  	private int deltaR = 0;
c5abccad   pfrison   PercTeacher inter...
10
11
12
13
14
  	
  	private long delaiL = DELAI_DEFAULT; // in us
  	private long delaiR = DELAI_DEFAULT; // in us
  	
  	public Action() {}
04949080   pfrison   PercTeacher stabl...
15
  	public Action(int deltaL, int deltaR) {
c5abccad   pfrison   PercTeacher inter...
16
17
18
19
  		this.deltaL = deltaL;
  		this.deltaR = deltaR;
  		this.endAction = false;
  	}
04949080   pfrison   PercTeacher stabl...
20
  	public Action(int deltaL, int deltaR, long delaiL, long delaiR) {
c5abccad   pfrison   PercTeacher inter...
21
22
23
24
25
26
27
  		this.deltaL = deltaL;
  		this.deltaR = deltaR;
  		this.delaiL = delaiL;
  		this.delaiR = delaiR;
  		this.endAction = false;
  	}
  
04949080   pfrison   PercTeacher stabl...
28
29
30
31
32
33
  	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;}
c5abccad   pfrison   PercTeacher inter...
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
  	
  	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
04949080   pfrison   PercTeacher stabl...
76
  				return "Going backward for " + String.valueOf(-deltaL) + " steps during " + String.valueOf((double) (-deltaL * delaiL) / 1000d) + " miliseconds";
c5abccad   pfrison   PercTeacher inter...
77
78
79
80
81
82
83
84
85
86
87
88
  		} 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";
  	}
  }