Drake
lcm_publisher_system.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <vector>
6 
15 
16 namespace drake {
17 
18 // Forward-declare so we can keep a pointer to a DrakeLcm if needed.
19 namespace lcm {
20 class DrakeLcm;
21 } // namespace lcm
22 
23 namespace systems {
24 namespace lcm {
25 
26 /**
27  * Publishes an LCM message containing information from its input port.
28  * Optionally sends a one-time initialization message.
29  *
30  * @note You should generally provide an LCM interface yourself, since there
31  * should normally be just one of these typically-heavyweight objects per
32  * program. However, if you're sure there isn't any other need for an LCM
33  * interface in your program, you can let %LcmPublisherSystem allocate and
34  * maintain a drake::lcm::DrakeLcm object internally.
35  *
36  * @ingroup message_passing
37  */
38 class LcmPublisherSystem : public LeafSystem<double> {
39  public:
41 
42  /**
43  * A factory method that returns an %LcmPublisherSystem that takes
44  * Value<LcmMessage> message objects on its sole abstract-valued input port.
45  *
46  * @tparam LcmMessage message type to serialize, e.g., lcmt_drake_signal.
47  *
48  * @param[in] channel The LCM channel on which to publish.
49  *
50  * @param lcm A pointer to the LCM subsystem to use, which must
51  * remain valid for the lifetime of this object. If null, a
52  * drake::lcm::DrakeLcm object is allocated and maintained internally, but
53  * see the note in the class comments.
54  */
55  template <typename LcmMessage>
56  static std::unique_ptr<LcmPublisherSystem> Make(
57  const std::string& channel,
59  return std::make_unique<LcmPublisherSystem>(
60  channel, std::make_unique<Serializer<LcmMessage>>(), lcm);
61  }
62 
63  /**
64  * A constructor for an %LcmPublisherSystem that takes LCM message objects on
65  * its sole abstract-valued input port. The LCM message type is determined by
66  * the provided `serializer`.
67  *
68  * @param[in] channel The LCM channel on which to publish.
69  *
70  * @param[in] serializer The serializer that converts between byte vectors
71  * and LCM message objects.
72  *
73  * @param lcm A pointer to the LCM subsystem to use, which must
74  * remain valid for the lifetime of this object. If null, a
75  * drake::lcm::DrakeLcm object is allocated and maintained internally, but
76  * see the note in the class comments.
77  */
78  LcmPublisherSystem(const std::string& channel,
79  std::unique_ptr<SerializerInterface> serializer,
81 
82  /**
83  * A constructor for an %LcmPublisherSystem that takes vector data on its sole
84  * vector-valued input port. The vector data is mapped to message content by
85  * the provided `translator`.
86  *
87  * @param[in] channel The LCM channel on which to publish.
88  *
89  * @param[in] translator The translator that converts between LCM message
90  * objects and drake::systems::VectorBase objects. This reference must remain
91  * valid for the lifetime of this object.
92  *
93  * @param lcm A pointer to the LCM subsystem to use, which must
94  * remain valid for the lifetime of this object. If null, a
95  * drake::lcm::DrakeLcm object is allocated and maintained internally, but
96  * see the note in the class comments.
97  */
98  LcmPublisherSystem(const std::string& channel,
99  const LcmAndVectorBaseTranslator& translator,
101 
102  /**
103  * Constructor that returns a publisher System that takes vector data on
104  * its sole vector-valued input port. The vector data are mapped to message
105  * contents by the `translator` found in the provided `translator_dictionary`.
106  *
107  * @param[in] channel The LCM channel on which to publish.
108  *
109  * @param[in] translator_dictionary A dictionary for obtaining the appropriate
110  * translator for a particular LCM channel. This reference must remain
111  * valid for the lifetime of this object.
112  *
113  * @param lcm A pointer to the LCM subsystem to use, which must
114  * remain valid for the lifetime of this object. If null, a
115  * drake::lcm::DrakeLcm object is allocated and maintained internally, but
116  * see the note in the class comments.
117  */
118  LcmPublisherSystem(const std::string& channel,
119  const LcmTranslatorDictionary& translator_dictionary,
121 
122  ~LcmPublisherSystem() override;
123 
124  /**
125  * This is the type of an initialization message publisher that can be
126  * provided via AddInitializationMessage().
127  */
128  using InitializationPublisher = std::function<void(
130 
131  /**
132  * Specifies a message-publishing function to be invoked once from an
133  * initialization event. If this method is not called, no initialization event
134  * will be created.
135  *
136  * You can only call this method once.
137  * @throws std::logic_error if called a second time.
138  *
139  * @pre The publisher function may not be null.
140  */
141  void AddInitializationMessage(
142  InitializationPublisher initialization_publisher);
143 
144  /**
145  * Returns the channel name supplied during construction.
146  */
147  const std::string& get_channel_name() const;
148 
149  /**
150  * Returns the default name for a system that publishes @p channel.
151  */
152  static std::string make_name(const std::string& channel);
153 
154  /**
155  * Sets the publishing period of this system. See
156  * LeafSystem::DeclarePeriodicPublish() for details about the semantics of
157  * parameter `period`.
158  */
159  void set_publish_period(double period);
160 
161  /**
162  * Returns the translator used by this publisher. This can be used to convert
163  * a serialized LCM message provided by
164  * DrakeMockLcm::get_last_published_message() into a BasicVector. It is useful
165  * in unit tests for verifying that a BasicVector was correctly published as
166  * an LCM message.
167  * @pre this system is using a vector-valued port (not abstract-valued).
168  */
169  const LcmAndVectorBaseTranslator& get_translator() const;
170 
171  /**
172  * Returns a mutable reference to the LCM object in use by this publisher.
173  * This may have been supplied in the constructor or may be an
174  * internally-maintained object of type drake::lcm::DrakeLcm.
175  */
177  DRAKE_DEMAND(lcm_ != nullptr);
178  return *lcm_;
179  }
180 
181  /**
182  * Returns the sole input port.
183  */
185  DRAKE_THROW_UNLESS(this->get_num_input_ports() == 1);
187  }
188 
189  DRAKE_DEPRECATED("Don't use the indexed overload; use the no-arg overload.")
190  const InputPort<double>& get_input_port(int index) const {
191  DRAKE_THROW_UNLESS(index == 0);
192  return get_input_port();
193  }
194 
195  // This system has no output ports.
196  void get_output_port(int) = delete;
197 
198  private:
199  // All constructors delegate to here. If the lcm pointer is null, we'll
200  // allocate and maintain a DrakeLcm object internally.
201  LcmPublisherSystem(const std::string& channel,
202  const LcmAndVectorBaseTranslator* translator,
203  std::unique_ptr<SerializerInterface> serializer,
205 
206  // Takes the VectorBase from the input port of the context and publishes
207  // it onto an LCM channel.
208  void DoPublish(
209  const Context<double>& context,
210  const std::vector<const systems::PublishEvent<double>*>&) const override;
211 
212  // The channel on which to publish LCM messages.
213  const std::string channel_;
214 
215  // Optionally, the method to call during initialization (empty if none).
216  InitializationPublisher initialization_publisher_;
217 
218  // Converts VectorBase objects into LCM message bytes.
219  // Will be non-null iff our input port is vector-valued.
220  const LcmAndVectorBaseTranslator* const translator_{};
221 
222  // Converts Value<LcmMessage> objects into LCM message bytes.
223  // Will be non-null iff our input port is abstract-valued.
224  std::unique_ptr<SerializerInterface> serializer_;
225 
226  // If we're not given a DrakeLcm object, we allocate one and keep it here.
227  // The unique_ptr is const, not the held object.
228  std::unique_ptr<drake::lcm::DrakeLcm> const owned_lcm_;
229 
230  // A const pointer to an LCM subsystem. Note that while the pointer is const,
231  // the LCM subsystem is not const. This may refer to an externally-supplied
232  // object or the owned_lcm_ object above.
234 };
235 
236 } // namespace lcm
237 } // namespace systems
238 } // namespace drake
drake::lcm::DrakeLcmInterface & lcm()
Returns a mutable reference to the LCM object in use by this publisher.
Definition: lcm_publisher_system.h:176
Provides a portable macro for use in generating compile-time warnings for use of code that is permitt...
const std::string channel_
Definition: drake_lcm_interface_test.cc:37
Provides a convenient wrapper to throw an exception when a condition is unmet.
Definition: bullet_model.cc:22
std::function< void(const Context< double > &context, drake::lcm::DrakeLcmInterface *lcm)> InitializationPublisher
This is the type of an initialization message publisher that can be provided via AddInitializationMes...
Definition: lcm_publisher_system.h:129
A dictionary that maps between LCM channel names and translators that convert between LCM message obj...
Definition: lcm_translator_dictionary.h:20
const InputPort< double > & get_input_port() const
Returns the sole input port.
Definition: lcm_publisher_system.h:184
#define DRAKE_THROW_UNLESS(condition)
Evaluates condition and iff the value is false will throw an exception with a message showing at leas...
Definition: drake_throw.h:23
std::vector< double > vector
Definition: translator_test.cc:20
Publishes an LCM message containing information from its input port.
Definition: lcm_publisher_system.h:38
DrakeMockLcm lcm_
Definition: drake_lcm_interface_test.cc:36
A pure virtual interface that enables LCM to be mocked.
Definition: drake_lcm_interface.h:22
static std::unique_ptr< LcmPublisherSystem > Make(const std::string &channel, drake::lcm::DrakeLcmInterface *lcm)
A factory method that returns an LcmPublisherSystem that takes Value<LcmMessage> message objects on i...
Definition: lcm_publisher_system.h:56
#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 superclass template that extends System with some convenience utilities that are not applicable to ...
Definition: leaf_system.h:81
#define DRAKE_DEPRECATED(message)
Use DRAKE_DEPRECATED("message") to discourage use of particular classes, typedefs, variables, non-static data members, functions, arguments, enumerations, and template specializations.
Definition: drake_deprecated.h:33
Defines an abstract parent class of all translators that convert between LCM message bytes and drake:...
Definition: lcm_and_vector_base_translator.h:18
This class represents a publish event.
Definition: event.h:332
#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
Provides careful macros to selectively enable or disable the special member functions for copy-constr...