Skip to content

Commit

Permalink
Resolve Fuel resources when included in models and worlds (#309)
Browse files Browse the repository at this point in the history
Co-authored-by: FirefoxMetzger <sebastian@wallkoetter.net>
  • Loading branch information
FirefoxMetzger authored Mar 31, 2021
1 parent def00f7 commit f375db8
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 20 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<a href="https://github.com/robotology/gym-ignition/actions">
<img src="https://github.com/robotology/gym-ignition/workflows/Docker%20Images/badge.svg" alt="Docker Images" />
</a>
<a href="ttps://www.codacy.com/gh/robotology/gym-ignition/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=robotology/gym-ignition&amp;utm_campaign=Badge_Grade">
<a href="https://www.codacy.com/gh/robotology/gym-ignition/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=robotology/gym-ignition&amp;utm_campaign=Badge_Grade">
<img src="https://api.codacy.com/project/badge/Grade/5536b05f8be94483b64ee883e7170a39" alt="Codacy Badge" />
</a>
</td>
Expand Down Expand Up @@ -101,9 +101,12 @@ our simulations, visit the _Motivations_ section of the [website](https://roboto

## Setup

1. Install the Ignition suite following the [official instructions](https://ignitionrobotics.org/docs/dome).
1. Install the latest Ignition suite following the [official instructions](https://ignitionrobotics.org/docs/dome).
1. Execute `pip install gym-ignition`, preferably in a virtual environment.

**Note**: `gym-ignition` currently only supports the latest version of the ignition suite. For more information on supported versions please refer to the [Support Policy](https://robotology.github.io/gym-ignition/master/installation/support_policy.html).


Then, for some simple examples, visit the _Getting Started_ section of the [website](https://robotology.github.io/gym-ignition).

You can decide to install only the C++ resources if you are not interested in using Python.
Expand Down
10 changes: 10 additions & 0 deletions cmake/ImportTargetsDome.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ alias_imported_target(
NAMESPACE_DEST ignition-physics
REQUIRED TRUE
)

alias_imported_target(
PACKAGE_ORIG ignition-fuel_tools5
PACKAGE_DEST ignition-fuel_tools
TARGETS_ORIG ignition-fuel_tools5
TARGETS_DEST ignition-fuel_tools
NAMESPACE_ORIG ignition-fuel_tools5
NAMESPACE_DEST ignition-fuel_tools
REQUIRED TRUE
)
6 changes: 4 additions & 2 deletions cpp/scenario/gazebo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ target_link_libraries(ScenarioGazebo
${ignition-common.ignition-common}
PRIVATE
ScenarioCore::CoreUtils
ScenarioGazebo::ExtraComponents)
ScenarioGazebo::ExtraComponents
${ignition-fuel_tools.ignition-fuel_tools})

set_target_properties(ScenarioGazebo PROPERTIES
PUBLIC_HEADER "${SCENARIO_GAZEBO_PUBLIC_HDRS}")
Expand All @@ -126,7 +127,8 @@ target_link_libraries(GazeboSimulator
ScenarioCore::CoreUtils
ScenarioGazebo::ScenarioGazebo
ScenarioGazebo::ExtraComponents
ScenarioGazeboPlugins::ECMSingleton)
ScenarioGazeboPlugins::ECMSingleton
${ignition-fuel_tools.ignition-fuel_tools})

set_target_properties(GazeboSimulator PROPERTIES
PUBLIC_HEADER include/scenario/gazebo/GazeboSimulator.h)
Expand Down
3 changes: 3 additions & 0 deletions cpp/scenario/gazebo/include/scenario/gazebo/GazeboSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class scenario::gazebo::GazeboSimulator
* inserted. The default empty world does not have the ground plane nor
* any physics. Both can be added by operating on the ``World`` object.
*
* @note This function can only be used while the simulator object is
* uninitialized.
*
* @param worldFile The path to the SDF world file.
* @param worldName Optionally override the name of the world defined in the
* SDF world file.
Expand Down
29 changes: 18 additions & 11 deletions cpp/scenario/gazebo/src/GazeboSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@
#include "scenario/gazebo/utils.h"
#include "scenario/plugins/gazebo/ECMSingleton.h"

#include <ignition/fuel_tools.hh>
#include <ignition/gazebo/Server.hh>
#include <ignition/gazebo/ServerConfig.hh>
#include <ignition/gazebo/components/Name.hh>
#include <ignition/gazebo/components/World.hh>
#include <ignition/transport/Node.hh>
#include <ignition/transport/Publisher.hh>
#include <sdf/Element.hh>
#include <sdf/Physics.hh>
#include <sdf/Root.hh>
#include <sdf/World.hh>
#include <sdf/sdf.hh>

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -96,7 +94,7 @@ struct detail::PhysicsData

class GazeboSimulator::Impl
{
public:
public: // attributes
sdf::ElementPtr sdfElement = nullptr;

struct
Expand All @@ -107,16 +105,17 @@ class GazeboSimulator::Impl
std::shared_ptr<ignition::gazebo::Server> server;
} gazebo;

using WorldName = std::string;
using GazeboWorldPtr = std::shared_ptr<scenario::gazebo::World>;
std::unordered_map<WorldName, GazeboWorldPtr> worlds;

public: // methods
bool insertSDFWorld(const sdf::World& world);
std::shared_ptr<ignition::gazebo::Server> getServer();

static std::shared_ptr<World>
CreateGazeboWorld(const std::string& worldName);

using WorldName = std::string;
using GazeboWorldPtr = std::shared_ptr<scenario::gazebo::World>;
std::unordered_map<WorldName, GazeboWorldPtr> worlds;

static detail::PhysicsData getPhysicsData(const sdf::Root& root,
const size_t worldIndex);
bool sceneBroadcasterActive(const std::string& worldName);
Expand All @@ -137,6 +136,14 @@ GazeboSimulator::GazeboSimulator(const double stepSize,
// Configure the physics profile
pImpl->gazebo.physics.rtf = rtf;
pImpl->gazebo.physics.maxStepSize = stepSize;

// Configure Fuel Callback
sdf::setFindCallback([](const std::string& uri) -> std::string {
auto fuelClient = ignition::fuel_tools::FuelClient();
const auto path =
ignition::fuel_tools::fetchResourceWithClient(uri, fuelClient);
return path;
});
}

GazeboSimulator::~GazeboSimulator()
Expand Down Expand Up @@ -384,8 +391,8 @@ bool GazeboSimulator::insertWorldFromSDF(const std::string& worldFile,
const std::string& worldName)
{
if (this->initialized()) {
sMessage << "Worlds must be inserted before the initialization"
<< std::endl;
sError << "Worlds must be inserted before the initialization"
<< std::endl;
return false;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/assets/worlds/fuel_support.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" ?>
<sdf version="1.5">
<world name="default">
<include>
<uri>https://fuel.ignitionrobotics.org/1.0/OpenRobotics/models/Ground Plane</uri>
</include>
<include>
<uri>https://fuel.ignitionrobotics.org/1.0/OpenRobotics/models/Sun</uri>
</include>
<model name="box">
<pose>0 0 0.5 0 0 0</pose>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</visual>
</link>
</model>
</world>
</sdf>
28 changes: 23 additions & 5 deletions tests/test_scenario/test_ignition_fuel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
# GNU Lesser General Public License v2.1 or any later version.

import pytest

pytestmark = pytest.mark.scenario

from ..common import utils
from scenario import core
from scenario import gazebo as scenario
from ..common.utils import gazebo_fixture as gazebo

from pathlib import Path

# Set the verbosity
scenario.set_verbosity(scenario.Verbosity_debug)


@pytest.mark.parametrize("gazebo",
[(0.001, 1.0, 1)],
indirect=True,
ids=utils.id_gazebo_fn)
@pytest.mark.parametrize(
"gazebo", [(0.001, 1.0, 1)], indirect=True, ids=utils.id_gazebo_fn
)
def test_download_model_from_fuel(gazebo: scenario.GazeboSimulator):

assert gazebo.initialize()
Expand All @@ -28,7 +30,8 @@ def test_download_model_from_fuel(gazebo: scenario.GazeboSimulator):
# Download a model from Fuel (testing a name with spaces)
model_name = "Electrical Box"
model_sdf = scenario.get_model_file_from_fuel(
f"https://fuel.ignitionrobotics.org/openrobotics/models/{model_name}", False)
f"https://fuel.ignitionrobotics.org/openrobotics/models/{model_name}", False
)
assert model_sdf

assert world.insert_model(model_sdf, core.Pose_identity())
Expand All @@ -41,3 +44,18 @@ def test_download_model_from_fuel(gazebo: scenario.GazeboSimulator):
assert other_model_name in world.model_names()

assert gazebo.run()


@pytest.mark.parametrize(
"gazebo", [(0.001, 1.0, 1)], indirect=True, ids=utils.id_gazebo_fn
)
def test_fuel_world(gazebo):
# (setup) load a world that includes a fuel model
worlds_folder = Path(__file__) / ".." / ".." / "assets" / "worlds"
world_file = worlds_folder / "fuel_support.sdf"
assert gazebo.insert_world_from_sdf(str(world_file.resolve()))
assert gazebo.initialize()
assert gazebo.run(paused=True)

# the actual test
assert "ground_plane" in gazebo.get_world().model_names()

0 comments on commit f375db8

Please sign in to comment.