add check connection timer

This commit is contained in:
Joe Dong
2022-12-28 17:11:30 +08:00
parent 2d82c14dec
commit 68662847c5
2 changed files with 12 additions and 1 deletions
@@ -39,6 +39,8 @@ class OBCameraNodeFactory : public rclcpp::Node {
static OBLogSeverity obLogSeverityFromString(const std::string& log_level); static OBLogSeverity obLogSeverityFromString(const std::string& log_level);
void checkConnectTimer();
void queryDevice(); void queryDevice();
private: private:
@@ -55,6 +57,7 @@ class OBCameraNodeFactory : public rclcpp::Node {
std::shared_ptr<std::thread> query_thread_ = nullptr; std::shared_ptr<std::thread> query_thread_ = nullptr;
std::recursive_mutex device_lock_; std::recursive_mutex device_lock_;
size_t device_num_ = 1; size_t device_num_ = 1;
rclcpp::TimerBase::SharedPtr check_connect_timer_ = nullptr;
}; };
} // namespace orbbec_camera } // namespace orbbec_camera
+9 -1
View File
@@ -52,6 +52,8 @@ void OBCameraNodeFactory::init() {
deviceDisconnectCallback(removed_list); deviceDisconnectCallback(removed_list);
deviceConnectCallback(added_list); deviceConnectCallback(added_list);
}); });
check_connect_timer_ =
this->create_wall_timer(std::chrono::milliseconds(1000), [this]() { checkConnectTimer(); });
query_thread_ = std::make_shared<std::thread>([this]() { queryDevice(); }); query_thread_ = std::make_shared<std::thread>([this]() { queryDevice(); });
} }
@@ -105,10 +107,16 @@ OBLogSeverity OBCameraNodeFactory::obLogSeverityFromString(const std::string &lo
} else if (log_level == "fatal") { } else if (log_level == "fatal") {
return OBLogSeverity::OB_LOG_SEVERITY_FATAL; return OBLogSeverity::OB_LOG_SEVERITY_FATAL;
} else { } else {
return OBLogSeverity::OB_LOG_SEVERITY_INFO; return OBLogSeverity::OB_LOG_SEVERITY_NONE;
} }
} }
void OBCameraNodeFactory::checkConnectTimer() {
if (!device_connected_) {
RCLCPP_ERROR_STREAM(logger_, "checkConnectTimer: device not connected");
return;
}
}
void OBCameraNodeFactory::queryDevice() { void OBCameraNodeFactory::queryDevice() {
while (is_alive_ && rclcpp::ok()) { while (is_alive_ && rclcpp::ok()) {
std::lock_guard<decltype(device_lock_)> lock(device_lock_); std::lock_guard<decltype(device_lock_)> lock(device_lock_);