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();
}
}
}
});
}
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();
}
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;
}
}
}
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;
}
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;
}
Aggregations