Skip to content

Commit

Permalink
UserCommands sdf light entity match.
Browse files Browse the repository at this point in the history
Allow for setting all matching light entities by setting
<set_all_light_entities> boolean in usercommands plugin's sdf.

Signed-off-by: Benjamin Perseghetti <[email protected]>
  • Loading branch information
bperseghetti committed Jan 22, 2024
1 parent 60cace1 commit a070d6d
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/systems/user_commands/UserCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,9 @@ class gz::sim::systems::UserCommandsPrivate

/// \brief Mutex to protect pending queue.
public: std::mutex pendingMutex;

/// \brief Bool to set all matching light entities.
public: bool setAllLightEntities = false;
};

/// \brief Pose3d equality comparison function.
Expand Down Expand Up @@ -604,7 +607,7 @@ bool UserCommandsInterface::HasContactSensor(const Entity _collision)

//////////////////////////////////////////////////
void UserCommands::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
EventManager &_eventManager)
{
Expand Down Expand Up @@ -674,6 +677,14 @@ void UserCommands::Configure(const Entity &_entity,
this->dataPtr->node.Subscribe(lightTopic, &UserCommandsPrivate::OnCmdLight,
this->dataPtr.get());

if (_sdf->HasElement("set_all_light_entities"))
{
this->dataPtr->setAllLightEntities =
_sdf->Get<bool>("set_all_light_entities");
gzmsg << "Set all light entities: " << this->dataPtr->setAllLightEntities
<< std::endl;

Check warning on line 685 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L682-L685

Added lines #L682 - L685 were not covered by tests
}

std::string materialColorTopic{
"/world/" + validWorldName + "/material_color"};
this->dataPtr->node.Subscribe(materialColorTopic,
Expand Down Expand Up @@ -842,17 +853,35 @@ bool UserCommandsPrivate::LightService(const msgs::Light &_req,
//////////////////////////////////////////////////
void UserCommandsPrivate::OnCmdLight(const msgs::Light &_msg)
{
auto msg = _msg.New();
msg->CopyFrom(_msg);
auto cmd = std::make_unique<LightCommand>(msg, this->iface);

// Push to pending
int numberOfEntities = 0;
auto entities = entitiesFromScopedName(_msg.name(),
*this->iface->ecm);
if (entities.empty())
{
std::lock_guard<std::mutex> lock(this->pendingMutex);
this->pendingCmds.push_back(std::move(cmd));
gzwarn << "Entity name: " << _msg.name() << ", is not found."
<< std::endl;
return;

Check warning on line 863 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L861-L863

Added lines #L861 - L863 were not covered by tests
}
}
for (const Entity &id : entities)
{
if ((numberOfEntities > 0) && (!setAllLightEntities))
{
break;

Check warning on line 869 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L869

Added line #L869 was not covered by tests
}
auto msg = _msg.New();
msg->CopyFrom(_msg);
numberOfEntities++;
msg->set_id(id);
auto cmd = std::make_unique<LightCommand>(msg, this->iface);

// Push to pending
{
std::lock_guard<std::mutex> lock(this->pendingMutex);
this->pendingCmds.push_back(std::move(cmd));
}
}
return;
}

//////////////////////////////////////////////////
bool UserCommandsPrivate::PoseService(const msgs::Pose &_req,
Expand Down

0 comments on commit a070d6d

Please sign in to comment.