use of com.qualcomm.robotcore.exception.RobotCoreException 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;
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class PeerDiscovery method fromByteArray.
@Override
public void fromByteArray(byte[] byteArray) throws RobotCoreException {
if (byteArray.length < cbBufferHistorical) {
throw new RobotCoreException("Expected buffer of at least %d bytes, received %d", cbBufferHistorical, byteArray.length);
}
ByteBuffer byteBuffer = getWholeReadBuffer(byteArray);
byte peerMessageType = byteBuffer.get();
short peerCbPayload = byteBuffer.getShort();
byte peerRobocolVersion = byteBuffer.get();
byte peerType = byteBuffer.get();
short peerSeqNum = byteBuffer.getShort();
// seem worthwhile yet
if (peerRobocolVersion != RobocolConfig.ROBOCOL_VERSION) {
throw new RobotCoreException(AppUtil.getDefContext().getString(R.string.incompatibleAppsError), AppUtil.getInstance().getAppName(), RobocolConfig.ROBOCOL_VERSION, AppUtil.getInstance().getRemoteAppName(), peerRobocolVersion);
}
// ALL robocol versions have the peer type
this.peerType = PeerType.fromByte(peerType);
// All but the first have the sequence number
if (peerRobocolVersion > 1) {
this.setSequenceNumber(peerSeqNum);
}
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class FtcConfigurationActivity method commandEvent.
@Override
public CallbackResult commandEvent(Command command) {
CallbackResult result = CallbackResult.NOT_HANDLED;
try {
String name = command.getName();
String extra = command.getExtra();
if (name.equals(CommandList.CMD_SCAN_RESP)) {
result = handleCommandScanResp(extra);
} else if (name.equals(CommandList.CMD_DISCOVER_LYNX_MODULES_RESP)) {
result = handleCommandDiscoverLynxModulesResp(extra);
} else if (name.equals(CommandList.CMD_REQUEST_PARTICULAR_CONFIGURATION_RESP)) {
result = handleCommandRequestParticularConfigurationResp(extra);
}
} catch (RobotCoreException e) {
RobotLog.logStacktrace(e);
}
return result;
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class FtcConfigurationActivity method readFile.
/**
* This method parses the XML of the active configuration file, and calls methods to populate
* the appropriate data structures to the configuration information can be displayed to the
* user.
*/
private void readFile() {
try {
XmlPullParser xmlPullParser = currentCfgFile.getXml();
if (xmlPullParser == null) {
throw new RobotCoreException("can't access configuration");
}
//
ReadXMLFileHandler parser = new ReadXMLFileHandler();
List<ControllerConfiguration> controllerList = parser.parse(xmlPullParser);
buildControllersFromXMLResults(controllerList);
populateListAndWarnDevices();
//
} catch (Exception e) {
String message = String.format(getString(R.string.errorParsingConfiguration), currentCfgFile.getName());
RobotLog.ee(TAG, e, message);
appUtil.showToast(UILocation.ONLY_LOCAL, context, message);
}
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class FtcConfigurationActivity method buildRobotConfigMapFromScanned.
private RobotConfigMap buildRobotConfigMapFromScanned(RobotConfigMap existingControllers, ScannedDevices scannedDevices) {
// Initialize deviceControllers using the set of serial numbers in the ScannedDevices. If the serial
// number appears in our existing map, then just carry that configuration over; otherwise, make us
// a new configuration appropriate for the type of the controller.
RobotConfigMap newRobotConfigMap = new RobotConfigMap();
configurationUtility.resetNameUniquifiers();
for (Map.Entry<SerialNumber, DeviceManager.DeviceType> entry : scannedDevices.entrySet()) {
SerialNumber serialNumber = entry.getKey();
ControllerConfiguration controllerConfiguration = null;
if (carryOver(serialNumber, existingControllers)) {
RobotLog.vv(TAG, "carrying over %s", serialNumber);
controllerConfiguration = existingControllers.get(serialNumber);
} else {
switch(entry.getValue()) {
case MODERN_ROBOTICS_USB_DC_MOTOR_CONTROLLER:
controllerConfiguration = configurationUtility.buildNewModernMotorController(serialNumber);
break;
case MODERN_ROBOTICS_USB_SERVO_CONTROLLER:
controllerConfiguration = configurationUtility.buildNewModernServoController(serialNumber);
break;
case MODERN_ROBOTICS_USB_LEGACY_MODULE:
controllerConfiguration = configurationUtility.buildNewLegacyModule(serialNumber);
break;
case MODERN_ROBOTICS_USB_DEVICE_INTERFACE_MODULE:
controllerConfiguration = configurationUtility.buildNewDeviceInterfaceModule(serialNumber);
break;
case LYNX_USB_DEVICE:
try {
RobotLog.vv(TAG, "buildRobotConfigMapFromScanned(%s)...", serialNumber);
HardwareDeviceManager deviceManager = new HardwareDeviceManager(utility.getActivity(), null);
ThreadPool.SingletonResult<LynxModuleMetaList> discoveryFuture = this.usbScanManager.startLynxModuleEnumerationIfNecessary(serialNumber);
controllerConfiguration = configurationUtility.buildNewLynxUsbDevice(serialNumber, deviceManager, discoveryFuture);
RobotLog.vv(TAG, "...buildRobotConfigMapFromScanned(%s)", serialNumber);
} catch (InterruptedException e) {
RobotLog.ee(TAG, "interrupt in buildRobotConfigMapFromScanned(%s)", serialNumber);
Thread.currentThread().interrupt();
} catch (RobotCoreException e) {
RobotLog.ee(TAG, e, "exception in buildRobotConfigMapFromScanned(%s)", serialNumber);
controllerConfiguration = null;
}
break;
}
}
if (controllerConfiguration != null) {
controllerConfiguration.setKnownToBeAttached(true);
newRobotConfigMap.put(serialNumber, controllerConfiguration);
}
}
return newRobotConfigMap;
}
Aggregations