Drake
cascade_system.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 
5 #include "drake/core/Function.h"
6 #include "drake/core/Gradient.h"
7 #include "drake/core/Vector.h"
8 #include "drake/systems/System.h"
9 
10 namespace Drake {
11 
20 template <class System1, class System2>
22  public:
23  template <typename ScalarType>
24  using StateVector1 = typename System1::template StateVector<ScalarType>;
25  template <typename ScalarType>
26  using StateVector2 = typename System2::template StateVector<ScalarType>;
28  template <typename ScalarType>
29  using StateVector = typename util::template type<ScalarType>;
30  template <typename ScalarType>
31  using InputVector = typename System1::template InputVector<ScalarType>;
32  template <typename ScalarType>
33  using System1OutputVector =
34  typename System1::template OutputVector<ScalarType>;
35  template <typename ScalarType>
36  using OutputVector = typename System2::template OutputVector<ScalarType>;
37  using System1Ptr = std::shared_ptr<System1>;
38  using System2Ptr = std::shared_ptr<System2>;
39 
40  static_assert(
41  std::is_same<typename System1::template OutputVector<double>,
42  typename System2::template InputVector<double>>::value,
43  "System 2 input vector must match System 1 output vector");
44 
45  CascadeSystem(const System1Ptr& _sys1, const System2Ptr& _sys2)
46  : sys1(_sys1), sys2(_sys2) {}
47 
48  template <typename ScalarType>
49  StateVector<ScalarType> dynamics(const ScalarType& t,
50  const StateVector<ScalarType>& x,
51  const InputVector<ScalarType>& u) const {
52  auto x1 = util::first(x);
53  auto x2 = util::second(x);
54  System1OutputVector<ScalarType> y1 = sys1->output(t, x1, u);
55  StateVector<ScalarType> xdot =
56  util::combine(sys1->dynamics(t, x1, u), sys2->dynamics(t, x2, y1));
57  return xdot;
58  }
59 
60  template <typename ScalarType>
61  OutputVector<ScalarType> output(const ScalarType& t,
62  const StateVector<ScalarType>& x,
63  const InputVector<ScalarType>& u) const {
64  auto x1 = util::first(x);
65  auto x2 = util::second(x);
66  System1OutputVector<ScalarType> y1 = sys1->output(t, x1, u);
67  OutputVector<ScalarType> y2 = sys2->output(t, x2, y1);
68  return y2;
69  }
70 
71  bool isTimeVarying() const {
72  return sys1->isTimeVarying() || sys2->isTimeVarying();
73  }
74  bool isDirectFeedthrough() const {
75  return sys1->isDirectFeedthrough() && sys2->isDirectFeedthrough();
76  }
77  size_t getNumStates() const {
78  return Drake::getNumStates(*sys1) + Drake::getNumStates(*sys2);
79  }
80  size_t getNumInputs() const { return Drake::getNumInputs(*sys1); }
81  size_t getNumOutputs() const { return Drake::getNumOutputs(*sys2); }
82 
83  public:
84  const System1Ptr& getSys1() const { return sys1; }
85 
86  const System2Ptr& getSys2() const { return sys2; }
87 
90  return util::combine(getInitialState(*(sys.sys1)),
91  getInitialState(*(sys.sys2)));
92  }
93 
94  private:
95  System1Ptr sys1;
96  System2Ptr sys2;
97 };
98 
103 template <typename System1, typename System2>
104 std::shared_ptr<CascadeSystem<System1, System2>> cascade(
105  const std::shared_ptr<System1>& sys1,
106  const std::shared_ptr<System2>& sys2) {
107  return std::make_shared<CascadeSystem<System1, System2>>(sys1, sys2);
108 }
109 
110 } // namespace Drake
std::shared_ptr< System1 > System1Ptr
Definition: cascade_system.h:37
OutputVector< ScalarType > output(const ScalarType &t, const StateVector< ScalarType > &x, const InputVector< ScalarType > &u) const
Definition: cascade_system.h:61
StateVector< ScalarType > dynamics(const ScalarType &t, const StateVector< ScalarType > &x, const InputVector< ScalarType > &u) const
Definition: cascade_system.h:49
std::vector< Number > x
Definition: IpoptSolver.cpp:169
typename System1::template OutputVector< ScalarType > System1OutputVector
Definition: cascade_system.h:34
typename System1::template InputVector< ScalarType > InputVector
Definition: cascade_system.h:31
size_t getNumStates() const
Definition: cascade_system.h:77
std::shared_ptr< System2 > System2Ptr
Definition: cascade_system.h:38
NOTE: The contents of this class are for the most part direct ports of drake/systems/plants//inverseK...
Definition: Function.h:14
const System2Ptr & getSys2() const
Definition: cascade_system.h:86
CascadeSystem<System1,System2>
Definition: cascade_system.h:21
bool isTimeVarying() const
Definition: cascade_system.h:71
std::size_t getNumOutputs(const System &sys)
getNumOutputs()
Definition: System.h:162
static Vector1< ScalarType > first(const typename helper::template type< ScalarType > &vec)
Definition: Vector.h:292
std::shared_ptr< CascadeSystem< System1, System2 > > cascade(const std::shared_ptr< System1 > &sys1, const std::shared_ptr< System2 > &sys2)
cascade(sys1, sys2)
Definition: cascade_system.h:104
typename System2::template StateVector< ScalarType > StateVector2
Definition: cascade_system.h:26
typename util::template type< ScalarType > StateVector
Definition: cascade_system.h:29
static double * t
Definition: inverseKinSnoptBackend.cpp:62
static helper::template type< ScalarType > combine(const Vector1< ScalarType > &vec1, const Vector2< ScalarType > &vec2)
Definition: Vector.h:304
std::size_t getNumStates(const System &sys)
getNumStates()
Definition: System.h:108
typename System1::template StateVector< ScalarType > StateVector1
Definition: cascade_system.h:24
std::size_t getNumInputs(const System &sys)
getNumInputs()
Definition: System.h:135
size_t getNumOutputs() const
Definition: cascade_system.h:81
CombinedVectorUtil.
Definition: Vector.h:284
static Vector2< ScalarType > second(const typename helper::template type< ScalarType > &vec)
Definition: Vector.h:298
size_t getNumInputs() const
Definition: cascade_system.h:80
const System1Ptr & getSys1() const
Definition: cascade_system.h:84
bool isDirectFeedthrough() const
Definition: cascade_system.h:74
THIS FILE IS DEPRECATED.
CascadeSystem(const System1Ptr &_sys1, const System2Ptr &_sys2)
Definition: cascade_system.h:45
friend StateVector< double > getInitialState(const CascadeSystem< System1, System2 > &sys)
Definition: cascade_system.h:88
typename System2::template OutputVector< ScalarType > OutputVector
Definition: cascade_system.h:36