Search in sources :

Example 1 with RobotUsbDevice

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

Example 2 with RobotUsbDevice

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;
}
Also used : RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)

Example 3 with 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;
    }
}
Also used : RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException) IOException(java.io.IOException) File(java.io.File) RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)

Example 4 with RobotUsbDevice

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

Example 5 with RobotUsbDevice

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;
        }
    }
}
Also used : 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