ROS2CameraSensorComponent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #pragma once
  9. #include <rclcpp/publisher.hpp>
  10. #include <sensor_msgs/msg/camera_info.hpp>
  11. #include <sensor_msgs/msg/image.hpp>
  12. #include <AzCore/Component/Component.h>
  13. #include <AzCore/std/containers/vector.h>
  14. #include "CameraSensor.h"
  15. #include <ROS2/ROS2Bus.h>
  16. #include <ROS2/Sensor/Events/TickBasedSource.h>
  17. #include <ROS2/Sensor/ROS2SensorComponentBase.h>
  18. #include <ROS2Sensors/Camera/CameraConfigurationRequestBus.h>
  19. #include <ROS2Sensors/Camera/CameraSensorConfiguration.h>
  20. #include <ROS2Sensors/ROS2SensorsTypeIds.h>
  21. namespace ROS2Sensors
  22. {
  23. //! ROS2 Camera sensor component class
  24. //! Allows turning an entity into a camera sensor
  25. //! Can be parametrized with following values:
  26. //! - camera name
  27. //! - camera image width and height in pixels
  28. //! - camera vertical field of view in degrees
  29. //! Camera frustum is facing negative Z axis; image plane is parallel to X,Y plane: X - right, Y - up
  30. class ROS2CameraSensorComponent
  31. : public ROS2::ROS2SensorComponentBase<ROS2::TickBasedSource>
  32. , protected CameraConfigurationRequestBus::Handler
  33. {
  34. public:
  35. using SensorBaseType = ROS2::ROS2SensorComponentBase<ROS2::TickBasedSource>;
  36. ROS2CameraSensorComponent() = default;
  37. ROS2CameraSensorComponent(
  38. const ROS2::SensorConfiguration& sensorConfiguration, const CameraSensorConfiguration& cameraConfiguration);
  39. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  40. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  41. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  42. ~ROS2CameraSensorComponent() override = default;
  43. AZ_COMPONENT(ROS2CameraSensorComponent, ROS2CameraSensorComponentTypeId, SensorBaseType);
  44. static void Reflect(AZ::ReflectContext* context);
  45. // AzToolsFramework::Components::EditorComponentBase overrides ..
  46. void Activate() override;
  47. void Deactivate() override;
  48. private:
  49. // CameraConfigurationRequestBus::Handler overrides ..
  50. void SetConfiguration(const CameraSensorConfiguration& configuration) override;
  51. const CameraSensorConfiguration GetConfiguration() override;
  52. AZ::Matrix3x3 GetCameraMatrix() override;
  53. float GetVerticalFOV() override;
  54. void SetVerticalFOV(float value) override;
  55. int GetWidth() override;
  56. void SetWidth(int value) override;
  57. int GetHeight() override;
  58. void SetHeight(int value) override;
  59. bool IsColorCamera() override;
  60. void SetColorCamera(bool value) override;
  61. bool IsDepthCamera() override;
  62. void SetDepthCamera(bool value) override;
  63. float GetNearClipDistance() override;
  64. void SetNearClipDistance(float value) override;
  65. float GetFarClipDistance() override;
  66. void SetFarClipDistance(float value) override;
  67. //! Helper that adds an image source.
  68. //! @tparam CameraType type of camera sensor (eg 'CameraColorSensor')
  69. template<typename CameraType>
  70. void SetImageSource()
  71. {
  72. const auto cameraName = GetCameraNameFromFrame(GetEntity());
  73. const CameraSensorDescription description{ cameraName, GetNamespace(), m_cameraConfiguration, m_sensorConfiguration };
  74. m_cameraSensor = AZStd::make_shared<CameraType>(description, GetEntityId());
  75. }
  76. //! Retrieve camera name from ROS2FrameComponent's FrameID.
  77. //! @param entity pointer entity that has ROS2FrameComponent.
  78. [[nodiscard]] AZStd::string GetCameraNameFromFrame(const AZ::Entity* entity) const;
  79. ///! Requests message publication from camera sensor.
  80. void FrequencyTick();
  81. //! Sets the camera sensor and image source.
  82. void SetCameraSensorConfiguration();
  83. CameraSensorConfiguration m_cameraConfiguration;
  84. AZStd::string m_frameName;
  85. AZStd::shared_ptr<CameraSensor> m_cameraSensor;
  86. };
  87. } // namespace ROS2Sensors