-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ROS 2 node as a component (#63)
**Public-Facing Changes** Refactor ROS 2 node as a component **Description** Allows to run the ROS2 version both as standalone node or as a component. For the standalone version we simply create a `ComponentManager` and load the foxglove bridge component. I'm not sure if this is best practice, but it has the advantage that it does not additional header files to be included which would require an additional refactoring. Fixes #28
- Loading branch information
Showing
4 changed files
with
71 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <rclcpp_components/component_manager.hpp> | ||
|
||
int main(int argc, char* argv[]) { | ||
rclcpp::init(argc, argv); | ||
|
||
auto dummyNode = std::make_shared<rclcpp::Node>("dummy"); | ||
auto numThreadsDescription = rcl_interfaces::msg::ParameterDescriptor{}; | ||
numThreadsDescription.name = "num_threads"; | ||
numThreadsDescription.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER; | ||
numThreadsDescription.description = | ||
"The number of threads to use for the ROS node executor. 0 means one thread per CPU core."; | ||
numThreadsDescription.read_only = true; | ||
numThreadsDescription.additional_constraints = "Must be a non-negative integer"; | ||
numThreadsDescription.integer_range.resize(1); | ||
numThreadsDescription.integer_range[0].from_value = 0; | ||
numThreadsDescription.integer_range[0].to_value = INT32_MAX; | ||
numThreadsDescription.integer_range[0].step = 1; | ||
constexpr int DEFAULT_NUM_THREADS = 0; | ||
dummyNode->declare_parameter("num_threads", DEFAULT_NUM_THREADS, numThreadsDescription); | ||
|
||
const auto numThreads = dummyNode->get_parameter("num_threads").as_int(); | ||
|
||
auto executor = | ||
rclcpp::executors::MultiThreadedExecutor::make_shared(rclcpp::ExecutorOptions{}, numThreads); | ||
|
||
rclcpp_components::ComponentManager componentManager(executor, "ComponentManager"); | ||
const auto componentResources = componentManager.get_component_resources("foxglove_bridge"); | ||
RCLCPP_INFO(componentManager.get_logger(), std::to_string(numThreads).c_str()); | ||
|
||
if (componentResources.empty()) { | ||
RCLCPP_INFO(componentManager.get_logger(), "No loadable resources found"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
auto componentFactory = componentManager.create_component_factory(componentResources.front()); | ||
auto node = componentFactory->create_node_instance(rclcpp::NodeOptions()); | ||
|
||
executor->spin(); | ||
rclcpp::shutdown(); | ||
|
||
return EXIT_SUCCESS; | ||
} |