use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class HardwareDeviceManager method createDeviceInterfaceModule.
@Override
public DeviceInterfaceModule createDeviceInterfaceModule(final SerialNumber serialNumber, String name) throws RobotCoreException, InterruptedException {
HardwareFactory.noteSerialNumberType(context, serialNumber, context.getString(R.string.moduleDisplayNameCDIM));
RobotLog.v("Creating %s", HardwareFactory.getDeviceDisplayName(context, serialNumber));
ModernRoboticsUsbDevice.OpenRobotUsbDevice openRobotUsbDevice = new ModernRoboticsUsbDevice.OpenRobotUsbDevice() {
@Override
public RobotUsbDevice open() throws RobotCoreException, InterruptedException {
RobotUsbDevice dev = null;
try {
dev = ModernRoboticsUsbUtil.openUsbDevice(true, usbManager, serialNumber);
byte[] deviceHeader = getModernRoboticsDeviceHeader(dev);
DeviceType type = getModernRoboticsDeviceType(dev, deviceHeader);
if (type != DeviceType.MODERN_ROBOTICS_USB_DEVICE_INTERFACE_MODULE) {
closeAndThrowOnFailedDeviceTypeCheck(dev, serialNumber);
}
dev.setFirmwareVersion(getModernRoboticsFirmwareVersion(deviceHeader));
} catch (RobotCoreException e) {
if (dev != null) {
// avoid leakage of open FT_Devices
dev.close();
}
throw e;
} catch (RuntimeException e) {
if (dev != null) {
// avoid leakage of open FT_Devices
dev.close();
}
throw e;
}
return dev;
}
};
ModernRoboticsUsbDeviceInterfaceModule deviceInterfaceModule = new ModernRoboticsUsbDeviceInterfaceModule(context, serialNumber, openRobotUsbDevice, manager);
deviceInterfaceModule.armOrPretend();
deviceInterfaceModule.initializeHardware();
return deviceInterfaceModule;
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class LynxUsbUtil method openUsbDevice.
public static RobotUsbDevice openUsbDevice(RobotUsbManager robotUsbManager, SerialNumber serialNumber) throws RobotCoreException {
String deviceDescription = "";
boolean found = false;
int deviceCount = robotUsbManager.scanForDevices();
for (int i = 0; i < deviceCount; ++i) {
if (serialNumber.equals(robotUsbManager.getDeviceSerialNumberByIndex(i))) {
found = true;
deviceDescription = robotUsbManager.getDeviceDescriptionByIndex(i) + " [" + serialNumber.getSerialNumber() + "]";
break;
}
}
if (!found) {
logMessageAndThrow("unable to find lynx USB device with serial number " + serialNumber.toString());
}
RobotUsbDevice result = null;
try {
result = robotUsbManager.openBySerialNumber(serialNumber);
} catch (RobotCoreException e) {
logMessageAndThrow("unable to open lynx USB device " + serialNumber + " - " + deviceDescription + ": " + e.getMessage());
}
try {
// Set BAUD rate for USB comm.
result.setBaudRate(LynxConstants.USB_BAUD_RATE);
result.setDataCharacteristics((byte) 8, (byte) 0, (byte) 0);
// We make the latency timer as small as possible in order to minimize the
// latency of reception of data: virtually *all* our packets have less than 62 bytes
// of our payload, so this can be significant. The difference between 1ms and 2ms (which
// is what the Modern Robotics USB uses as of current writing) has been observed with
// a USB packet sniffer to at times be on the order of 5-10ms additional latency.
result.setLatencyTimer(LynxConstants.LATENCY_TIMER);
} catch (RobotUsbException e) {
result.close();
logMessageAndThrow("Unable to open lynx USB device " + serialNumber + " - " + deviceDescription + ": " + e.getMessage());
}
return result;
}
Aggregations