Autoware.Auto
rungekutta.hpp
Go to the documentation of this file.
1 // Copyright 2020 Embotech AG, Zurich, Switzerland. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef PARKING_PLANNER__RUNGEKUTTA_HPP_
15 #define PARKING_PLANNER__RUNGEKUTTA_HPP_
16 
17 #include <common/types.hpp>
18 #include <functional>
19 #include <vector>
20 
21 namespace autoware
22 {
23 
24 namespace motion
25 {
26 
27 namespace planning
28 {
29 
30 namespace parking_planner
31 {
32 
34 
35 // Implement some operations on vectors of scalars
36 template<typename T>
37 std::vector<T> operator+(const std::vector<T> & vector1, const std::vector<T> & vector2)
38 {
39  std::vector<T> result = {};
40  result.reserve(vector1.size());
41  for (std::size_t k = 0; k < vector2.size(); k++) {
42  result.push_back(vector1[k] + vector2[k]);
43  }
44  return result;
45 }
46 
47 template<typename T>
48 std::vector<T> operator*(const T & scalar, const std::vector<T> & vector)
49 {
50  std::vector<T> result = {};
51  result.reserve(vector.size());
52  for (std::size_t k = 0; k < vector.size(); k++) {
53  result.push_back(scalar * vector[k]);
54  }
55  return result;
56 }
57 
58 
61 template<class X, class U, typename P>
62 X RK4(X x, U u, P p, std::function<X(X, U, P)> f, const float64_t stepsize, const std::size_t steps)
63 {
64  const auto dt = stepsize / static_cast<float64_t>(steps);
65  for (std::size_t k = 0; k < steps; ++k) {
66  const auto k1 = f(x, u, p);
67  const auto k2 = f(x + (dt / 2.0) * k1, u, p);
68  const auto k3 = f(x + (dt / 2.0) * k2, u, p);
69  const auto k4 = f(x + dt * k3, u, p);
70  const auto xnext = x + (dt / 6.0) * (k1 + (2.0 * k2) + (2.0 * k3) + k4);
71  x = xnext;
72  }
73  return x;
74 }
75 
76 // \brief Version where the function closes over the parameters, this is useful where we want to
77 // for example have the function be a member of an object (like BicycleModel). The reason
78 // this is not used everywhere is that CasADi wants its parameters to be passed
79 // in explicitly, otherwise its AD doesn't realize the parameter is there.
80 template<class X, class U>
81 X RK4(X x, U u, std::function<X(X, U)> f, const float64_t stepsize, const std::size_t steps)
82 {
83  // Create a shim that takes a dummy parameter, then ignores it so we can call the above
84  const std::function<X(X, U, uint8_t)> shim = [&f](X xx, U uu, uint8_t pp) {
85  (void)&pp; return f(xx, uu);
86  };
87 
88  return RK4(x, u, {}, shim, stepsize, steps);
89 }
90 
91 } // namespace parking_planner
92 } // namespace planning
93 } // namespace motion
94 } // namespace autoware
95 
96 #endif // PARKING_PLANNER__RUNGEKUTTA_HPP_
catr_diff.T
T
Definition: catr_diff.py:22
types.hpp
This file includes common type definition.
u
DifferentialState u
Definition: kinematic_bicycle.snippet.hpp:29
autoware::motion::planning::parking_planner::operator*
std::vector< T > operator*(const T &scalar, const std::vector< T > &vector)
Definition: rungekutta.hpp:48
f
DifferentialEquation f
Definition: kinematic_bicycle.snippet.hpp:44
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
x
DifferentialState x
Definition: kinematic_bicycle.snippet.hpp:28
motion
Definition: controller_base.hpp:30
autoware::motion::planning::parking_planner::operator+
std::vector< T > operator+(const std::vector< T > &vector1, const std::vector< T > &vector2)
Definition: rungekutta.hpp:37
autoware::motion::planning::parking_planner::RK4
X RK4(X x, U u, P p, std::function< X(X, U, P)> f, const float64_t stepsize, const std::size_t steps)
Template for an explicit Runge-Kutta integrator. CasADi has its own, but it for some reason doesn't c...
Definition: rungekutta.hpp:62
autoware::common::types::float64_t
double float64_t
Definition: types.hpp:37