Skip to content

Commit ba7c6a1

Browse files
committed
Parse '--executor-type'
Signed-off-by: Tim Clephas <[email protected]>
1 parent 60cc8ce commit ba7c6a1

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

rclcpp_components/src/component_container_isolated.cpp

+34-20
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,60 @@
1313
// limitations under the License.
1414

1515
#include <memory>
16-
#include <vector>
1716
#include <string>
17+
#include <vector>
1818

1919
#include "rclcpp/rclcpp.hpp"
2020
#include "rclcpp/utilities.hpp"
2121
#include "rclcpp_components/component_manager_isolated.hpp"
2222

2323
int main(int argc, char * argv[])
2424
{
25-
/// Component container with dedicated single-threaded executors for each components.
25+
/// Component container with dedicated executor for each component
2626
rclcpp::init(argc, argv);
27+
2728
// parse arguments
28-
bool use_multi_threaded_executor{false};
29-
bool use_events_executor{false};
29+
// valid entries: --executor-type single-threaded, --executor-type multi-threaded, --executor-type events
30+
// --use-multi-threaded-executor and --use_multi_threaded_executor are kept for backward compatibility
3031
std::vector<std::string> args = rclcpp::remove_ros_arguments(argc, argv);
31-
for (auto & arg : args) {
32-
if (arg == std::string("--use_multi_threaded_executor") ||
33-
arg == std::string("--use-multi-threaded-executor"))
32+
33+
std::string executor_type = "single-threaded"; // default
34+
for (size_t i = 0; i < args.size(); ++i) {
35+
if (args[i] == "--executor-type") {
36+
if (i + 1 < args.size()) {
37+
executor_type = args[i + 1];
38+
break;
39+
}
40+
} else if (
41+
args[i] == "--use-multi-threaded-executor" || args[i] == "--use_multi_threaded_executor")
3442
{
35-
use_multi_threaded_executor = true;
36-
} else if (arg == std::string("--use-events-executor")) {
37-
use_events_executor = true;
43+
executor_type = "multi-threaded";
3844
}
3945
}
46+
4047
// create executor and component manager
41-
auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
4248
rclcpp::Node::SharedPtr node;
43-
if (use_multi_threaded_executor) {
44-
using ComponentManagerIsolated =
45-
rclcpp_components::ComponentManagerIsolated<rclcpp::executors::MultiThreadedExecutor>;
49+
std::shared_ptr<rclcpp::Executor> exec;
50+
if (executor_type == "events") {
51+
using executor = rclcpp::experimental::executors::EventsExecutor;
52+
using ComponentManagerIsolated = rclcpp_components::ComponentManagerIsolated<executor>;
53+
exec = std::make_shared<executor>();
4654
node = std::make_shared<ComponentManagerIsolated>(exec);
47-
} else if (use_events_executor) {
48-
using ComponentManagerIsolated =
49-
rclcpp_components::ComponentManagerIsolated<rclcpp::experimental::executors::EventsExecutor>;
55+
} else if (executor_type == "multi-threaded") {
56+
using executor = rclcpp::executors::MultiThreadedExecutor;
57+
using ComponentManagerIsolated = rclcpp_components::ComponentManagerIsolated<executor>;
58+
exec = std::make_shared<executor>();
5059
node = std::make_shared<ComponentManagerIsolated>(exec);
51-
} else {
52-
using ComponentManagerIsolated =
53-
rclcpp_components::ComponentManagerIsolated<rclcpp::executors::SingleThreadedExecutor>;
60+
} else if (executor_type == "single-threaded") {
61+
using executor = rclcpp::executors::SingleThreadedExecutor;
62+
using ComponentManagerIsolated = rclcpp_components::ComponentManagerIsolated<executor>;
63+
exec = std::make_shared<executor>();
5464
node = std::make_shared<ComponentManagerIsolated>(exec);
65+
} else {
66+
std::cerr << "Invalid executor type: " << executor_type << std::endl;
67+
return 1;
5568
}
69+
5670
exec->add_node(node);
5771
exec->spin();
5872
}

0 commit comments

Comments
 (0)