forked from team857/2015RobotCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecorderOld.java
165 lines (157 loc) · 6.52 KB
/
RecorderOld.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
package team857.yetiRobot.record;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary;
import team857.yetiRobot.PeriodController;
public class RecorderOld implements PeriodController {
public static interface Selector {
public boolean recordButton(int stick);
public boolean recordAxes(int stick);
public File getOutputFile();
}
private Selector selector;
private BufferedOutputStream out = null;
private PeriodController teleopController;
private short[] buttons = new short[6];
private byte[][] axis = new byte[6][12];
private short tmp_short;
private byte[] tmp_sha;
private byte e_axis, e_buto;
private ByteBuffer e_update = ByteBuffer.allocate(84).order(ByteOrder.BIG_ENDIAN),
e_over = ByteBuffer.allocate(10).order(ByteOrder.BIG_ENDIAN);
public RecorderOld(PeriodController teleopController, RecorderOld.Selector selector){
this.teleopController = teleopController;
this.selector = selector;
}
private byte[] m_stb(short[] array){
byte[] x = new byte[array.length];
for(byte c = 0; c<array.length;c++) x[c] = (byte)array[c];
return x;
}
public void init(){
axis[0] = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)0));
axis[1] = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)1));
axis[2] = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)2));
axis[3] = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)3));
axis[4] = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)4));
axis[5] = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)5));
buttons[0] = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)0, ByteBuffer.allocateDirect(1));
buttons[1] = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)1, ByteBuffer.allocateDirect(1));
buttons[2] = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)2, ByteBuffer.allocateDirect(1));
buttons[3] = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)3, ByteBuffer.allocateDirect(1));
buttons[4] = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)4, ByteBuffer.allocateDirect(1));
buttons[5] = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)5, ByteBuffer.allocateDirect(1));
try {
out = new BufferedOutputStream(new FileOutputStream(selector.getOutputFile()));
} catch (FileNotFoundException e){
throw new RuntimeException("Output file can't be created");
} finally {
try {
e_update.putShort(buttons[0]).putShort(buttons[1]).putShort(buttons[2])
.putShort(buttons[3]).putShort(buttons[4]).putShort(buttons[5])
.put(axis[0]).put(axis[1]).put(axis[2]).put(axis[3]).put(axis[4]).put(axis[5]);
out.write(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, (byte)0xFF, (byte)0xFF});
out.write(e_update.array());
out.flush();
} catch (IOException e){
DriverStation.reportError(e.getMessage(), false);
}
}
teleopController.init();
}
public void run(double time){
teleopController.run(time);
e_axis = 0;
e_buto = 0;
e_update.clear();
e_over.clear();
e_over.putLong((long)Math.floor(time*1000));
if(selector.recordButton(0) && buttons[0]!=(tmp_short = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)0, ByteBuffer.allocateDirect(1)))){
buttons[0] = tmp_short;
e_update.putShort(tmp_short);
e_buto += 1;
}
if(selector.recordButton(1) && buttons[1]!=(tmp_short = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)1, ByteBuffer.allocateDirect(1)))){
buttons[1] = tmp_short;
e_update.putShort(tmp_short);
e_buto += 2;
}
if(selector.recordButton(2) && buttons[2]!=(tmp_short = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)2, ByteBuffer.allocateDirect(1)))){
buttons[2] = tmp_short;
e_update.putShort(tmp_short);
e_buto += 4;
}
if(selector.recordButton(3) && buttons[3]!=(tmp_short = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)3, ByteBuffer.allocateDirect(1)))){
buttons[3] = tmp_short;
e_update.putShort(tmp_short);
e_buto += 8;
}
if(selector.recordButton(4) && buttons[4]!=(tmp_short = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)4, ByteBuffer.allocateDirect(1)))){
buttons[4] = tmp_short;
e_update.putShort(tmp_short);
e_buto += 16;
}
if(selector.recordButton(5) && buttons[5]!=(tmp_short = (short) FRCNetworkCommunicationsLibrary.HALGetJoystickButtons((byte)5, ByteBuffer.allocateDirect(1)))){
buttons[5] = tmp_short;
e_update.putShort(tmp_short);
e_buto += 32;
}
if(selector.recordAxes(0) && !Arrays.equals(axis[0], (tmp_sha = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)0))))){
axis[0] = tmp_sha;
e_update.put(tmp_sha);
e_axis += 1;
}
if(selector.recordAxes(1) && !Arrays.equals(axis[1], (tmp_sha = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)1))))){
axis[1] = tmp_sha;
e_update.put(tmp_sha);
e_axis += 2;
}
if(selector.recordAxes(2) && !Arrays.equals(axis[2], (tmp_sha = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)2))))){
axis[2] = tmp_sha;
e_update.put(tmp_sha);
e_axis += 4;
}
if(selector.recordAxes(3) && !Arrays.equals(axis[3], (tmp_sha = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)3))))){
axis[3] = tmp_sha;
e_update.put(tmp_sha);
e_axis += 8;
}
if(selector.recordAxes(4) && !Arrays.equals(axis[4], (tmp_sha = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)4))))){
axis[4] = tmp_sha;
e_update.put(tmp_sha);
e_axis += 16;
}
if(selector.recordAxes(5) && !Arrays.equals(axis[5], (tmp_sha = m_stb(FRCNetworkCommunicationsLibrary.HALGetJoystickAxes((byte)5))))){
axis[5] = tmp_sha;
e_update.put(tmp_sha);
e_axis += 32;
}
if((e_axis > 0) || (e_buto > 0)){
e_over.put(e_buto);
e_over.put(e_axis);
try {
out.write(e_over.array());
out.write(Arrays.copyOfRange(e_update.array(),0,e_update.position()));
out.flush();
} catch(IOException e){
DriverStation.reportError("Failed to write line input sequence", false);
}
}
}
public void end(){
teleopController.end();
try {
out.flush();
out.close();
} catch(IOException e){
throw new RuntimeException("Could not finish recording session.");
}
}
}