Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle uninitialized node correctly #176

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Expand Up @@ -972,7 +978,7 @@ template<typename T>
inline void BaseControllerInterface::publish_transforms(
const std::vector<state_representation::CartesianPose>& transforms, const std::shared_ptr<T>& tf_broadcaster,
bool is_static) {
if (this->get_node() == nullptr) {
if (!is_node_initialized()) {
throw modulo_core::exceptions::CoreException("Failed send transform(s): Node is not initialized yet.");
}
std::string modifier = is_static ? "static " : "";
Expand Down
17 changes: 13 additions & 4 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 @@ -596,7 +596,7 @@ geometry_msgs::msg::TransformStamped BaseControllerInterface::lookup_ros_transfo
}

void BaseControllerInterface::add_tf_broadcaster() {
if (this->get_node() == nullptr) {
if (!is_node_initialized()) {
throw modulo_core::exceptions::CoreException("Failed to add TF broadcaster: Node is not initialized yet.");
}
if (this->tf_broadcaster_ == nullptr) {
Expand All @@ -609,7 +609,7 @@ void BaseControllerInterface::add_tf_broadcaster() {
}

void BaseControllerInterface::add_static_tf_broadcaster() {
if (this->get_node() == nullptr) {
if (!is_node_initialized()) {
throw modulo_core::exceptions::CoreException("Failed to add static TF broadcaster: Node is not initialized yet.");
}
if (this->static_tf_broadcaster_ == nullptr) {
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&) {
return false;
}
}

}// namespace modulo_controllers
Loading