ROS2GNSSSensorComponent.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "ROS2GNSSSensorComponent.h"
  9. #include <AzCore/Math/Matrix4x4.h>
  10. #include <ROS2/Utilities/ROS2Names.h>
  11. #include <Georeferencing/GeoreferenceBus.h>
  12. #include <ROS2Sensors/GNSS/GNSSPostProcessingRequestBus.h>
  13. namespace ROS2Sensors
  14. {
  15. namespace
  16. {
  17. const char* GNSSMsgType = "sensor_msgs::msg::NavSatFix";
  18. }
  19. void ROS2GNSSSensorComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serialize->Class<ROS2GNSSSensorComponent, SensorBaseType>()->Version(4);
  24. if (auto* editContext = serialize->GetEditContext())
  25. {
  26. editContext->Class<ROS2GNSSSensorComponent>("ROS2 GNSS Sensor", "GNSS sensor component")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  29. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  30. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/ROS2GNSSSensor.svg")
  31. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/ROS2GNSSSensor.svg")
  32. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly);
  33. }
  34. }
  35. }
  36. ROS2GNSSSensorComponent::ROS2GNSSSensorComponent()
  37. {
  38. ROS2::TopicConfiguration pc;
  39. pc.m_type = GNSSMsgType;
  40. pc.m_topic = "gnss";
  41. m_sensorConfiguration.m_frequency = 10;
  42. m_sensorConfiguration.m_publishersConfigurations.insert(AZStd::make_pair(GNSSMsgType, pc));
  43. }
  44. ROS2GNSSSensorComponent::ROS2GNSSSensorComponent(const ROS2::SensorConfiguration& sensorConfiguration)
  45. {
  46. m_sensorConfiguration = sensorConfiguration;
  47. }
  48. void ROS2GNSSSensorComponent::Activate()
  49. {
  50. ROS2SensorComponentBase::Activate();
  51. auto ros2Node = ROS2::ROS2Interface::Get()->GetNode();
  52. AZ_Assert(m_sensorConfiguration.m_publishersConfigurations.size() == 1, "Invalid configuration of publishers for GNSS sensor");
  53. const auto publisherConfig = m_sensorConfiguration.m_publishersConfigurations[GNSSMsgType];
  54. const auto fullTopic = ROS2::ROS2Names::GetNamespacedName(GetNamespace(), publisherConfig.m_topic);
  55. m_gnssPublisher = ros2Node->create_publisher<sensor_msgs::msg::NavSatFix>(fullTopic.data(), publisherConfig.GetQoS());
  56. m_gnssMsg.header.frame_id = "gnss_frame_id";
  57. StartSensor(
  58. m_sensorConfiguration.m_frequency,
  59. [this]([[maybe_unused]] auto&&... args)
  60. {
  61. if (!m_sensorConfiguration.m_publishingEnabled)
  62. {
  63. return;
  64. }
  65. FrequencyTick();
  66. });
  67. }
  68. void ROS2GNSSSensorComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  69. {
  70. provided.push_back(AZ_CRC_CE("ROS2GNSSSensor"));
  71. }
  72. void ROS2GNSSSensorComponent::Deactivate()
  73. {
  74. StopSensor();
  75. m_gnssPublisher.reset();
  76. ROS2SensorComponentBase::Deactivate();
  77. }
  78. void ROS2GNSSSensorComponent::FrequencyTick()
  79. {
  80. using namespace Georeferencing;
  81. AZ::Vector3 currentPosition{ 0.0f };
  82. AZ::TransformBus::EventResult(currentPosition, GetEntityId(), &AZ::TransformBus::Events::GetWorldTranslation);
  83. WGS::WGS84Coordinate currentPositionWGS84;
  84. GeoreferenceRequestsBus::BroadcastResult(currentPositionWGS84, &GeoreferenceRequests::ConvertFromLevelToWGS84, currentPosition);
  85. m_gnssMsg.latitude = currentPositionWGS84.m_latitude;
  86. m_gnssMsg.longitude = currentPositionWGS84.m_longitude;
  87. m_gnssMsg.altitude = currentPositionWGS84.m_altitude;
  88. m_gnssMsg.status.status = sensor_msgs::msg::NavSatStatus::STATUS_SBAS_FIX;
  89. m_gnssMsg.status.service = sensor_msgs::msg::NavSatStatus::SERVICE_GPS;
  90. GNSSPostProcessingRequestBus::Event(GetEntityId(), &GNSSPostProcessingRequests::ApplyPostProcessing, m_gnssMsg);
  91. m_gnssPublisher->publish(m_gnssMsg);
  92. }
  93. } // namespace ROS2Sensors