dynamics_solver.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019, Wolfgang Merkt
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_CORE_DYNAMICS_SOLVER_H_
31 #define EXOTICA_CORE_DYNAMICS_SOLVER_H_
32 
33 #include <exotica_core/factory.h>
34 #include <exotica_core/object.h>
35 #include <exotica_core/property.h>
36 #include <exotica_core/tools.h>
37 
38 #include <exotica_core/dynamics_solver_initializer.h>
39 
40 #define REGISTER_DYNAMICS_SOLVER_TYPE(TYPE, DERIV) EXOTICA_CORE_REGISTER(exotica::DynamicsSolver, TYPE, DERIV)
41 
42 namespace exotica
43 {
44 class Scene;
45 
47 {
48  RK1 = 0,
50  RK2,
51  RK4,
52 };
53 
54 template <typename T, int NX, int NU>
56 {
57 public:
58  typedef Eigen::Matrix<T, NX, 1> StateVector;
59  typedef Eigen::Matrix<T, NU, 1> ControlVector;
60  typedef Eigen::Matrix<T, NX, NX> StateDerivative;
61  typedef Eigen::Matrix<T, NX, NU> ControlDerivative;
62 
64  virtual ~AbstractDynamicsSolver();
65 
67  virtual void InstantiateBase(const Initializer& init);
68 
72  virtual void AssignScene(std::shared_ptr<Scene> scene_in);
73 
75  virtual void SetDt(double dt_in);
76 
78  virtual StateVector f(const StateVector& x, const ControlVector& u) = 0;
79 
81  virtual StateVector F(const StateVector& x, const ControlVector& u);
82 
84  virtual void ComputeDerivatives(const StateVector& x, const ControlVector& u);
85 
87  const StateDerivative& get_Fx() const;
88 
90  const ControlDerivative& get_Fu() const;
91 
93  const StateDerivative& get_fx() const;
94 
96  const ControlDerivative& get_fu() const;
97 
99  virtual StateDerivative fx(const StateVector& x, const ControlVector& u);
100 
102  virtual ControlDerivative fu(const StateVector& x, const ControlVector& u);
103 
105  StateDerivative fx_fd(const StateVector& x, const ControlVector& u);
106 
108  ControlDerivative fu_fd(const StateVector& x, const ControlVector& u);
109 
112  {
114  }
115 
116  // NOTE: Second order derivatives a 3D matrices, i.e. tensors
117  // We use the numerator convention (see https://en.wikipedia.org/wiki/Matrix_calculus)
118  // X_i,j,k = d(X_i,j)/d x_k
119  //
120  // Additionally, the first subscript is the *second* partial derivative.
121  // I.e. f_xu = (f_u)_x
122  // TODO: Eigen::Tensor to be replaced with exotica::Hessian
123  virtual Eigen::Tensor<T, 3> fxx(const StateVector& x, const ControlVector& u);
124  virtual Eigen::Tensor<T, 3> fuu(const StateVector& x, const ControlVector& u);
125  virtual Eigen::Tensor<T, 3> fxu(const StateVector& x, const ControlVector& u);
126 
130  // TODO: To be deprecated - or at least remove its use - as it's difficult to get partial derivatives.
131  StateVector Simulate(const StateVector& x, const ControlVector& u, T t);
132 
136  virtual StateVector StateDelta(const StateVector& x_1, const StateVector& x_2)
137  {
138  assert(x_1.size() == x_2.size());
139  return x_1 - x_2;
140  }
141 
142  void StateDelta(const StateVector& x_1, const StateVector& x_2, Eigen::VectorXdRef xout)
143  {
144  xout = StateDelta(x_1, x_2);
145  }
146 
149  virtual Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> dStateDelta(const StateVector& x_1, const StateVector& x_2, const ArgumentPosition first_or_second)
150  {
151  assert(x_1.size() == x_2.size());
152  assert(first_or_second == ArgumentPosition::ARG0 || first_or_second == ArgumentPosition::ARG1);
153 
155 
156  if (first_or_second == ArgumentPosition::ARG0)
157  return dStateDelta_;
158  else
159  return -1.0 * dStateDelta_;
160  }
161 
162  virtual Hessian ddStateDelta(const StateVector& x_1, const StateVector& x_2, const ArgumentPosition first_or_second)
163  {
164  // In Euclidean spaces, this is zero.
165 
166  assert(x_1.size() == x_2.size());
167  assert(first_or_second == ArgumentPosition::ARG0 || first_or_second == ArgumentPosition::ARG1);
168 
170 
171  return ddStateDelta_;
172  }
173 
176  virtual Eigen::Matrix<T, Eigen::Dynamic, 1> GetPosition(Eigen::VectorXdRefConst x_in);
177 
179  int get_num_controls() const;
180 
182  int get_num_positions() const;
183 
185  int get_num_velocities() const;
186 
188  int get_num_state() const;
189 
191  int get_num_state_derivative() const;
192 
194  T get_dt() const;
195 
197  Integrator get_integrator() const;
198 
200  void set_integrator(Integrator integrator_in);
201 
203  void SetIntegrator(const std::string& integrator_in);
204 
206  // returns: Two-column matrix, first column contains low control limits,
207  // second - the high control limits
208  const Eigen::MatrixXd& get_control_limits();
209 
211  void set_control_limits(Eigen::VectorXdRefConst control_limits_low, Eigen::VectorXdRefConst control_limits_high);
212 
214  const bool& get_has_state_limits() const
215  {
216  return has_state_limits_;
217  }
218 
220  void ClampToStateLimits(Eigen::Ref<Eigen::VectorXd> state_in);
221 
223  virtual ControlVector InverseDynamics(const StateVector& state);
224 
226  virtual void Integrate(const StateVector& x, const StateVector& dx, const double dt, StateVector& xout);
227 
228 private:
231  Eigen::MatrixXd dStateDelta_;
233 
234 protected:
235  int num_controls_ = -1;
236  int num_positions_ = -1;
237  int num_velocities_ = -1;
238  int num_state_ = -1;
240 
243 
244  bool has_state_limits_ = false;
245  Eigen::VectorXd state_limits_lower_;
246  Eigen::VectorXd state_limits_upper_;
247 
248  T dt_ = 0.01;
250  // TODO: Need to enforce control limits.
251  // First column is the low limits, second is the high limits.
252  Eigen::MatrixXd control_limits_;
253 
255  // TODO: To be deprecated in favour of explicit call to Integrate in Simulate
256  virtual StateVector SimulateOneStep(const StateVector& x, const ControlVector& u);
257 
259  Eigen::Tensor<T, 3> fxx_default_, fuu_default_, fxu_default_;
260 
261  Eigen::MatrixXd fx_;
262  Eigen::MatrixXd fu_;
263  Eigen::MatrixXd Fx_;
264  Eigen::MatrixXd Fu_;
265 };
266 
268 
269 typedef std::shared_ptr<exotica::DynamicsSolver> DynamicsSolverPtr;
270 } // namespace exotica
271 
272 #endif // EXOTICA_CORE_DYNAMICS_SOLVER_H_
Eigen::MatrixXd fu_
Internal storage of differential dynamics partial derivative fu computed by ComputeDerivatives.
Integrator integrator_
Chosen integrator. Defaults to Euler (RK1).
virtual Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > dStateDelta(const StateVector &x_1, const StateVector &x_2, const ArgumentPosition first_or_second)
Return the difference of the StateDelta operation between two state vectors. The ArgumentPosition arg...
const bool & get_has_second_order_derivatives() const
Returns whether second-order derivatives are available.
void set_control_limits(Eigen::VectorXdRefConst control_limits_low, Eigen::VectorXdRefConst control_limits_high)
Sets the control limits.
const StateDerivative & get_Fx() const
Returns derivative Fx computed by ComputeDerivatives.
Eigen::MatrixXd fx_
Internal storage of differential dynamics partial derivative fx computed by ComputeDerivatives.
Eigen::Tensor< T, 3 > fuu_default_
int num_controls_
Number of controls in the dynamic system.
Eigen::Ref< Eigen::VectorXd > VectorXdRef
Definition: conversions.h:54
StateVector Simulate(const StateVector &x, const ControlVector &u, T t)
Simulates the dynamic system from starting state x using control u for t seconds. ...
Forward Euler (explicit)
const ControlDerivative & get_Fu() const
Returns derivative Fu computed by ComputeDerivatives.
int get_num_velocities() const
Returns number of velocities.
virtual StateVector F(const StateVector &x, const ControlVector &u)
State transition function. This internally computes the differential dynamics and applies the chosen ...
const bool & get_has_state_limits() const
Returns whether state limits are available.
virtual ControlVector InverseDynamics(const StateVector &state)
Returns a control vector corresponding to the state vector assuming zero acceleration.
virtual StateVector SimulateOneStep(const StateVector &x, const ControlVector &u)
Integrates the dynamic system from state x with controls u applied for one timestep dt using the sele...
T get_dt() const
Returns integration timestep dt.
Integrator get_integrator() const
Returns used integration scheme.
T dt_
Internal timestep used for integration. Defaults to 10ms.
Eigen::MatrixXd control_limits_
ControlLimits. Default is empty vector.
int get_num_positions() const
Returns number of positions.
virtual StateVector StateDelta(const StateVector &x_1, const StateVector &x_2)
Return the difference of two state vectors. Used when e.g. angle differences need to be wrapped from ...
bool has_state_limits_
Whether the solver specifies state limits.
Eigen::VectorXd raw_control_limits_high_
int get_num_controls() const
Returns number of controls.
int get_num_state() const
Returns size of state space (nx)
const Eigen::MatrixXd & get_control_limits()
Returns the control limits vector.
virtual StateVector f(const StateVector &x, const ControlVector &u)=0
Forward dynamics. This computes the differential dynamics.
Eigen::Tensor< T, 3 > fxu_default_
virtual Hessian ddStateDelta(const StateVector &x_1, const StateVector &x_2, const ArgumentPosition first_or_second)
virtual void SetDt(double dt_in)
Sets the timestep dt to be used for integration.
std::shared_ptr< exotica::DynamicsSolver > DynamicsSolverPtr
int num_positions_
Number of positions in the dynamic system.
bool second_order_derivatives_initialized_
Whether fxx, fxu and fuu have been initialized to 0.
Eigen::Array< Eigen::MatrixXd, Eigen::Dynamic, 1 > Hessian
Definition: conversions.h:154
virtual Eigen::Matrix< T, Eigen::Dynamic, 1 > GetPosition(Eigen::VectorXdRefConst x_in)
Returns the position-part of the state vector to update the scene. For types including SE(3) and rota...
virtual void Integrate(const StateVector &x, const StateVector &dx, const double dt, StateVector &xout)
Integrates without performing dynamics.
virtual void ComputeDerivatives(const StateVector &x, const ControlVector &u)
Computes derivatives fx, fu, Fx, Fu [single call for efficiency, derivatives can be retrieved with ge...
int num_state_
Size of state space (num_positions + num_velocities)
Eigen::Matrix< T, NU, 1 > ControlVector
Convenience definition for a ControlVector (dimension NU x 1)
Eigen::Tensor< T, 3 > fxx_default_
int num_velocities_
Number of velocities in the dynamic system.
virtual void AssignScene(std::shared_ptr< Scene > scene_in)
Passes the Scene of the PlanningProblem to the DynamicsSolver.
virtual void InstantiateBase(const Initializer &init)
Instantiates the base properties of the DynamicsSolver.
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
ArgumentPosition
Argument position. Used as parameter to refer to an argument.
Definition: tools.h:123
Eigen::MatrixXd Fu_
Internal storage of state transition partial derivative Fu computed by ComputeDerivatives.
Semi-Implicit Euler.
const StateDerivative & get_fx() const
Returns derivative fx computed by ComputeDerivatives.
Eigen::Matrix< T, NX, NX > StateDerivative
Convenience definition for a StateDerivative.
void SetIntegrator(const std::string &integrator_in)
Sets integrator type based on request string.
Eigen::VectorXd state_limits_upper_
Upper state limits (configuration and velocity)
bool has_second_order_derivatives_
Whether this solver provides second order derivatives. If false (default), assumed to be all zeros...
virtual Eigen::Tensor< T, 3 > fxu(const StateVector &x, const ControlVector &u)
void StateDelta(const StateVector &x_1, const StateVector &x_2, Eigen::VectorXdRef xout)
Explicit trapezoid rule.
const ControlDerivative & get_fu() const
Returns derivative fu computed by ComputeDerivatives.
virtual StateDerivative fx(const StateVector &x, const ControlVector &u)
Derivative of the forward dynamics w.r.t. the state.
Eigen::Matrix< T, NX, NU > ControlDerivative
Convenience definition for a ControlDerivative.
AbstractDynamicsSolver< double, Eigen::Dynamic, Eigen::Dynamic > DynamicsSolver
void set_integrator(Integrator integrator_in)
Sets integrator type.
int get_num_state_derivative() const
Returns size of derivative vector of state space (ndx)
Runge-Kutta 4.
double x
ControlDerivative fu_fd(const StateVector &x, const ControlVector &u)
Derivative of the forward dynamics w.r.t. the control [finite differencing].
Eigen::MatrixXd Fx_
Internal storage of state transition partial derivative Fx computed by ComputeDerivatives.
virtual Eigen::Tensor< T, 3 > fuu(const StateVector &x, const ControlVector &u)
Eigen::VectorXd state_limits_lower_
Lower state limits (configuration and velocity)
void ClampToStateLimits(Eigen::Ref< Eigen::VectorXd > state_in)
Clamps the passed in state to the state limits.
virtual Eigen::Tensor< T, 3 > fxx(const StateVector &x, const ControlVector &u)
StateDerivative fx_fd(const StateVector &x, const ControlVector &u)
Derivative of the forward dynamics w.r.t. the state [finite differencing].
int num_state_derivative_
Size of the tangent vector to the state space (2 * num_velocities)
virtual ControlDerivative fu(const StateVector &x, const ControlVector &u)
Derivative of the forward dynamics w.r.t. the control.
Eigen::Matrix< T, NX, 1 > StateVector
Convenience definition for a StateVector containing both position and velocity (dimension NX x 1) ...


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49