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

Light entity match SDF boolean for UserCommands. #2295

Merged
merged 5 commits into from
Jan 31, 2024
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
186 changes: 130 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 @@
/// \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 @@ -610,7 +613,7 @@

//////////////////////////////////////////////////
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 +683,15 @@
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");
gzdbg << "Set all light entities: "
<< this->dataPtr->iface->setAllLightEntities
<< std::endl;
}

std::string materialColorTopic{
"/world/" + validWorldName + "/material_color"};
this->dataPtr->node.Subscribe(materialColorTopic,
Expand Down Expand Up @@ -859,7 +871,6 @@
}
}


bperseghetti marked this conversation as resolved.
Show resolved Hide resolved
//////////////////////////////////////////////////
bool UserCommandsPrivate::PoseService(const msgs::Pose &_req,
msgs::Boolean &_res)
Expand Down Expand Up @@ -1362,79 +1373,142 @@
//////////////////////////////////////////////////
bool LightCommand::Execute()
{
auto lightMsg = dynamic_cast<const msgs::Light *>(this->msg);
auto lightMsg = dynamic_cast<msgs::Light *>(this->msg);
bperseghetti marked this conversation as resolved.
Show resolved Hide resolved
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())
{
gzwarn << "Entity name: " << lightMsg->name() << ", is not found."
<< std::endl;
return false;
}
for (const Entity &id : entities)
{
lightEntity = kNullEntity;
lightMsg->set_id(id);
if (lightMsg->id() != kNullEntity)
{
lightEntity = lightMsg->id();
}
if (!lightEntity)
{
gzmsg << "Failed to find light entity named [" << lightMsg->name()
<< "]." << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1403-L1405

Added lines #L1403 - L1405 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1412

Added line #L1412 was not covered by tests
}

if (lightMsg->id() != kNullEntity)
{
lightEntity = lightMsg->id();
if (!lightEntity)
{
gzmsg << "Pose component not available" << std::endl;
return false;

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L1417 - L1418 were not covered by tests
}

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

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 1435 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1435

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L1437 - L1438 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 1448 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1448

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

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1454-L1456

Added lines #L1454 - L1456 were not covered by tests
}
else
{
lightEntity = this->iface->ecm->EntityByComponents(
components::Name(lightMsg->name()));
}
}
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 (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 1476 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1474-L1476

Added lines #L1474 - L1476 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 1482 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1482

Added line #L1482 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 1488 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1487-L1488

Added lines #L1487 - L1488 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();
}

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 1505 in src/systems/user_commands/UserCommands.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1505

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

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

View check run for this annotation

Codecov / codecov/patch

src/systems/user_commands/UserCommands.cc#L1507-L1508

Added lines #L1507 - L1508 were not covered by tests
state);
}
}

return true;
}

Expand Down
Loading
Loading