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

Support multi entity spawn #146

Merged
merged 2 commits into from
May 20, 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5.1 FATAL_ERROR)
#============================================================================
# Initialize the project
#============================================================================
project(ignition-gazebo2 VERSION 2.17.0)
project(ignition-gazebo2 VERSION 2.18.0)

#============================================================================
# Find ignition-cmake
Expand Down
5 changes: 4 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Ignition Gazebo 2.x

### Ignition Gazebo 2.xx.xx (2020-xx-xx)
### Ignition Gazebo 2.18.0 (2020-05-20)

1. Added a `/world/<world_name>/create_multiple` service that parallels the current `/world/<world_name>/create` service. The `create_multiple` service can handle an `ignition::msgs::EntityFactory_V` message that may contain one or more entities to spawn.
* [Pull Request 146](https://github.com/ignitionrobotics/ign-gazebo/pull/146)

### Ignition Gazebo 2.17.0 (2020-05-13)

Expand Down
32 changes: 32 additions & 0 deletions src/systems/user_commands/UserCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ class ignition::gazebo::systems::UserCommandsPrivate
public: bool CreateService(const msgs::EntityFactory &_req,
msgs::Boolean &_res);

/// \brief Callback for multiple create service
/// \param[in] _req Request containing one or more entity descriptions.
/// \param[in] _res True if message successfully received and queued.
/// It does not mean that the entities will be successfully spawned.
/// \return True if successful.
public: bool CreateServiceMultiple(
const msgs::EntityFactory_V &_req, msgs::Boolean &_res);

/// \brief Callback for remove service
/// \param[in] _req Request containing identification of entity to be removed.
/// \param[in] _res True if message successfully received and queued.
Expand Down Expand Up @@ -215,6 +223,11 @@ void UserCommands::Configure(const Entity &_entity,
this->dataPtr->node.Advertise(createService,
&UserCommandsPrivate::CreateService, this->dataPtr.get());

// Create service for EntityFactory_V
std::string createServiceMultiple{"/world/" + worldName + "/create_multiple"};
this->dataPtr->node.Advertise(createServiceMultiple,
&UserCommandsPrivate::CreateServiceMultiple, this->dataPtr.get());

ignmsg << "Create service on [" << createService << "]" << std::endl;

// Remove service
Expand Down Expand Up @@ -265,6 +278,25 @@ void UserCommands::PreUpdate(const UpdateInfo &/*_info*/,
// TODO(louise) Clear redo list
}

//////////////////////////////////////////////////
bool UserCommandsPrivate::CreateServiceMultiple(
const msgs::EntityFactory_V &_req, msgs::Boolean &_res)
{
std::lock_guard<std::mutex> lock(this->pendingMutex);
for (int i = 0; i < _req.data_size(); ++i)
{
const msgs::EntityFactory &msg = _req.data(i);
// Create command and push it to queue
auto msgCopy = msg.New();
msgCopy->CopyFrom(msg);
auto cmd = std::make_unique<CreateCommand>(msgCopy, this->iface);
this->pendingCmds.push_back(std::move(cmd));
}

_res.set_data(true);
return true;
}

//////////////////////////////////////////////////
bool UserCommandsPrivate::CreateService(const msgs::EntityFactory &_req,
msgs::Boolean &_res)
Expand Down
9 changes: 9 additions & 0 deletions src/systems/user_commands/UserCommands.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ namespace systems
/// * **Request type*: ignition.msgs.EntityFactory
/// * **Response type*: ignition.msgs.Boolean
///
/// # Spawn multiple entities
///
/// This service can spawn multiple entities in the same iteration,
/// thereby eliminating simulation steps between entity spawn times.
///
/// * **Service**: `/world/<world name>/create_multiple`
/// * **Request type*: ignition.msgs.EntityFactory_V
/// * **Response type*: ignition.msgs.Boolean
///
/// Try some examples described on examples/worlds/empty.sdf
class IGNITION_GAZEBO_VISIBLE UserCommands:
public System,
Expand Down