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

Fix enviroment system loading mechanism #1842

Merged
merged 33 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ee16b8d
Fix enviroment system loading mechanism
arjo129 Dec 20, 2022
2ce77fc
small changes
arjo129 Dec 21, 2022
3829724
Working on porting the visuals
arjo129 Jan 12, 2023
f0d1866
Actually send message for loading from ui to environment preload plugin.
arjo129 Feb 16, 2023
9b684cf
Rewrite EnvironmentVisualization Widget to be simpler.
arjo129 Feb 17, 2023
dd24227
fix crashes.
arjo129 Feb 17, 2023
712ebe0
Get a different :boom:
arjo129 Feb 17, 2023
d722ad8
Works some times.
arjo129 Feb 17, 2023
87586d7
Fixed synchronization issues.
arjo129 Feb 20, 2023
77f5472
No more :boom:s :tada:
arjo129 Feb 21, 2023
882edbe
style
arjo129 Feb 22, 2023
ea93a1b
Sprinkled with healthy dose of Doxygen
arjo129 Feb 22, 2023
4fd5c9e
Style
arjo129 Feb 23, 2023
d7c34c3
More style fixes
arjo129 Feb 23, 2023
7af4af9
Fix Typo with unit map
arjo129 Apr 14, 2023
57b949b
Address PR feedback
arjo129 Apr 14, 2023
afcf5c6
Style fixes
arjo129 Apr 17, 2023
6b56a43
Fix incorrect use of path.
arjo129 Apr 17, 2023
8779e84
Merge branch 'gz-sim7' into arjo/fix/environment_system
mjcarroll Apr 25, 2023
c2708db
Fix example loading issues.
arjo129 Jul 18, 2023
c30639a
Merge branch 'arjo/fix/environment_system' of github.com:gazebosim/gz…
arjo129 Jul 18, 2023
54c42b2
style
arjo129 Jul 18, 2023
fe1bc7c
Update src/systems/environment_preload/VisualizationTool.cc
arjo129 Aug 22, 2023
3cf7896
Adds a warning regarding loading plugins.
arjo129 Aug 24, 2023
36e7cc4
Merge remote-tracking branch 'origin' into arjo/fix/environment_system
arjo129 Aug 24, 2023
f90ef4f
Merge branch 'arjo/fix/environment_system' of github.com:gazebosim/gz…
arjo129 Aug 24, 2023
6b2c398
Automatically loads plugin if missing
arjo129 Aug 25, 2023
85b6eac
Address some feedback I missed
arjo129 Aug 25, 2023
04a3858
Address some feedback
arjo129 Aug 25, 2023
e3ed11a
Fixes issue described by @iche033.
arjo129 Aug 28, 2023
4d0a034
style
arjo129 Aug 28, 2023
0672777
Fixed failing tests
arjo129 Sep 4, 2023
9daebc8
Merge branch 'gz-sim7' into arjo/fix/environment_system
iche033 Sep 5, 2023
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
Prev Previous commit
Next Next commit
Sprinkled with healthy dose of Doxygen
Also refactored the visualization tool out.

Signed-off-by: Arjo Chakravarty <arjoc@intrinsic.ai>
  • Loading branch information
arjo129 committed Feb 22, 2023
commit ea93a1bb7d435a77109469e54d058dfae80e629d
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ class EnvironmentVisualizationTool
this->pcPub.Publish(vec);
}

/// \brief The sample resolution
public: gz::msgs::Vector3d vec;

/// \brief Publisher to publish sample resolution
public: transport::Node::Publisher pcPub;

/// \brief Gz transport node
public: transport::Node node;

/// \brief To synchronize member access.
Expand Down
1 change: 1 addition & 0 deletions src/systems/environment_preload/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
gz_add_system(environment-preload
SOURCES
EnvironmentPreload.cc
VisualizationTool.cc
PUBLIC_LINK_LIBS
gz-common${GZ_COMMON_VER}::gz-common${GZ_COMMON_VER}
gz-common${GZ_COMMON_VER}::io
Expand Down
30 changes: 24 additions & 6 deletions src/systems/environment_preload/EnvironmentPreload.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,58 @@ using Units = msgs::DataLoadPathOptions_DataAngularUnits;
//////////////////////////////////////////////////
class gz::sim::systems::EnvironmentPreloadPrivate
{
/// \brief Is the file loaded
public: bool loaded{false};

/// \brief SDF Description
public: std::shared_ptr<const sdf::Element> sdf;

/// \brief GzTransport node
public: transport::Node node;

/// \brief Data descriptions
public: msgs::DataLoadPathOptions dataDescription;

/// \brief mutex to protect the samples and data description
public: std::mutex mtx;

/// \brief Do we need to reload the system.
public: std::atomic<bool> needsReload{false};

/// \brief Visualization Helper
public: std::unique_ptr<EnvironmentVisualizationTool> visualizationPtr;

/// \brief Are visualizations enabled
public: bool visualize{false};

/// \brief Sample resolutions
public: math::Vector3d samples;

/// \brief Is the file loaded
public: bool fileLoaded{false};

public: bool logError{true};
/// \brief File loading error logger
public: bool logFileLoadError{true};

/// \brief Reference to data
public: std::shared_ptr<components::EnvironmentalData> envData;

//////////////////////////////////////////////////
public: EnvironmentPreloadPrivate() :
visualizationPtr(new EnvironmentVisualizationTool) {};

//////////////////////////////////////////////////
public: void OnLoadCommand(const msgs::DataLoadPathOptions &_msg)
{
std::lock_guard<std::mutex> lock(this->mtx);
this->dataDescription = _msg;
this->needsReload = true;
this->logError = true;
this->logFileLoadError = true;
this->visualizationPtr->FileReloaded();
gzdbg << "Loading file " << _msg.path() << "\n";
}

//////////////////////////////////////////////////
public: void OnVisualResChanged(const msgs::Vector3d &_resChanged)
{
std::lock_guard<std::mutex> lock(this->mtx);
Expand All @@ -102,6 +117,7 @@ class gz::sim::systems::EnvironmentPreloadPrivate
this->visualizationPtr->resample = true;
}

//////////////////////////////////////////////////
public: void ReadSdf(EntityComponentManager &_ecm)
{
if (!this->sdf->HasElement("data"))
Expand Down Expand Up @@ -202,6 +218,7 @@ class gz::sim::systems::EnvironmentPreloadPrivate
needsReload = true;
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
}

//////////////////////////////////////////////////
public: components::EnvironmentalData::ReferenceUnits ConvertUnits(
const Units &_unit)
{
Expand All @@ -216,6 +233,7 @@ class gz::sim::systems::EnvironmentPreloadPrivate
}
}

//////////////////////////////////////////////////
public: void LoadEnvironment(EntityComponentManager &_ecm)
{
try
Expand All @@ -233,11 +251,11 @@ class gz::sim::systems::EnvironmentPreloadPrivate
std::ifstream dataFile(this->dataDescription.path());
if (!dataFile.is_open())
{
if(logError)
if(logFileLoadError)
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
{
gzerr << "No environmental data file was found at " <<
this->dataDescription.path() << std::endl;
logError = false;
logFileLoadError = false;
}
return;
}
Expand All @@ -260,11 +278,11 @@ class gz::sim::systems::EnvironmentPreloadPrivate
}
catch (const std::invalid_argument &exc)
{
if(logError)
if(logFileLoadError)
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
{
gzerr << "Failed to load environment data" << std::endl
<< exc.what() << std::endl;
logError = false;
logFileLoadError = false;
}
}

Expand Down
211 changes: 211 additions & 0 deletions src/systems/environment_preload/VisualizationTool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "VisualizationTool.hh"

/////////////////////////////////////////////////
EnvironmentVisualizationTool::EnvironmentVisualizationTool()
{
this->pcPub =
this->node.Advertise<gz::msgs::PointCloudPacked>("/point_cloud");
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
}

/////////////////////////////////////////////////
void EnvironmentVisualizationTool::CreatePointCloudTopics(
const std::shared_ptr<components::EnvironmentalData> _data) {
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
this->pubs.clear();
this->sessions.clear();

for (auto key : _data->frame.Keys())
{
this->pubs.emplace(key, node.Advertise<gz::msgs::Float_V>(key));
gz::msgs::Float_V msg;
this->floatFields.emplace(key, msg);
this->sessions.emplace(key, _data->frame[key].CreateSession());
}
}

/////////////////////////////////////////////////
void EnvironmentVisualizationTool::FileReloaded()
{
this->finishedTime = false;
}

/////////////////////////////////////////////////
void EnvironmentVisualizationTool::Step(
const UpdateInfo &_info,
const EntityComponentManager& _ecm,
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
const std::shared_ptr<components::EnvironmentalData> _data,
double _xSamples, double _ySamples, double _zSamples)
{

arjo129 marked this conversation as resolved.
Show resolved Hide resolved
auto now = std::chrono::steady_clock::now();
std::chrono::duration<double> dt(now - this->lastTick);

if (this->resample)
{
this->CreatePointCloudTopics(_data);
this->ResizeCloud(_data, _ecm, _xSamples, _ySamples, _zSamples);
this->resample = false;
this->lastTick = now;
}

for (auto &it : this->sessions)
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
{
auto res =
_data->frame[it.first].StepTo(it.second,
std::chrono::duration<double>(_info.simTime).count());
if (res.has_value())
{
it.second = res.value();
}
else
{
this->finishedTime = true;
return;
}
}

// Publish at 2 hz for now. In future make reconfigureable.
if (dt.count() > 0.5 && !this->finishedTime)
{
this->Visualize(_data, _xSamples, _ySamples, _zSamples);
this->Publish();
lastTick = now;
}
}

/////////////////////////////////////////////////
void EnvironmentVisualizationTool::Visualize(
const std::shared_ptr<components::EnvironmentalData> data,
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
double _xSamples, double _ySamples, double _zSamples)
{

arjo129 marked this conversation as resolved.
Show resolved Hide resolved
for (auto key : data->frame.Keys())
{
const auto session = this->sessions[key];
auto frame = data->frame[key];
auto [lower_bound, upper_bound] = frame.Bounds(session);
auto step = upper_bound - lower_bound;
auto dx = step.X() / _xSamples;
auto dy = step.Y() / _ySamples;
auto dz = step.Z() / _zSamples;
std::size_t idx = 0;
for (std::size_t x_steps = 0; x_steps < ceil(_xSamples); x_steps++)
{
auto x = lower_bound.X() + x_steps * dx;
for (std::size_t y_steps = 0; y_steps < ceil(_ySamples); y_steps++)
{
auto y = lower_bound.Y() + y_steps * dy;
for (std::size_t z_steps = 0; z_steps < ceil(_zSamples); z_steps++)
{
auto z = lower_bound.Z() + z_steps * dz;
auto res = frame.LookUp(session, math::Vector3d(x, y, z));

if (res.has_value())
{
this->floatFields[key].mutable_data()->Set(idx,
static_cast<float>(res.value()));
}
else
{
this->floatFields[key].mutable_data()->Set(idx, std::nanf(""));
}
idx++;
}
}
}
}
}

/////////////////////////////////////////////////
void EnvironmentVisualizationTool::Publish()
{
pcPub.Publish(this->pcMsg);
for(auto &[key, pub] : this->pubs)
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
{
pub.Publish(this->floatFields[key]);
}
}

/////////////////////////////////////////////////
void EnvironmentVisualizationTool::ResizeCloud(
const std::shared_ptr<components::EnvironmentalData> _data,
const EntityComponentManager& _ecm,
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
unsigned int _xSamples, unsigned int _ySamples, unsigned int _zSamples)
arjo129 marked this conversation as resolved.
Show resolved Hide resolved
{
assert (pubs.size() > 0);
arjo129 marked this conversation as resolved.
Show resolved Hide resolved

// Assume all data have same point cloud.
gz::msgs::InitPointCloudPacked(pcMsg, "some_frame", true,
{{"xyz", gz::msgs::PointCloudPacked::Field::FLOAT32}});
auto numberOfPoints = _xSamples * _ySamples * _zSamples;
std::size_t dataSize{
static_cast<std::size_t>(numberOfPoints * pcMsg.point_step())};
pcMsg.mutable_data()->resize(dataSize);
pcMsg.set_height(1);
pcMsg.set_width(numberOfPoints);

auto session = this->sessions[this->pubs.begin()->first];
auto frame = _data->frame[this->pubs.begin()->first];
auto [lower_bound, upper_bound] =
frame.Bounds(session);
arjo129 marked this conversation as resolved.
Show resolved Hide resolved

auto step = upper_bound - lower_bound;
auto dx = step.X() / _xSamples;
auto dy = step.Y() / _ySamples;
auto dz = step.Z() / _zSamples;

// Populate point cloud
gz::msgs::PointCloudPackedIterator<float> xIter(pcMsg, "x");
gz::msgs::PointCloudPackedIterator<float> yIter(pcMsg, "y");
gz::msgs::PointCloudPackedIterator<float> zIter(pcMsg, "z");

for (std::size_t x_steps = 0; x_steps < _xSamples; x_steps++)
{
auto x = lower_bound.X() + x_steps * dx;
for (std::size_t y_steps = 0; y_steps < _ySamples; y_steps++)
{
auto y = lower_bound.Y() + y_steps * dy;
for (std::size_t z_steps = 0; z_steps < _zSamples; z_steps++)
{
auto z = lower_bound.Z() + z_steps * dz;
auto coords = getGridFieldCoordinates(
_ecm, math::Vector3d{x, y, z},
_data);
arjo129 marked this conversation as resolved.
Show resolved Hide resolved

if (!coords.has_value())
{
continue;
}

auto pos = coords.value();
*xIter = pos.X();
*yIter = pos.Y();
*zIter = pos.Z();
++xIter;
++yIter;
++zIter;
}
}
}
for (auto key : _data->frame.Keys())
{
this->floatFields[key].mutable_data()->Resize(
numberOfPoints, std::nanf(""));
}
}
Loading