forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcm_subscriber_system.cc
330 lines (288 loc) · 13 KB
/
lcm_subscriber_system.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "drake/systems/lcm/lcm_subscriber_system.h"
#include <functional>
#include <iostream>
#include <utility>
#include "drake/common/drake_assert.h"
#include "drake/common/text_logging.h"
#include "drake/systems/framework/basic_vector.h"
namespace drake {
namespace systems {
namespace lcm {
using drake::lcm::DrakeLcmInterface;
using std::make_unique;
namespace {
constexpr int kStateIndexMessage = 0;
constexpr int kStateIndexMessageCount = 1;
} // namespace
// TODO(jwnimmer-tri) The "serializer xor translator" disjoint implementations
// within the method bodies below are not ideal, because of the code smell, and
// because it is likely confusing for users. We should take further steps to
// make the Value<LcmMessage> port the primary output port, and find a better
// phrasing for the vector-valued output port for users. For now though, this
// implementation serves as a transition point where we don't have to rewrite
// the old code yet, but still can supply the AbstractValue port for new code.
// TODO(siyuan): Probably should only have AbstractValue output, and
// change / move the translator for vector base values to a separate translator
// block.
LcmSubscriberSystem::LcmSubscriberSystem(
const std::string& channel, const LcmAndVectorBaseTranslator* translator,
std::unique_ptr<SerializerInterface> serializer,
drake::lcm::DrakeLcmInterface* lcm)
: channel_(channel),
translator_(translator),
serializer_(std::move(serializer)),
lcm_interface_(lcm) {
DRAKE_DEMAND((translator_ != nullptr) != (serializer_ != nullptr));
DRAKE_DEMAND(lcm);
lcm->Subscribe(channel_, this);
if (translator_ != nullptr) {
// Invoke the translator allocate method once to provide a model value.
DeclareVectorOutputPort(*AllocateTranslatorOutputValue(),
&LcmSubscriberSystem::CalcTranslatorOutputValue);
} else {
// Use the "advanced" method to construct explicit non-member functors
// to deal with the unusual methods we have available.
DeclareAbstractOutputPort(
[this](const Context<double>&) {
return this->AllocateSerializerOutputValue();
},
[this](const Context<double>& context, AbstractValue* out) {
this->CalcSerializerOutputValue(context, out);
});
}
set_name(make_name(channel_));
}
LcmSubscriberSystem::LcmSubscriberSystem(
const std::string& channel, std::unique_ptr<SerializerInterface> serializer,
DrakeLcmInterface* lcm)
: LcmSubscriberSystem(channel, nullptr, std::move(serializer), lcm) {}
LcmSubscriberSystem::LcmSubscriberSystem(
const std::string& channel, const LcmAndVectorBaseTranslator& translator,
DrakeLcmInterface* lcm)
: LcmSubscriberSystem(channel, &translator, nullptr, lcm) {}
LcmSubscriberSystem::LcmSubscriberSystem(
const std::string& channel,
const LcmTranslatorDictionary& translator_dictionary,
DrakeLcmInterface* lcm)
: LcmSubscriberSystem(channel, translator_dictionary.GetTranslator(channel),
lcm) {}
LcmSubscriberSystem::~LcmSubscriberSystem() {}
void LcmSubscriberSystem::SetDefaultState(const Context<double>&,
State<double>* state) const {
if (translator_ != nullptr) {
DRAKE_DEMAND(serializer_ == nullptr);
ProcessMessageAndStoreToDiscreteState(&state->get_mutable_discrete_state());
} else {
DRAKE_DEMAND(translator_ == nullptr);
ProcessMessageAndStoreToAbstractState(&state->get_mutable_abstract_state());
}
}
// The decision to store the decoded message instead of the raw bytes in
// state is to avoid repeated decoding in DoCalcOutput for the latter case.
// However, this computational concern will not exist once caching is properly
// implemented and in place. Same for ProcessMessageAndStoreToAbstractState()
void LcmSubscriberSystem::ProcessMessageAndStoreToDiscreteState(
DiscreteValues<double>* discrete_state) const {
DRAKE_ASSERT(translator_ != nullptr);
DRAKE_ASSERT(serializer_ == nullptr);
std::lock_guard<std::mutex> lock(received_message_mutex_);
if (!received_message_.empty()) {
translator_->Deserialize(
received_message_.data(), received_message_.size(),
&discrete_state->get_mutable_vector(kStateIndexMessage));
}
discrete_state->get_mutable_vector(kStateIndexMessageCount)
.SetAtIndex(0, received_message_count_);
}
void LcmSubscriberSystem::ProcessMessageAndStoreToAbstractState(
AbstractValues* abstract_state) const {
DRAKE_ASSERT(translator_ == nullptr);
DRAKE_ASSERT(serializer_ != nullptr);
std::lock_guard<std::mutex> lock(received_message_mutex_);
if (!received_message_.empty()) {
serializer_->Deserialize(
received_message_.data(), received_message_.size(),
&abstract_state->get_mutable_value(kStateIndexMessage));
}
abstract_state->get_mutable_value(kStateIndexMessageCount)
.GetMutableValue<int>() = received_message_count_;
}
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) {
DRAKE_ASSERT(serializer_ != nullptr);
last_message_count =
context.get_abstract_state<int>(kStateIndexMessageCount);
} else {
DRAKE_ASSERT(serializer_ == nullptr);
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 {
const int last_message_count = GetMessageCount(context);
const int received_message_count = [this]() {
std::unique_lock<std::mutex> lock(received_message_mutex_);
return received_message_count_;
}();
// Has a new message. Schedule an update event.
if (last_message_count != received_message_count) {
// TODO(siyuan): should be context.get_time() once #5725 is resolved.
*time = context.get_time() + 0.0001;
if (translator_ == nullptr) {
EventCollection<UnrestrictedUpdateEvent<double>>& uu_events =
events->get_mutable_unrestricted_update_events();
uu_events.add_event(
std::make_unique<systems::UnrestrictedUpdateEvent<double>>(
Event<double>::TriggerType::kTimed));
} else {
EventCollection<DiscreteUpdateEvent<double>>& du_events =
events->get_mutable_discrete_update_events();
du_events.add_event(
std::make_unique<systems::DiscreteUpdateEvent<double>>(
Event<double>::TriggerType::kTimed));
}
} else {
// Special code to support LCM log playback. For the normal and mock LCM
// interfaces, this always returns inf and returns.
*time = lcm_interface_->GetNextMessageTime();
DRAKE_DEMAND(*time > context.get_time());
if (std::isinf(*time)) {
return;
}
// Schedule a publish event at the next message time. We use a publish
// event here because we only want to generate a side effect to dispatch
// the message properly to all the subscribers, not mutating this system's
// context.)
// Note that for every LCM subscriber in the diagram, they will all schedule
// this event for themselves. However, only the first subscriber executing
// the callback will advance the log and do the message dispatch. This is
// because once the first callback is executed, the front of the log will
// have a different timestamp than the context's time (scheduled callback
// time).
EventCollection<PublishEvent<double>>& pub_events =
events->get_mutable_publish_events();
PublishEvent<double>::PublishCallback callback = [this](
const Context<double>& c, const PublishEvent<double>&) {
// Want to keep polling from the message queue, if they happen
// to be occur at the exact same time.
while (lcm_interface_->GetNextMessageTime() == c.get_time()) {
lcm_interface_->DispatchMessageAndAdvanceLog(c.get_time());
}
};
pub_events.add_event(std::make_unique<systems::PublishEvent<double>>(
Event<double>::TriggerType::kTimed, callback));
}
}
std::unique_ptr<DiscreteValues<double>>
LcmSubscriberSystem::AllocateDiscreteState() const {
// Only make discrete states if we are outputting vector values.
if (translator_ != nullptr) {
DRAKE_DEMAND(serializer_ == nullptr);
std::vector<std::unique_ptr<BasicVector<double>>> discrete_state_vec(2);
discrete_state_vec[kStateIndexMessage] =
this->LcmSubscriberSystem::AllocateTranslatorOutputValue();
discrete_state_vec[kStateIndexMessageCount] =
std::make_unique<BasicVector<double>>(1);
return std::make_unique<DiscreteValues<double>>(
std::move(discrete_state_vec));
}
DRAKE_DEMAND(serializer_ != nullptr);
return LeafSystem<double>::AllocateDiscreteState();
}
std::unique_ptr<AbstractValues> LcmSubscriberSystem::AllocateAbstractState()
const {
// Only make abstract states if we are outputting abstract message.
if (serializer_ != nullptr) {
DRAKE_DEMAND(translator_ == nullptr);
std::vector<std::unique_ptr<systems::AbstractValue>> abstract_vals(2);
abstract_vals[kStateIndexMessage] =
this->LcmSubscriberSystem::AllocateSerializerOutputValue();
abstract_vals[kStateIndexMessageCount] = AbstractValue::Make<int>(0);
return std::make_unique<systems::AbstractValues>(std::move(abstract_vals));
}
DRAKE_DEMAND(translator_ != nullptr);
return LeafSystem<double>::AllocateAbstractState();
}
std::string LcmSubscriberSystem::make_name(const std::string& channel) {
return "LcmSubscriberSystem(" + channel + ")";
}
const std::string& LcmSubscriberSystem::get_channel_name() const {
return channel_;
}
// This is only called if our output port is vector-valued, because we are
// using a translator.
std::unique_ptr<BasicVector<double>>
LcmSubscriberSystem::AllocateTranslatorOutputValue() const {
DRAKE_DEMAND(translator_ != nullptr && serializer_ == nullptr);
auto result = translator_->AllocateOutputVector();
if (result) {
return result;
}
return std::make_unique<BasicVector<double>>(translator_->get_vector_size());
}
void LcmSubscriberSystem::CalcTranslatorOutputValue(
const Context<double>& context, BasicVector<double>* output_vector) const {
DRAKE_DEMAND(translator_ != nullptr && serializer_ == nullptr);
output_vector->SetFrom(context.get_discrete_state(kStateIndexMessage));
}
// This is only called if our output port is abstract-valued, because we are
// using a serializer.
std::unique_ptr<systems::AbstractValue>
LcmSubscriberSystem::AllocateSerializerOutputValue() const {
DRAKE_DEMAND(translator_ == nullptr && serializer_ != nullptr);
return serializer_->CreateDefaultValue();
}
void LcmSubscriberSystem::CalcSerializerOutputValue(
const Context<double>& context, AbstractValue* output_value) const {
DRAKE_DEMAND(serializer_.get() != nullptr);
output_value->SetFrom(
context.get_abstract_state().get_value(kStateIndexMessage));
}
void LcmSubscriberSystem::HandleMessage(const std::string& channel,
const void* message_buffer,
int message_size) {
SPDLOG_TRACE(drake::log(), "Receiving LCM {} message", channel);
if (channel == channel_) {
const uint8_t* const rbuf_begin =
static_cast<const uint8_t*>(message_buffer);
const uint8_t* const rbuf_end = rbuf_begin + message_size;
std::lock_guard<std::mutex> lock(received_message_mutex_);
received_message_.clear();
received_message_.insert(received_message_.begin(), rbuf_begin, rbuf_end);
received_message_count_++;
received_message_condition_variable_.notify_all();
} else {
std::cerr << "LcmSubscriberSystem: HandleMessage: WARNING: Received a "
<< "message for channel \"" << channel
<< "\" instead of channel \"" << channel_ << "\". Ignoring it."
<< std::endl;
}
}
int LcmSubscriberSystem::WaitForMessage(int old_message_count) const {
// The message buffer and counter are updated in HandleMessage(), which is
// a callback function invoked by a different thread owned by the
// drake::lcm::DrakeLcmInterface instance passed to the constructor. Thus,
// for thread safety, these need to be properly protected by a mutex.
std::unique_lock<std::mutex> lock(received_message_mutex_);
// This while loop is necessary to guard for spurious wakeup:
// https://en.wikipedia.org/wiki/Spurious_wakeup
while (old_message_count == received_message_count_)
// When wait returns, lock is atomically acquired. So it's thread safe to
// read received_message_count_.
received_message_condition_variable_.wait(lock);
int new_message_count = received_message_count_;
lock.unlock();
return new_message_count;
}
const LcmAndVectorBaseTranslator& LcmSubscriberSystem::get_translator() const {
DRAKE_DEMAND(translator_ != nullptr);
return *translator_;
}
} // namespace lcm
} // namespace systems
} // namespace drake