Skip to content

Commit

Permalink
Spawning with namespace (#444)
Browse files Browse the repository at this point in the history
Spawning with namespace

Signed-off-by: Piotr Jaroszek <[email protected]>
  • Loading branch information
pijaro authored Aug 24, 2023
1 parent a2d2da0 commit c5b7c01
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Gems/ROS2/Code/Include/ROS2/Frame/NamespaceConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ namespace ROS2
void PopulateNamespace(bool isRoot, const AZStd::string& entityName);
AZStd::string GetNamespace(const AZStd::string& parentNamespace) const;

//! Update namespace and strategy.
//! @param ns Desired namespace.
//! @param strategy Namespace strategy.
void SetNamespace(const AZStd::string& ns, NamespaceStrategy strategy);

private:
AZStd::string m_namespace;
NamespaceStrategy m_namespaceStrategy = NamespaceStrategy::Default;
Expand Down
5 changes: 5 additions & 0 deletions Gems/ROS2/Code/Include/ROS2/Frame/ROS2FrameComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ namespace ROS2
//! @return The name of the global frame with namespace attached. It is typically "odom", "map", "world".
AZStd::string GetGlobalFrameName() const;

//! Updates the namespace and namespace strategy of the underlying namespace configuration
//! @param ns Namespace to set.
//! @param strategy Namespace strategy to use.
void UpdateNamespaceConfiguration(const AZStd::string& ns, NamespaceConfiguration::NamespaceStrategy strategy);

private:
//////////////////////////////////////////////////////////////////////////
// AZ::TickBus::Handler overrides
Expand Down
7 changes: 7 additions & 0 deletions Gems/ROS2/Code/Source/Frame/NamespaceConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ namespace ROS2
return ROS2Names::GetNamespacedName(parentNamespace, m_namespace);
}

void NamespaceConfiguration::SetNamespace(const AZStd::string& ns, NamespaceStrategy strategy)
{
m_namespace = ns;
m_namespaceStrategy = strategy;
UpdateNamespace();
}

bool NamespaceConfiguration::IsNamespaceCustom() const
{
return m_namespaceStrategy == NamespaceConfiguration::NamespaceStrategy::Custom;
Expand Down
5 changes: 5 additions & 0 deletions Gems/ROS2/Code/Source/Frame/ROS2FrameComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ namespace ROS2
return ROS2Names::GetNamespacedName(GetNamespace(), AZStd::string("odom"));
}

void ROS2FrameComponent::UpdateNamespaceConfiguration(const AZStd::string& ns, NamespaceConfiguration::NamespaceStrategy strategy)
{
m_namespaceConfiguration.SetNamespace(ns, strategy);
}

bool ROS2FrameComponent::IsTopLevel() const
{
return GetGlobalFrameName() == GetParentFrameID();
Expand Down
23 changes: 19 additions & 4 deletions Gems/ROS2/Code/Source/Spawner/ROS2SpawnerComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <ROS2/ROS2Bus.h>
#include <ROS2/ROS2GemUtilities.h>
#include <ROS2/Utilities/ROS2Conversions.h>
#include <ROS2/Utilities/ROS2Names.h>

namespace ROS2
{
Expand Down Expand Up @@ -91,8 +92,17 @@ namespace ROS2
void ROS2SpawnerComponent::SpawnEntity(const SpawnEntityRequest request, SpawnEntityResponse response)
{
AZStd::string spawnableName(request->name.c_str());
AZStd::string spawnableNamespace(request->robot_namespace.c_str());
AZStd::string spawnPointName(request->xml.c_str(), request->xml.size());

auto namespaceValidation = ROS2Names::ValidateNamespace(spawnableNamespace);
if (!namespaceValidation.IsSuccess())
{
response->success = false;
response->status_message = namespaceValidation.GetError().data();
return;
}

auto spawnPoints = GetSpawnPoints();

if (!m_controller.GetSpawnables().contains(spawnableName))
Expand Down Expand Up @@ -131,9 +141,9 @@ namespace ROS2
1.0f };
}

optionalArgs.m_preInsertionCallback = [this, transform, spawnableName](auto id, auto view)
optionalArgs.m_preInsertionCallback = [this, transform, spawnableName, spawnableNamespace](auto id, auto view)
{
PreSpawn(id, view, transform, spawnableName);
PreSpawn(id, view, transform, spawnableName, spawnableNamespace);
};

spawner->SpawnAllEntities(m_tickets.at(spawnableName), optionalArgs);
Expand All @@ -145,7 +155,8 @@ namespace ROS2
AzFramework::EntitySpawnTicket::Id id [[maybe_unused]],
AzFramework::SpawnableEntityContainerView view,
const AZ::Transform& transform,
const AZStd::string& spawnableName)
const AZStd::string& spawnableName,
const AZStd::string& spawnableNamespace)
{
if (view.empty())
{
Expand All @@ -159,10 +170,14 @@ namespace ROS2
AZStd::string instanceName = AZStd::string::format("%s_%d", spawnableName.c_str(), m_counter++);
for (AZ::Entity* entity : view)
{ // Update name for the first entity with ROS2Frame in hierarchy (left to right)
const auto* frameComponent = Utils::GetGameOrEditorComponent<ROS2FrameComponent>(entity);
auto* frameComponent = Utils::GetGameOrEditorComponent<ROS2FrameComponent>(entity);
if (frameComponent)
{
entity->SetName(instanceName);
if (!spawnableNamespace.empty())
{
frameComponent->UpdateNamespaceConfiguration(spawnableNamespace, NamespaceConfiguration::NamespaceStrategy::Custom);
}
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion Gems/ROS2/Code/Source/Spawner/ROS2SpawnerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ namespace ROS2
AzFramework::EntitySpawnTicket::Id,
AzFramework::SpawnableEntityContainerView,
const AZ::Transform&,
const AZStd::string& spawnableName);
const AZStd::string& spawnableName,
const AZStd::string& spawnableNamespace);

void GetSpawnPointsNames(const GetSpawnPointsNamesRequest request, GetSpawnPointsNamesResponse response);
void GetSpawnPointInfo(const GetSpawnPointInfoRequest request, GetSpawnPointInfoResponse response);
Expand Down

0 comments on commit c5b7c01

Please sign in to comment.