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 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  /**
48  * A constructor for an %LcmPublisherSystem that takes LCM message objects on
49  * its sole abstract-valued input port. The LCM message type is determined by
50  * the provided `serializer`.
51  *
52  * @param[in] channel The LCM channel on which to publish.
53  *
54  * @param[in] serializer The serializer that converts between byte vectors
55  * and LCM message objects.
56  *
57  * @param lcm A non-null pointer to the LCM subsystem to publish on.
58  * The pointer must remain valid for the lifetime of this object.
59  */
60  LcmPublisherSystem(const std::string& channel,
61  std::unique_ptr<SerializerInterface> serializer,
63 
64  /**
65  * A constructor for an %LcmPublisherSystem that takes vector data on its sole
66  * vector-valued input port. The vector data is mapped to message content by
67  * the provided `translator`.
68  *
69  * @param[in] channel The LCM channel on which to publish.
70  *
71  * @param[in] translator The translator that converts between LCM message
72  * objects and drake::systems::VectorBase objects. This reference must remain
73  * valid for the lifetime of this object.
74  *
75  * @param lcm A non-null pointer to the LCM subsystem to publish on.
76  * The pointer must remain valid for the lifetime of this object.
77  */
78  LcmPublisherSystem(const std::string& channel,
79  const LcmAndVectorBaseTranslator& translator,
81 
82  /**
83  * Constructor that returns a publisher System that takes vector data on
84  * its sole vector-valued input port. The vector data are mapped to message
85  * contents by the `translator` found in the provided `translator_dictionary`.
86  *
87  * @param[in] channel The LCM channel on which to publish.
88  *
89  * @param[in] translator_dictionary A dictionary for obtaining the appropriate
90  * translator for a particular LCM channel. This reference must remain
91  * valid for the lifetime of this object.
92  *
93  * @param lcm A non-null pointer to the LCM subsystem to publish on. The
94  * pointer must remain valid for the lifetime of this object.
95  */
96  LcmPublisherSystem(const std::string& channel,
97  const LcmTranslatorDictionary& translator_dictionary,
99 
100  ~LcmPublisherSystem() override;
101 
102  const std::string& get_channel_name() const;
103 
104  /// Returns the default name for a system that publishes @p channel.
105  static std::string make_name(const std::string& channel);
106 
107  /**
108  * Sets the publishing period of this system. See
109  * LeafSystem::DeclarePeriodicPublish() for details about the semantics of
110  * parameter `period`.
111  */
112  void set_publish_period(double period);
113 
114  /**
115  * Takes the VectorBase from the input port of the context and publishes
116  * it onto an LCM channel.
117  */
118  void DoPublish(
119  const Context<double>& context,
120  const std::vector<const systems::PublishEvent<double>*>&) const override;
121 
122  /**
123  * Returns the translator used by this publisher. This can be used to convert
124  * a serialized LCM message provided by
125  * DrakeMockLcm::get_last_published_message() into a BasicVector. It is useful
126  * in unit tests for verifying that a BasicVector was correctly published as
127  * an LCM message.
128  * @pre this system is using a vector-valued port (not abstract-valued).
129  */
131 
132  /// Returns the sole input port.
136  }
137 
138  DRAKE_DEPRECATED("Don't use the indexed overload; use the no-arg overload.")
139  const InputPort<double>& get_input_port(int index) const {
140  DRAKE_THROW_UNLESS(index == 0);
141  return get_input_port();
142  }
143 
144  // This system has no output ports.
145  void get_output_port(int) = delete;
146 
147  private:
148  // All constructors delegate to here.
149  LcmPublisherSystem(const std::string& channel,
150  const LcmAndVectorBaseTranslator* translator,
151  std::unique_ptr<SerializerInterface> serializer,
153 
154  // The channel on which to publish LCM messages.
155  const std::string channel_;
156 
157  // Converts VectorBase objects into LCM message bytes.
158  // Will be non-null iff our input port is vector-valued.
159  const LcmAndVectorBaseTranslator* const translator_{};
160 
161  // Converts Value<LcmMessage> objects into LCM message bytes.
162  // Will be non-null iff our input port is abstract-valued.
163  std::unique_ptr<SerializerInterface> serializer_;
164 
165  // A const pointer to an LCM subsystem. Note that while the pointer is const,
166  // the LCM subsystem is not const.
167  drake::lcm::DrakeLcmInterface* const lcm_{};
168 };
169 
170 } // namespace lcm
171 } // namespace systems
172 } // namespace drake
Provides a portable macro for use in generating compile-time warnings for use of code that is permitt...
Provides a convenient wrapper to throw an exception when a condition is unmet.
Definition: automotive_demo.cc:90
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 InputPort< T > & get_input_port(int port_index) const
Returns the typed input port at index port_index.
Definition: system.h:984
const InputPort< double > & get_input_port() const
Returns the sole input port.
Definition: lcm_publisher_system.h:133
#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: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:41
A superclass template that extends System with some convenience utilities that are not applicable to ...
Definition: leaf_system.h:84
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
int get_num_input_ports() const
Returns the number of input ports currently allocated in this System.
Definition: system_base.h:172
This class represents a publish event.
Definition: event.h:332
~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
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
LcmPublisherSystem(const LcmPublisherSystem &)=delete