Search in sources :

Example 1 with LynxModuleMetaList

use of com.qualcomm.robotcore.hardware.LynxModuleMetaList in project robotcode by OutoftheBoxFTC.

the class ConfigurationUtility method buildNewLynxUsbDevice.

public LynxUsbDeviceConfiguration buildNewLynxUsbDevice(SerialNumber serialNumber, DeviceManager deviceManager, ThreadPool.SingletonResult<LynxModuleMetaList> discoveryFuture) throws RobotCoreException, InterruptedException {
    RobotLog.vv(TAG, "buildNewLynxUsbDevice(%s)...", serialNumber);
    try {
        LynxModuleMetaList metas = discoveryFuture.await();
        if (metas == null) {
            metas = new LynxModuleMetaList(serialNumber);
        }
        RobotLog.vv(TAG, "buildLynxUsbDevice(): discovered lynx modules: %s", metas);
        // 
        List<LynxModuleConfiguration> modules = new LinkedList<LynxModuleConfiguration>();
        for (LynxModuleMeta meta : metas) {
            modules.add(buildNewLynxModule(meta.getModuleAddress(), meta.isParent(), true));
        }
        DeviceConfiguration.sortByName(modules);
        RobotLog.vv(TAG, "buildNewLynxUsbDevice(%s): %d modules", serialNumber, modules.size());
        String name = createUniqueName(BuiltInConfigurationType.LYNX_USB_DEVICE, R.string.counted_lynx_usb_device_name);
        LynxUsbDeviceConfiguration result = new LynxUsbDeviceConfiguration(name, modules, serialNumber);
        return result;
    } finally {
        RobotLog.vv(TAG, "...buildNewLynxUsbDevice(%s): ", serialNumber);
    }
}
Also used : LynxModuleMeta(com.qualcomm.robotcore.hardware.LynxModuleMeta) LynxModuleMetaList(com.qualcomm.robotcore.hardware.LynxModuleMetaList) LinkedList(java.util.LinkedList)

Example 2 with LynxModuleMetaList

use of com.qualcomm.robotcore.hardware.LynxModuleMetaList in project robotcode by OutoftheBoxFTC.

the class USBScanManager method awaitLynxModules.

@NonNull
public LynxModuleMetaList awaitLynxModules(SerialNumber serialNumber) throws InterruptedException {
    LynxModuleDiscoveryState discoveryState = getDiscoveryState(serialNumber);
    LynxModuleMetaList result = discoveryState.lynxDiscoverySingleton.await();
    if (result == null) {
        RobotLog.vv(TAG, "USBScanManager.awaitLynxModules() returning made-up result");
        result = new LynxModuleMetaList(serialNumber);
    }
    return result;
}
Also used : LynxModuleMetaList(com.qualcomm.robotcore.hardware.LynxModuleMetaList) NonNull(android.support.annotation.NonNull)

Example 3 with LynxModuleMetaList

use of com.qualcomm.robotcore.hardware.LynxModuleMetaList in project robotcode by OutoftheBoxFTC.

the class USBScanManager method handleCommandDiscoverLynxModulesResponse.

public void handleCommandDiscoverLynxModulesResponse(String extra) throws RobotCoreException {
    RobotLog.vv(TAG, "handleCommandDiscoverLynxModulesResponse()...");
    LynxModuleMetaList lynxModules = LynxModuleMetaList.fromSerializationString(extra);
    LynxModuleDiscoveryState discoveryState = getDiscoveryState(lynxModules.serialNumber);
    synchronized (discoveryState.remoteLynxDiscoveryLock) {
        discoveryState.remoteLynxModules = lynxModules;
        discoveryState.lynxDiscoverySequence.advanceNext();
    }
    RobotLog.vv(TAG, "...handleCommandDiscoverLynxModulesResponse()");
}
Also used : LynxModuleMetaList(com.qualcomm.robotcore.hardware.LynxModuleMetaList)

Example 4 with LynxModuleMetaList

use of com.qualcomm.robotcore.hardware.LynxModuleMetaList in project robotcode by OutoftheBoxFTC.

the class LynxUsbDeviceImpl method discoverModules.

@Override
public LynxModuleMetaList discoverModules() throws RobotCoreException, InterruptedException {
    RobotLog.vv(TAG, "lynx discovery beginning...transmitting LynxDiscoveryCommand()...");
    // Initialize our set of known modules and send out discovery requests
    this.discoveredModules.clear();
    // Make ourselves a fake module so that we can (mostly) use the normal transmission infrastructure
    LynxModule fakeModule = new LynxModule(this, 0, /*ignored*/
    false);
    try {
        // Make a discovery command and send it out
        LynxDiscoveryCommand discoveryCommand = new LynxDiscoveryCommand(fakeModule);
        discoveryCommand.send();
        // Wait an interval sufficient so as to guarantee that we'll see all the replies
        // that are there to see
        long nsPerModuleInterval = 3 * ElapsedTime.MILLIS_IN_NANO;
        int maxNumberOfModules = 254;
        // "entire packet must be received within 50ms from the first Frame Byte"
        long nsPacketTimeMax = 50 * ElapsedTime.MILLIS_IN_NANO;
        // should be oodles
        long nsSlop = 200 * ElapsedTime.MILLIS_IN_NANO;
        long nsWait = nsPerModuleInterval * maxNumberOfModules + nsPacketTimeMax + nsSlop;
        long msWait = (nsWait / ElapsedTime.MILLIS_IN_NANO);
        nsWait = nsWait - msWait * ElapsedTime.MILLIS_IN_NANO;
        RobotLog.vv(TAG, "discovery waiting %dms and %dns", msWait, nsWait);
        Thread.sleep(msWait, (int) nsWait);
        RobotLog.vv(TAG, "discovery waiting complete: #modules=%d", discoveredModules.size());
    } catch (LynxNackException e) {
        throw e.wrap();
    } finally {
        // Tidy up
        fakeModule.close();
    }
    LynxModuleMetaList result = new LynxModuleMetaList(serialNumber, discoveredModules.values());
    RobotLog.vv(TAG, "...lynx discovery completed");
    return result;
}
Also used : LynxDiscoveryCommand(com.qualcomm.hardware.lynx.commands.standard.LynxDiscoveryCommand) LynxModuleMetaList(com.qualcomm.robotcore.hardware.LynxModuleMetaList)

Example 5 with LynxModuleMetaList

use of com.qualcomm.robotcore.hardware.LynxModuleMetaList 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

LynxModuleMetaList (com.qualcomm.robotcore.hardware.LynxModuleMetaList)7 SerialNumber (com.qualcomm.robotcore.util.SerialNumber)3 ThreadPool (com.qualcomm.robotcore.util.ThreadPool)3 USBScanManager (com.qualcomm.ftccommon.configuration.USBScanManager)2 LynxModuleMeta (com.qualcomm.robotcore.hardware.LynxModuleMeta)2 Map (java.util.Map)2 NonNull (android.support.annotation.NonNull)1 ScannedDevices (com.qualcomm.ftccommon.configuration.ScannedDevices)1 HardwareDeviceManager (com.qualcomm.hardware.HardwareDeviceManager)1 LynxDiscoveryCommand (com.qualcomm.hardware.lynx.commands.standard.LynxDiscoveryCommand)1 RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)1 RobotCoreLynxUsbDevice (com.qualcomm.robotcore.hardware.RobotCoreLynxUsbDevice)1 ControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration)1 LegacyModuleControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.LegacyModuleControllerConfiguration)1 MotorControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration)1 ServoControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ServoControllerConfiguration)1 Command (com.qualcomm.robotcore.robocol.Command)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1