Skip to content

Commit

Permalink
fix a counter mismatch bug in lcm_driven_loop (RobotLocomotion#6824)
Browse files Browse the repository at this point in the history
* fix a counter mismatch bug in lcm_driven_loop
  • Loading branch information
siyuanfeng-tri authored Aug 11, 2017
1 parent bfbc9ce commit f27795a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion drake/systems/lcm/lcm_driven_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ LcmDrivenLoop::LcmDrivenLoop(
}

const AbstractValue& LcmDrivenLoop::WaitForMessage() {
message_count_ = driving_sub_.WaitForMessage(message_count_);
driving_sub_.WaitForMessage(driving_sub_.GetMessageCount(*sub_context_));

driving_sub_.CalcNextUpdateTime(*sub_context_, sub_events_.get());

Expand Down
2 changes: 0 additions & 2 deletions drake/systems/lcm/lcm_driven_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ class LcmDrivenLoop {
std::unique_ptr<SystemOutput<double>> sub_output_;
std::unique_ptr<State<double>> sub_swap_state_;
std::unique_ptr<CompositeEventCollection<double>> sub_events_;

int message_count_{0};
};

} // namespace lcm
Expand Down
11 changes: 8 additions & 3 deletions drake/systems/lcm/lcm_subscriber_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ void LcmSubscriberSystem::ProcessMessageAndStoreToAbstractState(
.GetMutableValue<int>() = received_message_count_;
}

void LcmSubscriberSystem::DoCalcNextUpdateTime(
const Context<double>& context,
systems::CompositeEventCollection<double>* events, double* time) const {
int LcmSubscriberSystem::GetMessageCount(const Context<double>& context) const {
// Gets the last message count from either abstract state or discrete state.
int last_message_count;
if (translator_ == nullptr) {
Expand All @@ -141,6 +139,13 @@ void LcmSubscriberSystem::DoCalcNextUpdateTime(
last_message_count = static_cast<int>(
context.get_discrete_state(kStateIndexMessageCount)->GetAtIndex(0));
}
return last_message_count;
}

void LcmSubscriberSystem::DoCalcNextUpdateTime(
const Context<double>& context,
systems::CompositeEventCollection<double>* events, double* time) const {
int last_message_count = GetMessageCount(context);

std::unique_lock<std::mutex> lock(received_message_mutex_);
// Has a new message. Schedule an update event.
Expand Down
5 changes: 5 additions & 0 deletions drake/systems/lcm/lcm_subscriber_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class LcmSubscriberSystem : public LeafSystem<double>,
*/
int WaitForMessage(int old_message_count) const;

/**
* Returns the message counter stored in @p context.
*/
int GetMessageCount(const Context<double>& context) const;

protected:
void DoCalcNextUpdateTime(const Context<double>& context,
systems::CompositeEventCollection<double>* events,
Expand Down

0 comments on commit f27795a

Please sign in to comment.