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 
7 #include "drake/common/drake_copyable.h"
8 #include "drake/common/drake_deprecated.h"
9 #include "drake/common/drake_throw.h"
10 #include "drake/lcm/drake_lcm_interface.h"
11 #include "drake/systems/framework/leaf_system.h"
12 #include "drake/systems/lcm/lcm_and_vector_base_translator.h"
13 #include "drake/systems/lcm/lcm_translator_dictionary.h"
14 #include "drake/systems/lcm/serializer.h"
15 
16 namespace drake {
17 namespace systems {
18 namespace lcm {
19 
20 /**
21  * Publishes an LCM message containing information from its input port.
22  *
23  * @ingroup message_passing
24  */
25 class LcmPublisherSystem : public LeafSystem<double> {
26  public:
28 
29  /**
30  * A factory method that returns an %LcmPublisherSystem that takes
31  * Value<LcmMessage> message objects on its sole abstract-valued input port.
32  *
33  * @tparam LcmMessage message type to serialize, e.g., lcmt_drake_signal.
34  *
35  * @param[in] channel The LCM channel on which to publish.
36  *
37  * @param lcm A non-null pointer to the LCM subsystem. The pointer must remain
38  * valid for the lifetime of this object.
39  */
40  template <typename LcmMessage>
41  static std::unique_ptr<LcmPublisherSystem> Make(
42  const std::string& channel, drake::lcm::DrakeLcmInterface* lcm) {
43  return std::make_unique<LcmPublisherSystem>(
44  channel, std::make_unique<Serializer<LcmMessage>>(), lcm);
45  }
46 
47  // TODO(siyuan): add multiple DrakeLcmInterface, so you can publish to a log
48  // and real lcm at the same time.
49 
50  /**
51  * A constructor for an %LcmPublisherSystem that takes LCM message objects on
52  * its sole abstract-valued input port. The LCM message type is determined by
53  * the provided `serializer`.
54  *
55  * @param[in] channel The LCM channel on which to publish.
56  *
57  * @param[in] serializer The serializer that converts between byte vectors
58  * and LCM message objects.
59  *
60  * @param lcm A non-null pointer to the LCM subsystem to publish on.
61  * The pointer must remain valid for the lifetime of this object.
62  */
63  LcmPublisherSystem(const std::string& channel,
64  std::unique_ptr<SerializerInterface> serializer,
66 
67  /**
68  * A constructor for an %LcmPublisherSystem that takes vector data on its sole
69  * vector-valued input port. The vector data is mapped to message content by
70  * the provided `translator`.
71  *
72  * @param[in] channel The LCM channel on which to publish.
73  *
74  * @param[in] translator The translator that converts between LCM message
75  * objects and drake::systems::VectorBase objects. This reference must remain
76  * valid for the lifetime of this object.
77  *
78  * @param lcm A non-null pointer to the LCM subsystem to publish on.
79  * The pointer must remain valid for the lifetime of this object.
80  */
81  LcmPublisherSystem(const std::string& channel,
82  const LcmAndVectorBaseTranslator& translator,
84 
85  /**
86  * Constructor that returns a publisher System that takes vector data on
87  * its sole vector-valued input port. The vector data are mapped to message
88  * contents by the `translator` found in the provided `translator_dictionary`.
89  *
90  * @param[in] channel The LCM channel on which to publish.
91  *
92  * @param[in] translator_dictionary A dictionary for obtaining the appropriate
93  * translator for a particular LCM channel. This reference must remain
94  * valid for the lifetime of this object.
95  *
96  * @param lcm A non-null pointer to the LCM subsystem to publish on. The
97  * pointer must remain valid for the lifetime of this object.
98  */
99  LcmPublisherSystem(const std::string& channel,
100  const LcmTranslatorDictionary& translator_dictionary,
102 
103  ~LcmPublisherSystem() override;
104 
105  const std::string& get_channel_name() const;
106 
107  /// Returns the default name for a system that publishes @p channel.
108  static std::string make_name(const std::string& channel);
109 
110  /**
111  * Sets the publishing period of this system. See
112  * LeafSystem::DeclarePublishPeriodSec() for details about the semantics of
113  * parameter `period`.
114  */
115  void set_publish_period(double period);
116 
117  /**
118  * Takes the VectorBase from the input port of the context and publishes
119  * it onto an LCM channel.
120  */
121  void DoPublish(
122  const Context<double>& context,
123  const std::vector<const systems::PublishEvent<double>*>&) const override;
124 
125  /**
126  * Returns the translator used by this publisher. This can be used to convert
127  * a serialized LCM message provided by
128  * DrakeMockLcm::get_last_published_message() into a BasicVector. It is useful
129  * in unit tests for verifying that a BasicVector was correctly published as
130  * an LCM message.
131  * @pre this system is using a vector-valued port (not abstract-valued).
132  */
134 
135  /// Returns the sole input port.
139  }
140 
141  DRAKE_DEPRECATED("Don't use the indexed overload; use the no-arg overload.")
142  const InputPortDescriptor<double>& get_input_port(int index) const {
143  DRAKE_THROW_UNLESS(index == 0);
144  return get_input_port();
145  }
146 
147  // This system has no output ports.
148  void get_output_port(int) = delete;
149 
150  private:
151  // All constructors delegate to here.
152  LcmPublisherSystem(const std::string& channel,
153  const LcmAndVectorBaseTranslator* translator,
154  std::unique_ptr<SerializerInterface> serializer,
156 
157  // The channel on which to publish LCM messages.
158  const std::string channel_;
159 
160  // Converts VectorBase objects into LCM message bytes.
161  // Will be non-null iff our input port is vector-valued.
162  const LcmAndVectorBaseTranslator* const translator_{};
163 
164  // Converts Value<LcmMessage> objects into LCM message bytes.
165  // Will be non-null iff our input port is abstract-valued.
166  std::unique_ptr<SerializerInterface> serializer_;
167 
168  // A const pointer to an LCM subsystem. Note that while the pointer is const,
169  // the LCM subsystem is not const.
170  drake::lcm::DrakeLcmInterface* const lcm_{};
171 };
172 
173 } // namespace lcm
174 } // namespace systems
175 } // namespace drake
Definition: automotive_demo.cc:89
void set_publish_period(double period)
Sets the publishing period of this system.
Definition: lcm_publisher_system.cc:81
const std::string & get_channel_name() const
Definition: lcm_publisher_system.cc:77
A dictionary that maps between LCM channel names and translators that convert between LCM message obj...
Definition: lcm_translator_dictionary.h:20
const InputPortDescriptor< T > & get_input_port(int port_index) const
Returns the descriptor of the input port at index port_index.
Definition: system.h:977
#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:25
static std::string make_name(const std::string &channel)
Returns the default name for a system that publishes channel.
Definition: lcm_publisher_system.cc:73
A pure virtual interface that enables LCM to be mocked.
Definition: drake_lcm_interface.h:15
const InputPortDescriptor< double > & get_input_port() const
Returns the sole input port.
Definition: lcm_publisher_system.h:136
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:41
A superclass template that extends System with some convenience utilities that are not applicable to ...
Definition: leaf_system.h:82
void DoPublish(const Context< double > &context, const std::vector< const systems::PublishEvent< double > * > &) const override
Takes the VectorBase from the input port of the context and publishes it onto an LCM channel...
Definition: lcm_publisher_system.cc:85
#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
const LcmAndVectorBaseTranslator & get_translator() const
Returns the translator used by this publisher.
Definition: lcm_publisher_system.cc:109
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:215
~LcmPublisherSystem() override
Definition: lcm_publisher_system.cc:71
#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
int get_num_input_ports() const
Returns the number of input ports of the system.
Definition: system.h:967
LcmPublisherSystem(const LcmPublisherSystem &)=delete