Skip to content

Commit

Permalink
[systems/lcm] Narrow the subscriber output port dependencies (RobotLo…
Browse files Browse the repository at this point in the history
…comotion#15563)

This is likely to reduce unnecessary downstream re-calculations, e.g.,
when only the context time changes.
  • Loading branch information
jwnimmer-tri authored Aug 5, 2021
1 parent cdcb904 commit dc6263e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 27 deletions.
28 changes: 5 additions & 23 deletions systems/lcm/lcm_subscriber_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,16 @@ LcmSubscriberSystem::LcmSubscriberSystem(
subscription_->set_unsubscribe_on_delete(true);
}

// Use the "advanced" method to construct explicit non-member functors to
// deal with the unusual methods we have available.
DeclareAbstractOutputPort(
kUseDefaultName,
[this]() {
return this->AllocateSerializerOutputValue();
},
[this](const Context<double>& context, AbstractValue* out) {
this->CalcSerializerOutputValue(context, out);
});

// Declare our two states (message_value, message_count).
static_assert(kStateIndexMessage == 0, "");
this->DeclareAbstractState(*AllocateSerializerOutputValue());
auto message_state_index =
this->DeclareAbstractState(*serializer_->CreateDefaultValue());
static_assert(kStateIndexMessageCount == 1, "");
this->DeclareAbstractState(Value<int>(0));

// Our sole output is the message state.
this->DeclareStateOutputPort(kUseDefaultName, message_state_index);

// Declare an unrestricted forced update handler that is invoked when a
// "forced" trigger occurs. This gives the user flexibility to force update
// the abstract states.
Expand Down Expand Up @@ -142,17 +135,6 @@ const std::string& LcmSubscriberSystem::get_channel_name() const {
return channel_;
}

std::unique_ptr<AbstractValue>
LcmSubscriberSystem::AllocateSerializerOutputValue() const {
return serializer_->CreateDefaultValue();
}

void LcmSubscriberSystem::CalcSerializerOutputValue(
const Context<double>& context, AbstractValue* output_value) const {
output_value->SetFrom(
context.get_abstract_state().get_value(kStateIndexMessage));
}

void LcmSubscriberSystem::HandleMessage(const void* buffer, int size) {
DRAKE_LOGGER_TRACE("Receiving LCM {} message", channel_);
DRAKE_DEMAND(magic_number_ == kMagic);
Expand Down
4 changes: 0 additions & 4 deletions systems/lcm/lcm_subscriber_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ class LcmSubscriberSystem : public LeafSystem<double> {
systems::CompositeEventCollection<double>* events,
double* time) const final;

std::unique_ptr<AbstractValue> AllocateSerializerOutputValue() const;
void CalcSerializerOutputValue(const Context<double>& context,
AbstractValue* output_value) const;

systems::EventStatus ProcessMessageAndStoreToAbstractState(
const Context<double>&, State<double>* state) const;

Expand Down

0 comments on commit dc6263e

Please sign in to comment.