use of com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration in project robotcode by OutoftheBoxFTC.
the class RobotConfigMap method bindUnboundControllers.
/**
* For each controller in this map that currently lacks a real serial number, try to choose
* an unused selection from the scanned devices to associate with same.
*/
public void bindUnboundControllers(ScannedDevices scannedDevices) {
// First, find out whom we have to choose from that's not already used
ScannedDevices extraDevices = new ScannedDevices(scannedDevices);
for (ControllerConfiguration controllerConfiguration : this.controllerConfigurations()) {
extraDevices.remove(controllerConfiguration.getSerialNumber());
}
// Invert the map, so we can easily lookup (ConfigurationType -> extra controllers)
Map<ConfigurationType, List<SerialNumber>> extraByType = new HashMap<ConfigurationType, List<SerialNumber>>();
for (Map.Entry<SerialNumber, DeviceManager.DeviceType> pair : extraDevices.entrySet()) {
ConfigurationType configurationType = BuiltInConfigurationType.fromUSBDeviceType(pair.getValue());
if (configurationType != BuiltInConfigurationType.UNKNOWN) {
List<SerialNumber> list = extraByType.get(configurationType);
if (list == null) {
list = new LinkedList<SerialNumber>();
extraByType.put(configurationType, list);
}
list.add(pair.getKey());
}
}
// Figure out who's missing, and assign. Be careful about updating while iterating.
for (ControllerConfiguration controllerConfiguration : this.controllerConfigurations()) {
if (controllerConfiguration.getSerialNumber().isFake()) {
List<SerialNumber> list = extraByType.get(controllerConfiguration.getConfigurationType());
if (list != null && !list.isEmpty()) {
// Use the first available controller of the right type, and bind to it
SerialNumber newSerialNumber = list.remove(0);
controllerConfiguration.setSerialNumber(newSerialNumber);
}
}
}
// Make sure we're accurate on the way out
Collection<ControllerConfiguration> controllers = new ArrayList<ControllerConfiguration>(this.controllerConfigurations());
map.clear();
for (ControllerConfiguration controllerConfiguration : controllers) {
put(controllerConfiguration.getSerialNumber(), controllerConfiguration);
}
}
use of com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration in project robotcode by OutoftheBoxFTC.
the class RobotConfigMap method getEligibleSwapTargets.
/**
* Returns a list of the candidate configurations with which the target may be swapped.
* Candidates must be of the same configuration type as the target but must not be the
* target itself. We pull candidates both from what's in this {@link RobotConfigMap} and
* what's currently attached to the USB bus: those are possibly intersecting sets, but each
* may have members which are not in the other.
*/
public List<ControllerConfiguration> getEligibleSwapTargets(ControllerConfiguration target, ScannedDevices scannedDevices, Context context) {
List<ControllerConfiguration> result = new LinkedList<ControllerConfiguration>();
// Only our USB-attached devices are swappable
ConfigurationType type = target.getConfigurationType();
if (!(type == BuiltInConfigurationType.MOTOR_CONTROLLER || type == BuiltInConfigurationType.SERVO_CONTROLLER || type == BuiltInConfigurationType.DEVICE_INTERFACE_MODULE || type == BuiltInConfigurationType.LEGACY_MODULE_CONTROLLER)) {
return result;
}
if (target.getSerialNumber().isFake()) {
return result;
}
// First snarf candidates that are already in this robot configuration
for (ControllerConfiguration other : this.controllerConfigurations()) {
SerialNumber serialNumber = other.getSerialNumber();
if (serialNumber.isFake()) {
continue;
}
if (serialNumber.equals(target.getSerialNumber())) {
continue;
}
if (containsSerialNumber(result, serialNumber)) {
// shouldn't need this test, but it's harmless
continue;
}
if (other.getConfigurationType() == target.getConfigurationType()) {
result.add(other);
}
}
// Then add others we know about from scanning but haven't added yet
for (Map.Entry<SerialNumber, DeviceManager.DeviceType> entry : scannedDevices.entrySet()) {
SerialNumber serialNumber = entry.getKey();
if (serialNumber.isFake()) {
continue;
}
if (serialNumber.equals(target.getSerialNumber())) {
continue;
}
if (containsSerialNumber(result, serialNumber)) {
continue;
}
if (entry.getValue() == target.toUSBDeviceType()) {
String name = generateName(context, target.getConfigurationType(), result);
ControllerConfiguration controllerConfiguration = ControllerConfiguration.forType(name, entry.getKey(), target.getConfigurationType());
controllerConfiguration.setKnownToBeAttached(scannedDevices.containsKey(controllerConfiguration.getSerialNumber()));
result.add(controllerConfiguration);
}
}
return result;
}
use of com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration in project robotcode by OutoftheBoxFTC.
the class HardwareFactory method createHardwareMap.
// ------------------------------------------------------------------------------------------------
// Hardware management
// ------------------------------------------------------------------------------------------------
/**
* Create a hardware map
*
* @return HardwareMap
*/
public HardwareMap createHardwareMap(EventLoopManager manager) throws RobotCoreException, InterruptedException {
// We synchronize with scanning so that there's only one thread trying to open *new* FTDI devices at a time
synchronized (HardwareDeviceManager.scanDevicesLock) {
RobotLog.vv(TAG, "createHardwareMap()");
// Clear notion of embedded lynx module that we currently have. We'll discovery a new one,
// if he's there, when we go through the below.
WifiDirectInviteDialogMonitor.clearUILynxModule();
HardwareMap map = new HardwareMap(context);
if (xmlPullParser != null) {
DeviceManager deviceMgr = new HardwareDeviceManager(context, manager);
ReadXMLFileHandler readXmlFileHandler = new ReadXMLFileHandler();
List<ControllerConfiguration> ctrlConfList = readXmlFileHandler.parse(xmlPullParser);
for (ControllerConfiguration ctrlConf : ctrlConfList) {
ConfigurationType type = ctrlConf.getConfigurationType();
if (type == BuiltInConfigurationType.MOTOR_CONTROLLER) {
mapUsbMotorController(map, deviceMgr, (MotorControllerConfiguration) ctrlConf);
} else if (type == BuiltInConfigurationType.SERVO_CONTROLLER) {
mapUsbServoController(map, deviceMgr, (ServoControllerConfiguration) ctrlConf);
} else if (type == BuiltInConfigurationType.LEGACY_MODULE_CONTROLLER) {
mapUsbLegacyModule(map, deviceMgr, (LegacyModuleControllerConfiguration) ctrlConf);
} else if (type == BuiltInConfigurationType.DEVICE_INTERFACE_MODULE) {
mapCoreInterfaceDeviceModule(map, deviceMgr, (DeviceInterfaceModuleConfiguration) ctrlConf);
} else if (type == BuiltInConfigurationType.LYNX_USB_DEVICE) {
mapLynxUsbDevice(map, deviceMgr, (LynxUsbDeviceConfiguration) ctrlConf);
} else {
RobotLog.ee(TAG, "Unexpected controller type while parsing XML: " + type.toString());
}
}
} else {
// no XML to parse, just return empty map
RobotLog.vv(TAG, "no xml to parse: using empty map");
}
return map;
}
}
use of com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration 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);
}
}
});
}
use of com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration 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);
}
}
Aggregations