Drake
system3_input.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstddef>
4 #include <functional>
5 #include <memory>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
13 
14 namespace drake {
15 namespace systems {
16 
39 // TODO(sherm1) Deal with sample rates.
40 class InputPort3 {
41  public:
44 
55  // TODO(sherm1) Check that the output port's subsystem level makes sense.
56  void ConnectTo(const OutputPort3* output_port) {
57  CheckOutputPort(output_port);
58  output_port_ = output_port;
59  }
60 
64  void CheckOutputPort(const OutputPort3* proposed) const {
65  if (proposed != nullptr) DoCheckOutputPort(*proposed);
66  }
67 
69  const OutputPort3* get_connection() const { return output_port_; }
70 
71  protected:
76  virtual void DoCheckOutputPort(const OutputPort3& proposed) const {}
77 
78  private:
79  friend class AbstractSystem3; // Allow call to set_owner().
80 
81  // Set backpointer; no ownership implied.
82  void set_owner(AbstractSystem3* owner, int input_port_num) {
83  system_ = owner;
84  input_port_num_ = input_port_num;
85  }
86 
87  // This AbstractSystem is the owner of this InputPort3, where it has this port
88  // number.
89  class AbstractSystem3* system_{};
90  int input_port_num_{-1};
91 
92  // This is nullptr if this InputPort3 is not connected to an OutputPort3.
93  const OutputPort3* output_port_{};
94 };
95 
100 template <typename T>
101 class VectorInputPort3 : public InputPort3 {
102  public:
106 
109  explicit VectorInputPort3(int required_length)
110  : required_length_(required_length) {
111  DRAKE_ABORT_UNLESS(required_length >= -1);
112  // TODO(sherm1) Should be an std::logic_error rather than abort().
113  }
114 
115  private:
116  void DoCheckOutputPort(const OutputPort3& proposed) const override {
117  using namespace std::literals::string_literals;
118 
119  auto vector_output = dynamic_cast<const VectorOutputPort3<T>*>(&proposed);
120  if (vector_output == nullptr) {
121  throw std::logic_error(
122  "VectorInputPort3::ConnectTo(): Proposed connection was not a "s +
124  }
125  const int output_size = vector_output->get_model_vector().size();
126  if (required_length_ >= 0 && output_size != required_length_) {
127  throw std::out_of_range(
128  "VectorInputPort3::ConnectTo(): "s +
129  "Proposed connection was a VectorOutputPort3 of size " +
130  std::to_string(output_size) + " but must be size " +
131  std::to_string(required_length_) + " for this VectorInputPort3.");
132  }
133  // Looks good if we get here.
134  }
135 
136  int required_length_{-1}; // Means "any length is OK".
137 };
138 
139 } // namespace systems
140 } // namespace drake
An OutputPort3 represents a data output from a System.
Definition: system3_output.h:35
Definition: constants.h:3
An InputPort3 represents an external data input to a System.
Definition: system3_input.h:40
A VectorInputPort3 extends InputPort3 with the restriction that only VectorOutputPort3 connections ar...
Definition: system3_input.h:101
InputPort3()
Create an InputPort3 that accepts any OutputPort3 as a connection.
Definition: system3_input.h:43
Extends OutputPort3 for cases where the OutputPort3 is known to be vector-valued, with scalar element...
Definition: system3_output.h:110
#define DRAKE_ABORT_UNLESS(condition)
Evaluates condition and iff the value is false will ::abort() the program with a message showing at l...
Definition: drake_assert.h:39
VectorInputPort3()
Create a VectorInputPort3 that accepts any length VectorOutputPort3 as a connection.
Definition: system3_input.h:105
VectorInputPort3(int required_length)
Create a VectorInputPort3 that only permits a connection to a particular length of VectorOutputPort3...
Definition: system3_input.h:109
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
An abstract superclass for dynamical systems, encapsulating functionality that is independent of the ...
Definition: system3.h:68
void CheckOutputPort(const OutputPort3 *proposed) const
Given an OutputPort3 proposed as a connection for this InputPort3, throw an error message if the Outp...
Definition: system3_input.h:64
const OutputPort3 * get_connection() const
If connected, return a pointer to the output port otherwise nullptr.
Definition: system3_input.h:69
void ConnectTo(const OutputPort3 *output_port)
Connect this InputPort3 to a compatible OutputPort3.
Definition: system3_input.h:56
static const std::string & Get()
Attempts to return a nicely demangled and canonicalized type name that is the same on all platforms...
Definition: nice_type_name.h:48
TestContinuousSystem system_
Definition: continuous_system_test.cc:50
virtual void DoCheckOutputPort(const OutputPort3 &proposed) const
A concrete InputPort3 can override this method to restrict the kinds of OutputPort3 objects that are ...
Definition: system3_input.h:76