-
Notifications
You must be signed in to change notification settings - Fork 3
Add Spline Path Publisher Component #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6470d8a
init spline subscriber
w-czerski 6ccf97e
clang format
w-czerski 915d029
fix | get spline and convert into vector of vertex
w-czerski 1b9affd
remove auto
w-czerski 57322f8
apply review changes
w-czerski ee5c565
remove comments
w-czerski 399c2d1
use emplace | format | apply reviews
w-czerski 5b7dd26
change ros2 -> ROS 2
w-czerski 3fc175d
review resolved | topic changed to spline
w-czerski 9cf218a
review resolved | use proper ROS 2 naming
w-czerski 4651355
get ros2frame outside tick
w-czerski 45bd34c
add update frequency
w-czerski ffa7f34
add readme info | add update frequency
w-czerski 23e9891
add readme info | add update frequency
w-czerski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
Gems/RobotecSplineTools/Code/Source/Clients/SplinePublisher.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#include "SplinePublisher.h" | ||
|
||
#include <ROS2/ROS2Bus.h> | ||
#include <ROS2/Utilities/ROS2Names.h> | ||
#include <utility> | ||
|
||
namespace SplineTools | ||
{ | ||
void SplinePublisherConfiguration::Reflect(AZ::ReflectContext* context) | ||
{ | ||
if (const auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context)) | ||
{ | ||
serializeContext->Class<SplinePublisherConfiguration>() | ||
->Version(0) | ||
->Field("m_topicName", &SplinePublisherConfiguration::m_TopicConfig) | ||
->Field("m_updateFrequency", &SplinePublisherConfiguration::m_updateFrequency); | ||
|
||
if (const auto editContext = serializeContext->GetEditContext()) | ||
{ | ||
editContext | ||
->Class<SplinePublisherConfiguration>( | ||
"SplinePublisherConfiguration", "Configuration for the SplineSubscriber component") | ||
->ClassElement(AZ::Edit::ClassElements::Group, "SplineSubscriber Configuration") | ||
->DataElement( | ||
AZ::Edit::UIHandlers::Default, | ||
&SplinePublisherConfiguration::m_updateFrequency, | ||
"Update Frequency", | ||
"How often path should be published (in ticks).") | ||
->Attribute(AZ::Edit::Attributes::Min, 0.0) | ||
->Attribute(AZ::Edit::Attributes::Step, 1.0) | ||
->DataElement( | ||
AZ::Edit::UIHandlers::Default, &SplinePublisherConfiguration::m_TopicConfig, "Topic Config", "Topic Config"); | ||
} | ||
} | ||
} | ||
|
||
void SplinePublisher::Reflect(AZ::ReflectContext* context) | ||
{ | ||
SplinePublisherConfiguration::Reflect(context); | ||
if (const auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context)) | ||
{ | ||
serializeContext->Class<SplinePublisher, AZ::Component>()->Version(0)->Field("m_config", &SplinePublisher::m_config); | ||
|
||
if (const auto editContext = serializeContext->GetEditContext()) | ||
{ | ||
editContext->Class<SplinePublisher>("SplinePathPublisher", "Enables to publish spline as a ROS 2 path.") | ||
->ClassElement(AZ::Edit::ClassElements::EditorData, "SplinePathPublisher") | ||
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game")) | ||
->Attribute(AZ::Edit::Attributes::Category, "RobotecTools") | ||
->Attribute(AZ::Edit::Attributes::AutoExpand, true) | ||
->DataElement( | ||
AZ::Edit::UIHandlers::Default, | ||
&SplinePublisher::m_config, | ||
"Configuration", | ||
"Configuration for the SplinePathPublisher component"); | ||
} | ||
} | ||
} | ||
|
||
SplinePublisherConfiguration::SplinePublisherConfiguration() | ||
{ | ||
m_TopicConfig.m_type = "nav_msgs::msg::Path"; | ||
m_TopicConfig.m_topic = "spline"; | ||
} | ||
|
||
SplinePublisher::SplinePublisher(SplinePublisherConfiguration config) | ||
: m_config(std::move(config)) | ||
{ | ||
} | ||
|
||
void SplinePublisher::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required) | ||
{ | ||
required.push_back(AZ_CRC_CE("SplineService")); | ||
required.push_back(AZ_CRC_CE("ROS2Frame")); | ||
} | ||
|
||
void SplinePublisher::Activate() | ||
{ | ||
m_ros2FramePtr = GetEntity()->FindComponent<ROS2::ROS2FrameComponent>(); | ||
if (!m_ros2FramePtr) | ||
{ | ||
AZ_Warning("SplinePublisher::Activate", false, "ROS 2 frame component is not available!"); | ||
return; | ||
} | ||
|
||
// Format Ros Topic | ||
if (!m_ros2FramePtr->GetNamespace().empty()) | ||
{ | ||
m_config.m_TopicConfig.m_topic = | ||
AZStd::string::format("%s/%s", m_ros2FramePtr->GetNamespace().c_str(), m_config.m_TopicConfig.m_topic.c_str()); | ||
} | ||
|
||
// Create the ROS2 Publisher | ||
if (const auto ros2Node = ROS2::ROS2Interface::Get()->GetNode()) | ||
{ | ||
m_publisher = | ||
ros2Node->create_publisher<nav_msgs::msg::Path>(m_config.m_TopicConfig.m_topic.c_str(), m_config.m_TopicConfig.GetQoS()); | ||
|
||
AZ::TickBus::Handler::BusConnect(); | ||
} | ||
} | ||
|
||
void SplinePublisher::Deactivate() | ||
{ | ||
AZ::TickBus::Handler::BusDisconnect(); | ||
m_publisher.reset(); | ||
} | ||
|
||
void SplinePublisher::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time) | ||
{ | ||
if (m_frameNumber++ == m_config.m_updateFrequency) | ||
{ | ||
PublishSplineAsPath(); | ||
m_frameNumber = 0; | ||
} | ||
} | ||
|
||
void SplinePublisher::PublishSplineAsPath() const | ||
{ | ||
if (!m_publisher) | ||
{ | ||
return; | ||
} | ||
|
||
AZ_Assert(m_ros2FramePtr, "ROS 2 frame component is not available!"); | ||
|
||
nav_msgs::msg::Path pathMessage; | ||
pathMessage.header.frame_id = m_ros2FramePtr->GetFrameID().data(); | ||
pathMessage.header.stamp = ROS2::ROS2Interface::Get()->GetROSTimestamp(); | ||
|
||
// Get Spline | ||
AZStd::shared_ptr<AZ::Spline> spline; | ||
LmbrCentral::SplineComponentRequestBus::EventResult(spline, GetEntityId(), &LmbrCentral::SplineComponentRequests::GetSpline); | ||
if (!spline) | ||
{ | ||
AZ_Warning("SplinePublisher::PublishSplineAsPath", false, "Spline not found. Cannot generate spline path."); | ||
return; | ||
} | ||
|
||
// Get vertices from the spline | ||
const size_t vertexCount = spline->GetVertexCount(); | ||
pathMessage.poses.reserve(vertexCount); // Reserve known size | ||
|
||
for (size_t i = 0; i < vertexCount; ++i) | ||
{ | ||
const AZ::Vector3& vertex = spline->GetVertex(i); | ||
|
||
// Use emplace_back to construct PoseStamped in place | ||
pathMessage.poses.emplace_back(); | ||
auto& poseStamped = pathMessage.poses.back(); | ||
|
||
// Set the pose values directly | ||
poseStamped.pose.position.x = vertex.GetX(); | ||
poseStamped.pose.position.y = vertex.GetY(); | ||
poseStamped.pose.position.z = vertex.GetZ(); | ||
} | ||
|
||
m_publisher->publish(pathMessage); | ||
} | ||
} // namespace SplineTools |
56 changes: 56 additions & 0 deletions
56
Gems/RobotecSplineTools/Code/Source/Clients/SplinePublisher.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include <AzCore/Component/Component.h> | ||
#include <AzCore/Serialization/SerializeContext.h> | ||
#include <AzFramework/Components/TransformComponent.h> | ||
#include <LmbrCentral/Shape/SplineComponentBus.h> | ||
#include <ROS2/Communication/TopicConfiguration.h> | ||
#include <ROS2/Frame/ROS2FrameComponent.h> | ||
#include <SplineTools/SplineToolsTypeIds.h> | ||
#include <nav_msgs/msg/path.hpp> | ||
#include <rclcpp/rclcpp.hpp> | ||
|
||
namespace SplineTools | ||
{ | ||
struct SplinePublisherConfiguration | ||
{ | ||
AZ_TYPE_INFO(SplinePublisherConfiguration, SplinePublisherConfigTypeId); | ||
static void Reflect(AZ::ReflectContext* context); | ||
|
||
ROS2::TopicConfiguration m_TopicConfig{ rclcpp::ServicesQoS() }; | ||
int m_updateFrequency = 10; | ||
SplinePublisherConfiguration(); | ||
}; | ||
|
||
class SplinePublisher final | ||
: public AZ::Component | ||
, protected AZ::TickBus::Handler | ||
{ | ||
public: | ||
AZ_COMPONENT(SplinePublisher, SplinePublisherComponentTypeId); | ||
|
||
static void Reflect(AZ::ReflectContext* context); | ||
|
||
SplinePublisher() = default; | ||
~SplinePublisher() override = default; | ||
explicit SplinePublisher(SplinePublisherConfiguration config); | ||
|
||
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required); | ||
|
||
// AZ::Component interface implementation | ||
void Activate() override; | ||
void Deactivate() override; | ||
|
||
protected: | ||
// AZ::TickBus::Handler interface implementation | ||
void OnTick(float deltaTime, AZ::ScriptTimePoint time) override; | ||
|
||
private: | ||
void PublishSplineAsPath() const; | ||
|
||
SplinePublisherConfiguration m_config; | ||
int m_frameNumber = 0; | ||
rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr m_publisher; | ||
ROS2::ROS2FrameComponent* m_ros2FramePtr = nullptr; | ||
}; | ||
} // namespace SplineTools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 12 additions & 10 deletions
22
Gems/RobotecSplineTools/Code/splinetools_private_files.cmake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
|
||
set(FILES | ||
Source/SplineToolsModuleInterface.cpp | ||
Source/SplineToolsModuleInterface.h | ||
Source/Clients/SplineToolsSystemComponent.cpp | ||
Source/Clients/SplineToolsSystemComponent.h | ||
Source/Clients/VisualizeSplineComponent.cpp | ||
Source/Clients/VisualizeSplineComponent.h | ||
Source/Clients/SplineSubscriber.h | ||
Source/Clients/SplineSubscriber.cpp | ||
Source/Clients/SplineSubscriberConfig.h | ||
Source/Clients/SplineSubscriberConfig.cpp | ||
Source/Clients/SplinePublisher.cpp | ||
Source/Clients/SplinePublisher.h | ||
Source/Clients/SplineSubscriber.cpp | ||
Source/Clients/SplineSubscriber.h | ||
Source/Clients/SplineSubscriberConfig.cpp | ||
Source/Clients/SplineSubscriberConfig.h | ||
Source/Clients/SplineToolsSystemComponent.cpp | ||
Source/Clients/SplineToolsSystemComponent.h | ||
Source/Clients/VisualizeSplineComponent.cpp | ||
Source/Clients/VisualizeSplineComponent.h | ||
Source/SplineToolsModuleInterface.cpp | ||
Source/SplineToolsModuleInterface.h | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.