node_main_realsense2_camera_node.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 Open Source Robotics Foundation, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. #include "class_loader/class_loader.hpp"
  18. #include "rclcpp/rclcpp.hpp"
  19. #include "rclcpp_components/node_factory.hpp"
  20. #include "rclcpp_components/node_factory_template.hpp"
  21. #define NODE_MAIN_LOGGER_NAME "realsense2_camera_node"
  22. int main(int argc, char * argv[])
  23. {
  24. auto args = rclcpp::init_and_remove_ros_arguments(argc, argv);
  25. rclcpp::Logger logger = rclcpp::get_logger(NODE_MAIN_LOGGER_NAME);
  26. rclcpp::executors::SingleThreadedExecutor exec;
  27. rclcpp::NodeOptions options;
  28. options.arguments(args);
  29. std::vector<class_loader::ClassLoader * > loaders;
  30. std::vector<rclcpp_components::NodeInstanceWrapper> node_wrappers;
  31. std::string library_name = "librealsense2_camera.so";
  32. std::string class_name = "rclcpp_components::NodeFactoryTemplate<realsense2_camera::RealSenseNodeFactory>";
  33. RCLCPP_DEBUG(logger, "Load library %s", library_name.c_str());
  34. auto loader = new class_loader::ClassLoader(library_name);
  35. auto classes = loader->getAvailableClasses<rclcpp_components::NodeFactory>();
  36. for (const auto & clazz : classes) {
  37. std::string name = clazz.c_str();
  38. if (name.compare(class_name) == 0) {
  39. RCLCPP_DEBUG(logger, "Instantiate class %s", clazz.c_str());
  40. std::shared_ptr<rclcpp_components::NodeFactory> node_factory = nullptr;
  41. try {
  42. node_factory = loader->createInstance<rclcpp_components::NodeFactory>(clazz);
  43. } catch (const std::exception & ex) {
  44. RCLCPP_ERROR(logger, "Failed to load library %s", ex.what());
  45. return 1;
  46. } catch (...) {
  47. RCLCPP_ERROR(logger, "Failed to load library");
  48. return 1;
  49. }
  50. auto wrapper = node_factory->create_node_instance(options);
  51. auto node = wrapper.get_node_base_interface();
  52. node_wrappers.push_back(wrapper);
  53. exec.add_node(node);
  54. }
  55. }
  56. loaders.push_back(loader);
  57. exec.spin();
  58. for (auto wrapper : node_wrappers) {
  59. exec.remove_node(wrapper.get_node_base_interface());
  60. }
  61. node_wrappers.clear();
  62. rclcpp::shutdown();
  63. return 0;
  64. }