-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFtcConfig.java
More file actions
238 lines (204 loc) · 7.94 KB
/
FtcConfig.java
File metadata and controls
238 lines (204 loc) · 7.94 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
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
package com.qualcomm.ftcrobotcontroller.opmodes;
import android.content.Context;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class FtcConfig {
//
private enum ConfigStep {
TEST_GAMEPAD1,
TEST_GAMEPAD2,
COLOR,
DELAY,
AUTON_TYPE,
READY;
private static ConfigStep[] vals = values();
public ConfigStep next() { return vals[(this.ordinal()+1) % vals.length];}
public ConfigStep prev() { return vals[(this.ordinal()-1+vals.length) % vals.length];}
}
public class Param {
boolean colorIsRed;
int delayInSec;
AutonType autonType;
}
public enum AutonType {
GO_FOR_BEACON,
GO_FOR_MOUNTAIN;
private static AutonType[] vals = values();
public AutonType next() { return vals[(this.ordinal()+1) % vals.length];}
public AutonType prev() { return vals[(this.ordinal()-1+vals.length) % vals.length];}
}
// the parameters that need to be setup during configuration
boolean gamepad1IsOK, gamepad2IsOK;
public Param param;
// variables used during the configuration process
ConfigStep configStepState, currConfigStepCheck;
boolean back1, a1, b2, y1, start1;
boolean lastBack1, lastA1, lastB2, lastY1, lastStart1;
private String configFileName="FtcRobotConfig.txt";
public FtcConfig() {
param = new Param();
}
public void init(Context context, OpMode opMode) {
// setup initial configuration parameters here
gamepad1IsOK=false;
gamepad2IsOK=false;
//context=hardwareMap.appContext;
// read configuration data from file
try {
InputStream inputStream = context.openFileInput(configFileName);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
param.colorIsRed = Boolean.valueOf(bufferedReader.readLine());
param.delayInSec = Integer.valueOf(bufferedReader.readLine());
String autonTypeString = bufferedReader.readLine();
for (AutonType a : AutonType.values()) {
if (a.name().equals(autonTypeString)) {
param.autonType = a;
}
}
inputStream.close();
}
}
catch (Exception e) {
opMode.telemetry.addData("Exception", "Error reading config file: " + e.toString());
// can't read from file, so initialize to reasonable values
param.colorIsRed=true;
param.delayInSec=0;
param.autonType= AutonType.GO_FOR_BEACON;
}
// setup initial toggle memory states for buttons used
lastBack1=false; lastA1=false; lastB2=false; lastY1=false; lastStart1=false;
configStepState = ConfigStep.TEST_GAMEPAD1;
}
public void init_loop(Context context, OpMode opMode) {
// read the gamepad state
back1 = opMode.gamepad1.back || opMode.gamepad1.left_bumper || opMode.gamepad1.right_bumper;
a1 = opMode.gamepad1.a;
b2 = opMode.gamepad2.b;
y1 = opMode.gamepad1.y;
start1 = opMode.gamepad1.start;
opMode.telemetry.clearData();
currConfigStepCheck = ConfigStep.TEST_GAMEPAD1;
// message to driver about state of this config parameter
if (configStepState.ordinal() >= currConfigStepCheck.ordinal()) {
if (!gamepad1IsOK) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal(), "GAMEPAD1 NOT VERIFIED!!!");
}
}
// configure this parameter
if (configStepState == currConfigStepCheck) {
if (!gamepad1IsOK) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal() + "A", "Push A on Gamepad 1");
}
if (a1) {
gamepad1IsOK = true;
}
}
currConfigStepCheck = ConfigStep.TEST_GAMEPAD2;
// message to driver about state of this config parameter
if (configStepState.ordinal() >= currConfigStepCheck.ordinal()) {
if (!gamepad2IsOK) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal(), "GAMEPAD2 NOT VERIFIED!!!");
}
}
// configure this parameter
if (configStepState == currConfigStepCheck) {
if (!gamepad2IsOK) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal() + "A", "Push B on Gamepad 2");
}
if (b2) {
gamepad2IsOK = true;
}
}
currConfigStepCheck = ConfigStep.COLOR;
// message to driver about state of this config parameter
if (configStepState.ordinal() >= currConfigStepCheck.ordinal()) {
if (param.colorIsRed) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal(), "Color: Red");
} else {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal(), "Color: Blue");
}
}
// configure this parameter
if (configStepState == currConfigStepCheck) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal() + "A", "Push B for Red, X for Blue");
if (opMode.gamepad1.x) {
param.colorIsRed = false;
}
if (opMode.gamepad1.b) {
param.colorIsRed = true;
}
}
currConfigStepCheck = ConfigStep.DELAY;
// message to driver about state of this config parameter
if (configStepState.ordinal() >= currConfigStepCheck.ordinal()) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal(), "Delay: " + param.delayInSec + " sec");
}
// configure this parameter
if (configStepState == currConfigStepCheck) {
opMode.telemetry.addData("C" + configStepState.ordinal() + "A", "Push Y for +, A for -");
if (y1 && !lastY1) {
param.delayInSec++;
}
if (a1 && !lastA1) {
param.delayInSec--;
if (param.delayInSec < 0) {
param.delayInSec = 0;
}
}
}
currConfigStepCheck = ConfigStep.AUTON_TYPE;
// message to driver about state of this config parameter
if (configStepState.ordinal() >= currConfigStepCheck.ordinal()) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal(), "Auton: " + param.autonType.name());
}
// configure this parameter
if (configStepState == currConfigStepCheck) {
opMode.telemetry.addData("C" + configStepState.ordinal() + "A", "Push Y for +, A for -");
if (y1 && !lastY1) {
param.autonType = param.autonType.next();
}
if (a1 && !lastA1) {
param.autonType = param.autonType.prev();
}
}
currConfigStepCheck = ConfigStep.READY;
// message to driver about state of this config parameter
if (configStepState.ordinal() >= currConfigStepCheck.ordinal() ) {
opMode.telemetry.addData("C" + currConfigStepCheck.ordinal(), "READY TO GO!");
// may want to write configuration parameters to a file here if they are needed for teleop too!
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(configFileName, Context.MODE_PRIVATE));
// write each configuration parameter as a string on its own line
outputStreamWriter.write(Boolean.toString(param.colorIsRed)+"\n");
outputStreamWriter.write(Integer.toString(param.delayInSec)+"\n");
outputStreamWriter.write(param.autonType.name()+"\n");
outputStreamWriter.close();
}
catch (IOException e) {
opMode.telemetry.addData("Exception", "Configuration file write failed: " + e.toString());
}
}
if (configStepState != ConfigStep.READY) {
opMode.telemetry.addData("D" + configStepState.ordinal(), "Push Start for next option");
}
opMode.telemetry.addData("E" + configStepState.ordinal(), "Push Back or a Bumper to go back");
if (start1 && !lastStart1 && (configStepState.ordinal() < ConfigStep.READY.ordinal())) {
configStepState = configStepState.next();
}
if (back1 && !lastBack1 && (configStepState.ordinal() >0) ) {
configStepState = configStepState.prev();
}
// update toggle memory for next call
lastBack1=back1;
lastA1=a1;
lastB2=b2;
lastY1=y1;
lastStart1=start1;
}
}