2
0

CameraPublishers.cpp 6.4 KB

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