Drake
translator_system.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <utility>
5 
9 
10 namespace drake {
11 namespace systems {
12 namespace lcm {
13 
14 /// @cond
15 namespace translator_system_detail {
16 
17 template <typename DataType, bool is_vector>
18 struct DataTypeTraits {};
19 
20 // Convenience IO port getters for DataType derived from
21 // systems::VectorBase<double>.
22 template <typename DataType>
23 struct DataTypeTraits<DataType, true> {
24  // Returns a const reference of DataType that corresponds to the first
25  // input port in @p context. Assumes that the input port is vector valued,
26  // and is of type DataType.
27  static const DataType& get_data(const System<double>& sys,
28  const Context<double>& context) {
29  const DataType* const vector =
30  dynamic_cast<const DataType*>(sys.EvalVectorInput(context, 0));
31  DRAKE_DEMAND(vector != nullptr);
32  return *vector;
33  }
34 };
35 /// @endcond
36 
37 // Convenience IO port getters for DataType not derived from
38 // systems::VectorBase<double>.
39 template <typename DataType>
40 struct DataTypeTraits<DataType, false> {
41  // Returns a const reference of DataType that corresponds to the first
42  // input port in @p context. Assumes that the input port is abstract valued,
43  // and is of type DataType.
44  static const DataType& get_data(const System<double>& sys,
45  const Context<double>& context) {
46  const AbstractValue* const value = sys.EvalAbstractInput(context, 0);
47  DRAKE_DEMAND(value != nullptr);
48  return value->GetValue<DataType>();
49  }
50 };
51 
52 } // namespace translator_system_detail
53 
54 /**
55  * An encoding system that converts data of DataType to a Lcm message of
56  * MsgType. This system has exactly one input port and one output port. The
57  * output port is always of abstract value, which contains the encoded Lcm
58  * message. If DataType is derived from systems::VectorBase<double>, the
59  * input port will be vector valued. The input port will be abstract valued
60  * otherwise. The input and output ports' model values are specified by the
61  * translator instance passed to the constructor.
62  */
63 template <typename DataType, typename MsgType>
64 class LcmEncoderSystem : public LeafSystem<double> {
65  public:
67 
68  using TranslatorType = typename drake::lcm::TranslatorBase<DataType, MsgType>;
69 
70  /**
71  * Constructor when DataType is derived from systems::VectorBase<double>.
72  * Declares a vector valued input port and an abstract output port. Sets the
73  * model values for the input and output ports to @p translator's
74  * corresponding default values.
75  *
76  * @param translator Translator, whose ownership is transferred to this
77  * instance.
78  */
79  template <typename DataType1 = DataType,
80  typename = typename std::enable_if<std::is_base_of<
81  systems::VectorBase<double>, DataType1>::value>::type>
82  explicit LcmEncoderSystem(std::unique_ptr<TranslatorType> translator)
83  : translator_(std::move(translator)) {
84  DeclareVectorInputPort(translator_->get_default_data());
85  DeclareAbstractOutputPort(translator_->get_default_msg(),
86  &LcmEncoderSystem::EncodeMsg);
87  }
88 
89  /**
90  * Constructor when DataType is not derived from systems::VectorBase<double>.
91  * Declares an abstract valued input port and an abstract output port. Sets
92  * the model values for the input and output ports to @p translator's
93  * corresponding default values.
94  *
95  * @param translator Translator, whose ownership is transferred to this.
96  */
97  template <typename DataType1 = DataType,
98  typename = typename std::enable_if<!std::is_base_of<
99  systems::VectorBase<double>, DataType1>::value>::type,
100  typename = void>
101  explicit LcmEncoderSystem(std::unique_ptr<TranslatorType> translator)
102  : translator_(std::move(translator)) {
103  DeclareAbstractInputPort(Value<DataType1>(translator_->get_default_data()));
104  DeclareAbstractOutputPort(translator_->get_default_msg(),
105  &LcmEncoderSystem::EncodeMsg);
106  }
107 
108  /**
109  * Returns a const reference to the translator.
110  */
111  const TranslatorType& get_translator() const { return *translator_; }
112 
113  private:
114  void EncodeMsg(const Context<double>& context, MsgType* msg) const {
115  const DataType& data = translator_system_detail::DataTypeTraits<
116  DataType, std::is_base_of<VectorBase<double>,
117  DataType>::value>::get_data(*this, context);
118  translator_->Encode(data, msg);
119  translator_->EncodeTime(context.get_time(), msg);
120  }
121 
122  std::unique_ptr<TranslatorType> translator_;
123 };
124 
125 /**
126  * A decoding system that converts a Lcm message of MsgTypedata to data of
127  * DataType. This system has exactly one input port and one output port. The
128  * input port is always of abstract value, which contains the encoded Lcm
129  * message. If DataType is derived from systems::VectorBase<double>, the
130  * output port will be vector valued. The output port will be abstract valued
131  * otherwise. The input and output ports' model values are specified by the
132  * translator instance passed to the constructor.
133  */
134 template <typename DataType, typename MsgType>
135 class LcmDecoderSystem : public LeafSystem<double> {
136  public:
138 
139  using TranslatorType = typename drake::lcm::TranslatorBase<DataType, MsgType>;
140 
141  /**
142  * Constructor when DataType is derived from systems::VectorBase<double>.
143  * Declares an abstract input port and a vector valued output port. Sets the
144  * model values for the input and output ports to @p translator's
145  * corresponding default values.
146  *
147  * @param translator Translator, whose ownership is transferred to this.
148  */
149  template <typename DataType1 = DataType,
150  typename = typename std::enable_if<std::is_base_of<
151  systems::VectorBase<double>, DataType1>::value>::type>
152  explicit LcmDecoderSystem(std::unique_ptr<TranslatorType> translator)
153  : translator_(std::move(translator)) {
154  DeclareAbstractInputPort(Value<MsgType>(translator_->get_default_msg()));
155  DeclareVectorOutputPort(
156  translator_->get_default_data(),
157  &LcmDecoderSystem::DecodeMsg);
158  }
159 
160  /**
161  * Constructor when DataType is not derived from systems::VectorBase<double>.
162  * Declares an abstract input port and an abstract valued output port. Sets
163  * the model values for the input and output ports to @p translator's
164  * corresponding default values.
165  *
166  * @param translator Translator, whose ownership is transferred to this.
167  */
168  template <typename DataType1 = DataType,
169  typename = typename std::enable_if<!std::is_base_of<
170  systems::VectorBase<double>, DataType1>::value>::type,
171  typename = void>
172  explicit LcmDecoderSystem(std::unique_ptr<TranslatorType> translator)
173  : translator_(std::move(translator)) {
174  DeclareAbstractInputPort(Value<MsgType>(translator_->get_default_msg()));
175  DeclareAbstractOutputPort(
176  translator_->get_default_data(),
177  &LcmDecoderSystem::DecodeMsg);
178  }
179 
180  /**
181  * Returns a const reference to the translator.
182  */
183  const TranslatorType& get_translator() const { return *translator_; }
184 
185  private:
186  void DecodeMsg(const Context<double>& context, DataType* vector) const {
187  const MsgType& msg =
188  this->EvalAbstractInput(context, 0)->template GetValue<MsgType>();
189  translator_->Decode(msg, vector);
190  }
191 
192  std::unique_ptr<TranslatorType> translator_;
193 };
194 
195 } // namespace lcm
196 } // namespace systems
197 } // namespace drake
LcmEncoderSystem(std::unique_ptr< TranslatorType > translator)
Constructor when DataType is not derived from systems::VectorBase<double>.
Definition: translator_system.h:101
Definition: automotive_demo.cc:88
typename drake::lcm::TranslatorBase< DataType, MsgType > TranslatorType
Definition: translator_system.h:68
STL namespace.
typename drake::lcm::TranslatorBase< DataType, MsgType > TranslatorType
Definition: translator_system.h:139
VectorBase is an abstract base class that real-valued signals between Systems and real-valued System ...
Definition: vector_base.h:27
std::vector< double > vector
Definition: translator_test.cc:20
const T & GetValue() const
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:125
const TranslatorType & get_translator() const
Returns a const reference to the translator.
Definition: translator_system.h:183
int value
Definition: copyable_unique_ptr_test.cc:61
#define DRAKE_DEMAND(condition)
Evaluates condition and iff the value is false will trigger an assertion failure with a message showi...
Definition: drake_assert.h:45
A decoding system that converts a Lcm message of MsgTypedata to data of DataType. ...
Definition: translator_system.h:135
const T & get_time() const
Returns the current time in seconds.
Definition: context.h:49
A superclass template that extends System with some convenience utilities that are not applicable to ...
Definition: leaf_system.h:82
const TranslatorType & get_translator() const
Returns a const reference to the translator.
Definition: translator_system.h:111
A fully type-erased container class.
Definition: value.h:99
An encoding system that converts data of DataType to a Lcm message of MsgType.
Definition: translator_system.h:64
A container class for an arbitrary type T.
Definition: value.h:222
#define DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Classname)
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN deletes the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:33
LcmDecoderSystem(std::unique_ptr< TranslatorType > translator)
Constructor when DataType is not derived from systems::VectorBase<double>.
Definition: translator_system.h:172
static const DataType & get_data(const System< double > &sys, const Context< double > &context)
Definition: translator_system.h:44
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
int data
Definition: value_test.cc:19
const AbstractValue * EvalAbstractInput(const Context< T > &context, int port_index) const
Causes the abstract-valued input port with the given port_index to become up-to-date, delegating to our parent Diagram if necessary.
Definition: system.h:416