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

Implement reset interface in the physics system #1327

Merged
merged 28 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
11e029a
SystemInternal and WorldControl into own headers
mjcarroll Feb 15, 2022
56f28bd
Move System interaction to SystemManager
mjcarroll Feb 15, 2022
c35e3ce
Adjustments for Fortress
mjcarroll Feb 16, 2022
8266e73
Address reviewer feedback
mjcarroll Feb 17, 2022
ca91023
Introduce System::Reset API
mjcarroll Feb 8, 2022
85cb36e
SimulationRunner Reset implementation
mjcarroll Feb 8, 2022
144556d
Simulation Reset implementation in Physics system
mjcarroll Feb 8, 2022
4b023fa
Simulation reset example plugin
mjcarroll Feb 8, 2022
75e57fb
Add integration test for resetting physics
mjcarroll Feb 8, 2022
e3368e5
Fix diff test and add documentation
mjcarroll Feb 15, 2022
63ec622
Fix build and test errors after merge, some documentation and cleanup.
azeey Feb 24, 2022
222695d
Merge branch 'reset' into reset_physics
azeey Feb 24, 2022
e5f01bc
Merge branch 'main' into reset_physics
mjcarroll Mar 23, 2022
859df32
Bump year
mjcarroll Mar 29, 2022
8c669b0
Merge branch 'main' into reset_physics
mjcarroll Mar 29, 2022
749daca
Fix merge from main
mjcarroll Mar 29, 2022
371d8ce
Fix CI platform
mjcarroll Mar 29, 2022
d1fabbe
Update test/integration/reset.cc
mjcarroll Mar 30, 2022
8ddb739
Merge branch 'main' into reset_physics
mjcarroll Apr 4, 2022
32d21f5
Merge branch 'main' into reset_physics
chapulina Apr 8, 2022
a766933
Merge branch 'main' into reset_physics
mjcarroll Apr 18, 2022
c8ab4d5
Address reviewer feedback
mjcarroll Apr 18, 2022
0bd1d6b
Merge branch 'main' into reset_physics
mjcarroll May 2, 2022
3799ff3
Adjust randomized range to account for limits
mjcarroll May 3, 2022
fbf6d0e
Fix header for new location
mjcarroll May 3, 2022
5cff795
Merge branch 'main' into reset_physics
mjcarroll May 10, 2022
a519149
Fixup CMake dependencies for reset_plugin
mjcarroll May 10, 2022
b5f6bc8
Fix CMake
mjcarroll May 17, 2022
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
14 changes: 14 additions & 0 deletions examples/plugin/reset_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)

project(ResetPlugins)

find_package(ignition-plugin2 REQUIRED COMPONENTS register)
set(IGN_PLUGIN_VER ${ignition-plugin2_VERSION_MAJOR})

find_package(ignition-gazebo7 REQUIRED)
set(IGN_GAZEBO_VER ${ignition-gazebo7_VERSION_MAJOR})

add_library(JointPositionRandomizer SHARED JointPositionRandomizer.cc)
target_link_libraries(JointPositionRandomizer
PRIVATE ignition-plugin${IGN_PLUGIN_VER}::ignition-plugin${IGN_PLUGIN_VER}
PRIVATE ignition-gazebo${IGN_GAZEBO_VER}::core)
77 changes: 77 additions & 0 deletions examples/plugin/reset_plugin/JointPositionRandomizer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 <ignition/math/Rand.hh>
#include <ignition/gazebo/System.hh>
#include <ignition/gazebo/components/Joint.hh>
#include <ignition/gazebo/components/JointPositionReset.hh>
#include <ignition/gazebo/components/JointPositionLimitsCmd.hh>
#include <ignition/gazebo/components/JointType.hh>
#include <ignition/gazebo/components/ParentEntity.hh>
#include <ignition/plugin/Register.hh>

using namespace ignition;
using namespace gazebo;
namespace reset_plugin
{
class JointPositionRandomizer : public System,
public ISystemConfigure,
public ISystemReset
{
void Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &,
EntityComponentManager &, EventManager &) override
{
this->targetEntity = _entity;
}

void Reset(const UpdateInfo &_info, EntityComponentManager &_ecm) override
{
std::cout << "Reset joints\n";
auto joints = _ecm.EntitiesByComponents(
components::ParentEntity(this->targetEntity), components::Joint());

for (auto joint : joints)
{
auto jointType = _ecm.Component<components::JointType>(joint);

double pos = 0.0;

if (jointType->Data() == sdf::JointType::PRISMATIC)
{
pos = math::Rand::DblUniform(-0.8, 0.1);
std::cout << "prismatic joint (" << joint
<< ") pos: (" << pos << " m)"<< std::endl;
}
else if (jointType->Data() == sdf::JointType::REVOLUTE)
{
pos = math::Rand::DblUniform(0, IGN_PI);
std::cout << "revolute joint (" << joint
<< ") pos: (" << pos << " rad)"<< std::endl;
}
_ecm.SetComponentData<components::JointPositionReset>(joint, {pos});
}
}

private: Entity targetEntity;
};
} // namespace reset_plugin

IGNITION_ADD_PLUGIN(reset_plugin::JointPositionRandomizer,
ignition::gazebo::System,
reset_plugin::JointPositionRandomizer::ISystemConfigure,
reset_plugin::JointPositionRandomizer::ISystemReset)
34 changes: 34 additions & 0 deletions examples/plugin/reset_plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# System Reset API

This example uses the JointPositionRandomizer system to randomize the joint
positions of a robot arm at every reset.


## Build

~~~
cd examples/plugin/reset_plugin
mkdir build
cd build
cmake ..
make
~~~

This will generate the `JointPositionRandomizer` library under `build`.

## Run

Add the library to the path:

~~~
cd examples/plugin/reset_plugin
export IGN_GAZEBO_SYSTEM_PLUGIN_PATH=`pwd`/build
~~~

Then run a world that loads the plugin as follows:

ign gazebo -r -v 4 joint_position_randomizer.sdf

In another terminal, run the following to reset the world.

ign service -s /world/default/control --reqtype ignition.msgs.WorldControl --reptype ignition.msgs.Boolean --timeout 3000 --req 'reset: {all: true}'
44 changes: 44 additions & 0 deletions examples/plugin/reset_plugin/joint_position_randomizer.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<sdf version="1.8">
<!-- This example uses the JointPositionRandomizer system to randomize the joint positions of a robot arm at every reset.

To reset the world, run:

ign service -s /world/default/control \-\-reqtype ignition.msgs.WorldControl \-\-reptype ignition.msgs.Boolean \-\-timeout 3000 \-\-req 'reset: {all: true}'
-->

<world name="default">
<plugin filename="ignition-gazebo-physics-system" name="ignition::gazebo::systems::Physics"></plugin>
<plugin filename="ignition-gazebo-user-commands-system" name="ignition::gazebo::systems::UserCommands"></plugin>
<plugin filename="ignition-gazebo-scene-broadcaster-system" name="ignition::gazebo::systems::SceneBroadcaster"></plugin>
<light name="sun" type="directional"/>
<model name="ground_plane">
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
</collision>
<visual name="visual">
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
<material>
<ambient>0.8 0.8 0.8 1</ambient>
<diffuse>0.8 0.8 0.8 1</diffuse>
</material>
</visual>
</link>
</model>
<include>
<uri>https://fuel.ignitionrobotics.org/1.0/OpenRobotics/models/Simple Arm</uri>
<plugin filename="build/libJointPositionRandomizer.so" name="reset_plugin::JointPositionRandomizer"/>
</include>
</world>
</sdf>
4 changes: 4 additions & 0 deletions src/EntityComponentManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3174,6 +3174,7 @@ TEST_P(EntityComponentManagerFixture,
EXPECT_EQ(1, foundEntities);
}

//////////////////////////////////////////////////
TEST_P(EntityComponentManagerFixture, CopyEcm)
{
Entity entity = manager.CreateEntity();
Expand All @@ -3195,6 +3196,7 @@ TEST_P(EntityComponentManagerFixture, CopyEcm)
});
}

//////////////////////////////////////////////////
TEST_P(EntityComponentManagerFixture, ComputeDiff)
{
Entity entity1 = manager.CreateEntity();
Expand Down Expand Up @@ -3243,6 +3245,7 @@ TEST_P(EntityComponentManagerFixture, ComputeDiff)
}
}

//////////////////////////////////////////////////
TEST_P(EntityComponentManagerFixture, ResetToWithDeletedEntity)
{
Entity entity1 = manager.CreateEntity();
Expand Down Expand Up @@ -3293,6 +3296,7 @@ TEST_P(EntityComponentManagerFixture, ResetToWithDeletedEntity)
}
}

//////////////////////////////////////////////////
TEST_P(EntityComponentManagerFixture, ResetToWithAddedEntity)
{
Entity entity1 = manager.CreateEntity();
Expand Down
19 changes: 17 additions & 2 deletions src/systems/physics/CanonicalLinkModelTracker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ namespace systems::physics_system
/// \param[in] _ecm EntityComponentManager
public: void AddNewModels(const EntityComponentManager &_ecm);

/// \brief Save mappings for all models and their canonical links
/// \param[in] _ecm EntityComponentManager
public: void AddAllModels(const EntityComponentManager &_ecm);

/// \brief Get a topological ordering of models that have a particular
/// canonical link
/// \param[in] _canonicalLink The canonical link
Expand All @@ -79,7 +83,7 @@ namespace systems::physics_system

/// \brief An empty set of models that is returned from the
/// CanonicalLinkModels method for links that map to no models
private: const std::set<Entity> emptyModelOrdering{};
private: static inline const std::set<Entity> kEmptyModelOrdering{};
};

void CanonicalLinkModelTracker::AddNewModels(
Expand All @@ -93,6 +97,17 @@ namespace systems::physics_system
return true;
});
}
void CanonicalLinkModelTracker::AddAllModels(
const EntityComponentManager &_ecm)
{
_ecm.Each<components::Model, components::ModelCanonicalLink>(
[this](const Entity &_model, const components::Model *,
const components::ModelCanonicalLink *_canonicalLinkComp)
{
this->linkModelMap[_canonicalLinkComp->Data()].insert(_model);
return true;
});
}

const std::set<Entity> &CanonicalLinkModelTracker::CanonicalLinkModels(
const Entity _canonicalLink) const
Expand All @@ -102,7 +117,7 @@ namespace systems::physics_system
return it->second;

// if an invalid entity was given, it maps to no models
return this->emptyModelOrdering;
return this->kEmptyModelOrdering;
}

void CanonicalLinkModelTracker::RemoveLink(const Entity &_link)
Expand Down
Loading