ROS2SensorComponent.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <AzCore/Component/Entity.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/EditContextConstants.inl>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <ROS2/Frame/ROS2FrameComponent.h>
  13. #include <ROS2/ROS2Bus.h>
  14. #include <ROS2/ROS2GemUtilities.h>
  15. #include <ROS2/Sensor/ROS2SensorComponent.h>
  16. #include <ROS2/Utilities/ROS2Names.h>
  17. namespace ROS2
  18. {
  19. void ROS2SensorComponent::Activate()
  20. {
  21. AZ::TickBus::Handler::BusConnect();
  22. }
  23. void ROS2SensorComponent::Deactivate()
  24. {
  25. AZ::TickBus::Handler::BusDisconnect();
  26. }
  27. void ROS2SensorComponent::Reflect(AZ::ReflectContext* context)
  28. {
  29. SensorConfiguration::Reflect(context);
  30. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  31. {
  32. serialize->Class<ROS2SensorComponent, AZ::Component>()->Version(1)->Field(
  33. "SensorConfiguration", &ROS2SensorComponent::m_sensorConfiguration);
  34. if (AZ::EditContext* ec = serialize->GetEditContext())
  35. {
  36. ec->Class<ROS2SensorComponent>("ROS2 Sensor", "Base component for sensors")
  37. ->DataElement(
  38. AZ::Edit::UIHandlers::Default,
  39. &ROS2SensorComponent::m_sensorConfiguration,
  40. "Sensor configuration",
  41. "Sensor configuration");
  42. }
  43. }
  44. }
  45. AZStd::string ROS2SensorComponent::GetNamespace() const
  46. {
  47. auto* ros2Frame = Utils::GetGameOrEditorComponent<ROS2FrameComponent>(GetEntity());
  48. return ros2Frame->GetNamespace();
  49. };
  50. AZStd::string ROS2SensorComponent::GetFrameID() const
  51. {
  52. auto* ros2Frame = Utils::GetGameOrEditorComponent<ROS2FrameComponent>(GetEntity());
  53. return ros2Frame->GetFrameID();
  54. }
  55. void ROS2SensorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  56. {
  57. required.push_back(AZ_CRC_CE("ROS2Frame"));
  58. }
  59. void ROS2SensorComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  60. {
  61. Visualise(); // each frame
  62. if (!m_sensorConfiguration.m_publishingEnabled)
  63. {
  64. return;
  65. }
  66. auto frequency = m_sensorConfiguration.m_frequency;
  67. auto frameTime = frequency == 0 ? 1 : 1 / frequency;
  68. m_timeElapsedSinceLastTick += deltaTime;
  69. if (m_timeElapsedSinceLastTick < frameTime)
  70. return;
  71. m_timeElapsedSinceLastTick -= frameTime;
  72. if (deltaTime > frameTime)
  73. { // Frequency higher than possible, not catching up, just keep going with each frame.
  74. m_timeElapsedSinceLastTick = 0.0f;
  75. }
  76. // Note that sensor frequency can be limited by simulation tick rate (if higher sensor Hz is desired).
  77. FrequencyTick();
  78. }
  79. } // namespace ROS2