Skip to content

Commit

Permalink
Notify user and settings
Browse files Browse the repository at this point in the history
Signed-off-by: Michał <[email protected]>
  • Loading branch information
michalpelka committed Sep 4, 2023
1 parent 7df5911 commit 7e88d3d
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 72 deletions.
31 changes: 16 additions & 15 deletions Gems/ROS2/Code/Source/RobotImporter/FixURDF/FixURDF.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/

#include "FixURDF.h"
#include <AzCore/XML/rapidxml_print.h>
Expand All @@ -25,9 +25,8 @@ namespace ROS2::Utils
}
}

for (auto * link : links)
for (auto* link : links)
{

xml_node<>* inertial = urdf->document()->allocate_node(node_element, "inertial");

xml_node<>* mass = urdf->document()->allocate_node(node_element, "mass");
Expand Down Expand Up @@ -59,7 +58,6 @@ namespace ROS2::Utils
modifiedLinks.push_back(name->value());
}
link->append_node(inertial);

}

return modifiedLinks;
Expand Down Expand Up @@ -90,17 +88,18 @@ namespace ROS2::Utils
linkAndJointsName.insert(newName);
modifiedLinks.push_back(newName);
}
else{
else
{
linkAndJointsName.insert(name->value());
}

}
}
return modifiedLinks;
}

std::string ModifyURDFInMemory(const std::string& data)
AZStd::pair<std::string, AZStd::vector<AZStd::string>> ModifyURDFInMemory(const std::string& data)
{
AZStd::vector<AZStd::string> modifiedElements;
using namespace AZ::rapidxml;
xml_document<> doc;
doc.parse<0>(const_cast<char*>(data.c_str()));
Expand All @@ -112,7 +111,8 @@ namespace ROS2::Utils
AZ_Warning("ROS2", false, "Added missing inertia to links: ");
for (auto& link : links)
{
AZ_Warning("ROS2", false, " -> %s", link.c_str());
modifiedElements.push_back(link);
AZ_Warning("ROS2", false, " -> %s", link.c_str());
}
}

Expand All @@ -122,13 +122,14 @@ namespace ROS2::Utils
AZ_Warning("ROS2", false, "Renamed duplicated links and joints: ");
for (auto& dup : renames)
{
AZ_Warning("ROS2", false, " -> %s", dup.c_str());
modifiedElements.push_back(dup);
AZ_Warning("ROS2", false, " -> %s", dup.c_str());
}
}

std::string xmlDocString;
AZ::rapidxml::print(std::back_inserter(xmlDocString), *urdf, 0);

return xmlDocString;
return { xmlDocString, modifiedElements };
}
} // namespace ROS2::Utils
42 changes: 22 additions & 20 deletions Gems/ROS2/Code/Source/RobotImporter/FixURDF/FixURDF.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/

#pragma once

#include <AzCore/XML/rapidxml.h>
#include <AzCore/std/string/string.h>
#include <AzCore/std/containers/vector.h>

#include <AzCore/std/string/string.h>

namespace ROS2
{
namespace Utils
{
//! Modifies in memory URDF to add missing inertia to links, circle navigate SDF error 19
//! @param urdf - the in memory URDF to modify
//! @returns a list of links that were modified
AZStd::vector<AZStd::string> AddMissingInertiaToLink(AZ::rapidxml::xml_node<>* urdf);
namespace Utils
{
//! Modifies in memory URDF to add missing inertia to links, circle navigate SDF error 19
//! @param urdf - the in memory URDF to modify
//! @returns a list of links that were modified
AZStd::vector<AZStd::string> AddMissingInertiaToLink(AZ::rapidxml::xml_node<>* urdf);

//! Modifies names of links and joints to be unique, circle navigate SDF error 2
//! @param urdf - the in memory URDF to modify
//! @returns a list of links that were modified
AZStd::vector<AZStd::string> ChangeDuplications(AZ::rapidxml::xml_node<>* urdf);
//! Modifies names of links and joints to be unique, circle navigate SDF error 2
//! @param urdf - the in memory URDF to modify
//! @returns a list of links that were modified
AZStd::vector<AZStd::string> ChangeDuplications(AZ::rapidxml::xml_node<>* urdf);

std::string ModifyURDFInMemory(const std::string& data);
//! Modifies in memory URDF to add missing elements
//! @param urdf - the in memory URDF to modify
//! @returns a modified URDF and a list of XML element that were modified
AZStd::pair<std::string, AZStd::vector<AZStd::string>> ModifyURDFInMemory(const std::string& data);

} // namespace Utils
} // namespace Utils
} // namespace ROS2
9 changes: 8 additions & 1 deletion Gems/ROS2/Code/Source/RobotImporter/Pages/CheckUrdfPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace ROS2
CheckUrdfPage::CheckUrdfPage(QWizard* parent)
: QWizardPage(parent)
, m_success(false)
, m_warning(false)
{
m_log = new QTextEdit(this);
setTitle(tr("URDF opening results:"));
Expand All @@ -24,15 +25,21 @@ namespace ROS2
setLayout(layout);
}

void CheckUrdfPage::ReportURDFResult(const QString& status, bool isSuccess)
void CheckUrdfPage::ReportURDFResult(const QString& status, bool isSuccess, bool isWarning)
{
m_log->setMarkdown(status);
m_success = isSuccess;
m_warning = isWarning;
emit completeChanged();
}

bool CheckUrdfPage::isComplete() const
{
return m_success;
}

bool CheckUrdfPage::isWarning() const
{
return m_warning;
}
} // namespace ROS2
4 changes: 3 additions & 1 deletion Gems/ROS2/Code/Source/RobotImporter/Pages/CheckUrdfPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ namespace ROS2
Q_OBJECT
public:
explicit CheckUrdfPage(QWizard* parent);
void ReportURDFResult(const QString& result, bool isSuccess);
void ReportURDFResult(const QString& result, bool isSuccess, bool isWarning = false);
bool isComplete() const override;
bool isWarning() const;

private:
QTextEdit* m_log;
QString m_fileName;
bool m_success;
bool m_warning;
};
} // namespace ROS2
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace ROS2
sdf::ParserConfig parserConfig;
parserConfig.URDFSetPreserveFixedJoint(sdfBuilderSettings.m_urdfPreserveFixedJoints);

auto parsedUrdfOutcome = UrdfParser::ParseFromFile(filePath, parserConfig);
auto parsedUrdfOutcome = UrdfParser::ParseFromFile(filePath, parserConfig, sdfBuilderSettings);
if (!parsedUrdfOutcome)
{
const AZStd::string log = Utils::JoinSdfErrorsToString(parsedUrdfOutcome.GetSdfErrors());
Expand Down
45 changes: 31 additions & 14 deletions Gems/ROS2/Code/Source/RobotImporter/RobotImporterWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#include <AzCore/Utils/Utils.h>

#include "RobotImporterWidget.h"
#include <QApplication>
#include <QScreen>
#include <QTranslator>
#include <SdfAssetBuilder/SdfAssetBuilderSettings.h>
#include <URDF/URDFPrefabMaker.h>
#include <URDF/UrdfParser.h>
#include <Utils/ErrorUtils.h>
#include <Utils/FilePath.h>
#include <Utils/RobotImporterUtils.h>
#include <Utils/ErrorUtils.h>
#include <QApplication>
#include <QScreen>
#include <QTranslator>

namespace ROS2
{
Expand Down Expand Up @@ -119,8 +119,7 @@ namespace ROS2
report += "```\n";
if (outcome.m_logErrorOutput.size())
{
report +=
QString::fromUtf8(outcome.m_logErrorOutput.data(), static_cast<int>(outcome.m_logErrorOutput.size()));
report += QString::fromUtf8(outcome.m_logErrorOutput.data(), static_cast<int>(outcome.m_logErrorOutput.size()));
}
else
{
Expand All @@ -131,8 +130,8 @@ namespace ROS2
report += "```\n";
if (outcome.m_logStandardOutput.size())
{
report += QString::fromUtf8(
outcome.m_logStandardOutput.data(), static_cast<int>(outcome.m_logStandardOutput.size()));
report +=
QString::fromUtf8(outcome.m_logStandardOutput.data(), static_cast<int>(outcome.m_logStandardOutput.size()));
}
else
{
Expand All @@ -147,21 +146,36 @@ namespace ROS2
else if (Utils::IsFileUrdf(m_urdfPath))
{
// standard URDF
parsedUrdfOutcome = UrdfParser::ParseFromFile(m_urdfPath, parserConfig);
parsedUrdfOutcome = UrdfParser::ParseFromFile(m_urdfPath, parserConfig, sdfBuilderSettings);
}
else
{
AZ_Assert(false, "Unknown file extension : %s \n", m_urdfPath.c_str());
}
AZStd::string log;
bool urdfParsedSuccess{ parsedUrdfOutcome };
bool urdfParsedWithWarnings{ parsedUrdfOutcome.m_modifiedURDFContent.size() > 0};
if (urdfParsedSuccess)
{
if (urdfParsedWithWarnings)
{
report += "# " + tr("The URDF was parsed, but it was modified to be compatible") + "\n";
report += tr("Modified tags in URDF:") + "\n";
for (const auto& modifiedTag : parsedUrdfOutcome.m_modifiedURDFTags)
{
report += " - " + QString::fromUtf8(modifiedTag.data(), static_cast<int>(modifiedTag.size())) + "\n";
}
report += "\n# "+tr("The modified URDF code:") + "\n";
report += "```\n" + QString::fromStdString(parsedUrdfOutcome.m_modifiedURDFContent) + "```\n";
}
else
{
report += "# " + tr("The URDF was parsed and opened successfully") + "\n";
AZ_Printf("Wizard", "Wizard skips m_checkUrdfPage since there is no errors in URDF\n");
}
m_parsedUrdf = AZStd::move(parsedUrdfOutcome.GetRoot());
report += "# " + tr("The URDF was parsed and opened successfully") + "\n";
m_prefabMaker.reset();
// Report the status of skipping this page
AZ_Printf("Wizard", "Wizard skips m_checkUrdfPage since there is no errors in URDF\n");
m_meshNames = Utils::GetMeshesFilenames(&m_parsedUrdf, true, true);
m_assetPage->ClearAssetsList();
}
Expand All @@ -177,7 +191,7 @@ namespace ROS2
report += QString::fromUtf8(log.data(), int(log.size()));
report += "`";
}
m_checkUrdfPage->ReportURDFResult(report, urdfParsedSuccess);
m_checkUrdfPage->ReportURDFResult(report, urdfParsedSuccess, urdfParsedWithWarnings);
}
}

Expand Down Expand Up @@ -319,8 +333,7 @@ namespace ROS2
if (currentPage() == m_xacroParamsPage)
{
m_params = m_xacroParamsPage->GetXacroParameters();
if (const bool isFileUrdfOrXacro = Utils::IsFileXacro(m_urdfPath) || Utils::IsFileUrdf(m_urdfPath);
isFileUrdfOrXacro)
if (const bool isFileUrdfOrXacro = Utils::IsFileXacro(m_urdfPath) || Utils::IsFileUrdf(m_urdfPath); isFileUrdfOrXacro)
{
OpenUrdf();
}
Expand Down Expand Up @@ -350,6 +363,10 @@ namespace ROS2
{
if ((currentPage() == m_fileSelectPage && m_params.empty()) || currentPage() == m_xacroParamsPage)
{
if (m_parsedUrdf.Model() != nullptr && m_checkUrdfPage->isWarning())
{
return m_xacroParamsPage->nextId();
}
if (m_parsedUrdf.Model() != nullptr && m_checkUrdfPage->isComplete())
{
if (m_meshNames.size() == 0)
Expand Down
30 changes: 20 additions & 10 deletions Gems/ROS2/Code/Source/RobotImporter/URDF/UrdfParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <AzCore/std/string/string.h>
#include <RobotImporter/FixURDF/FixURDF.h>
#include <RobotImporter/Utils/ErrorUtils.h>
#include <RobotImporter/Utils/FilePath.cpp>

namespace ROS2::UrdfParser
{
Expand All @@ -26,8 +27,7 @@ namespace ROS2::UrdfParser
// @param consoleStream Reference to sdf::Console::ConsoleStream whose
// output will be redirected
// @param redirectStream reference to stream where console stream output is redirected to
RedirectSDFOutputStream(sdf::Console::ConsoleStream& consoleStream,
std::ostream& redirectStream)
RedirectSDFOutputStream(sdf::Console::ConsoleStream& consoleStream, std::ostream& redirectStream)
: m_consoleStreamRef(consoleStream)
, m_origConsoleStream(consoleStream)
{
Expand All @@ -51,7 +51,7 @@ namespace ROS2::UrdfParser
{
return m_root;
}
const sdf::Root& ParseResult::GetRoot() const &
const sdf::Root& ParseResult::GetRoot() const&
{
return m_root;
}
Expand Down Expand Up @@ -106,7 +106,8 @@ namespace ROS2::UrdfParser
return parseResult;
}

RootObjectOutcome ParseFromFile(AZ::IO::PathView filePath, const sdf::ParserConfig& parserConfig)
RootObjectOutcome ParseFromFile(
AZ::IO::PathView filePath, const sdf::ParserConfig& parserConfig, const SdfAssetBuilderSettings& settings)
{
// Store path in a AZ::IO::FixedMaxPath which is stack based structure that provides memory
// for the path string and is null terminated.
Expand All @@ -117,15 +118,24 @@ namespace ROS2::UrdfParser
{
auto fileNotFoundMessage = AZStd::fixed_string<1024>::format("File %.*s does not exist", AZ_PATH_ARG(urdfFilePath));
ParseResult fileNotFoundResult;
fileNotFoundResult.m_sdfErrors.emplace_back(sdf::ErrorCode::FILE_READ,
std::string{ fileNotFoundMessage.c_str(), fileNotFoundMessage.size() },
std::string{ urdfFilePath.c_str(), urdfFilePath.Native().size() });
fileNotFoundResult.m_sdfErrors.emplace_back(
sdf::ErrorCode::FILE_READ,
std::string{ fileNotFoundMessage.c_str(), fileNotFoundMessage.size() },
std::string{ urdfFilePath.c_str(), urdfFilePath.Native().size() });
return fileNotFoundResult;
}

std::string xmlStr((std::istreambuf_iterator<char>(istream)), std::istreambuf_iterator<char>());
// modify in memory
std::string xmlStrChanged = (ROS2::Utils::ModifyURDFInMemory(xmlStr));
return Parse(xmlStrChanged, parserConfig);
if (Utils::IsFileUrdf(filePath) && settings.m_fixURDF)
{
const auto& [modifiedXmlStr, modifiedElements] = (ROS2::Utils::ModifyURDFInMemory(xmlStr));

auto result = Parse(modifiedXmlStr, parserConfig);
result.m_modifiedURDFTags = modifiedElements;
result.m_modifiedURDFContent = modifiedXmlStr;
return result;
}
return Parse(xmlStr, parserConfig);
}
} // namespace ROS2
} // namespace ROS2::UrdfParser
Loading

0 comments on commit 7e88d3d

Please sign in to comment.