|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | #include <memory>
|
16 |
| -#include <vector> |
17 | 16 | #include <string>
|
| 17 | +#include <vector> |
18 | 18 |
|
19 | 19 | #include "rclcpp/rclcpp.hpp"
|
20 | 20 | #include "rclcpp/utilities.hpp"
|
21 | 21 | #include "rclcpp_components/component_manager_isolated.hpp"
|
22 | 22 |
|
23 | 23 | int main(int argc, char * argv[])
|
24 | 24 | {
|
25 |
| - /// Component container with dedicated single-threaded executors for each components. |
| 25 | + /// Component container with dedicated executor for each component |
26 | 26 | rclcpp::init(argc, argv);
|
| 27 | + |
27 | 28 | // 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 |
30 | 31 | 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") |
34 | 42 | {
|
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"; |
38 | 44 | }
|
39 | 45 | }
|
| 46 | + |
40 | 47 | // create executor and component manager
|
41 |
| - auto exec = std::make_shared<rclcpp::executors::SingleThreadedExecutor>(); |
42 | 48 | 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>(); |
46 | 54 | 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>(); |
50 | 59 | 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>(); |
54 | 64 | node = std::make_shared<ComponentManagerIsolated>(exec);
|
| 65 | + } else { |
| 66 | + std::cerr << "Invalid executor type: " << executor_type << std::endl; |
| 67 | + return 1; |
55 | 68 | }
|
| 69 | + |
56 | 70 | exec->add_node(node);
|
57 | 71 | exec->spin();
|
58 | 72 | }
|
0 commit comments