Search in sources :

Example 26 with RobotCoreException

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

Example 27 with RobotCoreException

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);
    }
}
Also used : RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) ByteBuffer(java.nio.ByteBuffer)

Example 28 with RobotCoreException

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;
}
Also used : CallbackResult(org.firstinspires.ftc.robotcore.internal.network.CallbackResult) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Example 29 with RobotCoreException

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);
    }
}
Also used : ReadXMLFileHandler(com.qualcomm.robotcore.hardware.configuration.ReadXMLFileHandler) XmlPullParser(org.xmlpull.v1.XmlPullParser) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) LegacyModuleControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.LegacyModuleControllerConfiguration) ControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration) ServoControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.ServoControllerConfiguration) MotorControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration) DuplicateNameException(com.qualcomm.robotcore.exception.DuplicateNameException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) IOException(java.io.IOException)

Example 30 with RobotCoreException

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;
}
Also used : SerialNumber(com.qualcomm.robotcore.util.SerialNumber) ThreadPool(com.qualcomm.robotcore.util.ThreadPool) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) LegacyModuleControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.LegacyModuleControllerConfiguration) ControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration) ServoControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.ServoControllerConfiguration) MotorControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration) Map(java.util.Map) HardwareDeviceManager(com.qualcomm.hardware.HardwareDeviceManager) LynxModuleMetaList(com.qualcomm.robotcore.hardware.LynxModuleMetaList)

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