Search in sources :

Example 1 with LynxUsbDeviceConfiguration

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

the class HardwareFactory method mapLynxUsbDevice.

private void mapLynxUsbDevice(HardwareMap map, DeviceManager deviceMgr, LynxUsbDeviceConfiguration lynxUsbDeviceConfiguration) throws RobotCoreException, InterruptedException {
    if (!lynxUsbDeviceConfiguration.isEnabled()) {
        return;
    }
    // Make a new LynxUsbDevice
    LynxUsbDevice lynxUsbDevice = (LynxUsbDevice) deviceMgr.createLynxUsbDevice(lynxUsbDeviceConfiguration.getSerialNumber(), lynxUsbDeviceConfiguration.getName());
    try {
        // If the system made up this device, let the live device know that too
        if (lynxUsbDeviceConfiguration.isSystemSynthetic()) {
            lynxUsbDevice.setSystemSynthetic(true);
        }
        // Are we the first USB-attached (as opposed to embedded) LynxUsbDevice?
        boolean isFirstLynxUsbDevice = !LynxConstants.isEmbeddedSerialNumber(lynxUsbDeviceConfiguration.getSerialNumber());
        for (LynxUsbDevice usbDevice : map.getAll(LynxUsbDevice.class)) {
            if (!LynxConstants.isEmbeddedSerialNumber(usbDevice.getSerialNumber())) {
                isFirstLynxUsbDevice = false;
                break;
            }
        }
        // Make all the modules first, since we need to ping the parent before pinging anyone else,
        // and so we need to figure out who that is.
        // 
        List<LynxModule> potentialModules = new ArrayList<LynxModule>();
        Map<Integer, String> moduleNames = new HashMap<Integer, String>();
        final int parentModuleAddress = lynxUsbDeviceConfiguration.getParentModuleAddress();
        for (DeviceConfiguration moduleConfiguration : lynxUsbDeviceConfiguration.getModules()) {
            int moduleAddress = moduleConfiguration.getPort();
            moduleNames.put(moduleAddress, moduleConfiguration.getName());
            // 
            LynxModule module = (LynxModule) deviceMgr.createLynxModule(lynxUsbDevice, moduleAddress, parentModuleAddress == moduleAddress, moduleConfiguration.getName());
            potentialModules.add(module);
            // If the system made up this device, let the live device know that too
            if (((LynxModuleConfiguration) moduleConfiguration).isSystemSynthetic()) {
                module.setSystemSynthetic(true);
            }
        }
        // Attach all the LynxModules to that LynxUsbDevice, parents first, so that parents get pinged first. Note that if some
        // modules aren't actually there, or are there but are wedged, these may throw exceptions.
        Map<Integer, LynxModule> connectedModules = new HashMap<Integer, LynxModule>();
        for (LynxModule module : potentialModules) {
            if (module.isParent()) {
                // nb: there should be only one parent
                connectModule(lynxUsbDevice, module, moduleNames, connectedModules, isFirstLynxUsbDevice && connectedModules.isEmpty());
            }
        }
        for (LynxModule module : potentialModules) {
            if (!module.isParent()) {
                connectModule(lynxUsbDevice, module, moduleNames, connectedModules, false);
            }
        }
        // Finish adding all the pieces to the modules we successfully talked to
        mapLynxModuleComponents(map, deviceMgr, lynxUsbDeviceConfiguration, lynxUsbDevice, connectedModules);
        // For the things that worked, remember the module names in the hwmap
        for (Map.Entry<Integer, LynxModule> pair : connectedModules.entrySet()) {
            int moduleAddress = pair.getKey();
            map.put(moduleNames.get(moduleAddress), pair.getValue());
        }
        // Remember the LynxUsbDevice too, so we we'll only enable one to charge the RC battery
        map.put(lynxUsbDeviceConfiguration.getName(), lynxUsbDevice);
    } catch (LynxNackException e) {
        throw e.wrap();
    } catch (RobotCoreException | RuntimeException e) {
        lynxUsbDevice.close();
        map.remove(lynxUsbDeviceConfiguration.getName(), lynxUsbDevice);
        throw e;
    }
}
Also used : LynxModule(com.qualcomm.hardware.lynx.LynxModule) LynxUsbDevice(com.qualcomm.hardware.lynx.LynxUsbDevice) LynxNackException(com.qualcomm.hardware.lynx.LynxNackException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) LynxUsbDeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxUsbDeviceConfiguration) LynxI2cDeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxI2cDeviceConfiguration) DeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.DeviceConfiguration) LynxModuleConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxModuleConfiguration) HardwareMap(com.qualcomm.robotcore.hardware.HardwareMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with LynxUsbDeviceConfiguration

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

the class HardwareFactory method mapLynxModuleComponents.

private void mapLynxModuleComponents(HardwareMap map, DeviceManager deviceMgr, LynxUsbDeviceConfiguration lynxUsbDeviceConfiguration, LynxUsbDevice lynxUsbDevice, Map<Integer, LynxModule> connectedModules) throws LynxNackException, RobotCoreException, InterruptedException {
    // Hook up the pieces to each module
    for (DeviceConfiguration moduleConfiguration : lynxUsbDeviceConfiguration.getModules()) {
        LynxModule module = connectedModules.get(moduleConfiguration.getPort());
        // Ignore modules that ultimately didn't connect
        if (module == null) {
            continue;
        }
        LynxModuleConfiguration lynxModuleConfiguration = (LynxModuleConfiguration) moduleConfiguration;
        // For each module, hook up motor controller and motors
        LynxDcMotorController mc = new LynxDcMotorController(context, module);
        map.dcMotorController.put(moduleConfiguration.getName(), mc);
        for (MotorConfiguration motorConf : lynxModuleConfiguration.getMotors()) {
            if (motorConf.isEnabled()) {
                DcMotor m = deviceMgr.createDcMotorEx(mc, motorConf.getPort(), motorConf.getMotorType(), motorConf.getName());
                map.dcMotor.put(motorConf.getName(), m);
            }
        }
        // And hook up servo controller and servos
        LynxServoController sc = new LynxServoController(context, module);
        map.servoController.put(moduleConfiguration.getName(), sc);
        for (DeviceConfiguration servoConf : lynxModuleConfiguration.getServos()) {
            mapServoEx(map, deviceMgr, servoConf, sc);
        }
        // And a voltage sensor
        LynxVoltageSensor voltageSensor = new LynxVoltageSensor(context, module);
        map.voltageSensor.put(moduleConfiguration.getName(), voltageSensor);
        // Also a PWM Output Controller
        LynxPwmOutputController pwmOutputController = new LynxPwmOutputController(context, module);
        map.put(moduleConfiguration.getName(), pwmOutputController);
        buildLynxDevices(lynxModuleConfiguration.getPwmOutputs(), map, deviceMgr, pwmOutputController);
        // Also an AnalogInputController
        LynxAnalogInputController analogInputController = new LynxAnalogInputController(context, module);
        map.put(moduleConfiguration.getName(), analogInputController);
        buildLynxDevices(lynxModuleConfiguration.getAnalogInputs(), map, deviceMgr, analogInputController);
        // And a digital channel controller
        LynxDigitalChannelController digitalChannelController = new LynxDigitalChannelController(context, module);
        map.put(moduleConfiguration.getName(), digitalChannelController);
        buildLynxDevices(lynxModuleConfiguration.getDigitalDevices(), map, deviceMgr, digitalChannelController);
        // And I2c devices
        buildLynxI2cDevices(lynxModuleConfiguration.getI2cDevices(), map, deviceMgr, module);
    }
}
Also used : LynxModule(com.qualcomm.hardware.lynx.LynxModule) LynxServoController(com.qualcomm.hardware.lynx.LynxServoController) LynxDcMotorController(com.qualcomm.hardware.lynx.LynxDcMotorController) LynxAnalogInputController(com.qualcomm.hardware.lynx.LynxAnalogInputController) LynxDigitalChannelController(com.qualcomm.hardware.lynx.LynxDigitalChannelController) LynxPwmOutputController(com.qualcomm.hardware.lynx.LynxPwmOutputController) MotorConfiguration(com.qualcomm.robotcore.hardware.configuration.MotorConfiguration) LynxVoltageSensor(com.qualcomm.hardware.lynx.LynxVoltageSensor) LynxUsbDeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxUsbDeviceConfiguration) LynxI2cDeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxI2cDeviceConfiguration) DeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.DeviceConfiguration) LynxModuleConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxModuleConfiguration) DcMotor(com.qualcomm.robotcore.hardware.DcMotor)

Example 3 with LynxUsbDeviceConfiguration

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

the class FtcConfigurationActivity method populateList.

/**
 * Populates the list with the relevant controllers from the deviceControllers variable.
 * That variable is either from scanned devices, or read in from an xml file.
 */
private void populateList() {
    ListView controllerListView = (ListView) findViewById(R.id.controllersList);
    // Before we launch, we want the scan to have completed
    try {
        scannedDevices = usbScanManager.awaitScannedDevices();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    // Make sure we'll report serial numbers correctly as attached or not
    tellControllersAboutAttachment();
    DeviceInfoAdapter adapter = new DeviceInfoAdapter(this, android.R.layout.simple_list_item_2, new LinkedList<ControllerConfiguration>(getRobotConfigMap().controllerConfigurations()));
    controllerListView.setAdapter(adapter);
    controllerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View v, int pos, long arg3) {
            ControllerConfiguration controllerConfiguration = (ControllerConfiguration) adapterView.getItemAtPosition(pos);
            ConfigurationType itemType = controllerConfiguration.getConfigurationType();
            if (itemType == BuiltInConfigurationType.MOTOR_CONTROLLER) {
                EditParameters parameters = initParameters(ModernRoboticsConstants.INITIAL_MOTOR_PORT, MotorConfiguration.class, controllerConfiguration, ((MotorControllerConfiguration) controllerConfiguration).getMotors());
                parameters.setConfigurationTypes(MotorConfiguration.getAllMotorConfigurationTypes());
                handleLaunchEdit(EditMotorControllerActivity.requestCode, EditMotorControllerActivity.class, parameters);
            } else if (itemType == BuiltInConfigurationType.SERVO_CONTROLLER) {
                EditParameters parameters = initParameters(ModernRoboticsConstants.INITIAL_SERVO_PORT, ServoConfiguration.class, controllerConfiguration, ((ServoControllerConfiguration) controllerConfiguration).getServos());
                handleLaunchEdit(EditServoControllerActivity.requestCode, EditServoControllerActivity.class, parameters);
            } else if (itemType == BuiltInConfigurationType.LEGACY_MODULE_CONTROLLER) {
                EditParameters parameters = initParameters(0, DeviceConfiguration.class, controllerConfiguration, ((LegacyModuleControllerConfiguration) controllerConfiguration).getDevices());
                handleLaunchEdit(EditLegacyModuleControllerActivity.requestCode, EditLegacyModuleControllerActivity.class, parameters);
            } else if (itemType == BuiltInConfigurationType.DEVICE_INTERFACE_MODULE) {
                EditParameters parameters = initParameters(0, DeviceConfiguration.class, controllerConfiguration, ((DeviceInterfaceModuleConfiguration) controllerConfiguration).getDevices());
                handleLaunchEdit(EditDeviceInterfaceModuleActivity.requestCode, EditDeviceInterfaceModuleActivity.class, parameters);
            } else if (itemType == BuiltInConfigurationType.LYNX_USB_DEVICE) {
                EditParameters parameters = initParameters(0, LynxModuleConfiguration.class, controllerConfiguration, ((LynxUsbDeviceConfiguration) controllerConfiguration).getDevices());
                handleLaunchEdit(EditLynxUsbDeviceActivity.requestCode, EditLynxUsbDeviceActivity.class, parameters);
            }
        }
    });
}
Also used : ConfigurationType(com.qualcomm.robotcore.hardware.configuration.ConfigurationType) BuiltInConfigurationType(com.qualcomm.robotcore.hardware.configuration.BuiltInConfigurationType) LynxUsbDeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxUsbDeviceConfiguration) MotorControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ListView(android.widget.ListView) 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) AdapterView(android.widget.AdapterView) MotorConfiguration(com.qualcomm.robotcore.hardware.configuration.MotorConfiguration) LegacyModuleControllerConfiguration(com.qualcomm.robotcore.hardware.configuration.LegacyModuleControllerConfiguration) LynxUsbDeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxUsbDeviceConfiguration) DeviceConfiguration(com.qualcomm.robotcore.hardware.configuration.DeviceConfiguration) LynxModuleConfiguration(com.qualcomm.robotcore.hardware.configuration.LynxModuleConfiguration)

Aggregations

DeviceConfiguration (com.qualcomm.robotcore.hardware.configuration.DeviceConfiguration)3 LynxModuleConfiguration (com.qualcomm.robotcore.hardware.configuration.LynxModuleConfiguration)3 LynxUsbDeviceConfiguration (com.qualcomm.robotcore.hardware.configuration.LynxUsbDeviceConfiguration)3 LynxModule (com.qualcomm.hardware.lynx.LynxModule)2 LynxI2cDeviceConfiguration (com.qualcomm.robotcore.hardware.configuration.LynxI2cDeviceConfiguration)2 MotorConfiguration (com.qualcomm.robotcore.hardware.configuration.MotorConfiguration)2 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 LynxAnalogInputController (com.qualcomm.hardware.lynx.LynxAnalogInputController)1 LynxDcMotorController (com.qualcomm.hardware.lynx.LynxDcMotorController)1 LynxDigitalChannelController (com.qualcomm.hardware.lynx.LynxDigitalChannelController)1 LynxNackException (com.qualcomm.hardware.lynx.LynxNackException)1 LynxPwmOutputController (com.qualcomm.hardware.lynx.LynxPwmOutputController)1 LynxServoController (com.qualcomm.hardware.lynx.LynxServoController)1 LynxUsbDevice (com.qualcomm.hardware.lynx.LynxUsbDevice)1 LynxVoltageSensor (com.qualcomm.hardware.lynx.LynxVoltageSensor)1 RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)1 DcMotor (com.qualcomm.robotcore.hardware.DcMotor)1