PX4 Firmware
PX4 Autopilot Software http://px4.io
integration.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "math.hpp"
4 
5 namespace matrix {
6 
7 template<typename Type, size_t M, size_t N>
9  Vector<Type, M> (*f)(Type, const Matrix<Type, M, 1> &x, const Matrix<Type, N, 1> & u),
10  const Matrix<Type, M, 1> & y0,
11  const Matrix<Type, N, 1> & u,
12  Type t0,
13  Type tf,
14  Type h0,
16 )
17 {
18  // https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
19  Type t1 = t0;
20  y1 = y0;
21  Type h = h0;
22  Vector<Type, M> k1, k2, k3, k4;
23  if (tf < t0) return -1; // make sure t1 > t0
24  while (t1 < tf) {
25  if (t1 + h0 < tf) {
26  h = h0;
27  } else {
28  h = tf - t1;
29  }
30  k1 = f(t1, y1, u);
31  k2 = f(t1 + h/2, y1 + k1*h/2, u);
32  k3 = f(t1 + h/2, y1 + k2*h/2, u);
33  k4 = f(t1 + h, y1 + k3*h, u);
34  y1 += (k1 + k2*2 + k3*2 + k4)*(h/6);
35  t1 += h;
36  }
37  return 0;
38 }
39 
40 } // namespace matrix
41 
42 // vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 :
int integrate_rk4(Vector< Type, M >(*f)(Type, const Matrix< Type, M, 1 > &x, const Matrix< Type, N, 1 > &u), const Matrix< Type, M, 1 > &y0, const Matrix< Type, N, 1 > &u, Type t0, Type tf, Type h0, Matrix< Type, M, 1 > &y1)
Definition: integration.hpp:8
static struct hrt_call t1
Definition: hrt_test.cpp:54
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8