Drake
pd_control_system.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 
6 #include "drake/core/Function.h"
7 #include "drake/core/Gradient.h"
8 #include "drake/core/Vector.h"
9 #include "drake/systems/System.h"
10 
11 namespace Drake {
12 
23 template <class System>
25  public:
26  template <typename ScalarType>
27  using StateVector = typename System::template StateVector<ScalarType>;
28  template <typename ScalarType>
29  using InputVector = typename System::template StateVector<ScalarType>;
30  template <typename ScalarType>
31  using OutputVector = typename System::template OutputVector<ScalarType>;
32  typedef std::shared_ptr<System> SystemPtr;
33 
34  template <typename DerivedA, typename DerivedB>
35  PDControlSystem(const SystemPtr& sys, const Eigen::MatrixBase<DerivedA>& Kp,
36  const Eigen::MatrixBase<DerivedB>& Kd)
37  : sys(sys), Kp(Kp), Kd(Kd) {
38  DRAKE_ASSERT(Drake::getNumInputs(*sys) == Kp.rows() &&
39  "Kp must have the same number of rows as the system has"
40  " inputs");
41  DRAKE_ASSERT(Kp.rows() == Kd.rows() &&
42  "Kd must have the same number of rows as Kp");
43  DRAKE_ASSERT(Drake::getNumStates(*sys) == Kp.cols() + Kd.cols() &&
44  "Kp and Kd must match the number of states");
45  }
46 
47  template <typename ScalarType>
48  StateVector<ScalarType> dynamics(const ScalarType& t,
49  const StateVector<ScalarType>& x,
50  const InputVector<ScalarType>& u) const {
51  typename System::template InputVector<ScalarType> system_u =
52  Kp * (toEigen(u).head(Kp.cols()) - toEigen(x).head(Kp.cols())) +
53  Kd * (toEigen(u).tail(Kd.cols()) - toEigen(x).tail(Kd.cols()));
54  return sys->dynamics(t, x, system_u);
55  }
56 
57  template <typename ScalarType>
58  OutputVector<ScalarType> output(const ScalarType& t,
59  const StateVector<ScalarType>& x,
60  const InputVector<ScalarType>& u) const {
61  typename System::template InputVector<ScalarType> system_u =
62  Kp * (toEigen(u).head(Kp.cols()) - toEigen(x).head(Kp.cols())) +
63  Kd * (toEigen(u).tail(Kd.cols()) - toEigen(x).tail(Kd.cols()));
64  return sys->output(t, x, system_u);
65  }
66 
67  bool isTimeVarying() const { return sys->isTimeVarying(); }
68  bool isDirectFeedthrough() const { return sys->isDirectFeedthrough(); }
69  size_t getNumStates() const { return Drake::getNumStates(*sys); }
70  size_t getNumInputs() const { return Drake::getNumStates(*sys); }
71  size_t getNumOutputs() const { return Drake::getNumOutputs(*sys); }
72 
73  public:
74  const SystemPtr& getSys() const { return sys; }
76  const PDControlSystem<System>& sys) {
77  return getInitialState(*sys.sys);
78  }
79 
80  private:
81  SystemPtr sys;
82  Eigen::MatrixXd Kp, Kd;
83 };
84 
85 } // end namespace Drake
typename System::template StateVector< ScalarType > StateVector
Definition: pd_control_system.h:27
size_t getNumOutputs() const
Definition: pd_control_system.h:71
PDControlSystem<System>
Definition: pd_control_system.h:24
std::vector< Number > x
Definition: IpoptSolver.cpp:169
NOTE: The contents of this class are for the most part direct ports of drake/systems/plants//inverseK...
Definition: Function.h:14
std::size_t getNumOutputs(const System &sys)
getNumOutputs()
Definition: System.h:162
typename System::template StateVector< ScalarType > InputVector
Definition: pd_control_system.h:29
static double * t
Definition: inverseKinSnoptBackend.cpp:62
size_t getNumInputs() const
Definition: pd_control_system.h:70
size_t getNumStates() const
Definition: pd_control_system.h:69
#define DRAKE_ASSERT(condition)
DRAKE_ASSERT(condition) is similar to the built-in assert(condition) from the C++ system header <cas...
Definition: drake_assert.h:35
bool isDirectFeedthrough() const
Definition: pd_control_system.h:68
std::shared_ptr< System > SystemPtr
Definition: pd_control_system.h:32
const SystemPtr & getSys() const
Definition: pd_control_system.h:74
Provides Drake&#39;s assertion implementation.
std::size_t getNumStates(const System &sys)
getNumStates()
Definition: System.h:108
PDControlSystem(const SystemPtr &sys, const Eigen::MatrixBase< DerivedA > &Kp, const Eigen::MatrixBase< DerivedB > &Kd)
Definition: pd_control_system.h:35
friend StateVector< double > getInitialState(const PDControlSystem< System > &sys)
Definition: pd_control_system.h:75
std::size_t getNumInputs(const System &sys)
getNumInputs()
Definition: System.h:135
bool isTimeVarying() const
Definition: pd_control_system.h:67
OutputVector< ScalarType > output(const ScalarType &t, const StateVector< ScalarType > &x, const InputVector< ScalarType > &u) const
Definition: pd_control_system.h:58
StateVector< ScalarType > dynamics(const ScalarType &t, const StateVector< ScalarType > &x, const InputVector< ScalarType > &u) const
Definition: pd_control_system.h:48
typename System::template OutputVector< ScalarType > OutputVector
Definition: pd_control_system.h:31
THIS FILE IS DEPRECATED.
const Eigen::Matrix< ScalarType, Rows, 1 > & toEigen(const Eigen::Matrix< ScalarType, Rows, 1 > &vec)
Definition: Vector.h:61