Drake
n_ary_system.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 
7 
8 
9 namespace drake {
10 
14 template <class UnitSystem>
15 class NArySystem {
16  public:
17  // Required by Drake::System concept.
18  template <typename ScalarType>
19  using StateVector = NAryState<
20  typename UnitSystem::template StateVector<ScalarType> >;
21  // Required by Drake::System concept.
22  template <typename ScalarType>
23  using InputVector = NAryState<
24  typename UnitSystem::template InputVector<ScalarType> >;
25  // Required by Drake::System concept.
26  template <typename ScalarType>
27  using OutputVector = NAryState<
28  typename UnitSystem::template OutputVector<ScalarType> >;
29 
31 
33  // TODO(maddog) shared_ptr is not strictly necessary here; we are using it
34  // to be compatible with cascade(). At some point, neither
35  // should use a shared_ptr interface.
36  void AddSystem(std::shared_ptr<UnitSystem> system) {
37  systems_.push_back(system);
38  }
39 
40  // Required by Drake::System concept.
41  template <typename ScalarType>
42  StateVector<ScalarType> dynamics(const ScalarType& time,
43  const StateVector<ScalarType>& state,
44  const InputVector<ScalarType>& input) const {
45  if ((state.count() >= 0) && (state.count() != systems_size())) {
46  throw std::invalid_argument("State count differs from systems count.");
47  }
48  if ((input.count() >= 0) && (input.count() != systems_size())) {
49  throw std::invalid_argument("Input count differs from systems count.");
50  }
51  StateVector<ScalarType> xdot(systems_size());
52  for (int i = 0; i < systems_size(); ++i) {
53  xdot.set(i, systems_[i]->dynamics(time, state.get(i), input.get(i)));
54  }
55  return xdot;
56  }
57 
58  // Required by Drake::System concept.
59  template <typename ScalarType>
60  OutputVector<ScalarType> output(const ScalarType& time,
61  const StateVector<ScalarType>& state,
62  const InputVector<ScalarType>& input) const {
63  if ((state.count() >= 0) && (state.count() != systems_size())) {
64  throw std::invalid_argument("State count differs from systems count.");
65  }
66  if ((input.count() >= 0) && (input.count() != systems_size())) {
67  throw std::invalid_argument("Input count differs from systems count.");
68  }
69  OutputVector<ScalarType> y(systems_size());
70  for (int i = 0; i < systems_size(); ++i) {
71  y.set(i, systems_[i]->output(time, state.get(i), input.get(i)));
72  }
73  return y;
74  }
75 
76  // Required by Drake::System concept.
77  bool isTimeVarying() const {
78  for (auto s : systems_) {
79  if (s->isTimeVarying()) { return true; }
80  }
81  return false;
82  }
83 
84  // Required by Drake::System concept.
85  bool isDirectFeedthrough() const {
86  for (auto s : systems_) {
87  if (s->isDirectFeedthrough()) { return true; }
88  }
89  return false;
90  }
91 
92  // Required by Drake::System concept.
93  std::size_t getNumStates() const {
94  return StateVector<double>::RowsFromUnitCount(systems_size());
95  }
96 
97  // Required by Drake::System concept.
98  std::size_t getNumInputs() const {
99  return InputVector<double>::RowsFromUnitCount(systems_size());
100  }
101 
102  // Required by Drake::System concept.
103  std::size_t getNumOutputs() const {
104  return OutputVector<double>::RowsFromUnitCount(systems_size());
105  }
106 
107  private:
108  int systems_size() const { return systems_.size(); }
109 
110  std::vector<std::shared_ptr<UnitSystem> > systems_;
111 };
112 
113 } // namespace drake
NArySystem()
Definition: n_ary_system.h:30
UnitVector get(int pos) const
Definition: n_ary_state.h:77
std::size_t getNumInputs() const
Definition: n_ary_system.h:98
std::size_t getNumOutputs() const
Definition: n_ary_system.h:103
static int RowsFromUnitCount(int count)
Determines how many Eigen matrix rows will be needed to represent.
Definition: n_ary_state.h:157
Definition: constants.h:3
NAryState is a Drake::Vector (concept implementation) which is a container of zero or more component ...
Definition: n_ary_state.h:26
A System which aggregates multiple instances of a UnitSystem system.
Definition: n_ary_system.h:15
StateVector< ScalarType > dynamics(const ScalarType &time, const StateVector< ScalarType > &state, const InputVector< ScalarType > &input) const
Definition: n_ary_system.h:42
void set(int pos, const UnitVector &unit)
Sets the value of the component UnitVector at position pos.
Definition: n_ary_state.h:92
int count() const
Definition: n_ary_state.h:54
OutputVector< ScalarType > output(const ScalarType &time, const StateVector< ScalarType > &state, const InputVector< ScalarType > &input) const
Definition: n_ary_system.h:60
bool isDirectFeedthrough() const
Definition: n_ary_system.h:85
bool isTimeVarying() const
Definition: n_ary_system.h:77
void AddSystem(std::shared_ptr< UnitSystem > system)
Adds.
Definition: n_ary_system.h:36
std::size_t getNumStates() const
Definition: n_ary_system.h:93