|
| 1 | +#include "SplinePublisher.h" |
| 2 | + |
| 3 | +#include <ROS2/ROS2Bus.h> |
| 4 | +#include <ROS2/Utilities/ROS2Names.h> |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +namespace SplineTools |
| 8 | +{ |
| 9 | + void SplinePublisherConfiguration::Reflect(AZ::ReflectContext* context) |
| 10 | + { |
| 11 | + if (const auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context)) |
| 12 | + { |
| 13 | + serializeContext->Class<SplinePublisherConfiguration>() |
| 14 | + ->Version(0) |
| 15 | + ->Field("m_topicName", &SplinePublisherConfiguration::m_TopicConfig) |
| 16 | + ->Field("m_updateFrequency", &SplinePublisherConfiguration::m_updateFrequency); |
| 17 | + |
| 18 | + if (const auto editContext = serializeContext->GetEditContext()) |
| 19 | + { |
| 20 | + editContext |
| 21 | + ->Class<SplinePublisherConfiguration>( |
| 22 | + "SplinePublisherConfiguration", "Configuration for the SplineSubscriber component") |
| 23 | + ->ClassElement(AZ::Edit::ClassElements::Group, "SplineSubscriber Configuration") |
| 24 | + ->DataElement( |
| 25 | + AZ::Edit::UIHandlers::Default, |
| 26 | + &SplinePublisherConfiguration::m_updateFrequency, |
| 27 | + "Update Frequency", |
| 28 | + "How often path should be published (in ticks).") |
| 29 | + ->Attribute(AZ::Edit::Attributes::Min, 0.0) |
| 30 | + ->Attribute(AZ::Edit::Attributes::Step, 1.0) |
| 31 | + ->DataElement( |
| 32 | + AZ::Edit::UIHandlers::Default, &SplinePublisherConfiguration::m_TopicConfig, "Topic Config", "Topic Config"); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + void SplinePublisher::Reflect(AZ::ReflectContext* context) |
| 38 | + { |
| 39 | + SplinePublisherConfiguration::Reflect(context); |
| 40 | + if (const auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context)) |
| 41 | + { |
| 42 | + serializeContext->Class<SplinePublisher, AZ::Component>()->Version(0)->Field("m_config", &SplinePublisher::m_config); |
| 43 | + |
| 44 | + if (const auto editContext = serializeContext->GetEditContext()) |
| 45 | + { |
| 46 | + editContext->Class<SplinePublisher>("SplinePathPublisher", "Enables to publish spline as a ROS 2 path.") |
| 47 | + ->ClassElement(AZ::Edit::ClassElements::EditorData, "SplinePathPublisher") |
| 48 | + ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game")) |
| 49 | + ->Attribute(AZ::Edit::Attributes::Category, "RobotecTools") |
| 50 | + ->Attribute(AZ::Edit::Attributes::AutoExpand, true) |
| 51 | + ->DataElement( |
| 52 | + AZ::Edit::UIHandlers::Default, |
| 53 | + &SplinePublisher::m_config, |
| 54 | + "Configuration", |
| 55 | + "Configuration for the SplinePathPublisher component"); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + SplinePublisherConfiguration::SplinePublisherConfiguration() |
| 61 | + { |
| 62 | + m_TopicConfig.m_type = "nav_msgs::msg::Path"; |
| 63 | + m_TopicConfig.m_topic = "spline"; |
| 64 | + } |
| 65 | + |
| 66 | + SplinePublisher::SplinePublisher(SplinePublisherConfiguration config) |
| 67 | + : m_config(std::move(config)) |
| 68 | + { |
| 69 | + } |
| 70 | + |
| 71 | + void SplinePublisher::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) |
| 72 | + { |
| 73 | + required.push_back(AZ_CRC_CE("SplineService")); |
| 74 | + required.push_back(AZ_CRC_CE("ROS2Frame")); |
| 75 | + } |
| 76 | + |
| 77 | + void SplinePublisher::Activate() |
| 78 | + { |
| 79 | + m_ros2FramePtr = GetEntity()->FindComponent<ROS2::ROS2FrameComponent>(); |
| 80 | + if (!m_ros2FramePtr) |
| 81 | + { |
| 82 | + AZ_Warning("SplinePublisher::Activate", false, "ROS 2 frame component is not available!"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Format Ros Topic |
| 87 | + if (!m_ros2FramePtr->GetNamespace().empty()) |
| 88 | + { |
| 89 | + m_config.m_TopicConfig.m_topic = |
| 90 | + AZStd::string::format("%s/%s", m_ros2FramePtr->GetNamespace().c_str(), m_config.m_TopicConfig.m_topic.c_str()); |
| 91 | + } |
| 92 | + |
| 93 | + // Create the ROS2 Publisher |
| 94 | + if (const auto ros2Node = ROS2::ROS2Interface::Get()->GetNode()) |
| 95 | + { |
| 96 | + m_publisher = |
| 97 | + ros2Node->create_publisher<nav_msgs::msg::Path>(m_config.m_TopicConfig.m_topic.c_str(), m_config.m_TopicConfig.GetQoS()); |
| 98 | + |
| 99 | + AZ::TickBus::Handler::BusConnect(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + void SplinePublisher::Deactivate() |
| 104 | + { |
| 105 | + AZ::TickBus::Handler::BusDisconnect(); |
| 106 | + m_publisher.reset(); |
| 107 | + } |
| 108 | + |
| 109 | + void SplinePublisher::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time) |
| 110 | + { |
| 111 | + if (m_frameNumber++ == m_config.m_updateFrequency) |
| 112 | + { |
| 113 | + PublishSplineAsPath(); |
| 114 | + m_frameNumber = 0; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + void SplinePublisher::PublishSplineAsPath() const |
| 119 | + { |
| 120 | + if (!m_publisher) |
| 121 | + { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + AZ_Assert(m_ros2FramePtr, "ROS 2 frame component is not available!"); |
| 126 | + |
| 127 | + nav_msgs::msg::Path pathMessage; |
| 128 | + pathMessage.header.frame_id = m_ros2FramePtr->GetFrameID().data(); |
| 129 | + pathMessage.header.stamp = ROS2::ROS2Interface::Get()->GetROSTimestamp(); |
| 130 | + |
| 131 | + // Get Spline |
| 132 | + AZStd::shared_ptr<AZ::Spline> spline; |
| 133 | + LmbrCentral::SplineComponentRequestBus::EventResult(spline, GetEntityId(), &LmbrCentral::SplineComponentRequests::GetSpline); |
| 134 | + if (!spline) |
| 135 | + { |
| 136 | + AZ_Warning("SplinePublisher::PublishSplineAsPath", false, "Spline not found. Cannot generate spline path."); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // Get vertices from the spline |
| 141 | + const size_t vertexCount = spline->GetVertexCount(); |
| 142 | + pathMessage.poses.reserve(vertexCount); // Reserve known size |
| 143 | + |
| 144 | + for (size_t i = 0; i < vertexCount; ++i) |
| 145 | + { |
| 146 | + const AZ::Vector3& vertex = spline->GetVertex(i); |
| 147 | + |
| 148 | + // Use emplace_back to construct PoseStamped in place |
| 149 | + pathMessage.poses.emplace_back(); |
| 150 | + auto& poseStamped = pathMessage.poses.back(); |
| 151 | + |
| 152 | + // Set the pose values directly |
| 153 | + poseStamped.pose.position.x = vertex.GetX(); |
| 154 | + poseStamped.pose.position.y = vertex.GetY(); |
| 155 | + poseStamped.pose.position.z = vertex.GetZ(); |
| 156 | + } |
| 157 | + |
| 158 | + m_publisher->publish(pathMessage); |
| 159 | + } |
| 160 | +} // namespace SplineTools |
0 commit comments