Search in sources :

Example 6 with LynxModuleMetaList

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

the class FtcEventLoop method handleCommandDiscoverLynxModules.

protected void handleCommandDiscoverLynxModules(String extra) throws RobotCoreException, InterruptedException {
    RobotLog.vv(FtcConfigurationActivity.TAG, "handling command DiscoverLynxModules");
    final SerialNumber serialNumber = new SerialNumber(extra);
    final USBScanManager usbScanManager = startUsbScanMangerIfNecessary();
    // Start a scan and wait for it to complete, but if a scan is already in progress, then just wait for that one to finish
    final ThreadPool.SingletonResult<LynxModuleMetaList> future = this.usbScanManager.startLynxModuleEnumerationIfNecessary(serialNumber);
    // Actually carry out the scan in a worker thread so that we don't hold up the receive loop for
    // full second or more that carrying out the discovery will take.
    ThreadPool.getDefault().execute(new Runnable() {

        @Override
        public void run() {
            try {
                LynxModuleMetaList lynxModules = future.await();
                if (lynxModules == null) {
                    lynxModules = new LynxModuleMetaList(serialNumber);
                }
                // Package up the raw module list and send that back to the DS
                String data = usbScanManager.packageCommandResponse(lynxModules);
                RobotLog.vv(FtcConfigurationActivity.TAG, "DiscoverLynxModules data='%s'", data);
                networkConnectionHandler.sendCommand(new Command(CommandList.CMD_DISCOVER_LYNX_MODULES_RESP, data));
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    });
}
Also used : SerialNumber(com.qualcomm.robotcore.util.SerialNumber) USBScanManager(com.qualcomm.ftccommon.configuration.USBScanManager) Command(com.qualcomm.robotcore.robocol.Command) ThreadPool(com.qualcomm.robotcore.util.ThreadPool) LynxModuleMetaList(com.qualcomm.robotcore.hardware.LynxModuleMetaList)

Example 7 with LynxModuleMetaList

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

the class FtcEventLoopBase method getUSBAccessibleLynxDevices.

protected List<USBAccessibleLynxModule> getUSBAccessibleLynxDevices(boolean includeModuleAddresses) throws RobotCoreException {
    RobotLog.vv(TAG, "getUSBAccessibleLynxDevices()...");
    // We do a raw, low level scan, not caring what's in the current hardware map, if anything.
    // This is important: a module might, for example, be in a state where it previously had a
    // failed firmware update, and all that's running is its bootloader. Such a beast would be
    // unable to respond to
    USBScanManager scanManager = startUsbScanMangerIfNecessary();
    final ThreadPool.SingletonResult<ScannedDevices> future = scanManager.startDeviceScanIfNecessary();
    try {
        ScannedDevices scannedDevices = future.await();
        List<USBAccessibleLynxModule> result = new ArrayList<USBAccessibleLynxModule>();
        // Return everything returned by the scan
        for (Map.Entry<SerialNumber, DeviceManager.DeviceType> entry : scannedDevices.entrySet()) {
            if (entry.getValue() == DeviceManager.DeviceType.LYNX_USB_DEVICE) {
                SerialNumber serialNumber = entry.getKey();
                // For the moment, serial numbers of the embedded module must be one. If the
                // embedded/synthetic module was discovered rather than assuming its address
                // to always one, this could be relaxed.
                result.add(new USBAccessibleLynxModule(serialNumber, !serialNumber.equals(LynxConstants.SERIAL_NUMBER_EMBEDDED)));
            }
        }
        // Return the embedded module if we're supposed to and if it wasn't already there (which it will be, I think, always, now)
        if (LynxConstants.enableLynxFirmwareUpdateForDragonboard()) {
            boolean found = false;
            for (USBAccessibleLynxModule module : result) {
                if (module.getSerialNumber().equals(LynxConstants.SERIAL_NUMBER_EMBEDDED)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                result.add(new USBAccessibleLynxModule(LynxConstants.SERIAL_NUMBER_EMBEDDED, false));
            }
        }
        // Add module addresses if asked
        if (includeModuleAddresses) {
            for (int i = 0; i < result.size(); ) {
                USBAccessibleLynxModule usbModule = result.get(i);
                RobotCoreLynxUsbDevice device = scanManager.getDeviceManager().createLynxUsbDevice(usbModule.getSerialNumber(), null);
                try {
                    LynxModuleMetaList lynxModuleMetas = device.discoverModules();
                    boolean foundParent = false;
                    boolean foundChild = false;
                    for (LynxModuleMeta meta : lynxModuleMetas) {
                        if (meta.getModuleAddress() == 0) {
                            // paranoia
                            continue;
                        }
                        if (meta.isParent()) {
                            usbModule.setModuleAddress(meta.getModuleAddress());
                            foundParent = true;
                        } else {
                            // We've got child modules connected: these are unsafe to update
                            foundChild = true;
                        }
                    }
                    if (foundParent && !foundChild) {
                        i++;
                    } else {
                        RobotLog.vv(TAG, "lynx module %s not actually accessible", usbModule.getSerialNumber());
                        result.remove(i);
                    }
                } finally {
                    if (device != null) {
                        device.close();
                    }
                }
            }
        }
        RobotLog.vv(TAG, "...getUSBAccessibleLynxDevices(): %d modules found", result.size());
        return result;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return new ArrayList<USBAccessibleLynxModule>();
    }
}
Also used : USBScanManager(com.qualcomm.ftccommon.configuration.USBScanManager) ThreadPool(com.qualcomm.robotcore.util.ThreadPool) ArrayList(java.util.ArrayList) LynxModuleMetaList(com.qualcomm.robotcore.hardware.LynxModuleMetaList) ScannedDevices(com.qualcomm.ftccommon.configuration.ScannedDevices) SerialNumber(com.qualcomm.robotcore.util.SerialNumber) LynxModuleMeta(com.qualcomm.robotcore.hardware.LynxModuleMeta) RobotCoreLynxUsbDevice(com.qualcomm.robotcore.hardware.RobotCoreLynxUsbDevice) Map(java.util.Map)

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