ROS2CameraSensorComponent.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "Camera/ROS2CameraSensorComponent.h"
  9. #include "Frame/ROS2FrameComponent.h"
  10. #include "ROS2/ROS2Bus.h"
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Component/TransformBus.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <Utilities/ROS2Names.h>
  16. #include <sensor_msgs/distortion_models.hpp>
  17. #include <sensor_msgs/image_encodings.hpp>
  18. namespace ROS2
  19. {
  20. namespace Internal
  21. {
  22. const char* kImageMessageType = "sensor_msgs::msg::Image";
  23. const char* kCameraInfoMessageType = "sensor_msgs::msg::CameraInfo";
  24. AZStd::pair<AZStd::string, PublisherConfiguration> MakePublisherConfigurationPair(
  25. const AZStd::string& topic, const AZStd::string& messageType)
  26. {
  27. PublisherConfiguration config;
  28. config.m_topic = topic;
  29. config.m_type = messageType;
  30. return AZStd::make_pair(messageType, config);
  31. }
  32. } // namespace Internal
  33. ROS2CameraSensorComponent::ROS2CameraSensorComponent()
  34. {
  35. m_sensorConfiguration.m_frequency = 10;
  36. m_sensorConfiguration.m_publishersConfigurations.insert(
  37. Internal::MakePublisherConfigurationPair("camera_image", Internal::kImageMessageType));
  38. m_sensorConfiguration.m_publishersConfigurations.insert(
  39. Internal::MakePublisherConfigurationPair("camera_info", Internal::kCameraInfoMessageType));
  40. }
  41. void ROS2CameraSensorComponent::Reflect(AZ::ReflectContext* context)
  42. {
  43. auto* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  44. if (serialize)
  45. {
  46. serialize->Class<ROS2CameraSensorComponent, ROS2SensorComponent>()
  47. ->Version(1)
  48. ->Field("CameraName", &ROS2CameraSensorComponent::m_cameraName)
  49. ->Field("VerticalFieldOfViewDeg", &ROS2CameraSensorComponent::m_VerticalFieldOfViewDeg)
  50. ->Field("Width", &ROS2CameraSensorComponent::m_width)
  51. ->Field("Height", &ROS2CameraSensorComponent::m_height);
  52. AZ::EditContext* ec = serialize->GetEditContext();
  53. if (ec)
  54. {
  55. ec->Class<ROS2CameraSensorComponent>("ROS2 Camera Sensor", "[Camera component]")
  56. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  57. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  58. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  59. ->DataElement(
  60. AZ::Edit::UIHandlers::Default, &ROS2CameraSensorComponent::m_cameraName, "Camera Name", "This is the camera name.")
  61. ->DataElement(
  62. AZ::Edit::UIHandlers::Default,
  63. &ROS2CameraSensorComponent::m_VerticalFieldOfViewDeg,
  64. "Vertical field of view",
  65. "Camera's vertical (y axis) field of view in degrees.")
  66. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2CameraSensorComponent::m_width, "Image width", "Image width")
  67. ->DataElement(AZ::Edit::UIHandlers::Default, &ROS2CameraSensorComponent::m_height, "Image height", "Image height");
  68. }
  69. }
  70. }
  71. void ROS2CameraSensorComponent::Activate()
  72. {
  73. ROS2SensorComponent::Activate();
  74. auto ros2Node = ROS2Interface::Get()->GetNode();
  75. const auto cameraImagePublisherConfig = m_sensorConfiguration.m_publishersConfigurations[Internal::kImageMessageType];
  76. AZStd::string cameraImageFullTopic = ROS2Names::GetNamespacedName(GetNamespace(), cameraImagePublisherConfig.m_topic);
  77. m_imagePublisher =
  78. ros2Node->create_publisher<sensor_msgs::msg::Image>(cameraImageFullTopic.data(), cameraImagePublisherConfig.GetQoS());
  79. const auto cameraInfoPublisherConfig = m_sensorConfiguration.m_publishersConfigurations[Internal::kCameraInfoMessageType];
  80. AZStd::string cameraInfoFullTopic = ROS2Names::GetNamespacedName(GetNamespace(), cameraInfoPublisherConfig.m_topic);
  81. AZ_TracePrintf("ROS2", "Creating publisher for camera info on topic %s", cameraInfoFullTopic.data());
  82. m_cameraInfoPublisher =
  83. ros2Node->create_publisher<sensor_msgs::msg::CameraInfo>(cameraInfoFullTopic.data(), cameraInfoPublisherConfig.GetQoS());
  84. m_cameraSensor.emplace(CameraSensorDescription{ m_cameraName, m_VerticalFieldOfViewDeg, m_width, m_height });
  85. }
  86. void ROS2CameraSensorComponent::Deactivate()
  87. {
  88. m_cameraSensor.reset();
  89. ROS2SensorComponent::Deactivate();
  90. }
  91. void ROS2CameraSensorComponent::FrequencyTick()
  92. {
  93. AZ::Transform transform = GetEntity()->GetTransform()->GetWorldTM();
  94. if (!m_cameraSensor)
  95. {
  96. return;
  97. }
  98. m_cameraSensor->RequestFrame(
  99. transform,
  100. [this](const AZ::RPI::AttachmentReadback::ReadbackResult& result)
  101. {
  102. const AZ::RHI::ImageDescriptor& descriptor = result.m_imageDescriptor;
  103. AZStd::string frameName = GetEntity()->FindComponent<ROS2FrameComponent>()->GetFrameID();
  104. sensor_msgs::msg::Image message;
  105. message.encoding = sensor_msgs::image_encodings::RGBA8;
  106. message.width = descriptor.m_size.m_width;
  107. message.height = descriptor.m_size.m_height;
  108. message.data = std::vector<uint8_t>(result.m_dataBuffer->data(), result.m_dataBuffer->data() + result.m_dataBuffer->size());
  109. message.header.frame_id = frameName.c_str();
  110. m_imagePublisher->publish(message);
  111. sensor_msgs::msg::CameraInfo cameraInfo;
  112. cameraInfo.header.frame_id = frameName.c_str();
  113. cameraInfo.width = descriptor.m_size.m_width;
  114. cameraInfo.height = descriptor.m_size.m_height;
  115. cameraInfo.distortion_model = sensor_msgs::distortion_models::PLUMB_BOB;
  116. cameraInfo.k = m_cameraSensor->GetCameraSensorDescription().m_cameraIntrinsics;
  117. m_cameraInfoPublisher->publish(cameraInfo);
  118. });
  119. }
  120. } // namespace ROS2