CameraPublishers.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "CameraPublishers.h"
  9. #include "CameraConstants.h"
  10. #include "CameraSensor.h"
  11. #include <ROS2/Communication/TopicConfiguration.h>
  12. #include <ROS2/ROS2Bus.h>
  13. #include <ROS2/Sensor/SensorConfiguration.h>
  14. #include <ROS2/Utilities/ROS2Names.h>
  15. namespace ROS2Sensors
  16. {
  17. namespace Internal
  18. {
  19. using TopicConfigurations = AZStd::unordered_map<CameraSensorDescription::CameraChannelType, ROS2::TopicConfiguration>;
  20. ROS2::TopicConfiguration GetTopicConfiguration(const ROS2::SensorConfiguration& sensorConfiguration, const AZStd::string& key)
  21. {
  22. auto ipos = sensorConfiguration.m_publishersConfigurations.find(key);
  23. AZ_Assert(ipos != sensorConfiguration.m_publishersConfigurations.end(), "Missing key in topic configuration!");
  24. return ipos != sensorConfiguration.m_publishersConfigurations.end() ? ipos->second : ROS2::TopicConfiguration{};
  25. }
  26. template<typename CameraType>
  27. TopicConfigurations GetCameraTopicConfiguration([[maybe_unused]] const ROS2::SensorConfiguration& sensorConfiguration)
  28. {
  29. AZ_Error("GetCameraTopicConfiguration", false, "Invalid camera template type!");
  30. return TopicConfigurations();
  31. }
  32. template<typename CameraType>
  33. TopicConfigurations GetCameraInfoTopicConfiguration([[maybe_unused]] const ROS2::SensorConfiguration& sensorConfiguration)
  34. {
  35. AZ_Error("GetCameraInfoTopicConfiguration", false, "Invalid camera template type!");
  36. return TopicConfigurations();
  37. }
  38. template<>
  39. TopicConfigurations GetCameraTopicConfiguration<CameraColorSensor>(const ROS2::SensorConfiguration& sensorConfiguration)
  40. {
  41. return { { CameraSensorDescription::CameraChannelType::RGB,
  42. GetTopicConfiguration(sensorConfiguration, CameraConstants::ColorImageConfig) } };
  43. }
  44. template<>
  45. TopicConfigurations GetCameraInfoTopicConfiguration<CameraColorSensor>(const ROS2::SensorConfiguration& sensorConfiguration)
  46. {
  47. return { { CameraSensorDescription::CameraChannelType::RGB,
  48. GetTopicConfiguration(sensorConfiguration, CameraConstants::ColorInfoConfig) } };
  49. }
  50. template<>
  51. TopicConfigurations GetCameraTopicConfiguration<CameraDepthSensor>(const ROS2::SensorConfiguration& sensorConfiguration)
  52. {
  53. return { { CameraSensorDescription::CameraChannelType::DEPTH,
  54. GetTopicConfiguration(sensorConfiguration, CameraConstants::DepthImageConfig) } };
  55. }
  56. template<>
  57. TopicConfigurations GetCameraInfoTopicConfiguration<CameraDepthSensor>(const ROS2::SensorConfiguration& sensorConfiguration)
  58. {
  59. return { { CameraSensorDescription::CameraChannelType::DEPTH,
  60. GetTopicConfiguration(sensorConfiguration, CameraConstants::DepthInfoConfig) } };
  61. }
  62. //! Helper that adds publishers based on predefined configuration.
  63. template<typename PublishedData>
  64. void AddPublishersFromConfiguration(
  65. const AZStd::string& cameraNamespace,
  66. const TopicConfigurations configurations,
  67. AZStd::unordered_map<CameraSensorDescription::CameraChannelType, std::shared_ptr<rclcpp::Publisher<PublishedData>>>& publishers)
  68. {
  69. for (const auto& [channel, configuration] : configurations)
  70. {
  71. AZStd::string fullTopic = ROS2::ROS2Names::GetNamespacedName(cameraNamespace, configuration.m_topic);
  72. auto ros2Node = ROS2::ROS2Interface::Get()->GetNode();
  73. auto publisher = ros2Node->create_publisher<PublishedData>(fullTopic.data(), configuration.GetQoS());
  74. publishers[channel] = publisher;
  75. }
  76. }
  77. //! Helper that adds publishers for a camera type.
  78. //! @tparam CameraType type of camera sensor (eg 'CameraColorSensor').
  79. //! @param cameraDescription complete information about camera configuration.
  80. //! @param imagePublishers publishers of raw image formats (color image, depth image, ..).
  81. //! @param infoPublishers publishers of camera_info messages for each image topic.
  82. template<typename CameraType>
  83. void AddCameraPublishers(
  84. const CameraSensorDescription& cameraDescription,
  85. AZStd::unordered_map<CameraSensorDescription::CameraChannelType, CameraPublishers::ImagePublisherPtrType>& imagePublishers,
  86. AZStd::unordered_map<CameraSensorDescription::CameraChannelType, CameraPublishers::CameraInfoPublisherPtrType>& infoPublishers)
  87. {
  88. const auto cameraImagePublisherConfigs = GetCameraTopicConfiguration<CameraType>(cameraDescription.m_sensorConfiguration);
  89. AddPublishersFromConfiguration(cameraDescription.m_cameraNamespace, cameraImagePublisherConfigs, imagePublishers);
  90. const auto cameraInfoPublisherConfigs = GetCameraInfoTopicConfiguration<CameraType>(cameraDescription.m_sensorConfiguration);
  91. AddPublishersFromConfiguration(cameraDescription.m_cameraNamespace, cameraInfoPublisherConfigs, infoPublishers);
  92. }
  93. } // namespace Internal
  94. CameraPublishers::CameraPublishers(const CameraSensorDescription& cameraDescription)
  95. {
  96. if (cameraDescription.m_cameraConfiguration.m_colorCamera)
  97. {
  98. Internal::AddCameraPublishers<CameraColorSensor>(cameraDescription, m_imagePublishers, m_infoPublishers);
  99. }
  100. if (cameraDescription.m_cameraConfiguration.m_depthCamera)
  101. {
  102. Internal::AddCameraPublishers<CameraDepthSensor>(cameraDescription, m_imagePublishers, m_infoPublishers);
  103. }
  104. }
  105. CameraPublishers::ImagePublisherPtrType CameraPublishers::GetImagePublisher(CameraSensorDescription::CameraChannelType type)
  106. {
  107. AZ_Error("GetImagePublisher", m_imagePublishers.count(type) == 1, "No publisher of this type, logic error!");
  108. return m_imagePublishers.at(type);
  109. }
  110. CameraPublishers::CameraInfoPublisherPtrType CameraPublishers::GetInfoPublisher(CameraSensorDescription::CameraChannelType type)
  111. {
  112. AZ_Error("GetInfoPublisher", m_infoPublishers.count(type) == 1, "No publisher of this type, logic error!");
  113. return m_infoPublishers.at(type);
  114. }
  115. } // namespace ROS2Sensors