Skip to content
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

ROS2 Relative spawning #806

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions Gems/ROS2/Code/Source/Spawner/ROS2SpawnerComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace ROS2
{
AZStd::string referenceFrame(request->reference_frame.c_str());
const bool isWGS{ referenceFrame == "wgs84" && m_controller.GetSupportWGS() };
const bool relativeSpawning{ referenceFrame == "relative" && m_controller.IsRelativeSpawningEnabled() };

SpawnEntityResponse response;

Expand Down Expand Up @@ -226,15 +227,31 @@ namespace ROS2
}
else
{
transform = { AZ::Vector3(
request->initial_pose.position.x, request->initial_pose.position.y, request->initial_pose.position.z),
AZ::Quaternion(
request->initial_pose.orientation.x,
request->initial_pose.orientation.y,
request->initial_pose.orientation.z,
request->initial_pose.orientation.w)
.GetNormalized(),
1.0f };
auto spawnPosition =
AZ::Vector3(request->initial_pose.position.x, request->initial_pose.position.y, request->initial_pose.position.z);
auto spawnRotation = AZ::Quaternion(
request->initial_pose.orientation.x,
request->initial_pose.orientation.y,
request->initial_pose.orientation.z,
request->initial_pose.orientation.w)
.GetNormalized();

if (relativeSpawning)
{
AZ_Error(
"Spawner",
m_controller.GetReferenceCoordinationSystem().IsValid(),
"Can't spawn relatively, reference frame not set or is invalid");

AZ::Transform referenceTm = AZ::Transform::CreateIdentity();
AZ::TransformBus::EventResult(
referenceTm, m_controller.GetReferenceCoordinationSystem(), &AZ::TransformBus::Events::GetWorldTM);

spawnPosition = referenceTm.TransformPoint(spawnPosition);
spawnRotation *= referenceTm.GetRotation();
}

transform = { spawnPosition, spawnRotation, 1.0f };
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ namespace ROS2
->Version(1)
->Field("Editor entity id", &ROS2SpawnerComponentConfig::m_editorEntityId)
->Field("Support WGS", &ROS2SpawnerComponentConfig::m_supportWGS)
->Field("Enable relative spawning", &ROS2SpawnerComponentConfig::m_enableRelativeSpawning)
->Field("Reference coordinate system", &ROS2SpawnerComponentConfig::m_referenceCoordinationSystem)
->Field("Spawnables", &ROS2SpawnerComponentConfig::m_spawnables)
->Field("Default spawn pose", &ROS2SpawnerComponentConfig::m_defaultSpawnPose)
->Field("Service names", &ROS2SpawnerComponentConfig::m_serviceNames)
Expand All @@ -86,6 +88,18 @@ namespace ROS2
editContext->Class<ROS2SpawnerComponentConfig>("ROS2SpawnerComponentConfig", "Config for ROS2SpawnerComponent")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->DataElement(AZ::Edit::UIHandlers::Default, &ROS2SpawnerComponentConfig::m_supportWGS, "Support WGS", "Support for spawning entities using WGS84 coordinate system")
->DataElement(
AZ::Edit::UIHandlers::Default,
&ROS2SpawnerComponentConfig::m_enableRelativeSpawning,
"Enable relative spawning",
"When 'reference_frame' is set to 'relative' then spawns entity in the reference coordinate system")
->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::EntireTree)
->DataElement(
AZ::Edit::UIHandlers::Default,
&ROS2SpawnerComponentConfig::m_referenceCoordinationSystem,
"Reference coordinate system",
"Reference coordinate system used for spawning")
->Attribute(AZ::Edit::Attributes::Visibility, &ROS2SpawnerComponentConfig::UseReferenceCoordinationSystemVisibility)
->DataElement(AZ::Edit::UIHandlers::Default, &ROS2SpawnerComponentConfig::m_spawnables, "Spawnables", "Spawnables")
->DataElement(
AZ::Edit::UIHandlers::Default,
Expand Down Expand Up @@ -123,6 +137,16 @@ namespace ROS2
return m_config.m_supportWGS;
}

bool ROS2SpawnerComponentController::IsRelativeSpawningEnabled() const
{
return m_config.m_enableRelativeSpawning;
}

AZ::EntityId ROS2SpawnerComponentController::GetReferenceCoordinationSystem() const
{
return m_config.m_referenceCoordinationSystem;
}

void ROS2SpawnerComponentController::Reflect(AZ::ReflectContext* context)
{
ROS2SpawnerComponentConfig::Reflect(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <AzCore/Component/EntityId.h>
#include <AzCore/Memory/Memory_fwd.h>
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/base.h>
#include <AzFramework/Spawnable/Spawnable.h>

Expand Down Expand Up @@ -41,13 +42,19 @@ namespace ROS2
~ROS2SpawnerComponentConfig() = default;

static void Reflect(AZ::ReflectContext* context);
[[nodiscard]] AZ::u32 UseReferenceCoordinationSystemVisibility()
{
return m_enableRelativeSpawning ? AZ::Edit::PropertyVisibility::Show : AZ::Edit::PropertyVisibility::Hide;
}

AZ::EntityId m_editorEntityId;
AZ::Transform m_defaultSpawnPose = { AZ::Vector3{ 0, 0, 0 }, AZ::Quaternion{ 0, 0, 0, 1 }, 1.0 };

ROS2SpawnerServiceNames m_serviceNames;
AZStd::unordered_map<AZStd::string, AZ::Data::Asset<AzFramework::Spawnable>> m_spawnables;
bool m_supportWGS{ true };
bool m_enableRelativeSpawning{ false };
AZ::EntityId m_referenceCoordinationSystem;
};

class ROS2SpawnerComponentController : public SpawnerRequestsBus::Handler
Expand Down Expand Up @@ -79,6 +86,8 @@ namespace ROS2
AZ::EntityId GetEditorEntityId() const;
AZStd::unordered_map<AZStd::string, AZ::Data::Asset<AzFramework::Spawnable>> GetSpawnables() const;
bool GetSupportWGS() const;
bool IsRelativeSpawningEnabled() const;
AZ::EntityId GetReferenceCoordinationSystem() const;

private:
ROS2SpawnerComponentConfig m_config;
Expand Down