-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_system.hh
53 lines (43 loc) · 1.66 KB
/
dynamic_system.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
////////////////////////////////////////
// Dynamic system base class template //
////////////////////////////////////////
#include <iostream>
#include "ode.hh"
// Declare templated traits class for Derived
template<typename Derived> struct DynamicSystem_traits {};
template <typename Derived>
class DynamicSystem
{
public:
using StateVector = typename DynamicSystem_traits<Derived>::StateVector;
using ControlVector = typename DynamicSystem_traits<Derived>::ControlVector;
StateVector continuous_dynamics(const StateVector & x, const ControlVector & u);
StateVector discrete_dynamics(const StateVector& x, const ControlVector& u, double dt);
void print(const StateVector &, const ControlVector&, double);
private:
Derived& impl() {
return *static_cast<Derived*>(this);
}
};
template <typename Derived>
typename DynamicSystem_traits<Derived>::StateVector
DynamicSystem<Derived>::continuous_dynamics(const StateVector & x, const ControlVector & u)
{
// Have a different function name for implementation to avoid dead loop
// which happens when you forget to implement a 'virtual' function
return impl().continuous_dynamics_impl(x, u);
}
template <typename Derived>
typename DynamicSystem_traits<Derived>::StateVector
DynamicSystem<Derived>::discrete_dynamics(const StateVector & x, const ControlVector & u, double dt)
{
return ODE_RK4(impl().continuous_dynamics_impl,
x, u, dt);
}
template <typename Derived>
void DynamicSystem<Derived>::print(const StateVector & x, const ControlVector & u, double t)
{
std::cout << t << " ";
std::cout << x.transpose() << " ";
std::cout << u.transpose() << std::endl;
}