Drake
lcm_subscriber_system.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <condition_variable>
4 #include <memory>
5 #include <mutex>
6 #include <string>
7 #include <vector>
8 
18 
19 namespace drake {
20 namespace systems {
21 namespace lcm {
22 
41 class LcmSubscriberSystem : public LeafSystem<double>,
43  public:
45 
46 
56  template <typename LcmMessage>
57  static std::unique_ptr<LcmSubscriberSystem> Make(
58  const std::string& channel, drake::lcm::DrakeLcmInterface* lcm) {
59  return std::make_unique<LcmSubscriberSystem>(
60  channel, std::make_unique<Serializer<LcmMessage>>(), lcm);
61  }
62 
75  LcmSubscriberSystem(const std::string& channel,
76  std::unique_ptr<SerializerInterface> serializer,
78 
93  LcmSubscriberSystem(const std::string& channel,
94  const LcmAndVectorBaseTranslator& translator,
96 
110  LcmSubscriberSystem(const std::string& channel,
111  const LcmTranslatorDictionary& translator_dictionary,
113 
114  ~LcmSubscriberSystem() override;
115 
117  static std::string make_name(const std::string& channel);
118 
119  const std::string& get_channel_name() const;
120 
129 
134  int WaitForMessage(int old_message_count) const;
135 
139  int GetMessageCount(const Context<double>& context) const;
140 
141  protected:
142  void DoCalcNextUpdateTime(const Context<double>& context,
144  double* time) const override;
145 
147  const Context<double>&,
149  State<double>* state) const override {
150  ProcessMessageAndStoreToAbstractState(state->get_mutable_abstract_state());
151  }
152 
153  std::unique_ptr<AbstractValues> AllocateAbstractState() const override;
154 
156  const Context<double>&,
158  DiscreteValues<double>* discrete_state) const override {
159  ProcessMessageAndStoreToDiscreteState(discrete_state);
160  }
161 
162  std::unique_ptr<DiscreteValues<double>> AllocateDiscreteState()
163  const override;
164 
165  void SetDefaultState(const Context<double>& context,
166  State<double>* state) const override;
167 
168  private:
169  // All constructors delegate to here.
170  LcmSubscriberSystem(const std::string& channel,
171  const LcmAndVectorBaseTranslator* translator,
172  std::unique_ptr<SerializerInterface> serializer,
174 
175  void ProcessMessageAndStoreToDiscreteState(
176  DiscreteValues<double>* discrete_state) const;
177 
178  void ProcessMessageAndStoreToAbstractState(
179  AbstractValues* abstract_state) const;
180 
181  // Callback entry point from LCM into this class. Also wakes up one thread
182  // block on notification_ if it's not nullptr.
183  void HandleMessage(const std::string& channel, const void* message_buffer,
184  int message_size) override;
185 
186  // This pair of methods is used for the output port when we're using a
187  // translator.
188  std::unique_ptr<BasicVector<double>> AllocateTranslatorOutputValue() const;
189  void CalcTranslatorOutputValue(const Context<double>& context,
190  BasicVector<double>* output_vector) const;
191 
192  // This pair of methods is used for the output port when we're using a
193  // serializer.
194  std::unique_ptr<systems::AbstractValue> AllocateSerializerOutputValue() const;
195  void CalcSerializerOutputValue(const Context<double>& context,
196  AbstractValue* output_value) const;
197 
198  // The channel on which to receive LCM messages.
199  const std::string channel_;
200 
201  // Converts LCM message bytes to VectorBase objects.
202  // Will be non-null iff our output port is vector-valued.
203  const LcmAndVectorBaseTranslator* const translator_{};
204 
205  // Converts LCM message bytes to Value<LcmMessage> objects.
206  // Will be non-null iff our output port is abstract-valued.
207  const std::unique_ptr<SerializerInterface> serializer_;
208 
209  // The mutex that guards received_message_ and received_message_count_.
210  mutable std::mutex received_message_mutex_;
211 
212  // A condition variable that's signaled every time the handler is called.
213  mutable std::condition_variable received_message_condition_variable_;
214 
215  // The bytes of the most recently received LCM message.
216  std::vector<uint8_t> received_message_;
217 
218  // A message counter that's incremented every time the handler is called.
219  int received_message_count_{0};
220 };
221 
222 } // namespace lcm
223 } // namespace systems
224 } // namespace drake
AbstractValues is a container for non-numerical state and parameters.
Definition: abstract_values.h:18
std::unique_ptr< DiscreteValues< double > > AllocateDiscreteState() const override
Reserves the discrete state as required by CreateDefaultContext.
Definition: lcm_subscriber_system.cc:175
~LcmSubscriberSystem() override
Definition: lcm_subscriber_system.cc:83
Definition: automotive_demo.cc:88
This class represents an unrestricted update event.
Definition: event.h:326
A dictionary that maps between LCM channel names and translators that convert between LCM message obj...
Definition: lcm_translator_dictionary.h:20
void DoCalcUnrestrictedUpdate(const Context< double > &, const std::vector< const systems::UnrestrictedUpdateEvent< double > * > &, State< double > *state) const override
Derived-class event handler for all simultaneous unrestricted update events.
Definition: lcm_subscriber_system.h:146
void DoCalcDiscreteVariableUpdates(const Context< double > &, const std::vector< const systems::DiscreteUpdateEvent< double > * > &, DiscreteValues< double > *discrete_state) const override
Derived-class event handler for all simultaneous discrete update events.
Definition: lcm_subscriber_system.h:155
std::vector< double > vector
Definition: translator_test.cc:20
AbstractValues * get_mutable_abstract_state()
Definition: state.h:73
LcmSubscriberSystem(const LcmSubscriberSystem &)=delete
A pure virtual interface that enables LCM to be mocked.
Definition: drake_lcm_interface.h:14
int WaitForMessage(int old_message_count) const
Blocks the caller until old_message_count is different from the internal message counter, and the internal message counter is returned.
Definition: lcm_subscriber_system.cc:271
Receives LCM messages from a given channel and outputs them to a System<double>&#39;s port...
Definition: lcm_subscriber_system.h:41
std::unique_ptr< AbstractValues > AllocateAbstractState() const override
Reserves the abstract state as required by CreateDefaultContext.
Definition: lcm_subscriber_system.cc:191
A superclass template that extends System with some convenience utilities that are not applicable to ...
Definition: leaf_system.h:82
const LcmAndVectorBaseTranslator & get_translator() const
Returns the translator used by this subscriber.
Definition: lcm_subscriber_system.cc:290
void SetDefaultState(const Context< double > &context, State< double > *state) const override
Assigns default values to all elements of the state.
Definition: lcm_subscriber_system.cc:85
static std::unique_ptr< LcmSubscriberSystem > Make(const std::string &channel, drake::lcm::DrakeLcmInterface *lcm)
Factory method that returns a subscriber System that provides Value<LcmMessage> message objects on it...
Definition: lcm_subscriber_system.h:57
A fully type-erased container class.
Definition: value.h:99
void DoCalcNextUpdateTime(const Context< double > &context, systems::CompositeEventCollection< double > *events, double *time) const override
Computes the next time at which this System must perform a discrete action.
Definition: lcm_subscriber_system.cc:145
const double time
Definition: robot_plan_interpolator_test.cc:63
const std::string & get_channel_name() const
Definition: lcm_subscriber_system.cc:210
Defines a message handler interface that must be implemented by all LCM subscribers within Drake...
Definition: drake_lcm_message_handler_interface.h:17
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 GetMessageCount(const Context< double > &context) const
Returns the message counter stored in context.
Definition: lcm_subscriber_system.cc:130
static std::string make_name(const std::string &channel)
Returns the default name for a system that subscribes to channel.
Definition: lcm_subscriber_system.cc:206
#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:35
This class represents a discrete update event.
Definition: event.h:257
Provides careful macros to selectively enable or disable the special member functions for copy-constr...