DMP_BBO library
DynamicalSystem.cpp
Go to the documentation of this file.
1 
25 
26 #include <cmath>
27 #include <iostream>
28 #include <eigen3/Eigen/Core>
29 
30 using namespace std;
31 using namespace Eigen;
32 
33 namespace DmpBbo {
34 
35 DynamicalSystem::DynamicalSystem(int order, double tau, Eigen::VectorXd initial_state, Eigen::VectorXd attractor_state, std::string name)
36  :
37  // For 1st order systems, the dimensionality of the state vector 'x' is 'dim'
38  // For 2nd order systems, the system is expanded to x = [y z], where 'y' and
39  // 'z' are both of dimensionality 'dim'. Therefore dim(x) is 2*dim
40  dim_(initial_state.size()*order),
41  // The dimensionality of the system before a potential rewrite
42  dim_orig_(initial_state.size()),
43  tau_(tau),initial_state_(initial_state),attractor_state_(attractor_state),
44  name_(name),integration_method_(RUNGE_KUTTA)
45 {
46  assert(order==1 || order==2);
47  assert(initial_state.size()==attractor_state.size());
48 }
49 
51 {
52 }
53 
54 void DynamicalSystem::integrateStart(const Eigen::VectorXd& x_init, Eigen::Ref<Eigen::VectorXd> x, Eigen::Ref<Eigen::VectorXd> xd)
55 {
56  set_initial_state(x_init);
57  integrateStart(x,xd);
58 }
59 
60 void DynamicalSystem::integrateStart(Eigen::Ref<Eigen::VectorXd> x, Eigen::Ref<Eigen::VectorXd> xd) const {
61  // Check size. Leads to faster numerical integration and makes Eigen::Ref easier to deal with
62  assert(x.size()==dim());
63  assert(xd.size()==dim());
64 
65  // Return value for state variables
66  // Pad the end with zeros: Why? In the spring-damper system, the state consists of x = [y z].
67  // The initial state only applies to y. Therefore, we set x = [y 0];
68  x.fill(0);
69  x.segment(0,initial_state_.size()) = initial_state_;
70 
71  // Return value (rates of change)
73 }
74 
75 void DynamicalSystem::integrateStep(double dt, const Eigen::Ref<const Eigen::VectorXd> x, Eigen::Ref<Eigen::VectorXd> x_updated, Eigen::Ref<Eigen::VectorXd> xd_updated) const
76 {
77  assert(dt>0.0);
78  // Check size. Leads to faster numerical integration and makes Eigen::Ref easier to deal with
79  assert(x.size()==dim());
80  if (integration_method_ == RUNGE_KUTTA)
81  integrateStepRungeKutta(dt, x, x_updated, xd_updated);
82  else
83  integrateStepEuler(dt, x, x_updated, xd_updated);
84 }
85 
86 
87 void DynamicalSystem::integrateStepEuler(double dt, const Ref<const VectorXd> x, Ref<VectorXd> x_updated, Ref<VectorXd> xd_updated) const
88 {
89  // simple Euler integration
90  differentialEquation(x,xd_updated);
91  x_updated = x + dt*xd_updated;
92 }
93 
94 void DynamicalSystem::integrateStepRungeKutta(double dt, const Ref<const VectorXd> x, Ref<VectorXd> x_updated, Ref<VectorXd> xd_updated) const
95 {
96  // 4th order Runge-Kutta for a 1st order system
97  // http://en.wikipedia.org/wiki/Runge-Kutta_method#The_Runge.E2.80.93Kutta_method
98 
99  int l = x.size();
100  VectorXd k1(l), k2(l), k3(l), k4(l);
101  differentialEquation(x,k1);
102  VectorXd input_k2 = x + dt*0.5*k1;
103  differentialEquation(input_k2,k2);
104  VectorXd input_k3 = x + dt*0.5*k2;
105  differentialEquation(input_k3,k3);
106  VectorXd input_k4 = x + dt*k3;
107  differentialEquation(input_k4,k4);
108 
109  x_updated = x + dt*(k1 + 2.0*(k2+k3) + k4)/6.0;
110  differentialEquation(x_updated,xd_updated);
111 }
112 
113 }
DynamicalSystem class header file.
virtual void integrateStep(double dt, const Eigen::Ref< const Eigen::VectorXd > x, Eigen::Ref< Eigen::VectorXd > x_updated, Eigen::Ref< Eigen::VectorXd > xd_updated) const
Integrate the system one time step.
virtual void set_initial_state(const Eigen::VectorXd &initial_state)
Mutator function for the initial state of the dynamical system.
int dim(void) const
Get the dimensionality of the dynamical system, i.e.
virtual void differentialEquation(const Eigen::Ref< const Eigen::VectorXd > &x, Eigen::Ref< Eigen::VectorXd > xd) const =0
The differential equation which defines the system.
virtual ~DynamicalSystem(void)
Destructor.
virtual void integrateStart(Eigen::Ref< Eigen::VectorXd > x, Eigen::Ref< Eigen::VectorXd > xd) const
Start integrating the system.