ROS2CameraSensorComponent.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "CameraSensorConfiguration.h"
  16. #include <ROS2/ROS2Bus.h>
  17. #include <ROS2/Sensor/Events/TickBasedSource.h>
  18. #include <ROS2Sensors/Camera/CameraCalibrationRequestBus.h>
  19. #include <ROS2Sensors/ROS2SensorsTypeIds.h>
  20. #include <ROS2Sensors/Sensor/ROS2SensorComponentBase.h>
  21. namespace ROS2
  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 ROS2SensorComponentBase<TickBasedSource>
  32. , public CameraCalibrationRequestBus::Handler
  33. {
  34. public:
  35. ROS2CameraSensorComponent() = default;
  36. ROS2CameraSensorComponent(const SensorConfiguration& sensorConfiguration, const CameraSensorConfiguration& cameraConfiguration);
  37. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  38. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  39. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  40. ~ROS2CameraSensorComponent() override = default;
  41. AZ_COMPONENT(ROS2CameraSensorComponent, ROS2Sensors::ROS2CameraSensorComponentTypeId, SensorBaseType);
  42. static void Reflect(AZ::ReflectContext* context);
  43. // AzToolsFramework::Components::EditorComponentBase overrides ..
  44. void Activate() override;
  45. void Deactivate() override;
  46. // CameraCalibrationRequestBus::Handler overrides ...
  47. AZ::Matrix3x3 GetCameraMatrix() const override;
  48. int GetWidth() const override;
  49. int GetHeight() const override;
  50. float GetVerticalFOV() const override;
  51. private:
  52. //! Helper that adds an image source.
  53. //! @tparam CameraType type of camera sensor (eg 'CameraColorSensor')
  54. template<typename CameraType>
  55. void SetImageSource()
  56. {
  57. const auto cameraName = GetCameraNameFromFrame(GetEntity());
  58. const CameraSensorDescription description{ cameraName, GetNamespace(), m_cameraConfiguration, m_sensorConfiguration };
  59. m_cameraSensor = AZStd::make_shared<CameraType>(description, GetEntityId());
  60. }
  61. //! Retrieve camera name from ROS2FrameComponent's FrameID.
  62. //! @param entity pointer entity that has ROS2FrameComponent.
  63. [[nodiscard]] AZStd::string GetCameraNameFromFrame(const AZ::Entity* entity) const;
  64. ///! Requests message publication from camera sensor.
  65. void FrequencyTick();
  66. CameraSensorConfiguration m_cameraConfiguration;
  67. AZStd::string m_frameName;
  68. AZStd::shared_ptr<CameraSensor> m_cameraSensor;
  69. };
  70. } // namespace ROS2