ActionList.java
3.77 KB
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
import java.util.ArrayList;
public class ActionList {
private ArrayList<Action> actions;
private SerialCom serialCom;
public ActionList(SerialCom serialCom) {
this.serialCom = serialCom;
actions = new ArrayList<>();
actions.add(new Action());
}
public ActionList(int[] leftDeltaArray, int[] rightDeltaArray, long[] leftDelaiArray, long[] rightDelaiArray) {
if(leftDelaiArray.length != rightDelaiArray.length
|| leftDelaiArray.length != leftDelaiArray.length
|| leftDelaiArray.length != rightDelaiArray.length)
throw new InvalidActionListArrays();
actions = new ArrayList<>();
for(int i=0; i<leftDeltaArray.length; i++)
actions.add(new Action(leftDeltaArray[i], rightDeltaArray[i], leftDelaiArray[i], rightDelaiArray[i]));
actions.add(new Action());
}
public void addToActionList(int i, int j, int step) {
switch (j * 10 + i) {
case 00: //ACTION_LEFT_PLUS;
addToLastAction(step, 0);
break;
case 02: //ACTION_LEFT_MINUS;
addToLastAction(-step, 0);
break;
case 20: //ACTION_RIGHT_PLUS;
addToLastAction(0, step);
break;
case 22: //ACTION_RIGHT_MINUS;
addToLastAction(0, -step);
break;
case 10: //ACTION_FORWARD;
addToLastAction(step, step);
break;
case 12: //ACTION_BACKWARD;
addToLastAction(-step, -step);
break;
case 01: //ACTION_CLOCKWISE;
addToLastAction(step, -step);
break;
case 21: //ACTION_ANTI_CLOCKWISE;
addToLastAction(-step, step);
break;
default:
throw new InvalidInputException();
}
}
public void completeAction() {
if(!actions.get(actions.size() - 1).isEmpty())
actions.add(new Action());
}
public void completeActionAndAddPause(long delai) {
if(actions.get(actions.size() - 1).isEmpty())
actions.set(actions.size() - 1, new Action(0, 0, delai, delai));
else
actions.add(new Action(0, 0, delai, delai));
completeAction();
}
private void addToLastAction(int deltaL, int deltaR) {
actions.get(actions.size() - 1).addDeltaL(deltaL);
actions.get(actions.size() - 1).addDeltaR(deltaR);
SerialCommands.sendDeltas(serialCom, deltaL, deltaR);
}
public void revertLastAction() {
if(actions.size() <= 0)
return;
Action toRevert = null;
if(actions.get(actions.size() - 1).isEmpty()) {
actions.remove(actions.size() - 1);
if(actions.size() != 0) {
toRevert = actions.get(actions.size() - 1);
actions.set(actions.size() - 1, new Action());
}
else
actions.add(new Action());
} else {
toRevert = actions.get(actions.size() - 1);
actions.set(actions.size() - 1, new Action());
}
if(toRevert != null)
SerialCommands.sendDeltas(serialCom, -toRevert.getDeltaL(), -toRevert.getDeltaR());
}
public String getLeftDeltaArray() {
String str = "{";
for(Action action : actions) {
if(!action.isEmpty())
str += String.valueOf(action.getDeltaL()) + ", ";
}
str = str.substring(0, str.length() - 2) + "}";
return str;
}
public String getRightDeltaArray() {
String str = "{";
for(Action action : actions) {
if(!action.isEmpty())
str += String.valueOf(action.getDeltaR()) + ", ";
}
str = str.substring(0, str.length() - 2) + "}";
return str;
}
public String getLeftDelaiArray() {
String str = "{";
for(Action action : actions) {
if(!action.isEmpty())
str += String.valueOf(action.getDeltaR()) + "L, ";
}
str = str.substring(0, str.length() - 2) + "}";
return str;
}
public String getRightDelaiArray() {
String str = "{";
for(Action action : actions) {
if(!action.isEmpty())
str += String.valueOf(action.getDeltaR()) + "L, ";
}
str = str.substring(0, str.length() - 2) + "}";
return str;
}
@Override
public String toString() {
String str = "";
for(Action action : actions)
str += action.toString() + "\n";
str = str.substring(0, str.length() - 1); // remove last \n
return str;
}
}