-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDatalogger.java
334 lines (275 loc) · 9.14 KB
/
Datalogger.java
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
This Datalogger class is provided for FTC OnBot Java (OBJ) programmers.
Most users will not need to edit this class; its methods are called
from a user's OpMode such as ConceptDatalogger.java or a revised version.
That OpMode specifies and collects data to be logged in a CSV file,
ready for download and charting.
For instructions, see the tutorial at the FTC Wiki:
https://github.com/FIRST-Tech-Challenge/FtcRobotController/wiki/Datalogging
Android Studio programmers can change the destination filepath at Line 295,
From: "/sdcard/FIRST/java/src/Datalogs/%s.txt"
To: "/sdcard/FIRST/Datalogs/%s.csv"
This change presumes OnBot Java will not be used to preview or download datalogs;
they will instead be manually transferred from the RC device.
Credit to @Windwoes (https://github.com/Windwoes).
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManagerNotifier;
import org.firstinspires.ftc.robotcore.internal.opmode.OpModeManagerImpl;
import org.firstinspires.ftc.robotcore.internal.system.AppUtil;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
public class Datalogger
{
private LoggableField[] fields;
private BufferedCsvWriter bufferedCsvWriter;
/*
* NOTE: We cannot simply pass `new OpModeNotifications()` inline to the call
* to register the listener, because the SDK stores the list of listeners in
* a WeakReference set. This causes the object to be garbage collected because
* nothing else is holding a reference to it.
*/
private OpModeNotifications opModeNotifications = new OpModeNotifications();
private Datalogger(BufferedCsvWriter bufferedCsvWriter, LoggableField[] fields)
{
this.bufferedCsvWriter = bufferedCsvWriter;
this.fields = fields;
OpModeManagerImpl.getOpModeManagerOfActivity(AppUtil.getInstance().getActivity()).registerListener(opModeNotifications);
writeHeader();
}
private class OpModeNotifications implements OpModeManagerNotifier.Notifications
{
@Override
public void onOpModePostStop(OpMode opMode)
{
close();
OpModeManagerImpl.getOpModeManagerOfActivity(AppUtil.getInstance().getActivity()).unregisterListener(this);
}
@Override
public void onOpModePreInit(OpMode opMode) {}
@Override
public void onOpModePreStart(OpMode opMode) {}
}
private void writeHeader()
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < fields.length; i++)
{
stringBuilder.append(fields[i].name);
if (i < fields.length-1)
{
stringBuilder.append(",");
}
}
try
{
bufferedCsvWriter.writeLine(stringBuilder.toString());
}
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException("Unable to initialize datalogger");
}
}
public void writeLine()
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < fields.length; i++)
{
fields[i].writeToBuffer(stringBuilder);
if (i < fields.length-1)
{
stringBuilder.append(",");
}
}
try
{
bufferedCsvWriter.writeLine(stringBuilder.toString());
}
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException("Error writing datalog line");
}
}
private void close()
{
try
{
bufferedCsvWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static abstract class LoggableField
{
protected final String name;
public LoggableField(String name)
{
this.name = name;
}
public abstract void writeToBuffer(StringBuilder out);
}
public static class GenericField extends LoggableField
{
private String str = "";
private static final String STR_FALSE = "false";
private static final String STR_TRUE = "true";
public GenericField(String name)
{
super(name);
}
@Override
public void writeToBuffer(StringBuilder out)
{
out.append(str);
}
public void set(String string)
{
str = string;
}
public void set(String format, Object... args)
{
str = String.format(format, args);
}
public void set(int val)
{
str = Integer.toString(val);
}
public void set(boolean val)
{
str = val ? STR_TRUE : STR_FALSE;
}
public void set(byte val)
{
str = String.format("0x%x", val);
}
public void set(float val)
{
str = String.format("%.3f", val);
}
// 6-7-22 Add overloaded method with optional format parameter.
public void set(String valFormat, float val)
{
str = String.format(valFormat, val);
}
public void set(double val)
{
str = String.format("%.3f", val);
}
// 6-7-22 Add overloaded method with optional format parameter.
public void set(String valFormat, double val)
{
str = String.format(valFormat, val);
}
// 6-7-22 Added this method so user OpMode telemetry can display
// field contents (instead of memory address).
@Override
public String toString()
{
return str;
}
}
private static class TimestampField extends LoggableField
{
private long tRef;
private final DecimalFormat timeFmt = new DecimalFormat("000.000");
public TimestampField(String name)
{
super(name);
tRef = System.currentTimeMillis();
}
public void resetRef()
{
tRef = System.currentTimeMillis();
}
@Override
public void writeToBuffer(StringBuilder out)
{
long deltaMs = System.currentTimeMillis() - tRef;
float delta = deltaMs / 1000f;
out.append(timeFmt.format(delta));
}
}
public enum AutoTimestamp
{
DECIMAL_SECONDS,
NONE
}
public static class Builder
{
private String filename;
private LoggableField[] fields;
private AutoTimestamp autoTimestamp;
public Builder setFilename(String filename)
{
this.filename = filename;
return this;
}
public Builder setFields(LoggableField... fields)
{
this.fields = fields;
return this;
}
public Builder setAutoTimestamp(AutoTimestamp autoTimestamp)
{
this.autoTimestamp = autoTimestamp;
return this;
}
public Datalogger build()
{
if (filename == null) throw new RuntimeException("Filename must not be null!");
if (filename.endsWith(".csv")) filename = filename.replace(".csv", "");
if (fields == null) throw new RuntimeException("Fields must not be null!");
if (fields.length == 0) throw new RuntimeException("Fields must be non-zero length!");
if (autoTimestamp == null) throw new RuntimeException("AutoTimestamp must not be null!");
if (autoTimestamp == AutoTimestamp.DECIMAL_SECONDS)
{
LoggableField[] tmp = new LoggableField[fields.length+1];
tmp[0] = new TimestampField("Timestamp");
System.arraycopy(fields, 0, tmp, 1, fields.length);
fields = tmp;
}
try
{
BufferedCsvWriter bufferedCsvWriter = new BufferedCsvWriter(String.format("/sdcard/FIRST/java/src/Datalogs/%s.txt", filename));
return new Datalogger(bufferedCsvWriter, fields);
}
catch (IOException e)
{
e.printStackTrace();
throw new RuntimeException("Unable to create output file handle :(");
}
}
}
private static class BufferedCsvWriter
{
private FileWriter fileWriter;
private BufferedWriter bufferedWriter;
public BufferedCsvWriter(String filepath) throws IOException
{
File tmp = new File(filepath);
if (!tmp.exists())
{
tmp.getParentFile().mkdirs();
}
fileWriter = new FileWriter(filepath, false);
bufferedWriter = new BufferedWriter(fileWriter);
}
public void writeLine(String line) throws IOException
{
bufferedWriter.write(line);
bufferedWriter.newLine();
}
public void close() throws IOException
{
bufferedWriter.close();
}
}
}