fixed get device by uid

This commit is contained in:
Joe Dong
2023-09-15 10:35:38 +08:00
parent 23068e428d
commit 369098cd85
+12 -32
View File
@@ -109,7 +109,8 @@ void OBCameraNodeDriver::onDeviceConnected(const std::shared_ptr<ob::DeviceList>
pthread_mutex_lock(orb_device_lock_); pthread_mutex_lock(orb_device_lock_);
std::shared_ptr<int> lock_holder(nullptr, std::shared_ptr<int> lock_holder(nullptr,
[this](int *) { pthread_mutex_unlock(orb_device_lock_); }); [this](int *) { pthread_mutex_unlock(orb_device_lock_); });
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000, "device list count " << device_list->deviceCount()); RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000,
"device list count " << device_list->deviceCount());
if (!device_) { if (!device_) {
try { try {
startDevice(device_list); startDevice(device_list);
@@ -275,38 +276,17 @@ std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDeviceBySerialNumber(
std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDeviceByUSBPort( std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDeviceByUSBPort(
const std::shared_ptr<ob::DeviceList> &list, const std::string &usb_port) { const std::shared_ptr<ob::DeviceList> &list, const std::string &usb_port) {
for (size_t i = 0; i < list->deviceCount(); i++) { try {
try { auto device = list->getDeviceByUid(usb_port.c_str());
auto pid = list->pid(i); return device;
if (isOpenNIDevice(pid)) { } catch (ob::Error &e) {
// openNI device RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.getMessage());
auto dev = list->getDevice(i); } catch (std::exception &e) {
auto device_info = dev->getDeviceInfo(); RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.what());
std::string uid = device_info->uid(); } catch (...) {
auto port_id = parseUsbPort(uid); RCLCPP_ERROR_STREAM(logger_, "Failed to get device info");
if (port_id == usb_port) {
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000,
"Device port id " << port_id << " matched");
return dev;
}
} else {
std::string uid = list->uid(i);
auto port_id = parseUsbPort(uid);
RCLCPP_ERROR_STREAM_THROTTLE(logger_, *get_clock(), 1000, "Device usb port: " << uid);
if (port_id == usb_port) {
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000,
"Device usb port " << uid << " matched");
return list->getDevice(i);
}
}
} catch (ob::Error &e) {
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.getMessage());
} catch (std::exception &e) {
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.what());
} catch (...) {
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info");
}
} }
return nullptr; return nullptr;
} }