Autoware.Auto
bicycle_model.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__BICYCLE_MODEL_HPP_
15 #define PARKING_PLANNER__BICYCLE_MODEL_HPP_
16 
17 #include <common/types.hpp>
18 #include <vector>
19 #include <string>
20 
22 #include "geometry.hpp"
23 #include "rungekutta.hpp"
24 
25 namespace autoware
26 {
27 
28 namespace motion
29 {
30 
31 namespace planning
32 {
33 
34 namespace parking_planner
35 {
37 
38 // We could introduce a separate class for this, but it really has the same fields
39 // and the same order, so we just alias
40 template<typename T>
42 
44 template<typename T>
46 {
47 public:
49  T length_front, T length_rear, T vehicle_width,
50  T front_overhang, T rear_overhang) noexcept
51  {
52  m_length_front = length_front;
53  m_length_rear = length_rear;
54  m_vehicle_width = vehicle_width;
55  m_front_overhang = front_overhang;
56  m_rear_overhang = rear_overhang;
57  }
58 
59  std::vector<T> serialize(void) const
60  {
61  return std::vector<T>{m_length_front, m_length_rear, m_vehicle_width, m_front_overhang,
62  m_rear_overhang};
63  }
64  //
65  // Turn a previsouly serialized state vector back into a VehicleState object
66  static BicycleModelParameters deserialize(std::vector<T> serialized_states)
67  {
68  if (serialized_states.size() != BicycleModelParameters::internal_parameters_number) {
69  throw std::length_error {"Need a vector of length " +
70  std::string{BicycleModelParameters::internal_parameters_number}
71  };
72  }
73 
75  serialized_states[0], serialized_states[1], serialized_states[2],
76  serialized_states[3],
77  serialized_states[4]);
78  }
79 
80  // Getters and setters, nothing fancy now but may get checks later
81  // TODO(enhancement,s.me) add nonnegativity checks
82  T get_length_front() const noexcept {return m_length_front;}
83  T get_length_rear() const noexcept {return m_length_rear;}
84  T get_vehicle_width() const noexcept {return m_vehicle_width;}
85  T get_front_overhang() const noexcept {return m_front_overhang;}
86  T get_rear_overhang() const noexcept {return m_rear_overhang;}
87  void set_length_front(T length_front) noexcept {m_length_front = length_front;}
88  void set_length_rear(T length_rear) noexcept {m_length_rear = length_rear;}
89  void set_vehicle_width(T vehicle_width) noexcept {m_vehicle_width = vehicle_width;}
90  void set_front_overhang(T front_overhang) noexcept {m_front_overhang = front_overhang;}
91  void set_rear_overhang(T rear_overhang) noexcept {m_rear_overhang = rear_overhang;}
92  static constexpr std::size_t get_serialized_length() noexcept
93  {
94  return internal_parameters_number;
95  }
96 
97 private:
98  static constexpr std::size_t internal_parameters_number = 5;
99 
102  T m_length_front;
103 
106  T m_length_rear;
107 
109  T m_vehicle_width;
110 
113  T m_front_overhang;
114 
117  T m_rear_overhang;
118 };
119 
121 template<typename T, typename V>
123 {
124 public:
126  : m_parameters(parameters)
127  {
128  // nothing other than the explicit initializer right now
129  }
130 
136  {
137  const auto lr = this->m_parameters.get_length_rear();
138  const auto lf = this->m_parameters.get_length_front();
139  const auto beta = atan(lr * tan(states.get_steering()) / (lf + lr));
141  states.get_velocity() * cos(states.get_heading() + beta),
142  states.get_velocity() * sin(states.get_heading() + beta),
143  commands.get_throttle(),
144  states.get_velocity() / lr * sin(beta),
145  commands.get_steering_rate()
146  );
147  }
148 
149 
158  VehicleState<T> states,
159  VehicleCommand<T> commands,
160  const float64_t stepsize,
161  const std::size_t number_of_steps
162  ) const
163  {
164  const auto states_vec = states.serialize();
165  const auto commands_vec = commands.serialize();
166 
167  const std::function<std::vector<T>(std::vector<T>, std::vector<T>)> dynfun =
168  [this](const auto states, const auto commands) {
169  return this->dynamics_serialized(states, commands);
170  };
171 
172  const auto results_vec = RK4(states_vec, commands_vec, dynfun, stepsize, number_of_steps);
173  return VehicleStateDerivative<T>::deserialize(results_vec);
174  }
175 
181  std::vector<T> dynamics_serialized(
182  std::vector<T> serialized_states,
183  std::vector<T> serialized_commands) const
184  {
185  return this->dynamics(
186  VehicleState<T>::deserialize(serialized_states),
187  VehicleCommand<T>::deserialize(serialized_commands) ).serialize();
188  }
189 
190 
195  {
196  std::vector<Point2D<T>> corners{};
197  const auto xfront = m_parameters.get_length_front() + m_parameters.get_front_overhang();
198  const auto xrear = m_parameters.get_length_rear() + m_parameters.get_rear_overhang();
199  const auto yhalf = 0.5 * m_parameters.get_vehicle_width();
200  corners.push_back(Point2D<T>(-xrear, yhalf) );
201  corners.push_back(Point2D<T>(-xrear, -yhalf) );
202  corners.push_back(Point2D<T>(xfront, -yhalf) );
203  corners.push_back(Point2D<T>(xfront, yhalf) );
204  auto polytope = Polytope2D<T>(corners);
205  polytope.rotate_and_shift(
206  states.get_heading(),
207  Point2D<T>({}, {}), Point2D<T>(states.get_x(), states.get_y()));
208  return polytope;
209  }
210 
211 private:
213  BicycleModelParameters<V> m_parameters;
214 };
215 
216 
217 } // namespace parking_planner
218 } // namespace planning
219 } // namespace motion
220 } // namespace autoware
221 
222 #endif // PARKING_PLANNER__BICYCLE_MODEL_HPP_
autoware::motion::planning::parking_planner::BicycleModelParameters::get_serialized_length
static constexpr std::size_t get_serialized_length() noexcept
Definition: bicycle_model.hpp:92
autoware::motion::planning::parking_planner::Point2D
Class describing a point in two-dimensional space.
Definition: geometry.hpp:45
autoware::motion::planning::parking_planner::BicycleModel::integrated_dynamics
VehicleStateDerivative< T > integrated_dynamics(VehicleState< T > states, VehicleCommand< T > commands, const float64_t stepsize, const std::size_t number_of_steps) const
Evaluate the integrated dynamics for given states and commands. A runge-kutta integration scheme is u...
Definition: bicycle_model.hpp:157
autoware::motion::planning::parking_planner::VehicleState::get_heading
T get_heading() const noexcept
Definition: parking_planner_types.hpp:79
autoware::motion::planning::parking_planner::BicycleModelParameters::set_rear_overhang
void set_rear_overhang(T rear_overhang) noexcept
Definition: bicycle_model.hpp:91
autoware::motion::planning::parking_planner::BicycleModelParameters::get_rear_overhang
T get_rear_overhang() const noexcept
Definition: bicycle_model.hpp:86
autoware::motion::planning::parking_planner::BicycleModelParameters::set_length_front
void set_length_front(T length_front) noexcept
Definition: bicycle_model.hpp:87
catr_diff.T
T
Definition: catr_diff.py:22
autoware::motion::planning::parking_planner::BicycleModel
Class implementing a kinematic bicycle model.
Definition: bicycle_model.hpp:122
autoware::motion::planning::parking_planner::BicycleModelParameters::get_length_rear
T get_length_rear() const noexcept
Definition: bicycle_model.hpp:83
types.hpp
This file includes common type definition.
beta
IntermediateState beta
Definition: kinematic_bicycle.snippet.hpp:42
autoware::motion::planning::parking_planner::VehicleCommand
Class to represent the inputs of a vehicle.
Definition: parking_planner_types.hpp:127
autoware::motion::planning::parking_planner::BicycleModelParameters::set_length_rear
void set_length_rear(T length_rear) noexcept
Definition: bicycle_model.hpp:88
autoware::motion::planning::parking_planner::BicycleModelParameters::get_vehicle_width
T get_vehicle_width() const noexcept
Definition: bicycle_model.hpp:84
autoware::motion::planning::parking_planner::VehicleCommand::get_throttle
T get_throttle() const noexcept
Definition: parking_planner_types.hpp:158
autoware::motion::planning::parking_planner::BicycleModelParameters::get_length_front
T get_length_front() const noexcept
Definition: bicycle_model.hpp:82
autoware::motion::planning::parking_planner::BicycleModelParameters::serialize
std::vector< T > serialize(void) const
Definition: bicycle_model.hpp:59
autoware::motion::planning::parking_planner::BicycleModelParameters::set_front_overhang
void set_front_overhang(T front_overhang) noexcept
Definition: bicycle_model.hpp:90
autoware::motion::planning::parking_planner::VehicleState::serialize
std::vector< T > serialize(void) const
Turn the object into flat vector of doubles (for use with numerical code)
Definition: parking_planner_types.hpp:55
autoware::motion::planning::parking_planner::VehicleState::get_velocity
T get_velocity() const noexcept
Definition: parking_planner_types.hpp:78
autoware::motion::planning::parking_planner::BicycleModelParameters::set_vehicle_width
void set_vehicle_width(T vehicle_width) noexcept
Definition: bicycle_model.hpp:89
autoware::motion::planning::parking_planner::BicycleModel::dynamics_serialized
std::vector< T > dynamics_serialized(std::vector< T > serialized_states, std::vector< T > serialized_commands) const
Serialized version of the continuous-time dynamics evaluation. See the help of the dynamics function ...
Definition: bicycle_model.hpp:181
autoware::motion::planning::parking_planner::Polytope2D
Class describing a polytope in two-dimensional space.
Definition: geometry.hpp:244
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
autoware::motion::planning::parking_planner::VehicleState
Class to represent the state of a vehicle.
Definition: parking_planner_types.hpp:37
geometry.hpp
autoware::motion::planning::parking_planner::BicycleModelParameters::get_front_overhang
T get_front_overhang() const noexcept
Definition: bicycle_model.hpp:85
autoware::motion::planning::parking_planner::VehicleState::get_steering
T get_steering() const noexcept
Definition: parking_planner_types.hpp:80
motion
Definition: controller_base.hpp:30
autoware::motion::planning::parking_planner::BicycleModel::BicycleModel
BicycleModel(BicycleModelParameters< V > parameters)
Definition: bicycle_model.hpp:125
autoware::motion::planning::parking_planner::VehicleCommand::serialize
std::vector< T > serialize(void) const
Turn the object into flat vector of doubles (for use with numerical code)
Definition: parking_planner_types.hpp:142
autoware::motion::planning::parking_planner::BicycleModel::compute_bounding_box
Polytope2D< T > compute_bounding_box(VehicleState< T > states) const
compute a bounding box of a given vehicle model for given states.
Definition: bicycle_model.hpp:194
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::motion::planning::parking_planner::VehicleState::get_y
T get_y() const noexcept
Definition: parking_planner_types.hpp:77
autoware::motion::planning::parking_planner::BicycleModel::dynamics
VehicleStateDerivative< T > dynamics(VehicleState< T > states, VehicleCommand< T > commands) const
Evaluate the dynamics for given states and commands.
Definition: bicycle_model.hpp:135
autoware::motion::planning::parking_planner::BicycleModelParameters::deserialize
static BicycleModelParameters deserialize(std::vector< T > serialized_states)
Definition: bicycle_model.hpp:66
autoware::motion::planning::parking_planner::BicycleModelParameters
Class for storing physical parameters of a bicycle model bicycle model.
Definition: bicycle_model.hpp:45
autoware::common::types::float64_t
double float64_t
Definition: types.hpp:37
autoware::motion::planning::parking_planner::VehicleState::deserialize
static VehicleState deserialize(std::vector< T > serialized_states)
Definition: parking_planner_types.hpp:61
autoware::motion::planning::parking_planner::VehicleCommand::get_steering_rate
T get_steering_rate() const noexcept
Definition: parking_planner_types.hpp:157
autoware::motion::planning::parking_planner::VehicleState::get_x
T get_x() const noexcept
Definition: parking_planner_types.hpp:76
parking_planner_types.hpp
autoware::motion::planning::parking_planner::BicycleModelParameters::BicycleModelParameters
BicycleModelParameters(T length_front, T length_rear, T vehicle_width, T front_overhang, T rear_overhang) noexcept
Definition: bicycle_model.hpp:48
rungekutta.hpp