Search in sources :

Example 6 with RobotUsbException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException in project robotcode by OutoftheBoxFTC.

the class LynxUsbDeviceImpl method resetDevice.

/**
 * Issues a hardware reset to the lynx module.
 */
public static void resetDevice(RobotUsbDevice robotUsbDevice) {
    RobotLog.vv(LynxModule.TAG, "resetDevice() serial=%s", robotUsbDevice.getSerialNumber());
    int msDelay = msCbusWiggle;
    try {
        if (LynxConstants.isEmbeddedSerialNumber(robotUsbDevice.getSerialNumber())) {
            boolean prevState = DragonboardLynxDragonboardIsPresentPin.getInstance().getState();
            RobotLog.vv(LynxModule.TAG, "resetting embedded usb device: isPresent: was=%s", prevState);
            // Make sure we're 'present'. Our reset pin won't operate unless we are
            if (!prevState) {
                DragonboardLynxDragonboardIsPresentPin.getInstance().setState(true);
                Thread.sleep(msDelay);
            }
            DragonboardLynxResetPin.getInstance().setState(true);
            Thread.sleep(msDelay);
            DragonboardLynxResetPin.getInstance().setState(false);
            Thread.sleep(msDelay);
            // ALWAYS remain 'present' unless we're explicitly configured not to
            if (LynxConstants.isRevControlHub() && LynxConstants.disableDragonboard()) {
                DragonboardLynxDragonboardIsPresentPin.getInstance().setState(false);
                Thread.sleep(msDelay);
            }
        } else {
            RobotUsbDeviceFtdi deviceFtdi = accessCBus(robotUsbDevice);
            if (deviceFtdi != null) {
                // Initialize with both lines deasserted
                deviceFtdi.cbus_setup(cbusMask, cbusNeitherAsserted);
                Thread.sleep(msDelay);
                // Assert reset
                deviceFtdi.cbus_write(cbusResetAsserted);
                Thread.sleep(msDelay);
                // Deassert reset
                deviceFtdi.cbus_write(cbusNeitherAsserted);
            }
        }
        // give the board a chance to recover
        // totally a finger in the wind
        Thread.sleep(msResetRecovery);
    } catch (InterruptedException | RobotUsbException e) {
        exceptionHandler.handleException(e);
    }
}
Also used : RobotUsbDeviceFtdi(com.qualcomm.robotcore.hardware.usb.ftdi.RobotUsbDeviceFtdi) RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)

Example 7 with RobotUsbException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException in project robotcode by OutoftheBoxFTC.

the class MonitoredUsbDeviceConnection method bulkTransfer.

public int bulkTransfer(final UsbEndpoint endpoint, final byte[] buffer, final int offset, final int length, final int timeout) throws InterruptedException, RobotUsbException {
    if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
        // read : we don't monitor these
        return delegate.bulkTransfer(endpoint, buffer, offset, length, timeout);
    } else {
        // write
        try {
            synchronized (// concurrency paranoia
            monitor) {
                this.callResult = FtDevice.RC_PARANOIA;
                if (acquireUsbWriteLock()) {
                    try {
                        this.endpoint = endpoint;
                        this.buffer = buffer;
                        this.offset = offset;
                        this.length = length;
                        this.timeout = timeout;
                        // 
                        failureType = FailureType.WRITE;
                        RobotUsbException timedOutException = monitor.monitor(bulkTransferAction, failureAction, msUsbWriteDurationMax, TimeUnit.MILLISECONDS);
                        if (timedOutException != null) {
                            throw timedOutException;
                        }
                    } catch (ExecutionException | CancellationException e) {
                        // We should have ruled out all of these cases with the above logic
                        throw RobotUsbUnspecifiedException.createChained(e, "write: internal error: unexpected exception from future");
                    } finally {
                        releaseUsbWriteLock();
                    }
                } else {
                    throw unableToAcquireWriteLockException();
                }
                // 
                return callResult;
            }
        } catch (RuntimeException e) {
            throw RobotUsbFTDIException.createChained(e, "runtime exception %s during write() of %d bytes on %s", e.getClass().getSimpleName(), length, serialNumber);
        }
    }
}
Also used : RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with RobotUsbException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException in project robotcode by OutoftheBoxFTC.

the class LynxUsbDeviceImpl method enterFirmwareUpdateModeUSB.

/**
 * If we are a USB-attached Lynx, then cause us to enter firmware update mode
 */
public static void enterFirmwareUpdateModeUSB(RobotUsbDevice robotUsbDevice) {
    RobotLog.vv(LynxModule.TAG, "enterFirmwareUpdateModeUSB() serial=%s", robotUsbDevice.getSerialNumber());
    RobotUsbDeviceFtdi deviceFtdi = accessCBus(robotUsbDevice);
    if (deviceFtdi != null) {
        try {
            int msDelay = msCbusWiggle;
            // Initialize with both lines deasserted
            deviceFtdi.cbus_setup(cbusMask, cbusNeitherAsserted);
            Thread.sleep(msDelay);
            // Assert nProg
            deviceFtdi.cbus_write(cbusProgAsserted);
            Thread.sleep(msDelay);
            // Assert nProg and nReset
            deviceFtdi.cbus_write(cbusBothAsserted);
            Thread.sleep(msDelay);
            // Deassert nReset
            deviceFtdi.cbus_write(cbusProgAsserted);
            Thread.sleep(msDelay);
            // Deassert nProg
            deviceFtdi.cbus_write(cbusNeitherAsserted);
            Thread.sleep(msResetRecovery);
        } catch (InterruptedException | RobotUsbException e) {
            exceptionHandler.handleException(e);
        }
    }
}
Also used : RobotUsbDeviceFtdi(com.qualcomm.robotcore.hardware.usb.ftdi.RobotUsbDeviceFtdi) RobotUsbException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)

Example 9 with RobotUsbException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException 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

RobotUsbException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)9 RobotUsbDevice (com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)2 RobotUsbDeviceFtdi (com.qualcomm.robotcore.hardware.usb.ftdi.RobotUsbDeviceFtdi)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 UsbEndpoint (android.hardware.usb.UsbEndpoint)1 LynxDatagram (com.qualcomm.hardware.lynx.commands.LynxDatagram)1 RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)1 File (java.io.File)1 IOException (java.io.IOException)1 RobotUsbStuckUsbWriteException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbStuckUsbWriteException)1