Search in sources :

Example 6 with RobotUsbDevice

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);
    }
}
Also used : RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)

Example 7 with RobotUsbDevice

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;
}
Also used : ModernRoboticsUsbDevice(com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) ModernRoboticsUsbDcMotorController(com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDcMotorController) RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)

Example 8 with RobotUsbDevice

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;
}
Also used : ModernRoboticsUsbDevice(com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice) ModernRoboticsUsbServoController(com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbServoController)

Example 9 with RobotUsbDevice

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;
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice) SerialNumber(com.qualcomm.robotcore.util.SerialNumber) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 10 with RobotUsbDevice

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);
}
Also used : ModernRoboticsUsbDevice(com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)

Aggregations

RobotUsbDevice (com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)12 RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)10 ModernRoboticsUsbDevice (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice)5 RobotUsbException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)4 LynxUsbDeviceImpl (com.qualcomm.hardware.lynx.LynxUsbDeviceImpl)1 ModernRoboticsUsbDcMotorController (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDcMotorController)1 ModernRoboticsUsbDeviceInterfaceModule (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDeviceInterfaceModule)1 ModernRoboticsUsbLegacyModule (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbLegacyModule)1 ModernRoboticsUsbServoController (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbServoController)1 RobotUsbManager (com.qualcomm.robotcore.hardware.usb.RobotUsbManager)1 SerialNumber (com.qualcomm.robotcore.util.SerialNumber)1 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1