Skip to content
This repository was archived by the owner on Sep 14, 2019. It is now read-only.

Commit e774c5f

Browse files
committed
parsePoint method, Position class, math fixes, etc.
1 parent 8e5407e commit e774c5f

4 files changed

Lines changed: 159 additions & 131 deletions

File tree

Robot2018/src/org/usfirst/frc/team199/Robot2018/autonomous/AutoUtils.java

Lines changed: 88 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,52 @@
55
import java.util.Map;
66

77
public class AutoUtils {
8-
private static double currX;
9-
private static double currY;
10-
private static double currRotation;
8+
9+
public static Position position = new Position(0, 0, 0);
10+
1111
/**
1212
* Parses the inputted script file into a map of scripts
1313
*
14-
* @param scriptFile the script file to parse
15-
* @return a map, with the key being the script name, and the argument
16-
* being a list of arrays that are instruction-argument pairs
14+
* @param scriptFile
15+
* the script file to parse
16+
* @return a map, with the key being the script name, and the argument being a
17+
* list of arrays that are instruction-argument pairs
1718
*/
1819
public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile) {
1920
Map<String, ArrayList<String[]>> autoScripts = new HashMap<String, ArrayList<String[]>>();
20-
21+
2122
String lines[] = scriptFile.split("\\r?\\n");
22-
23+
2324
ArrayList<String[]> currScript = new ArrayList<String[]>();
24-
String currScriptName = "";
25-
25+
String currScriptName = "";
26+
2627
int count = 1;
2728
for (String line : lines) {
2829
// remove comments
2930
int commentIndex = line.indexOf("#");
3031
if (commentIndex != -1)
3132
line = line.substring(0, commentIndex);
32-
33+
3334
// trim and remove extra whitespace just to make it neater
3435
line = line.trim().replaceAll("\\s+", " ");
35-
36+
3637
// if there's no instruction on this line, skip
3738
if (line.equals("")) {
3839
continue;
3940
}
40-
41-
// if current line is a label, store the previous script and make a new empty one
41+
42+
// if current line is a label, store the previous script and make a new empty
43+
// one
4244
if (line.endsWith(":")) {
4345
autoScripts.put(currScriptName, currScript);
4446
currScript = new ArrayList<String[]>();
4547
currScriptName = line.substring(0, line.length() - 1);
4648
} else {
47-
49+
4850
// first separate the command into instruction and args
4951
String instruction;
5052
String args;
51-
53+
5254
int separator = line.indexOf(' ');
5355
if (separator == -1) {
5456
instruction = line;
@@ -57,197 +59,185 @@ public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile
5759
instruction = line.substring(0, separator);
5860
args = line.substring(separator + 1);
5961
}
60-
62+
6163
// if it's valid, put it into the script
6264
if (isValidCommand(instruction, args, count)) {
63-
String[] command = {instruction, args};
65+
String[] command = { instruction, args };
6466
currScript.add(command);
6567
}
6668
}
6769
count++;
6870
}
69-
71+
7072
// puts the last script in
7173
autoScripts.put(currScriptName, currScript);
72-
74+
7375
// remove the stray one in the beginning
7476
autoScripts.remove("");
75-
77+
7678
return autoScripts;
7779
}
78-
79-
/*
80-
* All of these are getters and setters for the robot's position and orientation
81-
*/
82-
public static double getX() {
83-
return currX;
84-
}
85-
public static double getY() {
86-
return currY;
87-
}
88-
public static double getRot() {
89-
return currRotation;
90-
}
91-
public static void setX(double x) {
92-
currX = x;
93-
}
94-
public static void setY(double y) {
95-
currY = y;
96-
}
97-
public static void setRot(double rot) {
98-
currRotation = rot;
99-
}
100-
public static void changeX(double x) {
101-
currX += x;
102-
}
103-
public static void changeY(double y) {
104-
currY += y;
105-
}
106-
public static void changeRot(double rot) {
107-
currRotation += rot;
108-
}
109-
80+
11081
/**
11182
* Validates the command inputted to see if it's AAA compliant
11283
*
113-
* @param instruction the instruction/command name
114-
* @param args the arguments provided to the instruction. A blank String if none
115-
* @param lineNumber the lineNumber in the script file. used for logging warnings
84+
* @param instruction
85+
* the instruction/command name
86+
* @param args
87+
* the arguments provided to the instruction. A blank String if none
88+
* @param lineNumber
89+
* the lineNumber in the script file. used for logging warnings
11690
* @return if the command is valid
11791
*/
118-
public static boolean isValidCommand (String instruction, String args, int lineNumber) {
92+
public static boolean isValidCommand(String instruction, String args, int lineNumber) {
11993
// moveto takes in a set of points, and the last arg can be a number
12094
if (instruction.equals("moveto")) {
12195
if (args == "") {
12296
logWarning(lineNumber, "The command `moveto` requires at least one argument.");
12397
return false;
12498
}
125-
99+
126100
String[] splitArgs = args.split(" ");
127101
for (int i = 0; i < splitArgs.length - 1; i++) {
128102
if (!isPoint(splitArgs[i])) {
129-
logWarning(lineNumber, "The arguments for command `moveto` should be points formatted like this: "
130-
+ "`(x,y)`.");
103+
logWarning(lineNumber,
104+
"The arguments for command `moveto` should be points formatted like this: " + "`(x,y)`.");
131105
return false;
132106
}
133107
}
134-
108+
135109
if (!isDouble(splitArgs[splitArgs.length - 1]) && !isPoint(splitArgs[splitArgs.length - 1])) {
136110
logWarning(lineNumber, "The last argument for command `moveto` should be a number, or a point "
137111
+ "formatted like this: `(x,y)`.");
138112
return false;
139113
}
140-
}
141-
114+
}
115+
142116
// turn can take a number or point
143117
else if (instruction.equals("turn")) {
144118
if (args.contains(" ")) {
145119
logWarning(lineNumber, "Command `turn` only accepts one argument.");
146120
return false;
147121
}
148-
122+
149123
if (!isDouble(args) && !isPoint(args)) {
150124
logWarning(lineNumber, "The argument for command `turn` should be a number or a point formatted like "
151125
+ "this: `(x,y)`.");
152126
return false;
153127
}
154-
}
155-
128+
}
129+
156130
// move and wait can take only a number
157131
else if (instruction.equals("move") || instruction.equals("wait")) {
158132
if (args.contains(" ")) {
159133
logWarning(lineNumber, "Command `move` only accepts one argument.");
160134
return false;
161135
}
162-
136+
163137
if (!isDouble(args)) {
164138
logWarning(lineNumber, "The argument for command `move` should be a number.");
165139
return false;
166140
}
167141
}
168-
142+
169143
// switch, scale, exchange, intake, and end all don't have any args
170144
else if (instruction.equals("switch") || instruction.equals("scale") || instruction.equals("exchange")
171145
|| instruction.equals("intake") || instruction.equals("end")) {
172146
if (!args.equals("")) {
173147
logWarning(lineNumber, "Command `" + instruction + "` does not accept any arguments.");
174148
return false;
175149
}
176-
}
177-
150+
}
151+
178152
// Jump only takes one argument
179153
else if (instruction.equals("jump")) {
180154
if (args.contains(" ")) {
181155
logWarning(lineNumber, "Command `jump` only accepts one argument.");
182156
return false;
183157
}
184-
}
185-
186-
// if it's not even a valid instruction
158+
}
159+
160+
// if it's not even a valid instruction
187161
else {
188162
logWarning(lineNumber, "`" + instruction + "` is not a valid command.");
189163
return false;
190164
}
191-
165+
192166
// if everything is all good
193167
return true;
194168
}
195-
169+
196170
/**
197-
* Helper method used by isValidCommand() to log warnings for non-valid commands.
171+
* Helper method used by isValidCommand() to log warnings for non-valid
172+
* commands.
198173
*
199-
* @param lineNumber the line number in the script file
200-
* @param message the message to log
174+
* @param lineNumber
175+
* the line number in the script file
176+
* @param message
177+
* the message to log
201178
*/
202-
private static void logWarning (int lineNumber, String message) {
179+
private static void logWarning(int lineNumber, String message) {
203180
System.err.println("[WARNING] Line " + lineNumber + ": " + message);
204181
}
205-
182+
206183
/**
207-
* Helper method used by isValidCommand() to check if an argument is a
208-
* point, characterized by parentheses on the left and right, with two
209-
* numbers separated by a comma, with no whitespace in between.
184+
* Helper method used by isValidCommand() to check if an argument is a point,
185+
* characterized by parentheses on the left and right, with two numbers
186+
* separated by a comma, with no whitespace in between.
210187
*
211-
* @param s the argument
188+
* @param s
189+
* the argument
212190
* @return if the argument is a point
213191
*/
214-
public static boolean isPoint (String s) {
192+
public static boolean isPoint(String s) {
215193
// checks if it starts and ends with parentheses
216194
if (!s.startsWith("(") || !s.endsWith(")"))
217195
return false;
218-
196+
219197
// checks that there's one, and only one comma (like this phrase)
220198
int indexOfComma = s.indexOf(',');
221199
int count = 0;
222200
while (indexOfComma != -1) {
223-
count++;
224-
indexOfComma = s.indexOf(',', indexOfComma + 1);
201+
count++;
202+
indexOfComma = s.indexOf(',', indexOfComma + 1);
225203
}
226204
if (count != 1)
227205
return false;
228-
229-
206+
230207
// really ugly, but just checks if the stuff between the parentheses are numbers
231-
if (!isDouble(s.substring(1, s.indexOf(',')))
232-
|| !isDouble(s.substring(s.indexOf(',') + 1, s.length() - 1)))
208+
if (!isDouble(s.substring(1, s.indexOf(','))) || !isDouble(s.substring(s.indexOf(',') + 1, s.length() - 1)))
233209
return false;
234-
210+
235211
return true;
236212
}
237-
213+
238214
/**
239-
* Helper method used by isValidCommand() used to check if an argument is
240-
* able to be converted into a double
215+
* Helper method used by isValidCommand() used to check if an argument is able
216+
* to be converted into a double
241217
*
242-
* @param s the argument
218+
* @param s
219+
* the argument
243220
* @return if the argument is a double
244221
*/
245-
public static boolean isDouble (String s) {
222+
public static boolean isDouble(String s) {
246223
try {
247224
Double.parseDouble(s);
248225
} catch (Exception e) {
249226
return false;
250227
}
251228
return true;
252229
}
230+
231+
public static double[] parsePoint(String cmdArgs) {
232+
double[] point = new double[2];
233+
String parentheseless;
234+
String[] pointparts;
235+
if (AutoUtils.isPoint(cmdArgs)) {
236+
parentheseless = cmdArgs.substring(1, cmdArgs.length() - 1);
237+
pointparts = parentheseless.split(",");
238+
point[0] = Double.parseDouble(pointparts[0]);
239+
point[1] = Double.parseDouble(pointparts[1]);
240+
}
241+
return point;
242+
}
253243
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.usfirst.frc.team199.Robot2018.autonomous;
2+
3+
public class Position {
4+
private double currX;
5+
private double currY;
6+
private double currRotation;
7+
8+
public Position(double x, double y, double rot) {
9+
currX = x;
10+
currY = y;
11+
currRotation = rot;
12+
}
13+
14+
/*
15+
* All of these are getters and setters for the robot's position and orientation
16+
*/
17+
public double getX() {
18+
return currX;
19+
}
20+
21+
public double getY() {
22+
return currY;
23+
}
24+
25+
public double getRot() {
26+
return currRotation;
27+
}
28+
29+
public void setX(double x) {
30+
currX = x;
31+
}
32+
33+
public void setY(double y) {
34+
currY = y;
35+
}
36+
37+
public void setRot(double rot) {
38+
currRotation = rot;
39+
}
40+
41+
public void changeX(double x) {
42+
currX += x;
43+
}
44+
45+
public void changeY(double y) {
46+
currY += y;
47+
}
48+
49+
public void changeRot(double rot) {
50+
currRotation += rot;
51+
}
52+
}

0 commit comments

Comments
 (0)