use of com.qualcomm.robotcore.util.SerialNumber in project robotcode by OutoftheBoxFTC.
the class RobotConfigMap method swapSerialNumbers.
/**
* Swaps the serial numbers (and attachment status) of two controllers both known to be in this configuration
*/
public void swapSerialNumbers(ControllerConfiguration a, ControllerConfiguration b) {
SerialNumber aSerialNumber = a.getSerialNumber();
a.setSerialNumber(b.getSerialNumber());
b.setSerialNumber(aSerialNumber);
this.put(a.getSerialNumber(), a);
this.put(b.getSerialNumber(), b);
boolean knownToBeAttached = a.isKnownToBeAttached();
a.setKnownToBeAttached(b.isKnownToBeAttached());
b.setKnownToBeAttached(knownToBeAttached);
}
use of com.qualcomm.robotcore.util.SerialNumber 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.util.SerialNumber 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.util.SerialNumber in project robotcode by OutoftheBoxFTC.
the class HardwareDeviceManager method scanForUsbDevices.
// ------------------------------------------------------------------------------------------------
// Scanning
// ------------------------------------------------------------------------------------------------
/* (non-Javadoc)
* @see com.qualcomm.hardware.DeviceManager#scanForUsbDevices()
*
* Returns a map from serial number to DeviceType
*/
@Override
public Map<SerialNumber, DeviceType> scanForUsbDevices() throws RobotCoreException {
synchronized (scanDevicesLock) {
long start = System.nanoTime();
final Map<SerialNumber, DeviceType> deviceMap = new ConcurrentHashMap<SerialNumber, DeviceType>();
int devCount = usbManager.scanForDevices();
RobotLog.vv(TAG_USB_SCAN, "device count=%d", devCount);
if (devCount > 0) {
// Open all the USB devices attached to the robot controller. We do this in parallel so as to minimize latency to the user.
ExecutorService executorService = ThreadPool.newFixedThreadPool(devCount, "hw mgr usb scan");
final ConcurrentHashMap<SerialNumber, RobotUsbDevice> newlyFoundDevices = new ConcurrentHashMap<SerialNumber, RobotUsbDevice>();
try {
for (int id = 0; id < devCount; id++) {
final SerialNumber serialNumber = usbManager.getDeviceSerialNumberByIndex(id);
executorService.execute(new Runnable() {
@Override
public void run() {
try {
RobotLog.vv(TAG_USB_SCAN, "opening %s...", serialNumber);
//
// It turns out that ModernRoboticsUsbUtil.openUsbDevice doesn't contain any
// logic that is specific to ModernRobotics, but rather is generic, and so
// can be used even on Lynx devices
RobotUsbDevice device = ModernRoboticsUsbUtil.openUsbDevice(false, usbManager, serialNumber);
newlyFoundDevices.put(serialNumber, device);
//
} catch (Exception e) {
RobotLog.vv(TAG_USB_SCAN, "%s(%s) exception while opening %s", e.getClass().getSimpleName(), e.getMessage(), serialNumber.toString());
} finally {
RobotLog.vv(TAG_USB_SCAN, "... done opening %s", serialNumber);
}
}
});
}
// Wait for all those opens to finish
executorService.shutdown();
ThreadPool.awaitTerminationOrExitApplication(executorService, 30, TimeUnit.SECONDS, "USB Scanning Service", "internal error");
// Having opened everything, determine the type of each
for (Map.Entry<SerialNumber, RobotUsbDevice> pair : newlyFoundDevices.entrySet()) {
determineDeviceType(pair.getValue(), pair.getKey(), deviceMap);
}
// Also consider devices that are already open
for (RobotUsbDevice existingDevice : RobotUsbDeviceImplBase.getExtantDevices()) {
SerialNumber serialNumber = existingDevice.getSerialNumber();
if (!newlyFoundDevices.containsKey(serialNumber)) {
DeviceType deviceType = existingDevice.getDeviceType();
if (deviceType != DeviceType.FTDI_USB_UNKNOWN_DEVICE) {
RobotLog.vv(TAG_USB_SCAN, "added extant device %s type=%s", serialNumber.toString(), deviceType.toString());
deviceMap.put(serialNumber, deviceType);
}
}
}
} finally {
// On the way out, be sure to close all.
for (Map.Entry<SerialNumber, RobotUsbDevice> pair : newlyFoundDevices.entrySet()) {
RobotLog.vv(TAG_USB_SCAN, "closing %s", pair.getKey());
pair.getValue().close();
}
}
}
long end = System.nanoTime();
RobotLog.vv(TAG_USB_SCAN, "scanForUsbDevices() took %dms count=%d", (int) ((end - start) / ElapsedTime.MILLIS_IN_NANO), deviceMap.size());
return deviceMap;
}
}
use of com.qualcomm.robotcore.util.SerialNumber in project robotcode by OutoftheBoxFTC.
the class ReadXMLFileHandler method handleLegacyModule.
private ControllerConfiguration handleLegacyModule() throws IOException, XmlPullParserException, RobotCoreException {
String name = parser.getAttributeValue(null, DeviceConfiguration.XMLATTR_NAME);
String serialNumber = parser.getAttributeValue(null, ControllerConfiguration.XMLATTR_SERIAL_NUMBER);
List<DeviceConfiguration> modules = buildEmptyDevices(0, ModernRoboticsConstants.NUMBER_OF_LEGACY_MODULE_PORTS, BuiltInConfigurationType.NOTHING);
int eventType = parser.next();
ConfigurationType configurationType = deform(parser.getName());
while (eventType != XmlPullParser.END_DOCUMENT) {
// we shouldn't reach the end of the document here anyway...
if (eventType == XmlPullParser.END_TAG) {
if (configurationType == null) {
// just an empty <DEVICE> </> closing tag
continue;
}
if (configurationType == BuiltInConfigurationType.LEGACY_MODULE_CONTROLLER) {
// end of loop...
noteExistingName(BuiltInConfigurationType.LEGACY_MODULE_CONTROLLER, name);
LegacyModuleControllerConfiguration legacyModule = new LegacyModuleControllerConfiguration(name, modules, new SerialNumber(serialNumber));
legacyModule.setEnabled(true);
return legacyModule;
}
}
if (eventType == XmlPullParser.START_TAG) {
if (DEBUG) {
RobotLog.e("[handleLegacyModule] tagname: " + configurationType);
}
if (configurationType == BuiltInConfigurationType.COMPASS || configurationType == BuiltInConfigurationType.LIGHT_SENSOR || configurationType == BuiltInConfigurationType.IR_SEEKER || configurationType == BuiltInConfigurationType.ACCELEROMETER || configurationType == BuiltInConfigurationType.GYRO || configurationType == BuiltInConfigurationType.TOUCH_SENSOR || configurationType == BuiltInConfigurationType.TOUCH_SENSOR_MULTIPLEXER || configurationType == BuiltInConfigurationType.ULTRASONIC_SENSOR || configurationType == BuiltInConfigurationType.COLOR_SENSOR || configurationType == BuiltInConfigurationType.NOTHING) {
DeviceConfiguration dev = handleDevice();
modules.set(dev.getPort(), dev);
} else if (configurationType == BuiltInConfigurationType.MOTOR_CONTROLLER) {
ControllerConfiguration mc = handleMotorController(false);
modules.set(mc.getPort(), mc);
} else if (configurationType == BuiltInConfigurationType.SERVO_CONTROLLER) {
ControllerConfiguration sc = handleServoController(false);
modules.set(sc.getPort(), sc);
} else if (configurationType == BuiltInConfigurationType.MATRIX_CONTROLLER) {
ControllerConfiguration mc = handleMatrixController();
modules.set(mc.getPort(), mc);
}
}
eventType = parser.next();
configurationType = deform(parser.getName());
}
return new LegacyModuleControllerConfiguration(name, modules, new SerialNumber(serialNumber));
}
Aggregations