Skip to content

Commit 369098c

Browse files
committed
fixed get device by uid
1 parent 23068e4 commit 369098c

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

orbbec_camera/src/ob_camera_node_driver.cpp

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ void OBCameraNodeDriver::onDeviceConnected(const std::shared_ptr<ob::DeviceList>
109109
pthread_mutex_lock(orb_device_lock_);
110110
std::shared_ptr<int> lock_holder(nullptr,
111111
[this](int *) { pthread_mutex_unlock(orb_device_lock_); });
112-
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000, "device list count " << device_list->deviceCount());
112+
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000,
113+
"device list count " << device_list->deviceCount());
113114
if (!device_) {
114115
try {
115116
startDevice(device_list);
@@ -275,38 +276,17 @@ std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDeviceBySerialNumber(
275276

276277
std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDeviceByUSBPort(
277278
const std::shared_ptr<ob::DeviceList> &list, const std::string &usb_port) {
278-
for (size_t i = 0; i < list->deviceCount(); i++) {
279-
try {
280-
auto pid = list->pid(i);
281-
if (isOpenNIDevice(pid)) {
282-
// openNI device
283-
auto dev = list->getDevice(i);
284-
auto device_info = dev->getDeviceInfo();
285-
std::string uid = device_info->uid();
286-
auto port_id = parseUsbPort(uid);
287-
if (port_id == usb_port) {
288-
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000,
289-
"Device port id " << port_id << " matched");
290-
return dev;
291-
}
292-
} else {
293-
std::string uid = list->uid(i);
294-
auto port_id = parseUsbPort(uid);
295-
RCLCPP_ERROR_STREAM_THROTTLE(logger_, *get_clock(), 1000, "Device usb port: " << uid);
296-
if (port_id == usb_port) {
297-
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000,
298-
"Device usb port " << uid << " matched");
299-
return list->getDevice(i);
300-
}
301-
}
302-
} catch (ob::Error &e) {
303-
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.getMessage());
304-
} catch (std::exception &e) {
305-
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.what());
306-
} catch (...) {
307-
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info");
308-
}
279+
try {
280+
auto device = list->getDeviceByUid(usb_port.c_str());
281+
return device;
282+
} catch (ob::Error &e) {
283+
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.getMessage());
284+
} catch (std::exception &e) {
285+
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.what());
286+
} catch (...) {
287+
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info");
309288
}
289+
310290
return nullptr;
311291
}
312292

0 commit comments

Comments
 (0)