hc_cc_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 <cmath>
19 
22 
23 using namespace std;
24 
25 namespace steering
26 {
27 
28 HC_CC_State_Space::HC_CC_State_Space(double kappa, double sigma, double discretization)
29  : kappa_(kappa), sigma_(sigma), discretization_(discretization)
30 {
31  // assert positive inputs
32  assert(kappa > 0.0 && sigma > 0.0 && discretization > 0.0);
33  // intermediate configuration after first clothoid
34  double length_min = kappa / sigma;
35  double x_i, y_i, theta_i;
36  if (length_min > get_epsilon())
37  {
38  double kappa_i;
39  end_of_clothoid(0, 0, 0, 0, sigma, 1, length_min, &x_i, &y_i, &theta_i, &kappa_i);
40  }
41  else
42  {
43  x_i = 0;
44  y_i = 0;
45  theta_i = 0;
46  }
47  // radius
48  double xc, yc;
49  xc = x_i - sin(theta_i) / kappa;
50  yc = y_i + cos(theta_i) / kappa;
51  double radius = point_distance(xc, yc, 0.0, 0.0);
52  // mu
53  double mu = atan(fabs(xc / yc));
54  double sin_mu = sin(mu);
55  double cos_mu = cos(mu);
56  // delta_min
57  double delta_min = 0.5 * pow(kappa, 2) / sigma;
58  // assign
59  hc_cc_circle_param_.set_param(kappa, sigma, radius, mu, sin_mu, cos_mu, delta_min);
60 }
61 
63  const Measurement_Noise &measurement_noise, const Controller &controller)
64 {
65  ekf_.set_parameters(motion_noise, measurement_noise, controller);
66 }
67 
68 vector<State> HC_CC_State_Space::get_path(const State &state1, const State &state2) const
69 {
70  vector<Control> controls = get_controls(state1, state2);
71  return integrate(state1, controls);
72 }
73 
74 vector<State_With_Covariance> HC_CC_State_Space::get_path_with_covariance(const State_With_Covariance &state1,
75  const State &state2) const
76 {
77  vector<Control> controls = get_controls(state1.state, state2);
78  return integrate_with_covariance(state1, controls);
79 }
80 
81 vector<State> HC_CC_State_Space::integrate(const State &state, const vector<Control> &controls) const
82 {
83  vector<State> path;
84  State state_curr, state_next;
85  // reserve capacity of path
86  int n_states(0);
87  for (const auto &control : controls)
88  {
89  double abs_delta_s(fabs(control.delta_s));
90  n_states += ceil(abs_delta_s / discretization_);
91  }
92  path.reserve(n_states + 3);
93  // push back first state
94  state_curr.x = state.x;
95  state_curr.y = state.y;
96  state_curr.theta = state.theta;
97  state_curr.kappa = controls.front().kappa;
98  state_curr.d = sgn(controls.front().delta_s);
99  path.push_back(state_curr);
100 
101  for (const auto &control : controls)
102  {
103  double delta_s(control.delta_s);
104  double abs_delta_s(fabs(delta_s));
105  double kappa(control.kappa);
106  double s_seg(0.0);
107  double integration_step(0.0);
108  // push_back current state if curvature discontinuity
109  if (fabs(kappa - state_curr.kappa) > get_epsilon())
110  {
111  state_curr.kappa = kappa;
112  state_curr.d = sgn(delta_s);
113  path.push_back(state_curr);
114  }
115 
116  for (int i = 0, n = ceil(abs_delta_s / discretization_); i < n; ++i)
117  {
118  // get integration step
119  s_seg += discretization_;
120  if (s_seg > abs_delta_s)
121  {
122  integration_step = discretization_ - (s_seg - abs_delta_s);
123  s_seg = abs_delta_s;
124  }
125  else
126  {
127  integration_step = discretization_;
128  }
129  state_next = integrate_ODE(state_curr, control, integration_step);
130  path.push_back(state_next);
131  state_curr = state_next;
132  }
133  }
134  return path;
135 }
136 
137 vector<State_With_Covariance> HC_CC_State_Space::integrate_with_covariance(const State_With_Covariance &state,
138  const vector<Control> &controls) const
139 {
140  vector<State_With_Covariance> path_with_covariance;
141  State_With_Covariance state_curr, state_pred, state_next;
142  // reserve capacity of path
143  int n_states(0);
144  for (const auto &control : controls)
145  {
146  double abs_delta_s(fabs(control.delta_s));
147  n_states += ceil(abs_delta_s / discretization_);
148  }
149  path_with_covariance.reserve(n_states + 3);
150  // push back first state
151  state_curr.state.x = state.state.x;
152  state_curr.state.y = state.state.y;
153  state_curr.state.theta = state.state.theta;
154  state_curr.state.kappa = controls.front().kappa;
155  state_curr.state.d = sgn(controls.front().delta_s);
156  for (int i = 0; i < 16; i++)
157  {
158  state_curr.Sigma[i] = state.Sigma[i];
159  state_curr.Lambda[i] = state.Lambda[i];
160  state_curr.covariance[i] = state.covariance[i];
161  }
162  path_with_covariance.push_back(state_curr);
163 
164  for (const auto &control : controls)
165  {
166  double delta_s(control.delta_s);
167  double abs_delta_s(fabs(delta_s));
168  double kappa(control.kappa);
169  double s_seg(0.0);
170  double integration_step(0.0);
171  // push_back current state if curvature discontinuity
172  if (fabs(kappa - state_curr.state.kappa) > get_epsilon())
173  {
174  state_curr.state.kappa = kappa;
175  state_curr.state.d = sgn(delta_s);
176  path_with_covariance.push_back(state_curr);
177  }
178 
179  for (int i = 0, n = ceil(abs_delta_s / discretization_); i < n; ++i)
180  {
181  // get integration step
182  s_seg += discretization_;
183  if (s_seg > abs_delta_s)
184  {
185  integration_step = discretization_ - (s_seg - abs_delta_s);
186  s_seg = abs_delta_s;
187  }
188  else
189  {
190  integration_step = discretization_;
191  }
192  // predict
193  state_pred.state = integrate_ODE(state_curr.state, control, integration_step);
194  ekf_.predict(state_curr, control, integration_step, state_pred);
195  // update
196  state_next.state = state_pred.state;
197  ekf_.update(state_pred, state_next);
198 
199  path_with_covariance.push_back(state_next);
200  state_curr.state = state_next.state;
201  for (int i = 0; i < 16; i++)
202  {
203  state_curr.Sigma[i] = state_next.Sigma[i];
204  state_curr.Lambda[i] = state_next.Lambda[i];
205  state_curr.covariance[i] = state_next.covariance[i];
206  }
207  }
208  }
209  return path_with_covariance;
210 }
211 
212 State HC_CC_State_Space::interpolate(const State &state, const vector<Control> &controls, double t) const
213 {
214  State state_curr, state_next;
215  // get first state
216  state_curr.x = state.x;
217  state_curr.y = state.y;
218  state_curr.theta = state.theta;
219  state_curr.kappa = controls.front().kappa;
220  state_curr.d = sgn(controls.front().delta_s);
221  // get arc length at t
222  double s_path(0.0);
223  double s_inter(0.0);
224  for (const auto &control : controls)
225  {
226  s_path += fabs(control.delta_s);
227  }
228  if (t <= 0.0)
229  return state_curr;
230  else if (t > 1.0)
231  s_inter = s_path;
232  else
233  s_inter = t * s_path;
234 
235  double s(0.0);
236  bool interpolated = false;
237  for (const auto &control : controls)
238  {
239  if (interpolated)
240  break;
241 
242  double delta_s(control.delta_s);
243  double abs_delta_s(fabs(delta_s));
244  double kappa(control.kappa);
245  double s_seg(0.0);
246  double integration_step(0.0);
247  // update current state if curvature discontinuity
248  if (fabs(kappa - state_curr.kappa) > get_epsilon())
249  {
250  state_curr.kappa = kappa;
251  state_curr.d = sgn(delta_s);
252  }
253 
254  s += abs_delta_s;
255  if (s > s_inter)
256  {
257  abs_delta_s = abs_delta_s - (s - s_inter);
258  interpolated = true;
259  }
260 
261  for (int i = 0, n = ceil(abs_delta_s / discretization_); i < n; ++i)
262  {
263  // get integration step
264  s_seg += discretization_;
265  if (s_seg > abs_delta_s)
266  {
267  integration_step = discretization_ - (s_seg - abs_delta_s);
268  s_seg = abs_delta_s;
269  }
270  else
271  {
272  integration_step = discretization_;
273  }
274  state_next = integrate_ODE(state_curr, control, integration_step);
275  state_curr = state_next;
276  }
277  }
278  return state_curr;
279 }
280 
281 inline State HC_CC_State_Space::integrate_ODE(const State &state, const Control &control, double integration_step) const
282 {
283  State state_next;
284  double sigma(control.sigma);
285  double d(sgn(control.delta_s));
286  if (fabs(sigma) > get_epsilon())
287  {
288  end_of_clothoid(state.x, state.y, state.theta, state.kappa, sigma, d, integration_step, &state_next.x,
289  &state_next.y, &state_next.theta, &state_next.kappa);
290  state_next.d = d;
291  }
292  else
293  {
294  if (fabs(state.kappa) > get_epsilon())
295  {
296  end_of_circular_arc(state.x, state.y, state.theta, state.kappa, d, integration_step, &state_next.x, &state_next.y,
297  &state_next.theta);
298  state_next.kappa = state.kappa;
299  state_next.d = d;
300  }
301  else
302  {
303  end_of_straight_line(state.x, state.y, state.theta, d, integration_step, &state_next.x, &state_next.y);
304  state_next.theta = state.theta;
305  state_next.kappa = state.kappa;
306  state_next.d = d;
307  }
308  }
309  return state_next;
310 }
311 
312 } // namespace steering
steering::State_With_Covariance::state
State state
Expected state of the robot.
Definition: steering_functions.hpp:66
steering::State_With_Covariance::covariance
double covariance[16]
Covariance of the state given by Sigma + Lambda: (x_x x_y x_theta x_kappa y_x y_y y_theta y_kappa the...
Definition: steering_functions.hpp:78
steering::HC_CC_State_Space::hc_cc_circle_param_
HC_CC_Circle_Param hc_cc_circle_param_
Parameters of a hc-/cc-circle.
Definition: hc_cc_state_space.hpp:101
hc_cc_state_space.hpp
steering::State
Description of a kinematic car's state.
Definition: steering_functions.hpp:44
steering::point_distance
double point_distance(double x1, double y1, double x2, double y2)
Cartesian distance between two points.
Definition: utilities.cpp:67
steering::HC_CC_State_Space::get_path
std::vector< State > get_path(const State &state1, const State &state2) const
Returns path from state1 to state2.
Definition: hc_cc_state_space.cpp:68
plot_states.path
path
Definition: plot_states.py:88
steering::Control::sigma
double sigma
Sharpness (derivative of curvature with respect to arc length) of a segment.
Definition: steering_functions.hpp:91
s
XmlRpcServer s
steering::State::theta
double theta
Orientation of the robot.
Definition: steering_functions.hpp:70
steering::State_With_Covariance::Sigma
double Sigma[16]
Covariance of the state estimation due to motion and measurement noise.
Definition: steering_functions.hpp:69
steering::HC_CC_State_Space::get_path_with_covariance
std::vector< State_With_Covariance > get_path_with_covariance(const State_With_Covariance &state1, const State &state2) const
Returns path including covariances from state1 to state2.
Definition: hc_cc_state_space.cpp:74
steering::State::kappa
double kappa
Curvature at position (x,y)
Definition: steering_functions.hpp:73
steering::end_of_straight_line
void end_of_straight_line(double x_i, double y_i, double theta, double direction, double length, double *x_f, double *y_f)
Computation of the end point on a straight line x_i, y_i: initial configuration theta: angle of strai...
Definition: utilities.cpp:216
steering::HC_CC_State_Space::integrate
std::vector< State > integrate(const State &state, const std::vector< Control > &controls) const
Returns integrated states given a start state and controls.
Definition: hc_cc_state_space.cpp:81
steering::HC_CC_State_Space::ekf_
EKF ekf_
Extended Kalman Filter for uncertainty propagation.
Definition: hc_cc_state_space.hpp:104
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::EKF::update
void update(const State_With_Covariance &state_pred, State_With_Covariance &state_corr) const
Predicts the covariances.
Definition: ekf.cpp:251
steering::EKF::set_parameters
void set_parameters(const Motion_Noise &motion_noise, const Measurement_Noise &measurement_noise, const Controller &_controller)
Sets the parameters required by the EKF.
Definition: ekf.cpp:46
steering::HC_CC_State_Space::integrate_ODE
State integrate_ODE(const State &state, const Control &control, double integration_step) const
Returns integrated state given a start state, a control, and an integration step.
Definition: hc_cc_state_space.cpp:281
steering::HC_CC_Circle_Param::set_param
void set_param(double _kappa, double _sigma, double _radius, double _mu, double _sin_mu, double _cos_mu, double _delta_min)
Set parameters.
Definition: hc_cc_circle.cpp:39
steering::HC_CC_State_Space::set_filter_parameters
void set_filter_parameters(const Motion_Noise &motion_noise, const Measurement_Noise &measurement_noise, const Controller &controller)
Sets the parameters required by the filter.
Definition: hc_cc_state_space.cpp:62
steering::Motion_Noise
Parameters of the motion noise model according to the book: Probabilistic Robotics,...
Definition: steering_functions.hpp:96
steering::end_of_circular_arc
void end_of_circular_arc(double x_i, double y_i, double theta_i, double kappa, double direction, double length, double *x_f, double *y_f, double *theta_f)
Computation of the end point on a circular arc x_i, y_i, theta_i: initial configuration kappa: curvat...
Definition: utilities.cpp:208
steering::State::x
double x
Position in x of the robot.
Definition: steering_functions.hpp:64
steering::EKF::predict
void predict(const State_With_Covariance &state, const Control &control, double integration_step, State_With_Covariance &state_pred) const
Predicts the covariances based on the paper: Rapidly-exploring random belief trees for motion plannin...
Definition: ekf.cpp:227
steering::HC_CC_State_Space::interpolate
State interpolate(const State &state, const std::vector< Control > &controls, double t) const
Returns interpolated state at distance t in [0,1] (percentage of total path length)
Definition: hc_cc_state_space.cpp:212
steering::Control::delta_s
double delta_s
Signed arc length of a segment.
Definition: steering_functions.hpp:85
steering::State_With_Covariance::Lambda
double Lambda[16]
Covariance of the state estimate due to the absence of measurements.
Definition: steering_functions.hpp:72
steering::HC_CC_State_Space::get_controls
virtual std::vector< Control > get_controls(const State &state1, const State &state2) const =0
Virtual function that returns controls of the shortest path from state1 to state2.
steering::HC_CC_State_Space::discretization_
double discretization_
Discretization of path.
Definition: hc_cc_state_space.hpp:98
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
plot_states.d
d
Definition: plot_states.py:107
std
plot_states.kappa
kappa
Definition: plot_states.py:106
steering::State_With_Covariance
Description of a kinematic car's state with covariance.
Definition: steering_functions.hpp:63
steering::Controller
Parameters of the feedback controller.
Definition: steering_functions.hpp:121
steering
Definition: dubins_state_space.hpp:70
steering::sgn
double sgn(double x)
Return sign of a number.
Definition: utilities.cpp:62
steering::HC_CC_State_Space::integrate_with_covariance
std::vector< State_With_Covariance > integrate_with_covariance(const State_With_Covariance &state, const std::vector< Control > &controls) const
Returns integrated states including covariance given a start state and controls.
Definition: hc_cc_state_space.cpp:137
steering::State::d
double d
Definition: steering_functions.hpp:76
t
geometry_msgs::TransformStamped t
utilities.hpp
steering::Measurement_Noise
Parameters of the measurement noise.
Definition: steering_functions.hpp:108


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