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

Allow renaming breadcrumb models if there is a name conflict #155

Merged
merged 3 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### Ignition Gazebo 2.XX.XX (20XX-XX-XX)

1. Allow renaming breadcrumb models if there is a name conflict
* [Pull Request 155](https://github.com/ignitionrobotics/ign-gazebo/pull/155)

1. Add TriggeredPublisher system
* [Pull Request 139](https://github.com/ignitionrobotics/ign-gazebo/pull/139)

Expand Down
42 changes: 40 additions & 2 deletions src/systems/breadcrumbs/Breadcrumbs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ignition/gazebo/components/DetachableJoint.hh"
#include "ignition/gazebo/components/Geometry.hh"
#include "ignition/gazebo/components/Link.hh"
#include "ignition/gazebo/components/Model.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/ParentEntity.hh"
#include "ignition/gazebo/components/Performer.hh"
Expand Down Expand Up @@ -66,6 +67,9 @@ void Breadcrumbs::Configure(const Entity &_entity,
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
std::chrono::duration<double>(period));

this->allowRenaming =
_sdf->Get<bool>("allow_renaming", this->allowRenaming).first;

this->model = Model(_entity);

if (!_sdf->HasElement("breadcrumb"))
Expand Down Expand Up @@ -161,8 +165,42 @@ void Breadcrumbs::PreUpdate(const ignition::gazebo::UpdateInfo &_info,
this->numDeployments < this->maxDeployments)
{
sdf::Model modelToSpawn = *this->modelRoot.ModelByIndex(0);
modelToSpawn.SetName(modelToSpawn.Name() + "_" +
std::to_string(this->numDeployments));
std::string desiredName =
modelToSpawn.Name() + "_" + std::to_string(this->numDeployments);

std::vector<std::string> modelNames;
_ecm.Each<components::Name, components::Model>(
[&modelNames](const Entity &, const components::Name *_name,
const components::Model *)
{
modelNames.push_back(_name->Data());
return true;
});

// Check if there's a model with the same name.
auto it = std::find(modelNames.begin(), modelNames.end(), desiredName);
if (it != modelNames.end())
{
if (!this->allowRenaming)
{
ignwarn << "Entity named [" << desiredName
<< "] already exists and "
<< "[allow_renaming] is false. Entity not spawned."
<< std::endl;
return;
}

std::string newName = desiredName;
int counter = 0;
while (std::find(modelNames.begin(), modelNames.end(), newName) !=
modelNames.end())
{
newName = desiredName + "_" + std::to_string(++counter);
}
desiredName = newName;
}

modelToSpawn.SetName(desiredName);
modelToSpawn.SetPose(poseComp->Data() * modelToSpawn.Pose());
ignmsg << "Deploying " << modelToSpawn.Name() << " at "
<< modelToSpawn.Pose() << std::endl;
Expand Down
8 changes: 8 additions & 0 deletions src/systems/breadcrumbs/Breadcrumbs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ namespace systems
/// `<performer_volume>`: Geometry that represents the bounding volume of
/// the performer. Only `<geometry><box>` is supported currently. When this
/// parameter is present, the deployed models will be performers.
/// `<allow_renaming>`: If true, the deployed model will be renamed if another
/// model with the same name already exists in the world. If false and there
/// is another model with the same name, the breadcrumb will not be deployed.
/// Defaults to false.
/// `<breadcrumb>`: This is the model used as a template for deploying
/// breadcrumbs.
class IGNITION_GAZEBO_VISIBLE Breadcrumbs
Expand Down Expand Up @@ -128,6 +132,10 @@ namespace systems
/// \brief Whether the deployed models will be performers
private: bool isPerformer{false};

/// \brief Whether the deployed model should be renamed if a model with the
/// same name already exists
private: bool allowRenaming{false};
nkoenig marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Bounding volume of the performer
private: std::optional<sdf::Geometry> performerGeometry;

Expand Down
45 changes: 45 additions & 0 deletions test/integration/breadcrumbs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,48 @@ TEST_F(BreadcrumbsTest, DeployDisablePhysics)
server.AddSystem(testSystem.systemPtr);
server.Run(true, iterTestStart + 2001, false);
}

/////////////////////////////////////////////////
// The test verifies that if allow_renaming is true, the Breadcrumb system
// renames spawned models if a model with the same name exists.
TEST_F(BreadcrumbsTest, AllowRenaming)
{
// Start server
ServerConfig serverConfig;
const auto sdfFile = std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/breadcrumbs.sdf";
serverConfig.SetSdfFile(sdfFile);

Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));

using namespace std::chrono_literals;
server.SetUpdatePeriod(1ns);

transport::Node node;
auto deployB1 =
node.Advertise<msgs::Empty>("/model/vehicle_blue/breadcrumbs/B1/deploy");
auto noRenameDeploy =
node.Advertise<msgs::Empty>("/no_rename_deploy");
auto renameDeploy =
node.Advertise<msgs::Empty>("/rename_deploy");

server.Run(true, 1, false);
deployB1.Publish(msgs::Empty());
server.Run(true, 100, false);
EXPECT_TRUE(server.HasEntity("B1_0"));

// Deploying via "/no_rename_deploy" will try to spawn B1_0, but since the
// model already exists, the spawn should fail.
auto curEntityCount = server.EntityCount().value();
noRenameDeploy.Publish(msgs::Empty());
server.Run(true, 100, false);
EXPECT_EQ(curEntityCount, server.EntityCount().value());

// Deploying via "/rename_deploy" will try to spawn B1_0, but since the
// model already exists, it will spawn B1_0_1 instead.
renameDeploy.Publish(msgs::Empty());
server.Run(true, 100, false);
EXPECT_TRUE(server.HasEntity("B1_0_1"));
}
52 changes: 52 additions & 0 deletions test/worlds/breadcrumbs.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,58 @@
</sdf>
</breadcrumb>
</plugin>

<!-- Two Breadcrumbs containing a breadcrumb model that is already spawned (B1).-->
<plugin filename="libignition-gazebo-breadcrumbs-system.so" name="ignition::gazebo::systems::Breadcrumbs">
<max_deployments>3</max_deployments>
<allow_renaming>false</allow_renaming>
<topic>/no_rename_deploy</topic>
<breadcrumb>
<sdf version="1.6">
<model name="B1">
<pose>-10 0 0 0 0 0</pose>
<link name='body'>
<inertial>
<mass>0.6</mass>
<inertia>
<ixx>0.017</ixx>
<ixy>0</ixy>
<ixz>0</ixz>
<iyy>0.017</iyy>
<iyz>0</iyz>
<izz>0.009</izz>
</inertia>
</inertial>
</link>
</model>
</sdf>
</breadcrumb>
</plugin>
<plugin filename="libignition-gazebo-breadcrumbs-system.so" name="ignition::gazebo::systems::Breadcrumbs">
<max_deployments>3</max_deployments>
<allow_renaming>true</allow_renaming>
<topic>/rename_deploy</topic>
<breadcrumb>
<sdf version="1.6">
<model name="B1">
<pose>-20 0 0 0 0 0</pose>
<link name='body'>
<inertial>
<mass>0.6</mass>
<inertia>
<ixx>0.017</ixx>
<ixy>0</ixy>
<ixz>0</ixz>
<iyy>0.017</iyy>
<iyz>0</iyz>
<izz>0.009</izz>
</inertia>
</inertial>
</link>
</model>
</sdf>
</breadcrumb>
</plugin>
</model>
<plugin name="ignition::gazebo" filename="dummy">
<performer name="perf1">
Expand Down