Search in sources :

Example 11 with RobotUsbDevice

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

Example 12 with RobotUsbDevice

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