Skip to content

Commit a48b932

Browse files
committed
Distinguish STATUS and PRIORITY updates in notify() callbacks
to allow propagating status updates only if the STATUS actually changed.
1 parent fdd5ff8 commit a48b932

File tree

7 files changed

+35
-20
lines changed

7 files changed

+35
-20
lines changed

core/include/moveit/task_constructor/container_p.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class ContainerBasePrivate : public StagePrivate
156156

157157
/// copy external_state to a child's interface and remember the link in internal_external map
158158
template <Interface::Direction>
159-
void copyState(Interface::iterator external, const InterfacePtr& target, bool updated);
159+
void copyState(Interface::iterator external, const InterfacePtr& target, Interface::UpdateFlags updated);
160160
/// lift solution from internal to external level
161161
void liftSolution(const SolutionBasePtr& solution, const InterfaceState* internal_from,
162162
const InterfaceState* internal_to);
@@ -230,9 +230,9 @@ class ParallelContainerBasePrivate : public ContainerBasePrivate
230230
void validateInterfaces(const StagePrivate& child, InterfaceFlags& external, bool first = false) const;
231231

232232
private:
233-
/// callback for new externally received states
233+
/// notify callback for new externally received states
234234
template <typename Interface::Direction>
235-
void onNewExternalState(Interface::iterator external, bool updated);
235+
void onNewExternalState(Interface::iterator external, Interface::UpdateFlags updated);
236236
};
237237
PIMPL_FUNCTIONS(ParallelContainerBase)
238238

core/include/moveit/task_constructor/stage_p.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ class ConnectingPrivate : public ComputeBasePrivate
340340
template <Interface::Direction other>
341341
inline StatePair make_pair(Interface::const_iterator first, Interface::const_iterator second);
342342

343-
// get informed when new start or end state becomes available
343+
// notify callback to get informed about newly inserted (or updated) start or end states
344344
template <Interface::Direction other>
345-
void newState(Interface::iterator it, bool updated);
345+
void newState(Interface::iterator it, Interface::UpdateFlags updated);
346346

347347
// ordered list of pending state pairs
348348
ordered<StatePair> pending;

core/include/moveit/task_constructor/storage.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <moveit/macros/class_forward.h>
4343
#include <moveit/task_constructor/properties.h>
4444
#include <moveit/task_constructor/cost_queue.h>
45+
#include <moveit/task_constructor/utils.h>
4546
#include <moveit_task_constructor_msgs/Solution.h>
4647
#include <visualization_msgs/MarkerArray.h>
4748

@@ -199,7 +200,14 @@ class Interface : public ordered<InterfaceState*>
199200
FORWARD,
200201
BACKWARD,
201202
};
202-
using NotifyFunction = std::function<void(iterator, bool)>;
203+
enum Update
204+
{
205+
STATUS = 1 << 0,
206+
PRIORITY = 1 << 1,
207+
ALL = STATUS | PRIORITY,
208+
};
209+
using UpdateFlags = utils::Flags<Update>;
210+
using NotifyFunction = std::function<void(iterator, UpdateFlags)>;
203211

204212
class DisableNotify
205213
{

core/include/moveit/task_constructor/utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ class Flags
140140
Int i;
141141
};
142142

143-
#define DECLARE_FLAGS(Flags, Enum) using Flags = QFlags<Enum>;
144-
145143
const moveit::core::LinkModel* getRigidlyConnectedParentLinkModel(const moveit::core::RobotState& state,
146144
std::string frame);
147145

core/src/container.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ void ContainerBasePrivate::onNewFailure(const Stage& child, const InterfaceState
189189
}
190190

191191
template <Interface::Direction dir>
192-
void ContainerBasePrivate::copyState(Interface::iterator external, const InterfacePtr& target, bool updated) {
192+
void ContainerBasePrivate::copyState(Interface::iterator external, const InterfacePtr& target,
193+
Interface::UpdateFlags updated) {
193194
if (updated) { // propagate external state update to internal copies
194195
auto internals{ externalToInternalMap().equal_range(&*external) };
195196
for (auto& i = internals.first; i != internals.second; ++i) {
@@ -550,7 +551,7 @@ void SerialContainerPrivate::resolveInterface(InterfaceFlags expected) {
550551
validateInterface<START_IF_MASK>(*first.pimpl(), expected);
551552
// connect first child's (start) pull interface
552553
if (const InterfacePtr& target = first.pimpl()->starts())
553-
starts_.reset(new Interface([this, target](Interface::iterator it, bool updated) {
554+
starts_.reset(new Interface([this, target](Interface::iterator it, Interface::UpdateFlags updated) {
554555
this->copyState<Interface::FORWARD>(it, target, updated);
555556
}));
556557
} catch (InitStageException& e) {
@@ -575,7 +576,7 @@ void SerialContainerPrivate::resolveInterface(InterfaceFlags expected) {
575576
validateInterface<END_IF_MASK>(*last.pimpl(), expected);
576577
// connect last child's (end) pull interface
577578
if (const InterfacePtr& target = last.pimpl()->ends())
578-
ends_.reset(new Interface([this, target](Interface::iterator it, bool updated) {
579+
ends_.reset(new Interface([this, target](Interface::iterator it, Interface::UpdateFlags updated) {
579580
this->copyState<Interface::BACKWARD>(it, target, updated);
580581
}));
581582
} catch (InitStageException& e) {
@@ -670,11 +671,11 @@ void ParallelContainerBasePrivate::resolveInterface(InterfaceFlags expected) {
670671

671672
// States received by the container need to be copied to all children's pull interfaces.
672673
if (expected & READS_START)
673-
starts().reset(new Interface([this](Interface::iterator external, bool updated) {
674+
starts().reset(new Interface([this](Interface::iterator external, Interface::UpdateFlags updated) {
674675
this->onNewExternalState<Interface::FORWARD>(external, updated);
675676
}));
676677
if (expected & READS_END)
677-
ends().reset(new Interface([this](Interface::iterator external, bool updated) {
678+
ends().reset(new Interface([this](Interface::iterator external, Interface::UpdateFlags updated) {
678679
this->onNewExternalState<Interface::BACKWARD>(external, updated);
679680
}));
680681

@@ -713,7 +714,7 @@ void ParallelContainerBasePrivate::validateConnectivity() const {
713714
}
714715

715716
template <Interface::Direction dir>
716-
void ParallelContainerBasePrivate::onNewExternalState(Interface::iterator external, bool updated) {
717+
void ParallelContainerBasePrivate::onNewExternalState(Interface::iterator external, Interface::UpdateFlags updated) {
717718
for (const Stage::pointer& stage : children())
718719
copyState<dir>(external, stage->pimpl()->pullInterface<dir>(), updated);
719720
}

core/src/stage.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,12 @@ ConnectingPrivate::StatePair ConnectingPrivate::make_pair<Interface::FORWARD>(In
711711

712712
// TODO: bool updated -> uint_8 updated (bitfield of PRIORITY | STATUS)
713713
template <Interface::Direction dir>
714-
void ConnectingPrivate::newState(Interface::iterator it, bool updated) {
714+
void ConnectingPrivate::newState(Interface::iterator it, Interface::UpdateFlags updated) {
715715
auto parent_pimpl = parent()->pimpl();
716716
Interface::DisableNotify disable_source_interface(*pullInterface<dir>());
717717
if (updated) {
718-
if (pullInterface<opposite<dir>()>()->notifyEnabled()) // suppress recursive loop
718+
if (updated.testFlag(Interface::STATUS) && // only perform these costly operations if needed
719+
pullInterface<opposite<dir>()>()->notifyEnabled()) // suppress recursive loop
719720
{
720721
// If status has changed, propagate the update to the opposite side
721722
auto status = it->priority().status();

core/src/storage.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void Interface::add(InterfaceState& state) {
129129
moveFrom(it, container);
130130
// and finally call notify callback
131131
if (notify_)
132-
notify_(it, false);
132+
notify_(it, UpdateFlags());
133133
}
134134

135135
Interface::container_type Interface::remove(iterator it) {
@@ -140,16 +140,23 @@ Interface::container_type Interface::remove(iterator it) {
140140
}
141141

142142
void Interface::updatePriority(InterfaceState* state, const InterfaceState::Priority& priority) {
143-
if (priority == state->priority())
143+
const auto old_prio = state->priority();
144+
if (priority == old_prio)
144145
return; // nothing to do
145146

146147
auto it = std::find(begin(), end(), state); // find iterator to state
147148
assert(it != end()); // state should be part of this interface
148149

149150
state->priority_ = priority; // update priority
150151
update(it); // update position in ordered list
151-
if (notify_)
152-
notify_(it, true); // notify callback
152+
153+
if (notify_) {
154+
UpdateFlags updated(Update::ALL);
155+
if (old_prio.status() == priority.status())
156+
updated &= ~STATUS;
157+
158+
notify_(it, updated); // notify callback
159+
}
153160
}
154161

155162
std::ostream& operator<<(std::ostream& os, const Interface& interface) {

0 commit comments

Comments
 (0)