hc_reeds_shepp_state_space.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Copyright (c) 2017 Robert Bosch GmbH.
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * *********************************************************************/
17 
18 #include <algorithm>
19 #include <cmath>
20 #include <limits>
21 
24 
25 using namespace std;
26 
27 namespace steering
28 {
29 
30 HC_Reeds_Shepp_State_Space::HC_Reeds_Shepp_State_Space(double kappa, double sigma, double discretization)
31  : HC_CC_State_Space(kappa, sigma, discretization)
32  , hc00_reeds_shepp_state_space_(kappa, sigma, discretization)
33  , hc0pm_reeds_shepp_state_space_(kappa, sigma, discretization)
34  , hcpm0_reeds_shepp_state_space_(kappa, sigma, discretization)
35  , hcpmpm_reeds_shepp_state_space_(kappa, sigma, discretization)
36 {
37 }
38 
39 vector<pair<State, Control>> HC_Reeds_Shepp_State_Space::predict_state(const State &state) const
40 {
41  vector<pair<State, Control>> states_controls;
42 
43  // no prediction required
44  if ((fabs(state.kappa) < get_epsilon()) || ((kappa_ - fabs(state.kappa)) < get_epsilon()))
45  {
46  pair<State, Control> state_control;
47  state_control.first = state;
48  state_control.second.delta_s = 0.0;
49  state_control.second.kappa = state.kappa;
50  state_control.second.sigma = 0.0;
51  states_controls.push_back(state_control);
52  return states_controls;
53  }
54 
55  states_controls.reserve(4);
56  double sgn_kappa = sgn(state.kappa);
57  pair<State, Control> state_control1, state_control2, state_control3, state_control4;
58 
59  // assign controls
60  state_control1.second.delta_s = (kappa_ - sgn_kappa * state.kappa) / sigma_;
61  state_control1.second.kappa = state.kappa;
62  state_control1.second.sigma = sgn_kappa * sigma_;
63  states_controls.push_back(state_control1);
64 
65  state_control2.second.delta_s = -state_control1.second.delta_s;
66  state_control2.second.kappa = state.kappa;
67  state_control2.second.sigma = state_control1.second.sigma;
68  states_controls.push_back(state_control2);
69 
70  state_control3.second.delta_s = sgn_kappa * state.kappa / sigma_;
71  state_control3.second.kappa = state.kappa;
72  state_control3.second.sigma = -sgn_kappa * sigma_;
73  states_controls.push_back(state_control3);
74 
75  state_control4.second.delta_s = -state_control3.second.delta_s;
76  state_control4.second.kappa = state.kappa;
77  state_control4.second.sigma = state_control3.second.sigma;
78  states_controls.push_back(state_control4);
79 
80  // predict states with controls
81  for (auto &state_control : states_controls)
82  {
83  double d = sgn(state_control.second.delta_s);
84  double abs_delta_s = fabs(state_control.second.delta_s);
85  double sigma = state_control.second.sigma;
86  end_of_clothoid(state.x, state.y, state.theta, state.kappa, sigma, d, abs_delta_s, &state_control.first.x,
87  &state_control.first.y, &state_control.first.theta, &state_control.first.kappa);
88  }
89  return states_controls;
90 }
91 
92 double HC_Reeds_Shepp_State_Space::get_distance(const State &state1, const State &state2) const
93 {
94  vector<pair<State, Control>> start_states_controls = this->predict_state(state1);
95  vector<pair<State, Control>> end_states_controls = this->predict_state(state2);
96  vector<double> distances;
97  distances.reserve(16);
98 
99  // compute the path length for all predicted start and end states
100  for (const auto &start_state_control : start_states_controls)
101  {
102  State start_state = start_state_control.first;
103  Control start_control = start_state_control.second;
104  for (const auto &end_state_control : end_states_controls)
105  {
106  State end_state = end_state_control.first;
107  Control end_control = end_state_control.second;
108  // check if start and goal state are equal
109  if (state_equal(start_state, end_state))
110  {
111  Control control = subtract_control(start_control, end_control);
112  distances.push_back(fabs(control.delta_s));
113  }
114  // call appropriate state space
115  else
116  {
117  double distance = 0.0;
118  if (fabs(start_state.kappa) < get_epsilon())
119  {
120  if (fabs(end_state.kappa) < get_epsilon())
121  distance += hc00_reeds_shepp_state_space_.get_distance(start_state, end_state);
122  else
123  distance += hc0pm_reeds_shepp_state_space_.get_distance(start_state, end_state);
124  }
125  else
126  {
127  if (fabs(end_state.kappa) < get_epsilon())
128  distance += hcpm0_reeds_shepp_state_space_.get_distance(start_state, end_state);
129  else
130  distance += hcpmpm_reeds_shepp_state_space_.get_distance(start_state, end_state);
131  }
132  // adjust controls by intial and final control
133  if (fabs(start_control.delta_s) > get_epsilon())
134  distance += fabs(start_control.delta_s);
135  if (fabs(end_control.delta_s) > get_epsilon())
136  distance += fabs(end_control.delta_s);
137  distances.push_back(distance);
138  }
139  }
140  }
141  return *min_element(distances.begin(), distances.end());
142 }
143 
144 vector<Control> HC_Reeds_Shepp_State_Space::get_controls(const State &state1, const State &state2) const
145 {
146  vector<pair<State, Control>> start_states_controls = this->predict_state(state1);
147  vector<pair<State, Control>> end_states_controls = this->predict_state(state2);
148  vector<pair<vector<Control>, double>> hc_rs_controls_distance_pairs;
149  hc_rs_controls_distance_pairs.reserve(16);
150 
151  // compute the path for all predicted start and end states
152  for (const auto &start_state_control : start_states_controls)
153  {
154  State start_state = start_state_control.first;
155  Control start_control = start_state_control.second;
156  for (const auto &end_state_control : end_states_controls)
157  {
158  State end_state = end_state_control.first;
159  Control end_control = end_state_control.second;
160  vector<Control> hc_rs_controls;
161  // check if start and goal state are equal
162  if (state_equal(start_state, end_state))
163  {
164  Control control = subtract_control(start_control, end_control);
165  hc_rs_controls.push_back(control);
166  }
167  // call the appropriate state space
168  else
169  {
170  if (fabs(start_state.kappa) < get_epsilon())
171  {
172  if (fabs(end_state.kappa) < get_epsilon())
173  hc_rs_controls = hc00_reeds_shepp_state_space_.get_controls(start_state, end_state);
174  else
175  hc_rs_controls = hc0pm_reeds_shepp_state_space_.get_controls(start_state, end_state);
176  }
177  else
178  {
179  if (fabs(end_state.kappa) < get_epsilon())
180  hc_rs_controls = hcpm0_reeds_shepp_state_space_.get_controls(start_state, end_state);
181  else
182  hc_rs_controls = hcpmpm_reeds_shepp_state_space_.get_controls(start_state, end_state);
183  }
184  // adjust controls by intial and final control
185  if (fabs(start_control.delta_s) > get_epsilon())
186  {
187  hc_rs_controls.insert(hc_rs_controls.begin(), start_control);
188  }
189  if (fabs(end_control.delta_s) > get_epsilon())
190  {
191  reverse_control(end_control);
192  hc_rs_controls.insert(hc_rs_controls.end(), end_control);
193  }
194  }
195 
196  // compute the path length
197  double distance = 0.0;
198  for (const auto &control : hc_rs_controls)
199  distance += fabs(control.delta_s);
200 
201  // push back
202  pair<vector<Control>, double> hc_rs_controls_distance_pair;
203  hc_rs_controls_distance_pair.first = hc_rs_controls;
204  hc_rs_controls_distance_pair.second = distance;
205  hc_rs_controls_distance_pairs.push_back(hc_rs_controls_distance_pair);
206  }
207  }
208 
209  // sort the controls with respect to path length
210  sort(hc_rs_controls_distance_pairs.begin(), hc_rs_controls_distance_pairs.end(),
211  [](const pair<vector<Control>, double> &i, const pair<vector<Control>, double> &j) {
212  return i.second < j.second;
213  });
214 
215  return hc_rs_controls_distance_pairs[0].first;
216 }
217 
218 } // namespace steering
steering::State
Description of a kinematic car's state.
Definition: steering_functions.hpp:44
steering::HC_CC_State_Space::sigma_
double sigma_
Definition: hc_cc_state_space.hpp:95
steering::HC00_Reeds_Shepp_State_Space::get_controls
std::vector< Control > get_controls(const State &state1, const State &state2) const
Returns controls of the shortest path from state1 to state2.
Definition: hc00_reeds_shepp_state_space.cpp:1762
steering::HC_Reeds_Shepp_State_Space::hcpm0_reeds_shepp_state_space_
HCpm0_Reeds_Shepp_State_Space hcpm0_reeds_shepp_state_space_
Definition: hc_reeds_shepp_state_space.hpp:87
steering::State::theta
double theta
Orientation of the robot.
Definition: steering_functions.hpp:70
steering::State::kappa
double kappa
Curvature at position (x,y)
Definition: steering_functions.hpp:73
steering::HCpm0_Reeds_Shepp_State_Space::get_distance
double get_distance(const State &state1, const State &state2) const
Returns shortest path length from state1 to state2.
Definition: hcpm0_reeds_shepp_state_space.cpp:1783
steering::HC_Reeds_Shepp_State_Space::get_controls
std::vector< Control > get_controls(const State &state1, const State &state2) const
Returns controls of the shortest path from state1 to state2.
Definition: hc_reeds_shepp_state_space.cpp:144
steering::HC_Reeds_Shepp_State_Space::hcpmpm_reeds_shepp_state_space_
HCpmpm_Reeds_Shepp_State_Space hcpmpm_reeds_shepp_state_space_
Definition: hc_reeds_shepp_state_space.hpp:88
steering::HCpm0_Reeds_Shepp_State_Space::get_controls
std::vector< Control > get_controls(const State &state1, const State &state2) const
Returns controls of the shortest path from state1 to state2.
Definition: hcpm0_reeds_shepp_state_space.cpp:1791
steering::State::y
double y
Position in y of the robot.
Definition: steering_functions.hpp:67
steering::Control
Description of a path segment with its corresponding control inputs.
Definition: steering_functions.hpp:82
steering::state_equal
bool state_equal(const State &state1, const State &state2)
Checks whether two states are equal.
Definition: paths.cpp:263
steering::HC0pm_Reeds_Shepp_State_Space::get_distance
double get_distance(const State &state1, const State &state2) const
Returns shortest path length from state1 to state2.
Definition: hc0pm_reeds_shepp_state_space.cpp:1785
steering::HC0pm_Reeds_Shepp_State_Space::get_controls
std::vector< Control > get_controls(const State &state1, const State &state2) const
Returns controls of the shortest path from state1 to state2.
Definition: hc0pm_reeds_shepp_state_space.cpp:1793
steering::HC_Reeds_Shepp_State_Space::get_distance
double get_distance(const State &state1, const State &state2) const
Returns shortest path length from state1 to state2.
Definition: hc_reeds_shepp_state_space.cpp:92
steering::reverse_control
void reverse_control(Control &control)
Reverses a control.
Definition: paths.cpp:274
steering::State::x
double x
Position in x of the robot.
Definition: steering_functions.hpp:64
steering::HCpmpm_Reeds_Shepp_State_Space::get_controls
std::vector< Control > get_controls(const State &state1, const State &state2) const
Returns controls of the shortest path from state1 to state2.
Definition: hcpmpm_reeds_shepp_state_space.cpp:1839
steering::HC_CC_State_Space::kappa_
double kappa_
Curvature, sharpness of clothoid.
Definition: hc_cc_state_space.hpp:95
distance
double distance(double x0, double y0, double x1, double y1)
steering::Control::delta_s
double delta_s
Signed arc length of a segment.
Definition: steering_functions.hpp:85
steering::HC_CC_State_Space
Definition: hc_cc_state_space.hpp:45
steering::HC_Reeds_Shepp_State_Space::hc00_reeds_shepp_state_space_
HC00_Reeds_Shepp_State_Space hc00_reeds_shepp_state_space_
Required state spaces.
Definition: hc_reeds_shepp_state_space.hpp:85
steering::HC00_Reeds_Shepp_State_Space::get_distance
double get_distance(const State &state1, const State &state2) const
Returns shortest path length from state1 to state2.
Definition: hc00_reeds_shepp_state_space.cpp:1754
steering::end_of_clothoid
void end_of_clothoid(double x_i, double y_i, double theta_i, double kappa_i, double sigma, double direction, double length, double *x_f, double *y_f, double *theta_f, double *kappa_f)
Computation of the end point on a clothoid x_i, y_i, theta_i, kappa_i: initial configuration sigma: s...
Definition: utilities.cpp:176
steering::get_epsilon
double get_epsilon()
Return value of epsilon.
Definition: utilities.cpp:57
hc_reeds_shepp_state_space.hpp
plot_states.d
d
Definition: plot_states.py:107
steering::HCpmpm_Reeds_Shepp_State_Space::get_distance
double get_distance(const State &state1, const State &state2) const
Returns shortest path length from state1 to state2.
Definition: hcpmpm_reeds_shepp_state_space.cpp:1831
std
plot_states.kappa
kappa
Definition: plot_states.py:106
steering::HC_Reeds_Shepp_State_Space::predict_state
std::vector< std::pair< State, Control > > predict_state(const State &state) const
Predicts a state forwards and backwards to zero and max. curvature.
Definition: hc_reeds_shepp_state_space.cpp:39
steering
Definition: dubins_state_space.hpp:70
steering::HC_Reeds_Shepp_State_Space::hc0pm_reeds_shepp_state_space_
HC0pm_Reeds_Shepp_State_Space hc0pm_reeds_shepp_state_space_
Definition: hc_reeds_shepp_state_space.hpp:86
steering::sgn
double sgn(double x)
Return sign of a number.
Definition: utilities.cpp:62
steering::subtract_control
Control subtract_control(const Control &control1, const Control &control2)
Subtracts control2 from control1.
Definition: paths.cpp:281
utilities.hpp


steering_functions
Author(s): Holger Banzhaf
autogenerated on Mon Dec 11 2023 03:27:43