Search in sources :

Example 21 with SerialNumber

use of com.qualcomm.robotcore.util.SerialNumber 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

SerialNumber (com.qualcomm.robotcore.util.SerialNumber)21 ControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration)6 Map (java.util.Map)6 LynxModuleMetaList (com.qualcomm.robotcore.hardware.LynxModuleMetaList)3 BuiltInConfigurationType (com.qualcomm.robotcore.hardware.configuration.BuiltInConfigurationType)3 ConfigurationType (com.qualcomm.robotcore.hardware.configuration.ConfigurationType)3 ThreadPool (com.qualcomm.robotcore.util.ThreadPool)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 USBScanManager (com.qualcomm.ftccommon.configuration.USBScanManager)2 RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)2 DeviceManager (com.qualcomm.robotcore.hardware.DeviceManager)2 MotorControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration)2 ServoControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ServoControllerConfiguration)2 HashMap (java.util.HashMap)2 Nullable (android.support.annotation.Nullable)1 ScannedDevices (com.qualcomm.ftccommon.configuration.ScannedDevices)1 HardwareDeviceManager (com.qualcomm.hardware.HardwareDeviceManager)1 LynxModuleMeta (com.qualcomm.robotcore.hardware.LynxModuleMeta)1 RobotCoreLynxUsbDevice (com.qualcomm.robotcore.hardware.RobotCoreLynxUsbDevice)1