From 163b34cb0f3cc33e3f2fbdb94dab8aaa9e34545a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20D=C4=85browski?= Date: Wed, 27 Apr 2022 12:37:30 +0200 Subject: [PATCH] Switch to map for publishers, fixes #27 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adam DÄ…browski --- .../Source/Lidar/ROS2LidarSensorComponent.cpp | 21 ++++++++++++------- .../Source/Sensor/PublisherConfiguration.cpp | 5 +++-- .../Source/Sensor/SensorConfiguration.cpp | 14 ++++++------- .../Code/Source/Sensor/SensorConfiguration.h | 9 ++++---- Gems/ROS2/Code/Source/Utilities/ROS2Names.cpp | 1 - 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Gems/ROS2/Code/Source/Lidar/ROS2LidarSensorComponent.cpp b/Gems/ROS2/Code/Source/Lidar/ROS2LidarSensorComponent.cpp index 5a0be49ab..0319a1cdd 100644 --- a/Gems/ROS2/Code/Source/Lidar/ROS2LidarSensorComponent.cpp +++ b/Gems/ROS2/Code/Source/Lidar/ROS2LidarSensorComponent.cpp @@ -18,6 +18,11 @@ namespace ROS2 { + namespace Internal + { + const char* kPointCloudType = "sensor_msgs::msg::PointCloud2"; + } + void ROS2LidarSensorComponent::Reflect(AZ::ReflectContext* context) { if (AZ::SerializeContext* serialize = azrtti_cast(context)) @@ -40,11 +45,13 @@ namespace ROS2 ROS2LidarSensorComponent::ROS2LidarSensorComponent() { // TODO - replace with EditorComponent behavior - PublisherConfiguration pc; - pc.m_type = "sensor_msgs::msg::PointCloud2"; - pc.m_topic = "pc"; + auto pc = AZStd::make_shared(); + auto type = Internal::kPointCloudType; + pc->m_type = type; + pc->m_topic = "pc"; m_sensorConfiguration.m_frequency = 10; // TODO - dependent on lidar type - m_sensorConfiguration.m_publishersConfigurations = { pc }; + + m_sensorConfiguration.m_publishersConfigurations.insert(AZStd::make_pair(type, pc)); } void ROS2LidarSensorComponent::Activate() @@ -53,9 +60,9 @@ namespace ROS2 auto ros2Node = ROS2Interface::Get()->GetNode(); AZ_Assert(m_sensorConfiguration.m_publishersConfigurations.size() == 1, "Invalid configuration of publishers for lidar sensor"); - const auto& publisherConfig = m_sensorConfiguration.m_publishersConfigurations.front(); - AZStd::string fullTopic = ROS2Names::GetNamespacedName(GetNamespace(), publisherConfig.m_topic); - m_pointCloudPublisher = ros2Node->create_publisher(fullTopic.data(), publisherConfig.m_qos); + const auto publisherConfig = m_sensorConfiguration.m_publishersConfigurations[Internal::kPointCloudType]; + AZStd::string fullTopic = ROS2Names::GetNamespacedName(GetNamespace(), publisherConfig->m_topic); + m_pointCloudPublisher = ros2Node->create_publisher(fullTopic.data(), publisherConfig->m_qos); } void ROS2LidarSensorComponent::Deactivate() diff --git a/Gems/ROS2/Code/Source/Sensor/PublisherConfiguration.cpp b/Gems/ROS2/Code/Source/Sensor/PublisherConfiguration.cpp index acc82540b..4782a06e9 100644 --- a/Gems/ROS2/Code/Source/Sensor/PublisherConfiguration.cpp +++ b/Gems/ROS2/Code/Source/Sensor/PublisherConfiguration.cpp @@ -19,7 +19,6 @@ namespace ROS2 { if (auto serializeContext = azrtti_cast(context)) { - serializeContext->RegisterGenericType>(); serializeContext->Class() ->Version(1) ->Field("Type", &PublisherConfiguration::m_type) @@ -29,8 +28,10 @@ namespace ROS2 if (AZ::EditContext* ec = serializeContext->GetEditContext()) { - ec->Class("ROS2 Publisher configuration", "Publisher configuration") + ec->Class("Publisher configuration", "") ->ClassElement(AZ::Edit::ClassElements::EditorData, "") + ->DataElement(AZ::Edit::UIHandlers::Default, &PublisherConfiguration::m_type, "Type", "Type of topic messages") + ->Attribute(AZ::Edit::Attributes::ReadOnly, true) ->DataElement(AZ::Edit::UIHandlers::Default, &PublisherConfiguration::m_topic, "Topic", "Topic with no namespace") ; } diff --git a/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.cpp b/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.cpp index 2905a4fc0..20704268d 100644 --- a/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.cpp +++ b/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.cpp @@ -19,8 +19,10 @@ namespace ROS2 PublisherConfiguration::Reflect(context); if (auto serializeContext = azrtti_cast(context)) { + serializeContext->RegisterGenericType>(); + serializeContext->RegisterGenericType>>(); serializeContext->Class() - ->Version(1) + ->Version(2) ->Field("Visualise", &SensorConfiguration::m_visualise) ->Field("Publishing Enabled", &SensorConfiguration::m_publishingEnabled) ->Field("Frequency (HZ)", &SensorConfiguration::m_frequency) @@ -35,16 +37,12 @@ namespace ROS2 ->DataElement(AZ::Edit::UIHandlers::Default, &SensorConfiguration::m_publishingEnabled, "Publishing Enabled", "Toggle publishing for topic") ->DataElement(AZ::Edit::UIHandlers::Default, &SensorConfiguration::m_frequency, "Frequency", "Frequency of publishing") ->DataElement(AZ::Edit::UIHandlers::Default, &SensorConfiguration::m_publishersConfigurations, "Publishers", "Publishers") - ->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, false) ->Attribute(AZ::Edit::Attributes::AutoExpand, true) - ->Attribute(AZ::Edit::Attributes::IndexedChildNameLabelOverride, &SensorConfiguration::GetPublisherLabel) + ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly) + ->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, false) + ->ElementAttribute(AZ::Edit::Attributes::AutoExpand, true) ; } } } - - AZStd::string SensorConfiguration::GetPublisherLabel(int index) const - { - return m_publishersConfigurations[index].m_type; - } } // namespace ROS2 diff --git a/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.h b/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.h index cc7afac1a..6f67782af 100644 --- a/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.h +++ b/Gems/ROS2/Code/Source/Sensor/SensorConfiguration.h @@ -8,10 +8,11 @@ #pragma once +#include #include #include #include -#include +#include #include "PublisherConfiguration.h" namespace ROS2 @@ -20,22 +21,20 @@ namespace ROS2 struct SensorConfiguration { public: + AZ_CLASS_ALLOCATOR(SensorConfiguration, AZ::SystemAllocator, 0); AZ_RTTI(SensorConfiguration, "{4755363D-0B5A-42D7-BBEF-152D87BA10D7}"); SensorConfiguration() = default; virtual ~SensorConfiguration() = default; static void Reflect(AZ::ReflectContext* context); // Will typically be 1-3 elements (3 max for known sensors). - AZStd::vector m_publishersConfigurations; + AZStd::map> m_publishersConfigurations; // TODO - consider moving frequency, publishingEnabled to publisherConfiguration if any sensor has // a couple of publishers for which we want different values of these fields float m_frequency = 10; bool m_publishingEnabled = true; bool m_visualise = true; - - private: - AZStd::string GetPublisherLabel(int index) const; }; } // namespace ROS2 diff --git a/Gems/ROS2/Code/Source/Utilities/ROS2Names.cpp b/Gems/ROS2/Code/Source/Utilities/ROS2Names.cpp index 6e2bb10ee..ad65d9b5a 100644 --- a/Gems/ROS2/Code/Source/Utilities/ROS2Names.cpp +++ b/Gems/ROS2/Code/Source/Utilities/ROS2Names.cpp @@ -16,7 +16,6 @@ namespace ROS2 if (ns.empty()) { return name; - } return AZStd::string::format("%s/%s", ns.c_str(), name.c_str());;