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 
13 
14 namespace drake {
15 namespace systems {
16 namespace lcm {
17 
18 /**
19  * Publishes an LCM message containing information from its input port.
20  *
21  * @ingroup message_passing
22  */
23 class LcmPublisherSystem : public LeafSystem<double> {
24  public:
26 
27  /**
28  * A factory method that returns an %LcmPublisherSystem that takes
29  * Value<LcmMessage> message objects on its sole abstract-valued input port.
30  *
31  * @tparam LcmMessage message type to serialize, e.g., lcmt_drake_signal.
32  *
33  * @param[in] channel The LCM channel on which to publish.
34  *
35  * @param lcm A non-null pointer to the LCM subsystem. The pointer must remain
36  * valid for the lifetime of this object.
37  */
38  template <typename LcmMessage>
39  static std::unique_ptr<LcmPublisherSystem> Make(
40  const std::string& channel, drake::lcm::DrakeLcmInterface* lcm) {
41  return std::make_unique<LcmPublisherSystem>(
42  channel, std::make_unique<Serializer<LcmMessage>>(), lcm);
43  }
44 
45  // TODO(siyuan): add multiple DrakeLcmInterface, so you can publish to a log
46  // and real lcm at the same time.
47 
48  /**
49  * A constructor for an %LcmPublisherSystem that takes LCM message objects on
50  * its sole abstract-valued input port. The LCM message type is determined by
51  * the provided `serializer`.
52  *
53  * @param[in] channel The LCM channel on which to publish.
54  *
55  * @param[in] serializer The serializer that converts between byte vectors
56  * and LCM message objects.
57  *
58  * @param lcm A non-null pointer to the LCM subsystem to publish on.
59  * The pointer must remain valid for the lifetime of this object.
60  */
61  LcmPublisherSystem(const std::string& channel,
62  std::unique_ptr<SerializerInterface> serializer,
64 
65  /**
66  * A constructor for an %LcmPublisherSystem that takes vector data on its sole
67  * vector-valued input port. The vector data is mapped to message content by
68  * the provided `translator`.
69  *
70  * @param[in] channel The LCM channel on which to publish.
71  *
72  * @param[in] translator The translator that converts between LCM message
73  * objects and drake::systems::VectorBase objects. This reference must remain
74  * valid for the lifetime of this object.
75  *
76  * @param lcm A non-null pointer to the LCM subsystem to publish on.
77  * The pointer must remain valid for the lifetime of this object.
78  */
79  LcmPublisherSystem(const std::string& channel,
80  const LcmAndVectorBaseTranslator& translator,
82 
83  /**
84  * Constructor that returns a publisher System that takes vector data on
85  * its sole vector-valued input port. The vector data are mapped to message
86  * contents by the `translator` found in the provided `translator_dictionary`.
87  *
88  * @param[in] channel The LCM channel on which to publish.
89  *
90  * @param[in] translator_dictionary A dictionary for obtaining the appropriate
91  * translator for a particular LCM channel. This reference must remain
92  * valid for the lifetime of this object.
93  *
94  * @param lcm A non-null pointer to the LCM subsystem to publish on. The
95  * pointer must remain valid for the lifetime of this object.
96  */
97  LcmPublisherSystem(const std::string& channel,
98  const LcmTranslatorDictionary& translator_dictionary,
100 
101  ~LcmPublisherSystem() override;
102 
103  const std::string& get_channel_name() const;
104 
105  /// Returns the default name for a system that publishes @p channel.
106  static std::string make_name(const std::string& channel);
107 
108  /**
109  * Sets the publishing period of this system. See
110  * LeafSystem::DeclarePublishPeriodSec() for details about the semantics of
111  * parameter `period`.
112  */
113  void set_publish_period(double period);
114 
115  /**
116  * Takes the VectorBase from the input port of the context and publishes
117  * it onto an LCM channel.
118  */
119  void DoPublish(
120  const Context<double>& context,
121  const std::vector<const systems::PublishEvent<double>*>&) const override;
122 
123  /**
124  * Returns the translator used by this publisher. This can be used to convert
125  * a serialized LCM message provided by
126  * DrakeMockLcm::get_last_published_message() into a BasicVector. It is useful
127  * in unit tests for verifying that a BasicVector was correctly published as
128  * an LCM message.
129  * @pre this system is using a vector-valued port (not abstract-valued).
130  */
132 
133  private:
134  // All constructors delegate to here.
135  LcmPublisherSystem(const std::string& channel,
136  const LcmAndVectorBaseTranslator* translator,
137  std::unique_ptr<SerializerInterface> serializer,
139 
140  // The channel on which to publish LCM messages.
141  const std::string channel_;
142 
143  // Converts VectorBase objects into LCM message bytes.
144  // Will be non-null iff our input port is vector-valued.
145  const LcmAndVectorBaseTranslator* const translator_{};
146 
147  // Converts Value<LcmMessage> objects into LCM message bytes.
148  // Will be non-null iff our input port is abstract-valued.
149  std::unique_ptr<SerializerInterface> serializer_;
150 
151  // A const pointer to an LCM subsystem. Note that while the pointer is const,
152  // the LCM subsystem is not const.
153  drake::lcm::DrakeLcmInterface* const lcm_{};
154 };
155 
156 } // namespace lcm
157 } // namespace systems
158 } // namespace drake
Definition: automotive_demo.cc:88
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
std::vector< double > vector
Definition: translator_test.cc:20
Publishes an LCM message containing information from its input port.
Definition: lcm_publisher_system.h:23
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
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:39
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
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
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
LcmPublisherSystem(const LcmPublisherSystem &)=delete