motor_controller
RK4Integrator.hpp
Go to the documentation of this file.
1 /*
2  * PURPOSE --- Header file for a class for a 4'th order Runge-Kutta
3  * method for solving a system of n first order differential equations.
4  * If the initial equation is in the form of n'th order differential equation
5  * it must be converted to a system of n first order differential
6  * equations.
7  *
8  */
9 
10 #ifndef MOTOR_CONTROLLER_RK4_INTEGRATOR_HPP
11 #define MOTOR_CONTROLLER_RK4_INTEGRATOR_HPP
12 
13 #include <stdlib.h>
14 #include <vector>
15 
16 namespace motor_controller
17 {
18 
19 class RK4_SIM
20 {
21  public:
22  // Constructor
23  // The arguments are self-explanatory
24  // If you specify initial conditions, the parameter _initial_state
25  // should point to a vector of size _plant_order, so that correct
26  // initialization could be performed.
27  RK4_SIM(int _plant_order,
28  int _ctrl_order,
29  double _integration_step = 0.0001,
30  double _initial_time = 0.0,
31  double *_initial_state = NULL);
32 
33  // Destructor
34  virtual ~RK4_SIM();
35 
36  // Initilaizes simulation parameters
37  void init_param(double _integration_step,
38  double _initial_time,
39  double *_initial_state);
40 
41  void solve (void); // Performs one step simulation
42 
43  // DERIV contains the dynamic equations of the system in the
44  // form: xdot = f(t,x,u); arguments are the time t, present state
45  // values x, the present control value u, and values of the derivatives
46  // of the states xdot (calculated inside of the funtcion).
47  // It is overloaded in the derived class!!!
48  virtual void DERIV(const double t, const double *x,
49  const double *u, double *xdot) {};
50 
51  protected:
52  int plant_order; // Num of plant states
53  int ctrl_order; // Num of control inputs
54  double integration_step; // Integration step size
55 
56  public:
57 
58  std::vector<double> plant_state; // Current System states
59  std::vector<double> ctrl_input; // Current Controller output
60 
61  double current_time; // Current time
62  int rk4_sim_err; // Variable to hold an error number
63 
64  private:
65  std::vector<double> f1;
66  std::vector<double> f2;
67  std::vector<double> f3;
68  std::vector<double> f4;
69  std::vector<double> temp; // Runge-Kutta Coefficients
70 
71  // Functions which calculate RK coefficients
72  inline void F1 (void);
73  inline void F2 (void);
74  inline void F3 (void);
75  inline void F4 (void);
76 };
77 
78 }
79 #endif
80 
81 
void init_param(double _integration_step, double _initial_time, double *_initial_state)
Definition: RK4Integrator.cpp:50
std::vector< double > plant_state
Definition: RK4Integrator.hpp:58
double current_time
Definition: RK4Integrator.hpp:61
virtual void DERIV(const double t, const double *x, const double *u, double *xdot)
Definition: RK4Integrator.hpp:48
int plant_order
Definition: RK4Integrator.hpp:49
std::vector< double > ctrl_input
Definition: RK4Integrator.hpp:59
virtual ~RK4_SIM()
Definition: RK4Integrator.cpp:47
int rk4_sim_err
Definition: RK4Integrator.hpp:62
RK4_SIM(int _plant_order, int _ctrl_order, double _integration_step=0.0001, double _initial_time=0.0, double *_initial_state=NULL)
Definition: RK4Integrator.cpp:29
double integration_step
Definition: RK4Integrator.hpp:54
Definition: RK4Integrator.hpp:19
void solve(void)
Definition: RK4Integrator.cpp:79
int ctrl_order
Definition: RK4Integrator.hpp:53