Skip to content
Open
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
50 changes: 36 additions & 14 deletions rclcpp_components/src/component_container_isolated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

#include <memory>
#include <vector>
#include <string>
#include <vector>

#include "rclcpp/executors/single_threaded_executor.hpp"
#include "rclcpp/executors/multi_threaded_executor.hpp"
Expand All @@ -24,28 +24,50 @@

int main(int argc, char * argv[])
{
/// Component container with dedicated single-threaded executors for each components.
/// Component container with dedicated executor for each component
rclcpp::init(argc, argv);

// parse arguments
bool use_multi_threaded_executor{false};
// valid entries: --executor-type single-threaded, --executor-type multi-threaded, --executor-type events
std::vector<std::string> args = rclcpp::remove_ros_arguments(argc, argv);
for (auto & arg : args) {
if (arg == std::string("--use_multi_threaded_executor")) {
use_multi_threaded_executor = true;
rclcpp::Node::SharedPtr node;

std::string executor_type = "single-threaded"; // default
for (size_t i = 0; i < args.size(); ++i) {
if (args[i] == "--executor-type") {
if (i + 1 < args.size()) {
executor_type = args[i + 1];
break;
}
} else if (args[i] == "--use_multi_threaded_executor") { // backward compatibility
RCLCPP_WARN(node->get_logger(),
"--use_multi_threaded_executor is deprecated, use --executor-type multi-threaded instead.");
executor_type = "multi-threaded";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be probably nice to add message for user, that this option is deprecated?

}
}

// create executor and component manager
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
rclcpp::Node::SharedPtr node;
if (use_multi_threaded_executor) {
using ComponentManagerIsolated =
rclcpp_components::ComponentManagerIsolated<rclcpp::executors::MultiThreadedExecutor>;
std::shared_ptr<rclcpp::Executor> exec;
if (executor_type == "events") {
using executor = rclcpp::experimental::executors::EventsExecutor;
using ComponentManagerIsolated = rclcpp_components::ComponentManagerIsolated<executor>;
exec = std::make_shared<executor>();
node = std::make_shared<ComponentManagerIsolated>(exec);
} else {
using ComponentManagerIsolated =
rclcpp_components::ComponentManagerIsolated<rclcpp::executors::SingleThreadedExecutor>;
} else if (executor_type == "multi-threaded") {
using executor = rclcpp::executors::MultiThreadedExecutor;
using ComponentManagerIsolated = rclcpp_components::ComponentManagerIsolated<executor>;
exec = std::make_shared<executor>();
node = std::make_shared<ComponentManagerIsolated>(exec);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also behavior change compared to before. before, it creates ComponentManagerIsolated node with SingleThreadedExecutor always. but now creating ComponentManagerIsolated node with MultiThreadedExecutor, i do not think this is needed since component node is expected to do the simple task like load/unload the components. is this intentional change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the old code:

  if (use_multi_threaded_executor) {
    using ComponentManagerIsolated =
      rclcpp_components::ComponentManagerIsolated<rclcpp::executors::MultiThreadedExecutor>;

So I do not recognise this:

before, it creates ComponentManagerIsolated node with SingleThreadedExecutor always

Can you elaborate?

} else if (executor_type == "single-threaded") {
using executor = rclcpp::executors::SingleThreadedExecutor;
using ComponentManagerIsolated = rclcpp_components::ComponentManagerIsolated<executor>;
exec = std::make_shared<executor>();
node = std::make_shared<ComponentManagerIsolated>(exec);
} else {
std::cerr << "Invalid executor type: " << executor_type << std::endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually behavior change compared to before. before, even if user specify the wrong option, that falls back to SingleThreadedExecutor, but now it fails and user does not really know what can be set. it would be probably nice to show what and how executor types are set? or command line helper function can be introduced?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually behavior change compared to before.

Same as before, if nothing is supplied the single-threaded is used.

Only if previously the user supplied a non-existent option with a non-existent executor this is a behavior change:

ros2 run rclcpp_components component_container --executor-type something-wrong

But that would be a great coincidence I guess. As the --executor-type never existed.

return 1;
}

exec->add_node(node);
exec->spin();

Expand Down