Search in sources :

Example 16 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class EventLoopManager method startEventLoop.

private void startEventLoop() throws RobotCoreException {
    // call the init method
    try {
        changeState(RobotState.INIT);
        synchronized (eventLoopLock) {
            eventLoop.init(this);
        }
    } catch (Exception e) {
        RobotLog.ww(TAG, e, "Caught exception during looper init: " + e.toString());
        changeState(RobotState.EMERGENCY_STOP);
        this.refreshSystemTelemetry();
        throw new RobotCoreException("Robot failed to start: " + e.getMessage());
    }
    // reset the heartbeat timer
    lastHeartbeatReceived = new ElapsedTime(0);
    // start the new event loop
    changeState(RobotState.RUNNING);
    executorEventLoop = ThreadPool.newSingleThreadExecutor("executorEventLoop");
    executorEventLoop.execute(new Runnable() {

        @Override
        public void run() {
            // Run the main event loop
            new EventLoopRunnable().run();
            // When that terminates (perhaps due to an exception), run the idle loop until we're asked
            // to shutdown. This keeps *some* event loop always running so long as we're not shutdown.
            eventLoop = idleEventLoop;
            boolean runIdle = true;
            if (!Thread.currentThread().isInterrupted()) {
                RobotLog.vv(TAG, "switching to idleEventLoop");
                try {
                    synchronized (eventLoopLock) {
                        eventLoop.init(EventLoopManager.this);
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } catch (RobotCoreException e) {
                    RobotLog.ee(TAG, e, "internal error");
                    runIdle = false;
                }
                if (runIdle) {
                    new EventLoopRunnable().run();
                }
            }
        }
    });
}
Also used : RecvLoopRunnable(org.firstinspires.ftc.robotcore.internal.network.RecvLoopRunnable) SendOnceRunnable(org.firstinspires.ftc.robotcore.internal.network.SendOnceRunnable) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) ElapsedTime(com.qualcomm.robotcore.util.ElapsedTime) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) CancellationException(java.util.concurrent.CancellationException)

Example 17 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class EventLoopManager method sendTelemetryData.

/**
 * Send telemetry data
 * <p>
 * Send the telemetry data, and then clear the sent data
 *
 * @param telemetry telemetry data
 */
public void sendTelemetryData(TelemetryMessage telemetry) {
    try {
        // conveying state here helps global errors always be portrayed as in EMERGENCY_STOP state rather than waiting until next heartbeat
        telemetry.setRobotState(this.state);
        networkConnectionHandler.sendDatagram(new RobocolDatagram(telemetry.toByteArrayForTransmission()));
    } catch (RobotCoreException e) {
        RobotLog.ww(TAG, e, "Failed to send telemetry data");
    }
    // clear the stale telemetry data
    telemetry.clearData();
}
Also used : RobocolDatagram(com.qualcomm.robotcore.robocol.RobocolDatagram) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Example 18 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException 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)

Example 19 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException 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 20 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException 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)

Aggregations

RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)32 RobotUsbDevice (com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)10 ModernRoboticsUsbDevice (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice)5 IOException (java.io.IOException)5 CallbackResult (org.firstinspires.ftc.robotcore.internal.network.CallbackResult)4 DuplicateNameException (com.qualcomm.robotcore.exception.DuplicateNameException)3 ControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 RobotUsbException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)3 RobotConfigFile (com.qualcomm.ftccommon.configuration.RobotConfigFile)2 LegacyModuleControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.LegacyModuleControllerConfiguration)2 MotorControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration)2 ReadXMLFileHandler (com.qualcomm.robotcore.hardware.configuration.ReadXMLFileHandler)2 ServoControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ServoControllerConfiguration)2 Command (com.qualcomm.robotcore.robocol.Command)2 SerialNumber (com.qualcomm.robotcore.util.SerialNumber)2 ArrayList (java.util.ArrayList)2 CancellationException (java.util.concurrent.CancellationException)2 AlertDialog (android.app.AlertDialog)1