Files
OrbbecSDK_ROS2/orbbec_camera/src/ob_camera_node_driver.cpp
T

529 lines
21 KiB
C++
Raw Normal View History

2023-09-08 08:55:16 +08:00
/*******************************************************************************
2023-09-12 11:12:40 +08:00
* Copyright (c) 2023 Orbbec 3D Technology, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
2022-06-08 14:51:00 +08:00
#include "orbbec_camera/ob_camera_node_driver.h"
2024-09-23 20:22:23 +08:00
#include "orbbec_camera/utils.h"
2022-12-28 13:55:26 +08:00
#include <fcntl.h>
#include <semaphore.h>
#include <sys/shm.h>
2023-03-22 16:10:22 +08:00
#include <ament_index_cpp/get_package_share_directory.hpp>
2024-09-12 19:29:27 +08:00
#include <ament_index_cpp/get_package_prefix.hpp>
2023-08-22 21:11:59 +08:00
#include <rclcpp_components/register_node_macro.hpp>
2023-09-05 17:59:19 +08:00
#include <csignal>
2023-09-15 10:30:23 +08:00
#include <sys/mman.h>
2024-06-19 10:40:57 +08:00
#include <unistd.h>
2024-09-10 19:55:12 +08:00
#include <filesystem>
#include <fstream>
#include <iomanip> // For std::put_time
std::string g_camera_name = "orbbec_camera"; // Assuming this is declared elsewhere
void signalHandler(int sig) {
std::cout << "Received signal: " << sig << std::endl;
std::string log_dir = "Log/";
// get current time
std::time_t now = std::time(nullptr);
2024-09-23 20:22:23 +08:00
std::tm *local_time = std::localtime(&now);
2024-09-10 19:55:12 +08:00
// format date and time to string, format as "2024_05_20_12_34_56"
std::ostringstream time_stream;
time_stream << std::put_time(local_time, "%Y_%m_%d_%H_%M_%S");
// generate log file name
std::string log_file_name = g_camera_name + "_crash_stack_trace_" + time_stream.str() + ".log";
std::string log_file_path = log_dir + log_file_name;
if (!std::filesystem::exists(log_dir)) {
std::filesystem::create_directories(log_dir);
}
std::cout << "Log crash stack trace to " << log_file_path << std::endl;
std::ofstream log_file(log_file_path, std::ios::app);
if (log_file.is_open()) {
log_file << "Received signal: " << sig << std::endl;
backward::StackTrace st;
st.load_here(32); // Capture stack
backward::Printer p;
p.print(st, log_file); // Print stack to log file
}
log_file.close();
exit(sig); // Exit program
}
2024-06-19 10:40:57 +08:00
2022-06-06 19:00:12 +08:00
namespace orbbec_camera {
2024-09-23 20:22:23 +08:00
backward::SignalHandling OBCameraNodeDriver::sh;
OBCameraNodeDriver::OBCameraNodeDriver(const rclcpp::NodeOptions &node_options)
2022-06-06 19:00:12 +08:00
: Node("orbbec_camera_node", "/", node_options),
2024-09-23 20:22:23 +08:00
node_options_(node_options),
2023-03-22 16:10:22 +08:00
config_path_(ament_index_cpp::get_package_share_directory("orbbec_camera") +
2024-11-19 18:43:26 +08:00
"/config/OrbbecSDKConfig_v2.0.xml"),
2024-09-12 19:29:27 +08:00
logger_(this->get_logger()),
2024-09-23 20:22:23 +08:00
extension_path_(ament_index_cpp::get_package_prefix("orbbec_camera") + "/lib/extensions") {
2022-06-06 19:00:12 +08:00
init();
}
2023-02-06 17:26:10 +08:00
2024-09-23 20:22:23 +08:00
OBCameraNodeDriver::OBCameraNodeDriver(const std::string &node_name, const std::string &ns,
const rclcpp::NodeOptions &node_options)
2022-06-06 19:00:12 +08:00
: Node(node_name, ns, node_options),
2024-09-23 20:22:23 +08:00
node_options_(node_options),
2024-09-12 19:29:27 +08:00
config_path_(ament_index_cpp::get_package_share_directory("orbbec_camera") +
2024-11-19 18:43:26 +08:00
"/config/OrbbecSDKConfig_v2.0.xml"),
2024-09-12 19:29:27 +08:00
logger_(this->get_logger()),
2024-09-23 20:22:23 +08:00
extension_path_(ament_index_cpp::get_package_prefix("orbbec_camera") + "/lib/extensions") {
2022-06-06 19:00:12 +08:00
init();
}
OBCameraNodeDriver::~OBCameraNodeDriver() {
2022-06-06 19:00:12 +08:00
is_alive_.store(false);
if (device_count_update_thread_ && device_count_update_thread_->joinable()) {
device_count_update_thread_->join();
}
2022-12-28 13:55:26 +08:00
if (query_thread_ && query_thread_->joinable()) {
query_thread_->join();
2022-06-06 19:00:12 +08:00
}
2023-07-13 17:15:48 +08:00
if (reset_device_thread_ && reset_device_thread_->joinable()) {
reset_device_cond_.notify_all();
reset_device_thread_->join();
}
2024-10-16 22:25:35 +08:00
ob_camera_node_->stopGmslTrigger();
if (orb_device_lock_shm_fd_ != -1) {
close(orb_device_lock_shm_fd_);
orb_device_lock_shm_fd_ = -1;
}
shm_unlink(ORB_DEFAULT_LOCK_NAME.c_str());
2022-06-06 19:00:12 +08:00
}
2022-10-10 18:04:09 +08:00
void OBCameraNodeDriver::init() {
2024-09-10 19:55:12 +08:00
signal(SIGSEGV, signalHandler); // segment fault
signal(SIGABRT, signalHandler); // abort
signal(SIGFPE, signalHandler); // float point exception
signal(SIGILL, signalHandler); // illegal instruction
2024-09-12 19:29:27 +08:00
ob::Context::setExtensionsDirectory(extension_path_.c_str());
2024-09-28 19:03:42 +08:00
if (config_path_.empty()) {
ctx_ = std::make_unique<ob::Context>();
} else {
ctx_ = std::make_unique<ob::Context>(config_path_.c_str());
}
2023-02-06 17:26:10 +08:00
auto log_level_str = declare_parameter<std::string>("log_level", "none");
auto log_level = obLogSeverityFromString(log_level_str);
2024-04-13 15:45:10 +08:00
connection_delay_ = static_cast<int>(declare_parameter<int>("connection_delay", 100));
2024-07-17 16:56:52 +08:00
enable_sync_host_time_ = declare_parameter<bool>("enable_sync_host_time", true);
2024-09-10 19:55:12 +08:00
g_camera_name = declare_parameter<std::string>("camera_name", g_camera_name);
2024-04-07 11:04:45 +08:00
ob::Context::setLoggerToConsole(log_level);
2023-09-15 10:30:23 +08:00
orb_device_lock_shm_fd_ = shm_open(ORB_DEFAULT_LOCK_NAME.c_str(), O_CREAT | O_RDWR, 0666);
if (orb_device_lock_shm_fd_ < 0) {
RCLCPP_ERROR_STREAM(logger_, "Failed to open shared memory " << ORB_DEFAULT_LOCK_NAME);
return;
}
int ret = ftruncate(orb_device_lock_shm_fd_, sizeof(pthread_mutex_t));
if (ret < 0) {
RCLCPP_ERROR_STREAM(logger_, "Failed to truncate shared memory " << ORB_DEFAULT_LOCK_NAME);
return;
}
orb_device_lock_shm_addr_ =
2024-09-23 20:22:23 +08:00
static_cast<uint8_t *>(mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED,
orb_device_lock_shm_fd_, 0));
2023-09-15 10:30:23 +08:00
if (orb_device_lock_shm_addr_ == MAP_FAILED) {
RCLCPP_ERROR_STREAM(logger_, "Failed to map shared memory " << ORB_DEFAULT_LOCK_NAME);
return;
}
2024-07-29 09:50:02 +08:00
reboot_device_srv_ = this->create_service<std_srvs::srv::Empty>(
"reboot_device", std::bind(&OBCameraNodeDriver::rebootDeviceCallback, this,
std::placeholders::_1, std::placeholders::_2));
2023-09-15 10:30:23 +08:00
pthread_mutexattr_init(&orb_device_lock_attr_);
pthread_mutexattr_setpshared(&orb_device_lock_attr_, PTHREAD_PROCESS_SHARED);
2024-09-23 20:22:23 +08:00
orb_device_lock_ = (pthread_mutex_t *)orb_device_lock_shm_addr_;
2023-09-15 10:30:23 +08:00
pthread_mutex_init(orb_device_lock_, &orb_device_lock_attr_);
2022-06-06 19:00:12 +08:00
is_alive_.store(true);
2022-06-08 14:51:00 +08:00
parameters_ = std::make_shared<Parameters>(this);
2022-06-08 13:41:31 +08:00
serial_number_ = declare_parameter<std::string>("serial_number", "");
2023-02-17 22:25:24 +08:00
device_num_ = static_cast<int>(declare_parameter<int>("device_num", 1));
2023-07-05 17:50:44 +08:00
usb_port_ = declare_parameter<std::string>("usb_port", "");
2024-03-04 14:56:54 +08:00
net_device_ip_ = declare_parameter<std::string>("net_device_ip", "");
net_device_port_ = static_cast<int>(declare_parameter<int>("net_device_port", 0));
enumerate_net_device_ = declare_parameter<bool>("enumerate_net_device", false);
2024-12-18 16:10:09 +08:00
uvc_backend_ = declare_parameter<std::string>("uvc_backend", "libuvc");
if (uvc_backend_ == "libuvc") {
2024-12-18 16:10:09 +08:00
ctx_->setUvcBackendType(OB_UVC_BACKEND_TYPE_LIBUVC);
RCLCPP_INFO_STREAM(logger_, "setUvcBackendType:" << uvc_backend_);
} else if (uvc_backend_ == "v4l2") {
ctx_->setUvcBackendType(OB_UVC_BACKEND_TYPE_V4L2);
RCLCPP_INFO_STREAM(logger_, "setUvcBackendType:" << uvc_backend_);
} else {
ctx_->setUvcBackendType(OB_UVC_BACKEND_TYPE_LIBUVC);
RCLCPP_INFO_STREAM(logger_, "setUvcBackendType:" << uvc_backend_);
2024-12-18 16:10:09 +08:00
}
ctx_->enableNetDeviceEnumeration(enumerate_net_device_);
2024-09-23 20:22:23 +08:00
ctx_->setDeviceChangedCallback([this](const std::shared_ptr<ob::DeviceList> &removed_list,
const std::shared_ptr<ob::DeviceList> &added_list) {
2023-10-14 18:28:16 +08:00
onDeviceConnected(added_list);
2023-02-06 17:26:10 +08:00
onDeviceDisconnected(removed_list);
2022-06-06 19:00:12 +08:00
});
2022-12-28 17:11:30 +08:00
check_connect_timer_ =
this->create_wall_timer(std::chrono::milliseconds(1000), [this]() { checkConnectTimer(); });
2023-02-06 17:26:10 +08:00
CHECK_NOTNULL(check_connect_timer_);
2022-12-28 13:55:26 +08:00
query_thread_ = std::make_shared<std::thread>([this]() { queryDevice(); });
2023-07-13 17:15:48 +08:00
reset_device_thread_ = std::make_shared<std::thread>([this]() { resetDevice(); });
2022-06-06 19:00:12 +08:00
}
2024-09-23 20:22:23 +08:00
void OBCameraNodeDriver::onDeviceConnected(const std::shared_ptr<ob::DeviceList> &device_list) {
2023-02-06 17:40:01 +08:00
CHECK_NOTNULL(device_list);
if (device_list->getCount() == 0) {
2022-06-08 13:41:31 +08:00
return;
}
2022-06-06 19:00:12 +08:00
if (!device_) {
2023-10-12 20:52:10 +08:00
startDevice(device_list);
2022-06-06 19:00:12 +08:00
}
}
2024-09-23 20:22:23 +08:00
void OBCameraNodeDriver::onDeviceDisconnected(const std::shared_ptr<ob::DeviceList> &device_list) {
2023-02-06 17:40:01 +08:00
CHECK_NOTNULL(device_list);
if (device_list->getCount() == 0) {
2022-06-08 13:41:31 +08:00
return;
2022-06-06 19:00:12 +08:00
}
2023-02-06 17:26:10 +08:00
RCLCPP_INFO_STREAM(logger_, "onDeviceDisconnected");
for (size_t i = 0; i < device_list->getCount(); i++) {
std::string uid = device_list->getUid(i);
std::string serial_number = device_list->getSerialNumber(i);
2023-10-14 18:28:16 +08:00
std::lock_guard<decltype(device_lock_)> lock(device_lock_);
2023-02-15 21:20:02 +08:00
RCLCPP_INFO_STREAM(logger_, "device with " << uid << " disconnected");
2023-10-14 18:28:16 +08:00
if (uid == device_unique_id_ || serial_number_ == serial_number) {
RCLCPP_INFO_STREAM(logger_,
"device with " << uid << " disconnected, notify reset device thread.");
2023-07-13 17:15:48 +08:00
std::unique_lock<decltype(reset_device_mutex_)> reset_device_lock(reset_device_mutex_);
reset_device_flag_ = true;
reset_device_cond_.notify_all();
2022-12-28 13:55:26 +08:00
break;
2022-06-08 15:33:16 +08:00
}
}
2022-06-08 13:41:31 +08:00
}
2024-09-23 20:22:23 +08:00
OBLogSeverity OBCameraNodeDriver::obLogSeverityFromString(const std::string_view &log_level) {
2022-10-10 18:04:09 +08:00
if (log_level == "debug") {
return OBLogSeverity::OB_LOG_SEVERITY_DEBUG;
} else if (log_level == "info") {
return OBLogSeverity::OB_LOG_SEVERITY_INFO;
} else if (log_level == "warn") {
return OBLogSeverity::OB_LOG_SEVERITY_WARN;
} else if (log_level == "error") {
return OBLogSeverity::OB_LOG_SEVERITY_ERROR;
} else if (log_level == "fatal") {
return OBLogSeverity::OB_LOG_SEVERITY_FATAL;
} else {
2022-12-28 17:11:30 +08:00
return OBLogSeverity::OB_LOG_SEVERITY_NONE;
2022-10-10 18:04:09 +08:00
}
}
2022-12-28 13:55:26 +08:00
2023-03-01 22:01:08 +08:00
void OBCameraNodeDriver::checkConnectTimer() {
2023-02-06 17:26:10 +08:00
if (!device_connected_.load()) {
2023-09-11 17:57:02 +08:00
RCLCPP_DEBUG_STREAM(logger_,
2023-02-15 21:20:02 +08:00
"checkConnectTimer: device " << serial_number_ << " not connected");
2022-12-28 17:11:30 +08:00
return;
2023-03-01 22:01:08 +08:00
} else if (!ob_camera_node_) {
device_connected_.store(false);
2022-12-28 17:11:30 +08:00
}
}
2023-02-06 17:26:10 +08:00
void OBCameraNodeDriver::queryDevice() {
2024-04-13 15:45:10 +08:00
while (is_alive_ && rclcpp::ok() && !device_connected_.load()) {
2024-03-04 14:56:54 +08:00
if (!net_device_ip_.empty() && net_device_port_ != 0) {
connectNetDevice(net_device_ip_, net_device_port_);
} else {
auto device_list = ctx_->queryDeviceList();
if (device_list->getCount() == 0) {
2024-04-13 15:45:10 +08:00
RCLCPP_INFO_STREAM(logger_,
"queryDevice :No Device found, using usb event to trigger "
"OBCameraNodeDriver::onDeviceConnected");
2024-03-04 14:56:54 +08:00
return;
}
startDevice(device_list);
2022-12-28 13:55:26 +08:00
}
}
}
2023-07-13 17:15:48 +08:00
void OBCameraNodeDriver::resetDevice() {
while (is_alive_ && rclcpp::ok()) {
std::unique_lock<decltype(reset_device_mutex_)> lock(reset_device_mutex_);
reset_device_cond_.wait(lock,
2023-10-12 20:52:10 +08:00
[this]() { return !is_alive_ || !rclcpp::ok() || reset_device_flag_; });
2023-07-13 17:15:48 +08:00
if (!is_alive_ || !rclcpp::ok()) {
break;
}
2023-10-14 18:28:16 +08:00
RCLCPP_INFO_STREAM(logger_, "resetDevice : Reset device uid: " << device_unique_id_);
std::lock_guard<decltype(device_lock_)> device_lock(device_lock_);
{
ob_camera_node_.reset();
device_.reset();
device_info_.reset();
device_connected_ = false;
device_unique_id_.clear();
serial_number_.clear();
reset_device_flag_ = false;
}
RCLCPP_INFO_STREAM(logger_, "Reset device uid: " << device_unique_id_ << " done");
2023-07-13 17:15:48 +08:00
}
}
2023-02-17 20:18:55 +08:00
2024-07-29 09:50:02 +08:00
void OBCameraNodeDriver::rebootDeviceCallback(
const std::shared_ptr<std_srvs::srv::Empty::Request> request,
std::shared_ptr<std_srvs::srv::Empty::Response> response) {
(void)request;
(void)response;
if (!device_connected_) {
RCLCPP_WARN(logger_, "Device not connected");
return;
}
RCLCPP_INFO(logger_, "Reboot device");
ob_camera_node_->rebootDevice();
device_connected_ = false;
device_ = nullptr;
}
std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDevice(
2024-09-23 20:22:23 +08:00
const std::shared_ptr<ob::DeviceList> &list) {
2023-07-05 15:00:34 +08:00
std::shared_ptr<ob::Device> device = nullptr;
if (!serial_number_.empty()) {
2023-10-14 18:28:16 +08:00
RCLCPP_INFO_STREAM(logger_, "Connecting to device with serial number: " << serial_number_);
2023-07-05 15:00:34 +08:00
device = selectDeviceBySerialNumber(list, serial_number_);
} else if (!usb_port_.empty()) {
2023-10-14 18:28:16 +08:00
RCLCPP_INFO_STREAM(logger_, "Connecting to device with usb port: " << usb_port_);
2023-07-05 15:00:34 +08:00
device = selectDeviceByUSBPort(list, usb_port_);
} else if (device_num_ == 1) {
RCLCPP_INFO_STREAM(logger_, "Connecting to the default device");
return list->getDevice(0);
2023-07-05 15:00:34 +08:00
}
2023-02-17 20:18:55 +08:00
if (device == nullptr) {
2023-02-17 22:25:24 +08:00
RCLCPP_WARN_THROTTLE(logger_, *get_clock(), 1000, "Device with serial number %s not found",
serial_number_.c_str());
2023-02-17 20:18:55 +08:00
device_connected_ = false;
return nullptr;
}
return device;
}
2023-02-16 16:14:38 +08:00
std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDeviceBySerialNumber(
2024-09-23 20:22:23 +08:00
const std::shared_ptr<ob::DeviceList> &list, const std::string &serial_number) {
2023-02-17 20:18:55 +08:00
std::string lower_sn;
std::transform(serial_number.begin(), serial_number.end(), std::back_inserter(lower_sn),
[](auto ch) { return isalpha(ch) ? tolower(ch) : static_cast<int>(ch); });
for (size_t i = 0; i < list->getCount(); i++) {
2023-10-14 18:34:54 +08:00
RCLCPP_INFO_STREAM(logger_, "Before lock: Select device serial number: " << serial_number);
std::lock_guard<decltype(device_lock_)> lock(device_lock_);
RCLCPP_INFO_STREAM(logger_, "After lock: Select device serial number: " << serial_number);
2023-02-17 20:18:55 +08:00
try {
auto pid = list->getPid(i);
2023-03-02 20:15:01 +08:00
if (isOpenNIDevice(pid)) {
2023-02-17 20:18:55 +08:00
// openNI device
auto device = list->getDevice(i);
auto device_info = device->getDeviceInfo();
if (device_info->getSerialNumber() == serial_number) {
2024-09-10 19:55:12 +08:00
RCLCPP_INFO_STREAM(
logger_, "Device serial number " << device_info->getSerialNumber() << " matched");
2023-02-17 20:18:55 +08:00
return device;
2023-02-15 15:05:47 +08:00
}
2022-12-28 13:55:26 +08:00
} else {
std::string sn = list->getSerialNumber(i);
2023-02-17 22:25:24 +08:00
RCLCPP_INFO_STREAM_THROTTLE(logger_, *get_clock(), 1000, "Device serial number: " << sn);
2023-02-17 20:18:55 +08:00
if (sn == serial_number) {
2023-03-02 20:15:01 +08:00
RCLCPP_INFO_STREAM(logger_, "Device serial number " << sn << " matched");
2023-02-17 20:18:55 +08:00
return list->getDevice(i);
2022-12-28 13:55:26 +08:00
}
}
2024-09-23 20:22:23 +08:00
} catch (ob::Error &e) {
2023-02-17 22:25:24 +08:00
RCLCPP_ERROR_STREAM_THROTTLE(logger_, *get_clock(), 1000,
"Failed to get device info " << e.getMessage());
2024-09-23 20:22:23 +08:00
} catch (std::exception &e) {
2023-02-17 22:25:24 +08:00
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.what());
2023-02-17 20:18:55 +08:00
} catch (...) {
2023-02-17 22:25:24 +08:00
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info");
2022-12-28 13:55:26 +08:00
}
2022-06-06 19:00:12 +08:00
}
2023-02-17 20:18:55 +08:00
return nullptr;
}
2023-07-05 15:00:34 +08:00
std::shared_ptr<ob::Device> OBCameraNodeDriver::selectDeviceByUSBPort(
2024-09-23 20:22:23 +08:00
const std::shared_ptr<ob::DeviceList> &list, const std::string &usb_port) {
2023-09-15 10:35:38 +08:00
try {
2023-10-14 18:28:16 +08:00
RCLCPP_INFO_STREAM(logger_, "Before lock: Select device usb port: " << usb_port);
std::lock_guard<decltype(device_lock_)> lock(device_lock_);
RCLCPP_INFO_STREAM(logger_, "After lock: Select device usb port: " << usb_port);
2023-09-15 10:35:38 +08:00
auto device = list->getDeviceByUid(usb_port.c_str());
2024-09-23 20:22:23 +08:00
if (device) {
RCLCPP_INFO_STREAM(logger_, "getDeviceByUid device usb port " << usb_port << " done");
} else {
RCLCPP_ERROR_STREAM(logger_, "getDeviceByUid device usb port " << usb_port << " failed");
RCLCPP_ERROR_STREAM(logger_,
"Please use script to get usb port: "
"ros2 run orbbec_camera list_devices_node");
}
2023-09-15 10:35:38 +08:00
return device;
2024-09-23 20:22:23 +08:00
} catch (ob::Error &e) {
2023-09-15 10:35:38 +08:00
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.getMessage());
2024-09-23 20:22:23 +08:00
} catch (std::exception &e) {
2023-09-15 10:35:38 +08:00
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info " << e.what());
} catch (...) {
RCLCPP_ERROR_STREAM(logger_, "Failed to get device info");
2023-07-05 15:00:34 +08:00
}
2023-09-15 10:35:38 +08:00
2023-07-05 15:00:34 +08:00
return nullptr;
}
2024-09-23 20:22:23 +08:00
void OBCameraNodeDriver::initializeDevice(const std::shared_ptr<ob::Device> &device) {
2023-02-17 20:18:55 +08:00
device_ = device;
2023-02-06 17:26:10 +08:00
CHECK_NOTNULL(device_);
CHECK_NOTNULL(device_.get());
if (ob_camera_node_) {
ob_camera_node_.reset();
}
2024-09-23 20:22:23 +08:00
int retry_count = 0;
constexpr int max_retries = 3;
bool initialized = false;
device_info_ = device_->getDeviceInfo();
RCLCPP_INFO_STREAM(logger_, "Try to connect device via " << device_info_->connectionType());
while (retry_count < max_retries && !initialized) {
try {
ob_camera_node_ = std::make_unique<OBCameraNode>(this, device_, parameters_,
node_options_.use_intra_process_comms());
initialized = true;
} catch (const ob::Error &e) {
RCLCPP_ERROR_STREAM(logger_, "Failed to initialize device (Attempt "
<< retry_count + 1 << " of " << max_retries
<< "): " << e.getMessage());
} catch (const std::exception &e) {
RCLCPP_ERROR_STREAM(logger_, "Failed to initialize device (Attempt " << retry_count + 1
<< " of " << max_retries
<< "): " << e.what());
} catch (...) {
RCLCPP_ERROR_STREAM(logger_, "Failed to initialize device (Attempt "
<< retry_count + 1 << " of " << max_retries << ")");
}
retry_count++;
}
if (!initialized) {
RCLCPP_ERROR_STREAM(logger_,
"Device initialization failed after " << max_retries << " attempts.");
throw std::runtime_error("Device initialization failed after " + std::to_string(max_retries) +
" attempts.");
}
2023-10-13 21:40:32 +08:00
ob_camera_node_->startIMU();
2024-05-07 11:08:38 +08:00
ob_camera_node_->startStreams();
2024-10-16 22:25:35 +08:00
2023-02-06 17:26:10 +08:00
device_connected_ = true;
device_info_ = device_->getDeviceInfo();
serial_number_ = device_info_->getSerialNumber();
2023-02-06 17:26:10 +08:00
CHECK_NOTNULL(device_info_.get());
device_unique_id_ = device_info_->getUid();
2024-09-29 20:28:24 +08:00
2024-09-10 20:23:20 +08:00
if (enable_sync_host_time_ && !isOpenNIDevice(device_info_->pid())) {
TRY_EXECUTE_BLOCK(device_->timerSyncWithHost());
}
2024-09-10 20:23:20 +08:00
RCLCPP_INFO_STREAM(logger_, "Device " << device_info_->getName() << " connected");
RCLCPP_INFO_STREAM(logger_, "Serial number: " << device_info_->getSerialNumber());
RCLCPP_INFO_STREAM(logger_, "Firmware version: " << device_info_->getFirmwareVersion());
RCLCPP_INFO_STREAM(logger_, "Hardware version: " << device_info_->getHardwareVersion());
2023-02-15 21:20:02 +08:00
RCLCPP_INFO_STREAM(logger_, "device unique id: " << device_unique_id_);
2024-05-07 11:08:38 +08:00
RCLCPP_INFO_STREAM(logger_, "Current node pid: " << getpid());
RCLCPP_INFO_STREAM(logger_, "usb connect type: " << device_info_->getConnectionType());
auto time_cost = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - start_time_);
RCLCPP_INFO_STREAM(logger_, "Start device cost " << time_cost.count() << " ms");
2024-09-10 20:23:20 +08:00
} // namespace orbbec_camera
2023-02-17 20:18:55 +08:00
2024-09-23 20:22:23 +08:00
void OBCameraNodeDriver::connectNetDevice(const std::string &net_device_ip, int net_device_port) {
2024-03-04 14:56:54 +08:00
if (net_device_ip.empty() || net_device_port == 0) {
RCLCPP_ERROR_STREAM(logger_, "Invalid net device ip or port");
return;
}
2024-04-13 15:45:10 +08:00
std::this_thread::sleep_for(std::chrono::milliseconds(connection_delay_));
2024-03-04 14:56:54 +08:00
auto device = ctx_->createNetDevice(net_device_ip.c_str(), net_device_port);
if (device == nullptr) {
RCLCPP_ERROR_STREAM(logger_, "Failed to connect to net device " << net_device_ip);
return;
}
initializeDevice(device);
}
2024-09-23 20:22:23 +08:00
void OBCameraNodeDriver::startDevice(const std::shared_ptr<ob::DeviceList> &list) {
2023-02-17 20:18:55 +08:00
if (device_connected_) {
return;
}
if (list->getCount() == 0) {
2023-02-17 20:18:55 +08:00
RCLCPP_WARN(logger_, "No device found");
return;
}
start_time_ = std::chrono::high_resolution_clock::now();
2023-02-17 20:18:55 +08:00
if (device_) {
device_.reset();
}
std::this_thread::sleep_for(std::chrono::milliseconds(connection_delay_));
2023-10-14 18:28:16 +08:00
pthread_mutex_lock(orb_device_lock_);
std::shared_ptr<int> lock_holder(nullptr,
2024-09-23 20:22:23 +08:00
[this](int *) { pthread_mutex_unlock(orb_device_lock_); });
2023-10-14 18:28:16 +08:00
bool start_device_failed = false;
try {
auto start_time = std::chrono::high_resolution_clock::now();
2023-10-14 18:28:16 +08:00
auto device = selectDevice(list);
auto end_time = std::chrono::high_resolution_clock::now();
auto time_cost = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
RCLCPP_INFO_STREAM(logger_, "Select device cost " << time_cost.count() << " ms");
2023-10-14 18:28:16 +08:00
if (device == nullptr) {
RCLCPP_WARN_THROTTLE(logger_, *get_clock(), 1000, "Device with serial number %s not found",
serial_number_.c_str());
device_connected_ = false;
return;
}
start_time = std::chrono::high_resolution_clock::now();
2023-10-14 18:28:16 +08:00
initializeDevice(device);
end_time = std::chrono::high_resolution_clock::now();
time_cost = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
RCLCPP_INFO_STREAM(logger_, "Initialize device cost " << time_cost.count() << " ms");
2024-10-16 22:25:35 +08:00
auto pid = device->getDeviceInfo()->getPid();
if (GEMINI_335LG_PID == pid) {
ob_camera_node_->startGmslTrigger();
}
2024-09-23 20:22:23 +08:00
} catch (ob::Error &e) {
2023-10-14 18:28:16 +08:00
RCLCPP_ERROR_STREAM(logger_, "Failed to initialize device " << e.getMessage());
start_device_failed = true;
2024-09-23 20:22:23 +08:00
} catch (std::exception &e) {
2023-10-14 18:28:16 +08:00
RCLCPP_ERROR_STREAM(logger_, "Failed to initialize device " << e.what());
start_device_failed = true;
} catch (...) {
RCLCPP_ERROR_STREAM(logger_, "Failed to initialize device");
start_device_failed = true;
}
if (start_device_failed) {
2023-02-17 20:18:55 +08:00
device_connected_ = false;
2023-10-14 18:28:16 +08:00
std::unique_lock<decltype(reset_device_mutex_)> reset_device_lock(reset_device_mutex_);
reset_device_flag_ = true;
reset_device_cond_.notify_all();
2023-02-17 20:18:55 +08:00
}
}
2022-06-06 19:00:12 +08:00
} // namespace orbbec_camera
2023-08-22 21:11:59 +08:00
2023-10-12 20:52:10 +08:00
RCLCPP_COMPONENTS_REGISTER_NODE(orbbec_camera::OBCameraNodeDriver)