use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class FtcEventLoopBase method getLynxUsbDeviceForFirmwareUpdate.
protected LynxUsbDeviceContainer getLynxUsbDeviceForFirmwareUpdate(SerialNumber serialNumber) {
// Is is it something that's already open?
for (LynxUsbDeviceImpl lynxUsbDevice : ftcEventLoopHandler.getExtantLynxDeviceImpls()) {
if (lynxUsbDevice.getSerialNumber().equals(serialNumber)) {
return new LynxUsbDeviceContainer(lynxUsbDevice);
}
}
// No, then open it
try {
RobotUsbManager robotUsbManager = HardwareDeviceManager.createUsbManager(AppUtil.getDefContext());
RobotUsbDevice robotUsbDevice = LynxUsbUtil.openUsbDevice(robotUsbManager, serialNumber);
return new LynxUsbDeviceContainer(robotUsbDevice);
} catch (RobotCoreException e) {
// ignored;
}
return null;
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class ModernRoboticsUsbUtil method openUsbDevice.
// ----------------------------------------------------------------------------------------------
// Device management
// ----------------------------------------------------------------------------------------------
/**
* Note: when doScan is false, several openUsbDevice() calls (for different serial numbers) can execute concurrently.
*/
public static RobotUsbDevice openUsbDevice(boolean doScan, RobotUsbManager robotUsbManager, SerialNumber serialNumber) throws RobotCoreException {
String description = "";
boolean found = false;
int deviceCount = doScan ? robotUsbManager.scanForDevices() : robotUsbManager.getScanCount();
for (int index = 0; index < deviceCount; ++index) {
if (serialNumber.equals(robotUsbManager.getDeviceSerialNumberByIndex(index))) {
found = true;
description = robotUsbManager.getDeviceDescriptionByIndex(index) + " [" + serialNumber.toString() + "]";
break;
}
}
if (!found) {
RobotLog.logAndThrow("Unable to find USB device with serial number " + serialNumber.toString());
}
RobotUsbDevice robotUsbDevice = null;
try {
robotUsbDevice = robotUsbManager.openBySerialNumber(serialNumber);
} catch (Exception e) {
throw RobotCoreException.createChained(e, "Unable to open USB device " + serialNumber + " - " + description + ": " + e.getMessage());
}
try {
robotUsbDevice.setBaudRate(ModernRoboticsConstants.USB_BAUD_RATE);
robotUsbDevice.setDataCharacteristics((byte) 8, (byte) 0, (byte) 0);
robotUsbDevice.setLatencyTimer(ModernRoboticsConstants.LATENCY_TIMER);
} catch (Exception e) {
robotUsbDevice.close();
throw RobotCoreException.createChained(e, "Unable to parameterize USB device " + serialNumber + " - " + description + ": " + e.getMessage());
}
try {
/**
* TODO: This timeout of 400ms can almost assuredly be reduced with some further analysis.
*
* "From: Berling, Jonathan
* Sent: Tuesday, January 3, 2017
*
* [...] When we were developing that code, if we tried to open too many USB devices too
* quickly they would sometimes fail. We never looked into the cause. It could have been
* anything from the USB devices themselves, to Android, to the type of hub we were using,
* etc. (This was long before MR was even considering making a USB hub.)"
*/
Thread.sleep(400L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return robotUsbDevice;
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class RobotUsbManagerTty method openBySerialNumber.
@Override
public RobotUsbDevice openBySerialNumber(SerialNumber serialNumber) throws RobotCoreException {
synchronized (getLock()) {
if (serialNumberEmbedded.equals(serialNumber)) {
if (!RobotUsbDeviceImplBase.isOpen(serialNumber)) {
File file = findSerialDevTty();
SerialPort serialPort = null;
try {
serialPort = new SerialPort(file, LynxConstants.SERIAL_MODULE_BAUD_RATE);
} catch (IOException e) {
throw RobotCoreException.createChained(e, "exception in %s.open(%s)", TAG, file.getPath());
}
RobotUsbDeviceTty deviceTTY = new RobotUsbDeviceTty(serialPort, serialNumberEmbedded, file);
deviceTTY.setFirmwareVersion(new RobotUsbDevice.FirmwareVersion(1, 0));
deviceTTY.setDeviceType(DeviceManager.DeviceType.LYNX_USB_DEVICE);
deviceTTY.setUsbIdentifiers(RobotUsbDevice.USBIdentifiers.createLynxIdentifiers());
try {
deviceTTY.setBaudRate(LynxConstants.SERIAL_MODULE_BAUD_RATE);
} catch (RobotUsbException e) {
/*ignored*/
}
return deviceTTY;
}
}
return null;
}
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class HardwareDeviceManager method createUsbLegacyModule.
/* (non-Javadoc)
* @see com.qualcomm.hardware.DeviceManager#createUsbLegacyModule(com.qualcomm.robotcore.util.SerialNumber)
*/
@Override
public LegacyModule createUsbLegacyModule(final SerialNumber serialNumber, String name) throws RobotCoreException, InterruptedException {
HardwareFactory.noteSerialNumberType(context, serialNumber, context.getString(R.string.moduleDisplayNameLegacyModule));
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_LEGACY_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;
}
};
ModernRoboticsUsbLegacyModule legacyModule = new ModernRoboticsUsbLegacyModule(context, serialNumber, openRobotUsbDevice, manager);
legacyModule.armOrPretend();
legacyModule.initializeHardware();
return legacyModule;
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class ArmableUsbDevice method armDevice.
// ----------------------------------------------------------------------------------------------
// Internal arming and disarming
// ----------------------------------------------------------------------------------------------
protected void armDevice() throws RobotCoreException, InterruptedException {
synchronized (armingLock) {
// An arming attempt clears any extant warning
internalClearGlobalWarning();
Assert.assertTrue(this.robotUsbDevice == null);
RobotUsbDevice device = null;
try {
// Open the USB device
if (DEBUG) {
RobotLog.vv(getTag(), "opening %s...", serialNumber);
}
device = this.openRobotUsbDevice.open();
if (DEBUG) {
RobotLog.vv(getTag(), "...opening, now arming %s...", serialNumber);
}
// Arm using that device
armDevice(device);
if (DEBUG) {
RobotLog.vv(getTag(), "...arming %s...", serialNumber);
}
} catch (RobotCoreException | RuntimeException e) {
RobotLog.logExceptionHeader(getTag(), e, "exception arming %s", serialNumber);
// robustly recover from what is just a USB read or write error.
if (device != null) {
device.close();
}
setGlobalWarning(String.format(context.getString(R.string.warningUnableToOpen), HardwareFactory.getDeviceDisplayName(context, serialNumber)));
throw e;
} catch (InterruptedException e) {
if (device != null) {
device.close();
}
throw e;
}
}
}
Aggregations