Skip to content

Commit afb228d

Browse files
Publish all periodic change components in Scene Broadcaster (#544)
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent 8998740 commit afb228d

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

include/ignition/gazebo/EntityComponentManager.hh

+6
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ namespace ignition
487487
/// \return True if there are any components with one-time changes.
488488
public: bool HasOneTimeComponentChanges() const;
489489

490+
/// \brief Get the components types that are marked as periodic changes.
491+
/// \return All the components that at least one entity marked as
492+
/// periodic changes.
493+
public: std::unordered_set<ComponentTypeId>
494+
ComponentTypesWithPeriodicChanges() const;
495+
490496
/// \brief Set the absolute state of the ECM from a serialized message.
491497
/// Entities / components that are in the new state but not in the old
492498
/// one will be created.

src/EntityComponentManager.cc

+12
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,18 @@ bool EntityComponentManager::HasOneTimeComponentChanges() const
419419
return !this->dataPtr->oneTimeChangedComponents.empty();
420420
}
421421

422+
/////////////////////////////////////////////////
423+
std::unordered_set<ComponentTypeId>
424+
EntityComponentManager::ComponentTypesWithPeriodicChanges() const
425+
{
426+
std::unordered_set<ComponentTypeId> periodicComponents;
427+
for (const auto& compPair : this->dataPtr->periodicChangedComponents)
428+
{
429+
periodicComponents.insert(compPair.first);
430+
}
431+
return periodicComponents;
432+
}
433+
422434
/////////////////////////////////////////////////
423435
bool EntityComponentManager::HasEntity(const Entity _entity) const
424436
{

src/EntityComponentManager_TEST.cc

+25
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,7 @@ TEST_P(EntityComponentManagerFixture, SetChanged)
20822082
auto c2 = manager.CreateComponent<IntComponent>(e2, IntComponent(456));
20832083

20842084
EXPECT_TRUE(manager.HasOneTimeComponentChanges());
2085+
EXPECT_EQ(0u, manager.ComponentTypesWithPeriodicChanges().size());
20852086
EXPECT_EQ(ComponentState::OneTimeChange,
20862087
manager.ComponentState(e1, c1.first));
20872088
EXPECT_EQ(ComponentState::OneTimeChange,
@@ -2093,16 +2094,39 @@ TEST_P(EntityComponentManagerFixture, SetChanged)
20932094
// updated
20942095
manager.RunSetAllComponentsUnchanged();
20952096
EXPECT_FALSE(manager.HasOneTimeComponentChanges());
2097+
EXPECT_EQ(0u, manager.ComponentTypesWithPeriodicChanges().size());
20962098
EXPECT_EQ(ComponentState::NoChange,
20972099
manager.ComponentState(e1, c1.first));
20982100
EXPECT_EQ(ComponentState::NoChange,
20992101
manager.ComponentState(e2, c2.first));
21002102

21012103
// Mark as changed
21022104
manager.SetChanged(e1, c1.first, ComponentState::PeriodicChange);
2105+
2106+
// check that only e1 c1 is serialized into a message
2107+
msgs::SerializedStateMap stateMsg;
2108+
manager.State(stateMsg);
2109+
{
2110+
ASSERT_EQ(1, stateMsg.entities_size());
2111+
2112+
auto iter = stateMsg.entities().find(e1);
2113+
const auto &e1Msg = iter->second;
2114+
EXPECT_EQ(e1, e1Msg.id());
2115+
ASSERT_EQ(1, e1Msg.components_size());
2116+
2117+
auto compIter = e1Msg.components().begin();
2118+
const auto &e1c1Msg = compIter->second;
2119+
EXPECT_EQ(IntComponent::typeId, e1c1Msg.type());
2120+
EXPECT_EQ(123, std::stoi(e1c1Msg.component()));
2121+
}
2122+
21032123
manager.SetChanged(e2, c2.first, ComponentState::OneTimeChange);
21042124

21052125
EXPECT_TRUE(manager.HasOneTimeComponentChanges());
2126+
// Expect a single component type to be marked as PeriodicChange
2127+
ASSERT_EQ(1u, manager.ComponentTypesWithPeriodicChanges().size());
2128+
EXPECT_EQ(IntComponent().TypeId(),
2129+
*manager.ComponentTypesWithPeriodicChanges().begin());
21062130
EXPECT_EQ(ComponentState::PeriodicChange,
21072131
manager.ComponentState(e1, c1.first));
21082132
EXPECT_EQ(ComponentState::OneTimeChange,
@@ -2112,6 +2136,7 @@ TEST_P(EntityComponentManagerFixture, SetChanged)
21122136
EXPECT_TRUE(manager.RemoveComponent(e1, c1.first));
21132137

21142138
EXPECT_TRUE(manager.HasOneTimeComponentChanges());
2139+
EXPECT_EQ(0u, manager.ComponentTypesWithPeriodicChanges().size());
21152140
EXPECT_EQ(ComponentState::NoChange,
21162141
manager.ComponentState(e1, c1.first));
21172142

src/systems/scene_broadcaster/SceneBroadcaster.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,13 @@ void SceneBroadcaster::PostUpdate(const UpdateInfo &_info,
293293
{
294294
_manager.State(*this->dataPtr->stepMsg.mutable_state(), {}, {}, true);
295295
}
296-
// Otherwise publish just selected components
296+
// Otherwise publish just periodic change components
297297
else
298298
{
299299
IGN_PROFILE("SceneBroadcast::PostUpdate UpdateState");
300+
auto periodicComponents = _manager.ComponentTypesWithPeriodicChanges();
300301
_manager.State(*this->dataPtr->stepMsg.mutable_state(),
301-
{}, {components::Pose::typeId});
302+
{}, periodicComponents);
302303
}
303304

304305
// Full state on demand

0 commit comments

Comments
 (0)