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 26, 2024
1 parent 08b5cfe commit 52a74ac
Showing 1 changed file with 139 additions and 56 deletions.
195 changes: 139 additions & 56 deletions src/systems/user_commands/UserCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class UserCommandsInterface
/// \return True if a contact sensor is connected to the collision entity,
/// false otherwise
public: bool HasContactSensor(const Entity _collision);

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

/// \brief All user commands should inherit from this class so they can be
Expand Down Expand Up @@ -545,6 +548,7 @@ class gz::sim::systems::UserCommandsPrivate

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

};

/// \brief Pose3d equality comparison function.
Expand Down Expand Up @@ -610,7 +614,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 @@ -680,6 +684,15 @@ 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->iface->setAllLightEntities =
_sdf->Get<bool>("set_all_light_entities");
gzmsg << "Set all light entities: "
<< this->dataPtr->iface->setAllLightEntities
<< std::endl;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L689-L693

Added lines #L689 - L693 were not covered by tests
}

std::string materialColorTopic{
"/world/" + validWorldName + "/material_color"};
this->dataPtr->node.Subscribe(materialColorTopic,
Expand Down Expand Up @@ -859,7 +872,6 @@ void UserCommandsPrivate::OnCmdLight(const msgs::Light &_msg)
}
}


//////////////////////////////////////////////////
bool UserCommandsPrivate::PoseService(const msgs::Pose &_req,
msgs::Boolean &_res)
Expand Down Expand Up @@ -1362,79 +1374,150 @@ LightCommand::LightCommand(msgs::Light *_msg,
//////////////////////////////////////////////////
bool LightCommand::Execute()
{
auto lightMsg = dynamic_cast<const msgs::Light *>(this->msg);
auto lightMsg = dynamic_cast<msgs::Light *>(this->msg);
if (nullptr == lightMsg)
{
gzerr << "Internal error, null light message" << std::endl;
return false;
}
Entity lightEntity = kNullEntity;
if (this->iface->setAllLightEntities)
{
auto entities = entitiesFromScopedName(lightMsg->name(),
*this->iface->ecm);
if (entities.empty())

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1387-L1388

Added lines #L1387 - L1388 were not covered by tests
{
gzwarn << "Entity name: " << lightMsg->name() << ", is not found."
<< std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1390-L1392

Added lines #L1390 - L1392 were not covered by tests
}
for (const Entity &id : entities)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1394

Added line #L1394 was not covered by tests
{
lightEntity = kNullEntity;
lightMsg->set_id(id);
if (lightMsg->id() != kNullEntity)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1396-L1398

Added lines #L1396 - L1398 were not covered by tests
{
lightEntity = lightMsg->id();

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1400

Added line #L1400 was not covered by tests
}
if (kNullEntity == lightEntity)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1402

Added line #L1402 was not covered by tests
{
gzerr << "Failed to find light with name [" << lightMsg->name()
<< "] and parent ID ["
<< lightMsg->parent_id() << "]." << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1404-L1407

Added lines #L1404 - L1407 were not covered by tests
}

if (!lightEntity)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1410

Added line #L1410 was not covered by tests
{
gzmsg << "Failed to find light entity named [" << lightMsg->name()
<< "]." << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1412-L1414

Added lines #L1412 - L1414 were not covered by tests
}

Entity lightEntity{kNullEntity};
auto lightPose =
this->iface->ecm->Component<components::Pose>(lightEntity);
if (nullptr == lightPose)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1418-L1419

Added lines #L1418 - L1419 were not covered by tests
{
lightEntity = kNullEntity;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1421

Added line #L1421 was not covered by tests
}

if (lightMsg->id() != kNullEntity)
{
lightEntity = lightMsg->id();
if (!lightEntity)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1424

Added line #L1424 was not covered by tests
{
gzmsg << "Pose component not available" << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1426-L1427

Added lines #L1426 - L1427 were not covered by tests
}

if (lightMsg->has_pose())

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1430

Added line #L1430 was not covered by tests
{
lightPose->Data().Pos() = msgs::Convert(lightMsg->pose()).Pos();

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1432

Added line #L1432 was not covered by tests
}

auto lightCmdComp =
this->iface->ecm->Component<components::LightCmd>(lightEntity);
if (!lightCmdComp)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1436-L1437

Added lines #L1436 - L1437 were not covered by tests
{
this->iface->ecm->CreateComponent(
lightEntity, components::LightCmd(*lightMsg));

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1439-L1440

Added lines #L1439 - L1440 were not covered by tests
}
else
{
auto state = lightCmdComp->SetData(*lightMsg, this->lightEql) ?

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1444

Added line #L1444 was not covered by tests
ComponentState::OneTimeChange :
ComponentState::NoChange;
this->iface->ecm->SetChanged(lightEntity, components::LightCmd::typeId,

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1446-L1447

Added lines #L1446 - L1447 were not covered by tests
state);
}
}
}
else if (!lightMsg->name().empty())
else
{
if (lightMsg->parent_id() != kNullEntity)
lightEntity = kNullEntity;
if (lightMsg->id() != kNullEntity)
{
lightEntity = this->iface->ecm->EntityByComponents(
components::Name(lightMsg->name()),
components::ParentEntity(lightMsg->parent_id()));
lightEntity = lightMsg->id();

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1457

Added line #L1457 was not covered by tests
}
else
else if (!lightMsg->name().empty())
{
if (lightMsg->parent_id() != kNullEntity)
{
lightEntity = this->iface->ecm->EntityByComponents(
components::Name(lightMsg->name()),
components::ParentEntity(lightMsg->parent_id()));

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1463-L1465

Added lines #L1463 - L1465 were not covered by tests
}
else
{
lightEntity = this->iface->ecm->EntityByComponents(
components::Name(lightMsg->name()));
}
}
if (kNullEntity == lightEntity)
{
lightEntity = this->iface->ecm->EntityByComponents(
components::Name(lightMsg->name()));
gzerr << "Failed to find light with name [" << lightMsg->name()
<< "], ID [" << lightMsg->id() << "] and parent ID ["
<< lightMsg->parent_id() << "]." << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1475-L1478

Added lines #L1475 - L1478 were not covered by tests
}
}
if (kNullEntity == lightEntity)
{
gzerr << "Failed to find light with name [" << lightMsg->name()
<< "], ID [" << lightMsg->id() << "] and parent ID ["
<< lightMsg->parent_id() << "]." << std::endl;
return false;
}

if (!lightEntity)
{
gzmsg << "Failed to find light entity named [" << lightMsg->name()
<< "]." << std::endl;
return false;
}
if (!lightEntity)
{
gzmsg << "Failed to find light entity named [" << lightMsg->name()
<< "]." << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1483-L1485

Added lines #L1483 - L1485 were not covered by tests
}

auto lightPose = this->iface->ecm->Component<components::Pose>(lightEntity);
if (nullptr == lightPose)
lightEntity = kNullEntity;
auto lightPose = this->iface->ecm->Component<components::Pose>(lightEntity);
if (nullptr == lightPose)
{
lightEntity = kNullEntity;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1491

Added line #L1491 was not covered by tests
}

if (!lightEntity)
{
gzmsg << "Pose component not available" << std::endl;
return false;
}
if (!lightEntity)
{
gzmsg << "Pose component not available" << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1496-L1497

Added lines #L1496 - L1497 were not covered by tests
}

if (lightMsg->has_pose())
{
lightPose->Data().Pos() = msgs::Convert(lightMsg->pose()).Pos();
}
if (lightMsg->has_pose())
{
lightPose->Data().Pos() = msgs::Convert(lightMsg->pose()).Pos();

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1502

Added line #L1502 was not covered by tests
}

auto lightCmdComp =
this->iface->ecm->Component<components::LightCmd>(lightEntity);
if (!lightCmdComp)
{
this->iface->ecm->CreateComponent(
lightEntity, components::LightCmd(*lightMsg));
}
else
{
auto state = lightCmdComp->SetData(*lightMsg, this->lightEql) ?
ComponentState::OneTimeChange :
ComponentState::NoChange;
this->iface->ecm->SetChanged(lightEntity, components::LightCmd::typeId,
state);
auto lightCmdComp =
this->iface->ecm->Component<components::LightCmd>(lightEntity);
if (!lightCmdComp)
{
this->iface->ecm->CreateComponent(
lightEntity, components::LightCmd(*lightMsg));
}
else
{
auto state = lightCmdComp->SetData(*lightMsg, this->lightEql) ?

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1514

Added line #L1514 was not covered by tests
ComponentState::OneTimeChange :
ComponentState::NoChange;
this->iface->ecm->SetChanged(lightEntity, components::LightCmd::typeId,

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1516-L1517

Added lines #L1516 - L1517 were not covered by tests
state);
}
}

return true;
}

Expand Down

0 comments on commit 52a74ac

Please sign in to comment.