Skip to content

Commit 163b34c

Browse files
committed
Switch to map for publishers, fixes #27
Signed-off-by: Adam Dąbrowski <[email protected]>
1 parent f513870 commit 163b34c

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

Gems/ROS2/Code/Source/Lidar/ROS2LidarSensorComponent.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
namespace ROS2
2020
{
21+
namespace Internal
22+
{
23+
const char* kPointCloudType = "sensor_msgs::msg::PointCloud2";
24+
}
25+
2126
void ROS2LidarSensorComponent::Reflect(AZ::ReflectContext* context)
2227
{
2328
if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
@@ -40,11 +45,13 @@ namespace ROS2
4045

4146
ROS2LidarSensorComponent::ROS2LidarSensorComponent()
4247
{ // TODO - replace with EditorComponent behavior
43-
PublisherConfiguration pc;
44-
pc.m_type = "sensor_msgs::msg::PointCloud2";
45-
pc.m_topic = "pc";
48+
auto pc = AZStd::make_shared<PublisherConfiguration>();
49+
auto type = Internal::kPointCloudType;
50+
pc->m_type = type;
51+
pc->m_topic = "pc";
4652
m_sensorConfiguration.m_frequency = 10; // TODO - dependent on lidar type
47-
m_sensorConfiguration.m_publishersConfigurations = { pc };
53+
54+
m_sensorConfiguration.m_publishersConfigurations.insert(AZStd::make_pair(type, pc));
4855
}
4956

5057
void ROS2LidarSensorComponent::Activate()
@@ -53,9 +60,9 @@ namespace ROS2
5360
auto ros2Node = ROS2Interface::Get()->GetNode();
5461
AZ_Assert(m_sensorConfiguration.m_publishersConfigurations.size() == 1, "Invalid configuration of publishers for lidar sensor");
5562

56-
const auto& publisherConfig = m_sensorConfiguration.m_publishersConfigurations.front();
57-
AZStd::string fullTopic = ROS2Names::GetNamespacedName(GetNamespace(), publisherConfig.m_topic);
58-
m_pointCloudPublisher = ros2Node->create_publisher<sensor_msgs::msg::PointCloud2>(fullTopic.data(), publisherConfig.m_qos);
63+
const auto publisherConfig = m_sensorConfiguration.m_publishersConfigurations[Internal::kPointCloudType];
64+
AZStd::string fullTopic = ROS2Names::GetNamespacedName(GetNamespace(), publisherConfig->m_topic);
65+
m_pointCloudPublisher = ros2Node->create_publisher<sensor_msgs::msg::PointCloud2>(fullTopic.data(), publisherConfig->m_qos);
5966
}
6067

6168
void ROS2LidarSensorComponent::Deactivate()

Gems/ROS2/Code/Source/Sensor/PublisherConfiguration.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace ROS2
1919
{
2020
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
2121
{
22-
serializeContext->RegisterGenericType<AZStd::vector<PublisherConfiguration>>();
2322
serializeContext->Class<PublisherConfiguration>()
2423
->Version(1)
2524
->Field("Type", &PublisherConfiguration::m_type)
@@ -29,8 +28,10 @@ namespace ROS2
2928

3029
if (AZ::EditContext* ec = serializeContext->GetEditContext())
3130
{
32-
ec->Class<PublisherConfiguration>("ROS2 Publisher configuration", "Publisher configuration")
31+
ec->Class<PublisherConfiguration>("Publisher configuration", "")
3332
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
33+
->DataElement(AZ::Edit::UIHandlers::Default, &PublisherConfiguration::m_type, "Type", "Type of topic messages")
34+
->Attribute(AZ::Edit::Attributes::ReadOnly, true)
3435
->DataElement(AZ::Edit::UIHandlers::Default, &PublisherConfiguration::m_topic, "Topic", "Topic with no namespace")
3536
;
3637
}

Gems/ROS2/Code/Source/Sensor/SensorConfiguration.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ namespace ROS2
1919
PublisherConfiguration::Reflect(context);
2020
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
2121
{
22+
serializeContext->RegisterGenericType<AZStd::shared_ptr<PublisherConfiguration>>();
23+
serializeContext->RegisterGenericType<AZStd::map<AZStd::string, AZStd::shared_ptr<PublisherConfiguration>>>();
2224
serializeContext->Class<SensorConfiguration>()
23-
->Version(1)
25+
->Version(2)
2426
->Field("Visualise", &SensorConfiguration::m_visualise)
2527
->Field("Publishing Enabled", &SensorConfiguration::m_publishingEnabled)
2628
->Field("Frequency (HZ)", &SensorConfiguration::m_frequency)
@@ -35,16 +37,12 @@ namespace ROS2
3537
->DataElement(AZ::Edit::UIHandlers::Default, &SensorConfiguration::m_publishingEnabled, "Publishing Enabled", "Toggle publishing for topic")
3638
->DataElement(AZ::Edit::UIHandlers::Default, &SensorConfiguration::m_frequency, "Frequency", "Frequency of publishing")
3739
->DataElement(AZ::Edit::UIHandlers::Default, &SensorConfiguration::m_publishersConfigurations, "Publishers", "Publishers")
38-
->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, false)
3940
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
40-
->Attribute(AZ::Edit::Attributes::IndexedChildNameLabelOverride, &SensorConfiguration::GetPublisherLabel)
41+
->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
42+
->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, false)
43+
->ElementAttribute(AZ::Edit::Attributes::AutoExpand, true)
4144
;
4245
}
4346
}
4447
}
45-
46-
AZStd::string SensorConfiguration::GetPublisherLabel(int index) const
47-
{
48-
return m_publishersConfigurations[index].m_type;
49-
}
5048
} // namespace ROS2

Gems/ROS2/Code/Source/Sensor/SensorConfiguration.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
#pragma once
1010

11+
#include <AzCore/Memory/SystemAllocator.h>
1112
#include <AzCore/RTTI/RTTI.h>
1213
#include <AzCore/Serialization/SerializeContext.h>
1314
#include <AzCore/std/smart_ptr/shared_ptr.h>
14-
#include <AzCore/std/containers/vector.h>
15+
#include <AzCore/std/containers/map.h>
1516
#include "PublisherConfiguration.h"
1617

1718
namespace ROS2
@@ -20,22 +21,20 @@ namespace ROS2
2021
struct SensorConfiguration
2122
{
2223
public:
24+
AZ_CLASS_ALLOCATOR(SensorConfiguration, AZ::SystemAllocator, 0);
2325
AZ_RTTI(SensorConfiguration, "{4755363D-0B5A-42D7-BBEF-152D87BA10D7}");
2426
SensorConfiguration() = default;
2527
virtual ~SensorConfiguration() = default;
2628
static void Reflect(AZ::ReflectContext* context);
2729

2830
// Will typically be 1-3 elements (3 max for known sensors).
29-
AZStd::vector<PublisherConfiguration> m_publishersConfigurations;
31+
AZStd::map<AZStd::string, AZStd::shared_ptr<PublisherConfiguration>> m_publishersConfigurations;
3032

3133
// TODO - consider moving frequency, publishingEnabled to publisherConfiguration if any sensor has
3234
// a couple of publishers for which we want different values of these fields
3335
float m_frequency = 10;
3436
bool m_publishingEnabled = true;
3537
bool m_visualise = true;
36-
37-
private:
38-
AZStd::string GetPublisherLabel(int index) const;
3938
};
4039
} // namespace ROS2
4140

Gems/ROS2/Code/Source/Utilities/ROS2Names.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace ROS2
1616
if (ns.empty())
1717
{
1818
return name;
19-
2019
}
2120

2221
return AZStd::string::format("%s/%s", ns.c_str(), name.c_str());;

0 commit comments

Comments
 (0)