Skip to content

Commit

Permalink
fix: add helper function to check if node is initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
bpapaspyros committed Dec 5, 2024
1 parent 1e4d52f commit db834d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ class BaseControllerInterface : public controller_interface::ControllerInterface
*/
std::timed_mutex& get_command_mutex();

/**
* @brief Check if the node has been initialized or not.
* @return True if the node is initialized, false otherwise
*/
bool is_node_initialized() const;

private:
/**
* @brief Parameter validation function
Expand Down
13 changes: 11 additions & 2 deletions source/modulo_controllers/src/BaseControllerInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <modulo_core/exceptions.hpp>
#include <modulo_core/translators/message_readers.hpp>
#include <stdexcept>

template<class... Ts>
struct overloaded : Ts... {
Expand Down Expand Up @@ -543,10 +544,9 @@ void BaseControllerInterface::add_service(
}

void BaseControllerInterface::add_tf_listener() {
if (this->get_node() == nullptr) {
if (!is_node_initialized()) {
throw modulo_core::exceptions::CoreException("Failed to add TF buffer and listener: Node is not initialized yet.");
}

if (this->tf_buffer_ == nullptr || this->tf_listener_ == nullptr) {
RCLCPP_DEBUG(this->get_node()->get_logger(), "Adding TF buffer and listener.");
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_NONE);
Expand Down Expand Up @@ -654,4 +654,13 @@ std::timed_mutex& BaseControllerInterface::get_command_mutex() {
return command_mutex_;
}

bool BaseControllerInterface::is_node_initialized() const {
try {
get_node();
return true;
} catch (const std::runtime_error& ex) {
return false;
}
}

}// namespace modulo_controllers

0 comments on commit db834d6

Please sign in to comment.