-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeam25UltrasonicSensor.java
364 lines (315 loc) · 10.5 KB
/
Team25UltrasonicSensor.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// add package here
/*
* FTC Team 25: cmacfarl, February 25, 2016
*/
import com.qualcomm.hardware.matrix.MatrixI2cTransaction;
import com.qualcomm.hardware.modernrobotics.ModernRoboticsI2cGyro;
import com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbLegacyModule;
import com.qualcomm.robotcore.hardware.I2cController;
import com.qualcomm.robotcore.hardware.LegacyModulePortDeviceImpl;
import com.qualcomm.robotcore.hardware.UltrasonicSensor;
import com.qualcomm.robotcore.util.RobotLog;
import com.qualcomm.robotcore.util.TypeConversion;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Lock;
/**
* Ultrasonic Sensor
*/
public class Team25UltrasonicSensor extends LegacyModulePortDeviceImpl implements UltrasonicSensor, I2cController.I2cPortReadyCallback {
private static final boolean debug = false;
public static final int I2C_ADDRESS = 0x02;
public static final byte I2C_DATA_OFFSET = 4;
public static final byte COMMAND_SINGLE_SHOT = 0x01;
public static final byte COMMAND_CONTINUOUS = 0x02;
public static final byte OFFSET_COMMAND = 0x41;
public static final byte OFFSET_DISTANCE = 0x42;
public static final int BUFFER_LENGTH = 8;
protected enum I2cTransactionState {
QUEUED,
PENDING_I2C_READ,
PENDING_I2C_WRITE,
PENDING_READ_DONE,
DONE
}
public class UltrasonicI2cTransaction {
I2cTransactionState state;
byte[] buffer;
byte offset;
byte len;
boolean write;
/*
* Generic read transaction.
*/
public UltrasonicI2cTransaction()
{
offset = OFFSET_DISTANCE;
len = BUFFER_LENGTH;
write = false;
}
/*
* Write the command byte.
*/
public UltrasonicI2cTransaction(byte data)
{
offset = OFFSET_COMMAND;
buffer = new byte[1];
buffer[0] = data;
len = (byte)buffer.length;
write = true;
}
public boolean isEqual(UltrasonicI2cTransaction transaction)
{
if (this.offset != transaction.offset) {
return false;
} else {
switch (this.offset) {
case OFFSET_COMMAND:
if (Arrays.equals(this.buffer, transaction.buffer)) {
return true;
}
break;
default:
return false;
}
}
return false;
}
};
public static final int MAX_PORT = 5;
public static final int MIN_PORT = 4;
private Lock readLock;
private byte[] readBuffer;
private Lock writeLock;
private byte[] writeBuffer;
private ModernRoboticsUsbLegacyModule legacyModule;
private ConcurrentLinkedQueue<UltrasonicI2cTransaction> transactionQueue;
private volatile boolean waitingForGodot = false;
private UltrasonicMemoryMap memoryMap;
public class UltrasonicMemoryMap {
byte Measurement_0;
byte Measurement_1;
byte Measurement_2;
byte Measurement_3;
byte Measurement_4;
byte Measurement_5;
byte Measurement_6;
byte Measurement_7;
};
public Team25UltrasonicSensor(ModernRoboticsUsbLegacyModule legacyModule, int physicalPort)
{
super(legacyModule, physicalPort);
this.legacyModule = legacyModule;
transactionQueue = new ConcurrentLinkedQueue<UltrasonicI2cTransaction>();
memoryMap = new UltrasonicMemoryMap();
throwIfPortIsInvalid(physicalPort);
finishConstruction();
}
@Override
protected void moduleNowArmedOrPretending()
{
this.readBuffer = module.getI2cReadCache(physicalPort);
this.readLock = module.getI2cReadCacheLock(physicalPort);
this.writeBuffer = module.getI2cWriteCache(physicalPort);
this.writeLock = module.getI2cWriteCacheLock(physicalPort);
byte[] buf = {COMMAND_SINGLE_SHOT};
legacyModule.setWriteMode(physicalPort, I2C_ADDRESS, OFFSET_COMMAND);
legacyModule.setData(physicalPort, buf, buf.length);
legacyModule.enable9v(physicalPort, true);
legacyModule.setI2cPortActionFlag(physicalPort);
legacyModule.readI2cCacheFromController(physicalPort);
legacyModule.registerForI2cPortReadyCallback(this, physicalPort);
}
@Override
public double getUltrasonicLevel()
{
queueTransaction(new UltrasonicI2cTransaction());
waitOnRead();
return TypeConversion.unsignedByteToDouble(memoryMap.Measurement_0);
}
public UltrasonicMemoryMap getMemoryMap()
{
return memoryMap;
}
/*
* Callback method, will be called by the Legacy Module when the port is ready, assuming we
* registered that call
*/
protected boolean queueTransaction(UltrasonicI2cTransaction transaction, boolean force)
{
/*
* Yes, inefficient, but if the queue is more than a few transactions
* deep we have other problems. The force parameter allows a controller
* to queue a transaction regardless of whether or not a matching
* transaction is already queued.
*/
if (!force) {
Iterator<UltrasonicI2cTransaction> it = transactionQueue.iterator();
while (it.hasNext()) {
UltrasonicI2cTransaction t = (UltrasonicI2cTransaction)it.next();
if (t.isEqual(transaction)) {
buginf("NO Queue transaction " + transaction.toString());
return false;
}
}
/*
* One might ask if we have a property match, but a value mismatch, why
* not replace the new value with the old? That would result in transaction
* reordering which might not be desirable. Something to think on.
*/
}
/*
* Doesn't exist, plop it in.
*/
buginf("YES Queue transaction " + transaction.toString());
transactionQueue.add(transaction);
return true;
}
protected boolean queueTransaction(UltrasonicI2cTransaction transaction)
{
return queueTransaction(transaction, false);
}
public void doPing()
{
queueTransaction(new UltrasonicI2cTransaction(COMMAND_SINGLE_SHOT));
}
@Override
public void portIsReady(int port)
{
if (transactionQueue.isEmpty()) {
return;
}
UltrasonicI2cTransaction transaction = transactionQueue.peek();
/*
* If the transaction is in the PENDING_I2C state then if this is a read
* go fetch the result (and wait for another round trip to this function.
*
* If it's a write, we are done, pull it off the transaction queue.
*
* Process the next transaction if the queue is not empty.
*/
if (transaction.state == I2cTransactionState.PENDING_I2C_READ) {
/*
* Go do a usb read, and then come back here.
*/
legacyModule.readI2cCacheFromModule(physicalPort);
transaction.state = I2cTransactionState.PENDING_READ_DONE;
return;
} else if (transaction.state == I2cTransactionState.PENDING_I2C_WRITE) {
/*
* It was a write, dequeue it and see if we have anything else.
*/
transaction = transactionQueue.poll();
/*
* Now are we empty? If so we are done.
*/
if (transactionQueue.isEmpty()) {
return;
}
/*
* Not done, grab the next transaction.
*/
transaction = transactionQueue.peek();
} else if (transaction.state == I2cTransactionState.PENDING_READ_DONE) {
handleReadDone();
transaction = transactionQueue.poll();
if (transactionQueue.isEmpty()) {
return;
}
transaction = transactionQueue.peek();
}
try {
if (transaction.write) {
legacyModule.enableI2cWriteMode(port, I2C_ADDRESS, transaction.offset, transaction.len);
legacyModule.copyBufferIntoWriteBuffer(port, transaction.buffer);
transaction.state = I2cTransactionState.PENDING_I2C_WRITE;
} else {
legacyModule.enableI2cReadMode(port, I2C_ADDRESS, transaction.offset, transaction.len);
transaction.state = I2cTransactionState.PENDING_I2C_READ;
}
legacyModule.writeI2cCacheToController(port);
} catch (IllegalArgumentException e) {
RobotLog.e(e.getMessage());
}
}
private void populateMemoryMap()
{
try {
readLock.lock();
memoryMap.Measurement_0 = readBuffer[I2C_DATA_OFFSET];
memoryMap.Measurement_1 = readBuffer[I2C_DATA_OFFSET + 1];
memoryMap.Measurement_2 = readBuffer[I2C_DATA_OFFSET + 2];
memoryMap.Measurement_3 = readBuffer[I2C_DATA_OFFSET + 3];
memoryMap.Measurement_4 = readBuffer[I2C_DATA_OFFSET + 4];
memoryMap.Measurement_5 = readBuffer[I2C_DATA_OFFSET + 5];
memoryMap.Measurement_6 = readBuffer[I2C_DATA_OFFSET + 6];
memoryMap.Measurement_7 = readBuffer[I2C_DATA_OFFSET + 7];
} finally {
readLock.unlock();
}
}
private void handleReadDone()
{
populateMemoryMap();
synchronized (this) {
if (waitingForGodot) {
waitingForGodot = false;
this.notify();
}
}
}
private void waitOnRead()
{
synchronized(this) {
waitingForGodot = true;
try {
while (waitingForGodot) {
this.wait(0);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/*
* A convenience function for turning off/on local debugs.
*/
protected void buginf(String s)
{
if (debug) {
RobotLog.i(s);
}
}
@Override
public String status() {
return String.format("NXT Ultrasonic Sensor, connected via device %s, port %d",
module.getSerialNumber().toString(), physicalPort); }
@Override
public String getDeviceName() {
return "NXT Ultrasonic Sensor";
}
@Override
public String getConnectionInfo() {
return module.getConnectionInfo() + "; port " + physicalPort;
}
@Override
public int getVersion() {
return 1;
}
@Override
public String toString()
{
return String.format("Ultrasonic: %6.1f", getUltrasonicLevel());
}
@Override
public void close() {
// take no action
}
private void throwIfPortIsInvalid(int port) {
if (port < MIN_PORT || port > MAX_PORT) {
throw new IllegalArgumentException(
String.format( "Port %d is invalid for " + getDeviceName()+ "; valid ports are %d or %d", port, MIN_PORT, MAX_PORT));
}
}
}