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

don't create components for entities that don't exist #927

Merged
merged 1 commit into from
Jul 23, 2021
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
9 changes: 9 additions & 0 deletions src/EntityComponentManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,15 @@ ComponentKey EntityComponentManager::CreateComponentImplementation(
const Entity _entity, const ComponentTypeId _componentTypeId,
const components::BaseComponent *_data)
{
// make sure the entity exists
if (!this->HasEntity(_entity))
{
ignerr << "Trying to create a component of type [" << _componentTypeId
<< "] attached to entity [" << _entity << "], but this entity does not "
<< "exist. This create component request will be ignored." << std::endl;
return ComponentKey();
}

// If type hasn't been instantiated yet, create a storage for it
if (!this->HasComponentType(_componentTypeId))
{
Expand Down
10 changes: 10 additions & 0 deletions src/EntityComponentManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,16 @@ TEST_P(EntityComponentManagerFixture, EntitiesAndComponents)
EXPECT_FALSE(manager.EntityHasComponentType(entity, DoubleComponent::typeId));
EXPECT_FALSE(manager.EntityHasComponentType(entity2, IntComponent::typeId));

// Try to add a component to an entity that does not exist
EXPECT_FALSE(manager.HasEntity(kNullEntity));
EXPECT_FALSE(manager.EntityHasComponentType(kNullEntity,
IntComponent::typeId));
EXPECT_EQ(ComponentKey(), manager.CreateComponent<IntComponent>(kNullEntity,
IntComponent(123)));
EXPECT_FALSE(manager.HasEntity(kNullEntity));
EXPECT_FALSE(manager.EntityHasComponentType(kNullEntity,
IntComponent::typeId));

// Remove all entities
manager.RequestRemoveEntities();
EXPECT_EQ(3u, manager.EntityCount());
Expand Down
13 changes: 10 additions & 3 deletions src/systems/diff_drive/DiffDrive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ void DiffDrive::Configure(const Entity &_entity,
this->dataPtr->limiterAng->SetMaxJerk(maxJerk);
}


double odomFreq = _sdf->Get<double>("odom_publish_frequency", 50).first;
if (odomFreq > 0)
{
Expand Down Expand Up @@ -324,6 +323,10 @@ void DiffDrive::PreUpdate(const ignition::gazebo::UpdateInfo &_info,

for (Entity joint : this->dataPtr->leftJoints)
{
// skip this entity if it has been removed
if (!_ecm.HasEntity(joint))
continue;

// Update wheel velocity
auto vel = _ecm.Component<components::JointVelocityCmd>(joint);

Expand All @@ -340,6 +343,10 @@ void DiffDrive::PreUpdate(const ignition::gazebo::UpdateInfo &_info,

for (Entity joint : this->dataPtr->rightJoints)
{
// skip this entity if it has been removed
if (!_ecm.HasEntity(joint))
continue;

// Update wheel velocity
auto vel = _ecm.Component<components::JointVelocityCmd>(joint);

Expand All @@ -358,15 +365,15 @@ void DiffDrive::PreUpdate(const ignition::gazebo::UpdateInfo &_info,
// don't exist.
auto leftPos = _ecm.Component<components::JointPosition>(
this->dataPtr->leftJoints[0]);
if (!leftPos)
if (!leftPos && _ecm.HasEntity(this->dataPtr->leftJoints[0]))
{
_ecm.CreateComponent(this->dataPtr->leftJoints[0],
components::JointPosition());
}

auto rightPos = _ecm.Component<components::JointPosition>(
this->dataPtr->rightJoints[0]);
if (!rightPos)
if (!rightPos && _ecm.HasEntity(this->dataPtr->rightJoints[0]))
{
_ecm.CreateComponent(this->dataPtr->rightJoints[0],
components::JointPosition());
Expand Down