Drake
drake_visualizer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 
9 #include "drake/lcmt_viewer_draw.hpp"
10 #include "drake/lcmt_viewer_load_robot.hpp"
17 
18 namespace drake {
19 namespace systems {
20 
56 class DrakeVisualizer : public LeafSystem<double> {
57  public:
59 
60 
79  bool enable_playback = false);
80 
86  void set_publish_period(double period);
87 
88  // TODO(SeanCurtis-TRI): Optional features:
89  // 1. Specify number of loops (<= 0 --> infinite looping)
90  // 2. Specify range of playback [start, end] for cached data from times
91  // in the range [0, T], such that start < end, start >= 0 and
92  // end <= T. (Although, putting end > T *is* valid, it would
93  // manifest as a *pause* at the end of the playback before finishing.
94  // 3. Optionally force the replay to emit the messages to load the
95  // geometry again.
96  // 4. Specify playback rate.
97  // 5. Add a wall-clock scale factor; e.g., play faster than real time,
98  // slower than real time, etc.
104  void ReplayCachedSimulation() const;
105 
109  void PlaybackTrajectory(
110  const PiecewisePolynomial<double>& input_trajectory) const;
111 
112  private:
113  // Returns true if initialization phase has been completed.
114  bool is_load_message_sent(const Context<double>& context) const {
115  return context.get_discrete_state(0)->GetAtIndex(0) > 0;
116  }
117 
118  // Sets the discrete state to @p flag.
119  void set_is_load_message_sent(DiscreteValues<double>* state,
120  bool flag) const {
121  if (flag)
122  state->get_mutable_vector(0)->SetAtIndex(0, 1);
123  else
124  state->get_mutable_vector(0)->SetAtIndex(0, 0);
125  }
126 
127  // Set the default to "initialization phase has not been completed."
128  void SetDefaultState(const Context<double>&, State<double>* state)
129  const override {
130  set_is_load_message_sent(state->get_mutable_discrete_state(), false);
131  }
132 
133  // If initialization has not been completed, schedule a DiscreteStateUpdate
134  // shortly to perform the initialization. Otherwise, returns
135  // LeafSystem<double>::DoCalcNextUpdateTime(context, events)
136  void DoCalcNextUpdateTime(const Context<double>& context,
138  double* time) const override;
139 
140  // Sets the initialization flag to true, and calls PublishLoadRobot().
141  void DoCalcDiscreteVariableUpdates(
142  const Context<double>& context,
144  DiscreteValues<double>* discrete_state) const override;
145 
146  // Publishes a draw message if initialization is completed. Otherwise, it
147  // emits a warning and return.
148  void DoPublish(const systems::Context<double>& context,
149  const std::vector<const PublishEvent<double>*>&)
150  const override;
151 
152  // Publishes a lcmt_viewer_load_robot message containing a description
153  // of what should be visualized. The message is intended to be received by the
154  // Drake Visualizer.
155  void PublishLoadRobot() const;
156 
157  // A pointer to the LCM subsystem. It is through this object that LCM messages
158  // are published.
159  drake::lcm::DrakeLcmInterface* const lcm_;
160 
161  // The LCM load message to send to the Drake Visualizer.
162  const lcmt_viewer_load_robot load_message_;
163 
164  // The translator that converts from the RigidBodyTree's generalized state
165  // vector to a lcmt_viewer_draw message.
166  const ViewerDrawTranslator draw_message_translator_;
167 
168  // The (optional) log used for recording and playback.
169  std::unique_ptr<SignalLog<double>> log_{nullptr};
170 };
171 
172 } // namespace systems
173 } // namespace drake
Definition: automotive_demo.cc:88
const DiscreteValues< T > * get_discrete_state() const
Definition: context.h:133
This is a Drake System block that takes a RigidBodyTree and publishes LCM messages that are intended ...
Definition: drake_visualizer.h:56
void ReplayCachedSimulation() const
Causes the visualizer to playback its cached data at real time.
Definition: drake_visualizer.cc:60
std::vector< double > vector
Definition: translator_test.cc:20
BasicVector< T > * get_mutable_vector()
Definition: discrete_values.h:88
void PlaybackTrajectory(const PiecewisePolynomial< double > &input_trajectory) const
Plays back (at real time) a trajectory representing the input signal.
Definition: drake_visualizer.cc:98
A pure virtual interface that enables LCM to be mocked.
Definition: drake_lcm_interface.h:14
DiscreteValues< T > * get_mutable_discrete_state()
Definition: state.h:60
Specializes LcmAndVectorBaseTranslator to handle LCM messages of type drake::lcmt_viewer_draw.
Definition: viewer_draw_translator.h:21
A superclass template that extends System with some convenience utilities that are not applicable to ...
Definition: leaf_system.h:82
void SetAtIndex(int index, const T &value)
Replaces the state at the given index with the value.
Definition: vector_base.h:59
const double time
Definition: robot_plan_interpolator_test.cc:63
void set_publish_period(double period)
Sets the publishing period of this system.
Definition: drake_visualizer.cc:33
This class represents a publish event.
Definition: event.h:194
#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
DrakeVisualizer(const DrakeVisualizer &)=delete
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...