Skip to content

Commit

Permalink
publish hooks for processing
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Hanca <[email protected]>
  • Loading branch information
jhanca-robotecai committed Sep 13, 2023
1 parent ccb5bd7 commit f540795
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
*/

#include "ROS2RobotImporterEditorSystemComponent.h"
#include <RobotImporter/URDF/UrdfParser.h>
#include <RobotImporter/Utils/FilePath.h>
#include <RobotImporter/Utils/ErrorUtils.h>
#include "RobotImporterWidget.h"
#include <SdfAssetBuilder/SdfAssetBuilderSettings.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/StringFunc/StringFunc.h>
#include <AzCore/Utils/Utils.h>
#include <AzCore/std/chrono/chrono.h>
#include <AzCore/std/containers/vector.h>
#include <AzCore/std/string/string.h>
#include <AzCore/StringFunc/StringFunc.h>
#include <AzCore/std/utility/move.h>
#include <AzToolsFramework/API/ViewPaneOptions.h>

#include <RobotImporter/URDF/UrdfParser.h>
#include <RobotImporter/Utils/ErrorUtils.h>
#include <RobotImporter/Utils/FilePath.h>
#include <SDFormat/ROS2SensorHooks.h>
#include <SdfAssetBuilder/SdfAssetBuilderSettings.h>

#include <sdf/sdf.hh>

Expand All @@ -32,7 +34,16 @@ namespace ROS2
{
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<ROS2RobotImporterEditorSystemComponent, ROS2RobotImporterSystemComponent>()->Version(0);
const auto& importerHookCamera = ROS2::SDFormat::ROS2SensorHooks::ROS2CameraSensor();
const auto& importerHookGNSS = ROS2::SDFormat::ROS2SensorHooks::ROS2GNSSSensor();
const auto& importerHookImu = ROS2::SDFormat::ROS2SensorHooks::ROS2ImuSensor();
const auto& importerHookLidar = ROS2::SDFormat::ROS2SensorHooks::ROS2LidarSensor();
serializeContext->Class<ROS2RobotImporterEditorSystemComponent, ROS2RobotImporterSystemComponent>()->Version(0)->Attribute(
"SensorImporterHooks",
AZStd::vector<ROS2::SDFormat::SensorImporterHook>{ AZStd::move(importerHookCamera),
AZStd::move(importerHookGNSS),
AZStd::move(importerHookImu),
AZStd::move(importerHookLidar) });
}

if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
Expand Down Expand Up @@ -111,8 +122,7 @@ namespace ROS2
{
const AZStd::string log = Utils::JoinSdfErrorsToString(parsedUrdfOutcome.GetSdfErrors());

AZ_Warning("ROS2RobotImporterEditorSystemComponent", false, "URDF parsing failed with errors:\nRefer to %s",
log.c_str());
AZ_Warning("ROS2RobotImporterEditorSystemComponent", false, "URDF parsing failed with errors:\nRefer to %s", log.c_str());
return false;
}

Expand Down Expand Up @@ -195,30 +205,32 @@ namespace ROS2
// Use the name of the first model tag in the SDF for the prefab
// Otherwise use the name of the first world tag in the SDF
AZStd::string prefabName;
if (const sdf::Model* model = parsedUrdfRoot.Model();
model != nullptr)
if (const sdf::Model* model = parsedUrdfRoot.Model(); model != nullptr)
{
prefabName = AZStd::string(model->Name().c_str(), model->Name().size()) + ".prefab";
}

if (uint64_t urdfWorldCount = parsedUrdfRoot.WorldCount();
prefabName.empty() && urdfWorldCount > 0)
if (uint64_t urdfWorldCount = parsedUrdfRoot.WorldCount(); prefabName.empty() && urdfWorldCount > 0)
{
const sdf::World* parsedUrdfWorld = parsedUrdfRoot.WorldByIndex(0);
prefabName = AZStd::string(parsedUrdfWorld->Name().c_str(), parsedUrdfWorld->Name().size()) + ".prefab";
}

if (prefabName.empty())
{
AZ_Error("ROS2RobotImporterEditorSystemComponent", false, "URDF file converted to SDF %.*s contains no worlds."
" O3DE Prefab cannot be created", AZ_STRING_ARG(filePath));
AZ_Error(
"ROS2RobotImporterEditorSystemComponent",
false,
"URDF file converted to SDF %.*s contains no worlds."
" O3DE Prefab cannot be created",
AZ_STRING_ARG(filePath));
return false;
}


const AZ::IO::Path prefabPathRelative(AZ::IO::Path("Assets") / "Importer" / prefabName);
const AZ::IO::Path prefabPath(AZ::IO::Path(AZ::Utils::GetProjectPath()) / prefabPathRelative);
AZStd::unique_ptr<URDFPrefabMaker> prefabMaker = AZStd::make_unique<URDFPrefabMaker>(filePath, &parsedUrdfRoot, prefabPath.String(), urdfAssetsMapping, useArticulation);
AZStd::unique_ptr<URDFPrefabMaker> prefabMaker =
AZStd::make_unique<URDFPrefabMaker>(filePath, &parsedUrdfRoot, prefabPath.String(), urdfAssetsMapping, useArticulation);

auto prefabOutcome = prefabMaker->CreatePrefabFromURDF();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace ROS2
{
report += " - " + QString::fromUtf8(modifiedTag.data(), static_cast<int>(modifiedTag.size())) + "\n";
}
report += "\n# "+tr("The modified URDF code:") + "\n";
report += "\n# " + tr("The modified URDF code:") + "\n";
report += "```\n" + QString::fromStdString(parsedUrdfOutcome.m_modifiedURDFContent) + "```\n";
}
else
Expand Down
18 changes: 11 additions & 7 deletions Gems/ROS2/Code/Source/RobotImporter/URDF/SensorsMaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "SensorsMaker.h"

#include <AzCore/Component/EntityId.h>
#include <AzCore/std/containers/vector.h>
#include <AzToolsFramework/Entity/EditorEntityHelpers.h>

#include <ROS2/RobotImporter/SDFormatSensorImporterHook.h>
Expand All @@ -25,22 +26,25 @@ namespace ROS2
serializeContext->EnumerateAll(
[&](const AZ::SerializeContext::ClassData* classData, const AZ::Uuid& typeId) -> bool
{
auto* attribute = AZ::FindAttribute(AZ::Crc32("SDFormatSensorImporter"), classData->m_attributes);
auto* attribute = AZ::FindAttribute(AZ::Crc32("SensorImporterHooks"), classData->m_attributes);
if (attribute == nullptr)
{
return true;
}

AZ::AttributeReader reader(nullptr, attribute);
SDFormat::SensorImporterHook sensorHook;
if (reader.Read<SDFormat::SensorImporterHook>(sensorHook))
AZStd::vector<ROS2::SDFormat::SensorImporterHook> sensorHooks;
if (reader.Read<AZStd::vector<ROS2::SDFormat::SensorImporterHook>>(sensorHooks))
{
for (const auto& t : sensorHook.m_sensorTypes)
for (const auto& hook : sensorHooks)
{
if (sensor->Type() == t)
for (const auto& t : hook.m_sensorTypes)
{
sensorHook.m_sdfSensorToComponentCallback(*entity, *sensor);
return false;
if (sensor->Type() == t)
{
hook.m_sdfSensorToComponentCallback(*entity, *sensor);
return false;
}
}
}
}
Expand Down

0 comments on commit f540795

Please sign in to comment.