-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
lcm_scope_system.cc
58 lines (50 loc) · 1.72 KB
/
lcm_scope_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
#include "drake/systems/lcm/lcm_scope_system.h"
#include <memory>
#include "drake/common/drake_throw.h"
#include "drake/lcmt_scope.hpp"
namespace drake {
namespace systems {
namespace lcm {
namespace {
void Convert(
const double time,
const VectorX<double>& input,
lcmt_scope* output) {
output->utime = static_cast<int64_t>(time * 1e6);
output->size = input.size();
output->value.resize(output->size);
for (int i = 0; i < output->size; ++i) {
output->value[i] = input[i];
}
}
} // namespace
LcmScopeSystem::LcmScopeSystem(int size) {
const InputPort<double>& input_port = this->DeclareVectorInputPort(
"input", size);
this->DeclareAbstractOutputPort(
"output",
[]() { return std::make_unique<Value<lcmt_scope>>(lcmt_scope{}); },
[&input_port](const Context<double>& context, AbstractValue* output) {
const auto& input = input_port.Eval(context);
auto& message = output->get_mutable_value<lcmt_scope>();
Convert(context.get_time(), input, &message);
});
}
std::tuple<LcmScopeSystem*, LcmPublisherSystem*> LcmScopeSystem::AddToBuilder(
systems::DiagramBuilder<double>* builder,
drake::lcm::DrakeLcmInterface* lcm,
const OutputPort<double>& signal,
const std::string& channel,
double publish_period) {
DRAKE_THROW_UNLESS(builder != nullptr);
DRAKE_THROW_UNLESS(lcm != nullptr);
auto* scope = builder->AddSystem<LcmScopeSystem>(signal.size());
builder->Connect(signal, scope->get_input_port());
auto* publisher = builder->AddSystem(LcmPublisherSystem::Make<lcmt_scope>(
channel, lcm, publish_period));
builder->Connect(*scope, *publisher);
return {scope, publisher};
}
} // namespace lcm
} // namespace systems
} // namespace drake