ROS2CameraSensorComponent.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "ROS2CameraSensorComponent.h"
  9. #include "CameraUtilities.h"
  10. #include <ROS2/Frame/ROS2FrameComponent.h>
  11. namespace ROS2Sensors
  12. {
  13. ROS2CameraSensorComponent::ROS2CameraSensorComponent(
  14. const ROS2::SensorConfiguration& sensorConfiguration, const CameraSensorConfiguration& cameraConfiguration)
  15. : m_cameraConfiguration(cameraConfiguration)
  16. {
  17. m_sensorConfiguration = sensorConfiguration;
  18. }
  19. void ROS2CameraSensorComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. CameraSensorConfiguration::Reflect(context);
  22. auto* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  23. if (serialize)
  24. {
  25. serialize->Class<ROS2CameraSensorComponent, SensorBaseType>()->Version(5)->Field(
  26. "CameraSensorConfig", &ROS2CameraSensorComponent::m_cameraConfiguration);
  27. }
  28. }
  29. void ROS2CameraSensorComponent::Activate()
  30. {
  31. ROS2SensorComponentBase::Activate();
  32. SetCameraSensorConfiguration();
  33. const auto* component = GetEntity()->FindComponent<ROS2::ROS2FrameComponent>();
  34. AZ_Assert(component, "Entity has no ROS2FrameComponent");
  35. m_frameName = component->GetFrameID();
  36. CameraConfigurationRequestBus::Handler::BusConnect(GetEntityId());
  37. StartSensor(
  38. m_sensorConfiguration.m_frequency,
  39. [this]([[maybe_unused]] auto&&... args)
  40. {
  41. if (!m_sensorConfiguration.m_publishingEnabled)
  42. {
  43. return;
  44. }
  45. FrequencyTick();
  46. });
  47. }
  48. void ROS2CameraSensorComponent::Deactivate()
  49. {
  50. StopSensor();
  51. m_cameraSensor.reset();
  52. CameraConfigurationRequestBus::Handler::BusDisconnect(GetEntityId());
  53. ROS2SensorComponentBase::Deactivate();
  54. }
  55. void ROS2CameraSensorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  56. {
  57. required.push_back(AZ_CRC("ROS2Frame"));
  58. }
  59. void ROS2CameraSensorComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  60. {
  61. incompatible.push_back(AZ_CRC_CE("ROS2CameraSensor"));
  62. }
  63. void ROS2CameraSensorComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  64. {
  65. provided.push_back(AZ_CRC_CE("ROS2CameraSensor"));
  66. }
  67. void ROS2CameraSensorComponent::FrequencyTick()
  68. {
  69. if (!m_cameraSensor)
  70. {
  71. return;
  72. }
  73. const AZ::Transform& transform = GetEntity()->GetTransform()->GetWorldTM();
  74. const auto timestamp = ROS2::ROS2Interface::Get()->GetROSTimestamp();
  75. std_msgs::msg::Header messageHeader;
  76. messageHeader.stamp = timestamp;
  77. messageHeader.frame_id = m_frameName.c_str();
  78. m_cameraSensor->RequestMessagePublication(transform, messageHeader);
  79. }
  80. AZStd::string ROS2CameraSensorComponent::GetCameraNameFromFrame(const AZ::Entity* entity) const
  81. {
  82. const auto* component = GetEntity()->FindComponent<ROS2::ROS2FrameComponent>();
  83. AZ_Assert(component, "Entity %s has no ROS2CameraSensorComponent", entity->GetName().c_str());
  84. if (component)
  85. {
  86. AZStd::string cameraName = component->GetFrameID();
  87. AZStd::replace(cameraName.begin(), cameraName.end(), '/', '_');
  88. return cameraName;
  89. }
  90. return AZStd::string{};
  91. }
  92. void ROS2CameraSensorComponent::SetCameraSensorConfiguration()
  93. {
  94. if (m_cameraConfiguration.m_colorCamera && m_cameraConfiguration.m_depthCamera)
  95. {
  96. SetImageSource<CameraRGBDSensor>();
  97. }
  98. else if (m_cameraConfiguration.m_colorCamera)
  99. {
  100. SetImageSource<CameraColorSensor>();
  101. }
  102. else if (m_cameraConfiguration.m_depthCamera)
  103. {
  104. SetImageSource<CameraDepthSensor>();
  105. }
  106. }
  107. void ROS2CameraSensorComponent::SetConfiguration(const CameraSensorConfiguration& configuration)
  108. {
  109. m_cameraConfiguration = configuration;
  110. m_cameraSensor.reset();
  111. // SetCameraSensorConfiguration() is called in the next tick to ensure that the camera sensor is
  112. // reset.
  113. AZ::SystemTickBus::QueueFunction(
  114. [this]()
  115. {
  116. SetCameraSensorConfiguration();
  117. });
  118. }
  119. const CameraSensorConfiguration ROS2CameraSensorComponent::GetConfiguration()
  120. {
  121. return m_cameraConfiguration;
  122. }
  123. AZ::Matrix3x3 ROS2CameraSensorComponent::GetCameraMatrix()
  124. {
  125. return CameraUtils::MakeCameraIntrinsics(
  126. m_cameraConfiguration.m_width, m_cameraConfiguration.m_height, m_cameraConfiguration.m_verticalFieldOfViewDeg);
  127. }
  128. float ROS2CameraSensorComponent::GetVerticalFOV()
  129. {
  130. return m_cameraConfiguration.m_verticalFieldOfViewDeg;
  131. }
  132. void ROS2CameraSensorComponent::SetVerticalFOV(float value)
  133. {
  134. if (value < CameraSensorConfiguration::m_minVerticalFieldOfViewDeg ||
  135. value > CameraSensorConfiguration::m_maxVerticalFieldOfViewDeg)
  136. {
  137. AZ_Warning(
  138. "ROS2CameraSensorComponent",
  139. false,
  140. "Vertical field of view value %f is out of bounds [%f, %f].",
  141. value,
  142. CameraSensorConfiguration::m_minVerticalFieldOfViewDeg,
  143. CameraSensorConfiguration::m_maxVerticalFieldOfViewDeg);
  144. return;
  145. }
  146. auto cameraConfiguration = m_cameraConfiguration;
  147. cameraConfiguration.m_verticalFieldOfViewDeg = value;
  148. SetConfiguration(cameraConfiguration);
  149. }
  150. int ROS2CameraSensorComponent::GetWidth()
  151. {
  152. return m_cameraConfiguration.m_width;
  153. }
  154. void ROS2CameraSensorComponent::SetWidth(int value)
  155. {
  156. if (value < CameraSensorConfiguration::m_minWidth)
  157. {
  158. AZ_Warning(
  159. "ROS2CameraSensorComponent",
  160. false,
  161. "Width value %d is less than the minimum allowed width %d.",
  162. value,
  163. CameraSensorConfiguration::m_minWidth);
  164. return;
  165. }
  166. auto cameraConfiguration = m_cameraConfiguration;
  167. cameraConfiguration.m_width = value;
  168. SetConfiguration(cameraConfiguration);
  169. }
  170. int ROS2CameraSensorComponent::GetHeight()
  171. {
  172. return m_cameraConfiguration.m_height;
  173. }
  174. void ROS2CameraSensorComponent::SetHeight(int value)
  175. {
  176. if (value < CameraSensorConfiguration::m_minHeight)
  177. {
  178. AZ_Warning(
  179. "ROS2CameraSensorComponent",
  180. false,
  181. "Height value %d is less than the minimum allowed height %d.",
  182. value,
  183. CameraSensorConfiguration::m_minHeight);
  184. return;
  185. }
  186. auto cameraConfiguration = m_cameraConfiguration;
  187. cameraConfiguration.m_height = value;
  188. SetConfiguration(cameraConfiguration);
  189. }
  190. bool ROS2CameraSensorComponent::IsColorCamera()
  191. {
  192. return m_cameraConfiguration.m_colorCamera;
  193. }
  194. void ROS2CameraSensorComponent::SetColorCamera(bool value)
  195. {
  196. auto cameraConfiguration = m_cameraConfiguration;
  197. m_cameraConfiguration.m_colorCamera = value;
  198. SetConfiguration(cameraConfiguration);
  199. }
  200. bool ROS2CameraSensorComponent::IsDepthCamera()
  201. {
  202. return m_cameraConfiguration.m_depthCamera;
  203. }
  204. void ROS2CameraSensorComponent::SetDepthCamera(bool value)
  205. {
  206. auto cameraConfiguration = m_cameraConfiguration;
  207. cameraConfiguration.m_depthCamera = value;
  208. SetConfiguration(cameraConfiguration);
  209. }
  210. float ROS2CameraSensorComponent::GetNearClipDistance()
  211. {
  212. return m_cameraConfiguration.m_nearClipDistance;
  213. }
  214. void ROS2CameraSensorComponent::SetNearClipDistance(float value)
  215. {
  216. auto cameraConfiguration = m_cameraConfiguration;
  217. cameraConfiguration.m_nearClipDistance = value;
  218. SetConfiguration(cameraConfiguration);
  219. }
  220. float ROS2CameraSensorComponent::GetFarClipDistance()
  221. {
  222. return m_cameraConfiguration.m_farClipDistance;
  223. }
  224. void ROS2CameraSensorComponent::SetFarClipDistance(float value)
  225. {
  226. auto cameraConfiguration = m_cameraConfiguration;
  227. cameraConfiguration.m_farClipDistance = value;
  228. SetConfiguration(cameraConfiguration);
  229. }
  230. } // namespace ROS2Sensors