use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class ArmableUsbDevice method pretendDevice.
protected void pretendDevice() throws RobotCoreException, InterruptedException {
synchronized (armingLock) {
// Make a pretend device
RobotUsbDevice device = this.getPretendDevice(this.serialNumber);
// Arm using that device
armDevice(device);
}
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class HardwareDeviceManager method createUsbDcMotorController.
/* (non-Javadoc)
* @see com.qualcomm.hardware.DeviceManager#createUsbDcMotorController(com.qualcomm.robotcore.util.SerialNumber)
*/
@Override
public DcMotorController createUsbDcMotorController(final SerialNumber serialNumber, String name) throws RobotCoreException, InterruptedException {
HardwareFactory.noteSerialNumberType(context, serialNumber, context.getString(R.string.moduleDisplayNameMotorController));
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_DC_MOTOR_CONTROLLER) {
closeAndThrowOnFailedDeviceTypeCheck(dev, serialNumber);
}
dev.setFirmwareVersion(getModernRoboticsFirmwareVersion(deviceHeader));
} catch (RobotCoreException | RuntimeException e) {
if (dev != null) {
// avoid leakage of open FT_Devices
dev.close();
}
throw e;
}
return dev;
}
};
ModernRoboticsUsbDcMotorController controller = new ModernRoboticsUsbDcMotorController(context, serialNumber, openRobotUsbDevice, manager);
controller.armOrPretend();
controller.initializeHardware();
return controller;
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class HardwareDeviceManager method createUsbServoController.
/* (non-Javadoc)
* @see com.qualcomm.hardware.DeviceManager#createUsbServoController(com.qualcomm.robotcore.util.SerialNumber)
*/
@Override
public ServoController createUsbServoController(final SerialNumber serialNumber, String name) throws RobotCoreException, InterruptedException {
HardwareFactory.noteSerialNumberType(context, serialNumber, context.getString(R.string.moduleDisplayNameServoController));
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_SERVO_CONTROLLER) {
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;
}
};
ModernRoboticsUsbServoController controller = new ModernRoboticsUsbServoController(context, serialNumber, openRobotUsbDevice, manager);
controller.armOrPretend();
controller.initializeHardware();
return controller;
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class HardwareDeviceManager method scanForUsbDevices.
// ------------------------------------------------------------------------------------------------
// Scanning
// ------------------------------------------------------------------------------------------------
/* (non-Javadoc)
* @see com.qualcomm.hardware.DeviceManager#scanForUsbDevices()
*
* Returns a map from serial number to DeviceType
*/
@Override
public Map<SerialNumber, DeviceType> scanForUsbDevices() throws RobotCoreException {
synchronized (scanDevicesLock) {
long start = System.nanoTime();
final Map<SerialNumber, DeviceType> deviceMap = new ConcurrentHashMap<SerialNumber, DeviceType>();
int devCount = usbManager.scanForDevices();
RobotLog.vv(TAG_USB_SCAN, "device count=%d", devCount);
if (devCount > 0) {
// Open all the USB devices attached to the robot controller. We do this in parallel so as to minimize latency to the user.
ExecutorService executorService = ThreadPool.newFixedThreadPool(devCount, "hw mgr usb scan");
final ConcurrentHashMap<SerialNumber, RobotUsbDevice> newlyFoundDevices = new ConcurrentHashMap<SerialNumber, RobotUsbDevice>();
try {
for (int id = 0; id < devCount; id++) {
final SerialNumber serialNumber = usbManager.getDeviceSerialNumberByIndex(id);
executorService.execute(new Runnable() {
@Override
public void run() {
try {
RobotLog.vv(TAG_USB_SCAN, "opening %s...", serialNumber);
//
// It turns out that ModernRoboticsUsbUtil.openUsbDevice doesn't contain any
// logic that is specific to ModernRobotics, but rather is generic, and so
// can be used even on Lynx devices
RobotUsbDevice device = ModernRoboticsUsbUtil.openUsbDevice(false, usbManager, serialNumber);
newlyFoundDevices.put(serialNumber, device);
//
} catch (Exception e) {
RobotLog.vv(TAG_USB_SCAN, "%s(%s) exception while opening %s", e.getClass().getSimpleName(), e.getMessage(), serialNumber.toString());
} finally {
RobotLog.vv(TAG_USB_SCAN, "... done opening %s", serialNumber);
}
}
});
}
// Wait for all those opens to finish
executorService.shutdown();
ThreadPool.awaitTerminationOrExitApplication(executorService, 30, TimeUnit.SECONDS, "USB Scanning Service", "internal error");
// Having opened everything, determine the type of each
for (Map.Entry<SerialNumber, RobotUsbDevice> pair : newlyFoundDevices.entrySet()) {
determineDeviceType(pair.getValue(), pair.getKey(), deviceMap);
}
// Also consider devices that are already open
for (RobotUsbDevice existingDevice : RobotUsbDeviceImplBase.getExtantDevices()) {
SerialNumber serialNumber = existingDevice.getSerialNumber();
if (!newlyFoundDevices.containsKey(serialNumber)) {
DeviceType deviceType = existingDevice.getDeviceType();
if (deviceType != DeviceType.FTDI_USB_UNKNOWN_DEVICE) {
RobotLog.vv(TAG_USB_SCAN, "added extant device %s type=%s", serialNumber.toString(), deviceType.toString());
deviceMap.put(serialNumber, deviceType);
}
}
}
} finally {
// On the way out, be sure to close all.
for (Map.Entry<SerialNumber, RobotUsbDevice> pair : newlyFoundDevices.entrySet()) {
RobotLog.vv(TAG_USB_SCAN, "closing %s", pair.getKey());
pair.getValue().close();
}
}
}
long end = System.nanoTime();
RobotLog.vv(TAG_USB_SCAN, "scanForUsbDevices() took %dms count=%d", (int) ((end - start) / ElapsedTime.MILLIS_IN_NANO), deviceMap.size());
return deviceMap;
}
}
use of com.qualcomm.robotcore.hardware.usb.RobotUsbDevice in project robotcode by OutoftheBoxFTC.
the class HardwareDeviceManager method createLynxUsbDevice.
// ------------------------------------------------------------------------------------------------
// Creation
// ------------------------------------------------------------------------------------------------
/**
* Note: unlike other creation methods, creating a Lynx USB device will succeed even if
* the device is already open (in which case it will return a new delegate to the existing
* instance).
*/
@Override
public RobotCoreLynxUsbDevice createLynxUsbDevice(final SerialNumber serialNumber, String name) throws RobotCoreException, InterruptedException {
HardwareFactory.noteSerialNumberType(context, serialNumber, context.getString(R.string.moduleDisplayNameLynxUsbDevice));
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 = LynxUsbUtil.openUsbDevice(usbManager, serialNumber);
if (!dev.getUsbIdentifiers().isLynxDevice()) {
closeAndThrowOnFailedDeviceTypeCheck(dev, serialNumber);
}
DeviceType type = getLynxDeviceType(dev);
Assert.assertTrue(type == DeviceType.LYNX_USB_DEVICE);
} catch (RobotCoreException | RuntimeException e) {
if (dev != null) {
// avoid leakage of open FT_Devices
dev.close();
}
throw e;
}
return dev;
}
};
return LynxUsbDeviceImpl.findOrCreateAndArm(context, serialNumber, manager, openRobotUsbDevice);
}
Aggregations