73 const double DUBINS_EPS = 1e-6;
74 const double DUBINS_ZERO = -1e-9;
76 Dubins_State_Space::Dubins_Path dubinsLSL(
double d,
double alpha,
double beta)
78 double ca = cos(
alpha), sa = sin(
alpha), cb = cos(beta), sb = sin(beta);
79 double tmp = 2. +
d *
d - 2. * (ca * cb + sa * sb -
d * (sa - sb));
80 if (tmp >= DUBINS_ZERO)
82 double theta = atan2(cb - ca,
d + sa - sb);
84 double p = sqrt(max(tmp, 0.));
85 double q =
twopify(beta - theta);
86 assert(fabs(p * cos(
alpha + t) - sa + sb -
d) < DUBINS_EPS);
87 assert(fabs(p * sin(
alpha + t) + ca - cb) < DUBINS_EPS);
88 assert(
twopify(
alpha + t + q - beta + .5 * DUBINS_EPS) < DUBINS_EPS);
89 return Dubins_State_Space::Dubins_Path(Dubins_State_Space::dubins_path_type[0], t, p, q);
91 return Dubins_State_Space::Dubins_Path();
94 Dubins_State_Space::Dubins_Path dubinsRSR(
double d,
double alpha,
double beta)
96 double ca = cos(
alpha), sa = sin(
alpha), cb = cos(beta), sb = sin(beta);
97 double tmp = 2. +
d *
d - 2. * (ca * cb + sa * sb -
d * (sb - sa));
98 if (tmp >= DUBINS_ZERO)
100 double theta = atan2(ca - cb,
d - sa + sb);
102 double p = sqrt(max(tmp, 0.));
103 double q =
twopify(-beta + theta);
104 assert(fabs(p * cos(
alpha - t) + sa - sb -
d) < DUBINS_EPS);
105 assert(fabs(p * sin(
alpha - t) - ca + cb) < DUBINS_EPS);
106 assert(
twopify(
alpha - t - q - beta + .5 * DUBINS_EPS) < DUBINS_EPS);
107 return Dubins_State_Space::Dubins_Path(Dubins_State_Space::dubins_path_type[1], t, p, q);
109 return Dubins_State_Space::Dubins_Path();
112 Dubins_State_Space::Dubins_Path dubinsRSL(
double d,
double alpha,
double beta)
114 double ca = cos(
alpha), sa = sin(
alpha), cb = cos(beta), sb = sin(beta);
115 double tmp =
d *
d - 2. + 2. * (ca * cb + sa * sb -
d * (sa + sb));
116 if (tmp >= DUBINS_ZERO)
118 double p = sqrt(max(tmp, 0.));
119 double theta = atan2(ca + cb,
d - sa - sb) - atan2(2., p);
121 double q =
twopify(beta - theta);
122 assert(fabs(p * cos(
alpha - t) - 2. * sin(
alpha - t) + sa + sb -
d) < DUBINS_EPS);
123 assert(fabs(p * sin(
alpha - t) + 2. * cos(
alpha - t) - ca - cb) < DUBINS_EPS);
124 assert(
twopify(
alpha - t + q - beta + .5 * DUBINS_EPS) < DUBINS_EPS);
125 return Dubins_State_Space::Dubins_Path(Dubins_State_Space::dubins_path_type[2], t, p, q);
127 return Dubins_State_Space::Dubins_Path();
130 Dubins_State_Space::Dubins_Path dubinsLSR(
double d,
double alpha,
double beta)
132 double ca = cos(
alpha), sa = sin(
alpha), cb = cos(beta), sb = sin(beta);
133 double tmp = -2. +
d *
d + 2. * (ca * cb + sa * sb +
d * (sa + sb));
134 if (tmp >= DUBINS_ZERO)
136 double p = sqrt(max(tmp, 0.));
137 double theta = atan2(-ca - cb,
d + sa + sb) - atan2(-2., p);
139 double q =
twopify(-beta + theta);
140 assert(fabs(p * cos(
alpha + t) + 2. * sin(
alpha + t) - sa - sb -
d) < DUBINS_EPS);
141 assert(fabs(p * sin(
alpha + t) - 2. * cos(
alpha + t) + ca + cb) < DUBINS_EPS);
142 assert(
twopify(
alpha + t - q - beta + .5 * DUBINS_EPS) < DUBINS_EPS);
143 return Dubins_State_Space::Dubins_Path(Dubins_State_Space::dubins_path_type[3], t, p, q);
145 return Dubins_State_Space::Dubins_Path();
148 Dubins_State_Space::Dubins_Path dubinsRLR(
double d,
double alpha,
double beta)
150 double ca = cos(
alpha), sa = sin(
alpha), cb = cos(beta), sb = sin(beta);
151 double tmp = .125 * (6. -
d *
d + 2. * (ca * cb + sa * sb +
d * (sa - sb)));
154 double p =
TWO_PI - acos(tmp);
155 double theta = atan2(ca - cb,
d - sa + sb);
158 assert(fabs(2. * sin(
alpha - t + p) - 2. * sin(
alpha - t) -
d + sa - sb) < DUBINS_EPS);
159 assert(fabs(-2. * cos(
alpha - t + p) + 2. * cos(
alpha - t) - ca + cb) < DUBINS_EPS);
160 assert(
twopify(
alpha - t + p - q - beta + .5 * DUBINS_EPS) < DUBINS_EPS);
161 return Dubins_State_Space::Dubins_Path(Dubins_State_Space::dubins_path_type[4], t, p, q);
163 return Dubins_State_Space::Dubins_Path();
166 Dubins_State_Space::Dubins_Path dubinsLRL(
double d,
double alpha,
double beta)
168 double ca = cos(
alpha), sa = sin(
alpha), cb = cos(beta), sb = sin(beta);
169 double tmp = .125 * (6. -
d *
d + 2. * (ca * cb + sa * sb -
d * (sa - sb)));
172 double p =
TWO_PI - acos(tmp);
173 double theta = atan2(-ca + cb,
d + sa - sb);
176 assert(fabs(-2. * sin(
alpha + t - p) + 2. * sin(
alpha + t) -
d - sa + sb) < DUBINS_EPS);
177 assert(fabs(2. * cos(
alpha + t - p) - 2. * cos(
alpha + t) + ca - cb) < DUBINS_EPS);
178 assert(
twopify(
alpha + t - p + q - beta + .5 * DUBINS_EPS) < DUBINS_EPS);
179 return Dubins_State_Space::Dubins_Path(Dubins_State_Space::dubins_path_type[5], t, p, q);
181 return Dubins_State_Space::Dubins_Path();
184 Dubins_State_Space::Dubins_Path dubins(
double d,
double alpha,
double beta)
186 if (
d < DUBINS_EPS && fabs(
alpha - beta) < DUBINS_EPS)
187 return Dubins_State_Space::Dubins_Path(Dubins_State_Space::dubins_path_type[0], 0,
d, 0);
189 Dubins_State_Space::Dubins_Path
path(dubinsLSL(
d,
alpha, beta)), tmp(dubinsRSR(
d,
alpha, beta));
190 double len, minLength =
path.length();
192 if ((len = tmp.length()) < minLength)
197 tmp = dubinsRSL(
d,
alpha, beta);
198 if ((len = tmp.length()) < minLength)
203 tmp = dubinsLSR(
d,
alpha, beta);
204 if ((len = tmp.length()) < minLength)
209 tmp = dubinsRLR(
d,
alpha, beta);
210 if ((len = tmp.length()) < minLength)
215 tmp = dubinsLRL(
d,
alpha, beta);
216 if ((tmp.length()) < minLength)
222 const Dubins_State_Space::Dubins_Path_Segment_Type Dubins_State_Space::dubins_path_type[6][3] = {
223 { DUBINS_LEFT, DUBINS_STRAIGHT, DUBINS_LEFT }, { DUBINS_RIGHT, DUBINS_STRAIGHT, DUBINS_RIGHT },
224 { DUBINS_RIGHT, DUBINS_STRAIGHT, DUBINS_LEFT }, { DUBINS_LEFT, DUBINS_STRAIGHT, DUBINS_RIGHT },
225 { DUBINS_RIGHT, DUBINS_LEFT, DUBINS_RIGHT }, { DUBINS_LEFT, DUBINS_RIGHT, DUBINS_LEFT }
230 double dx = state2.
x - state1.
x, dy = state2.
y - state1.
y, th = atan2(dy, dx),
d = sqrt(dx * dx + dy * dy) * kappa_;
232 return steering::dubins(
d,
alpha, beta);
235 void Dubins_State_Space::set_filter_parameters(
const Motion_Noise &motion_noise,
238 ekf_.set_parameters(motion_noise, measurement_noise, controller);
244 return kappa_inv_ * this->dubins(state1, state2).length();
246 return kappa_inv_ * this->dubins(state2, state1).length();
251 vector<Control> dubins_controls;
252 dubins_controls.reserve(3);
255 path = this->dubins(state1, state2);
257 path = this->dubins(state2, state1);
258 for (
unsigned int i = 0; i < 3; ++i)
261 switch (
path.type_[i])
265 control.
kappa = kappa_;
268 case DUBINS_STRAIGHT:
275 control.
kappa = -kappa_;
279 dubins_controls.push_back(control);
284 reverse(dubins_controls.begin(), dubins_controls.end());
285 for (
auto &control : dubins_controls)
286 control.delta_s = -control.delta_s;
288 return dubins_controls;
293 vector<Control> dubins_controls =
get_controls(state1, state2);
294 return integrate(state1, dubins_controls);
298 const State &state2)
const
301 return integrate_with_covariance(state1, controls);
304 vector<State> Dubins_State_Space::integrate(
const State &state,
const vector<Control> &controls)
const
307 State state_curr, state_next;
310 for (
const auto &control : controls)
312 double abs_delta_s(fabs(control.delta_s));
313 n_states += ceil(abs_delta_s / discretization_);
315 path.reserve(n_states + 3);
317 state_curr.
x = state.
x;
318 state_curr.
y = state.
y;
321 for (
const auto &control : controls)
323 double delta_s(control.delta_s);
324 double abs_delta_s(fabs(delta_s));
326 double integration_step(0.0);
328 state_curr.
kappa = control.kappa;
329 state_curr.
d =
sgn(delta_s);
330 path.push_back(state_curr);
332 for (
int i = 0, n = ceil(abs_delta_s / discretization_); i < n; ++i)
335 s_seg += discretization_;
336 if (s_seg > abs_delta_s)
338 integration_step = discretization_ - (s_seg - abs_delta_s);
343 integration_step = discretization_;
345 state_next =
integrate_ODE(state_curr, control, integration_step);
346 path.push_back(state_next);
347 state_curr = state_next;
354 const vector<Control> &controls)
const
356 vector<State_With_Covariance> path_with_covariance;
360 for (
const auto &control : controls)
362 double abs_delta_s(fabs(control.delta_s));
363 n_states += ceil(abs_delta_s / discretization_);
365 path_with_covariance.reserve(n_states + 3);
370 for (
int i = 0; i < 16; i++)
377 for (
const auto &control : controls)
379 double delta_s(control.delta_s);
380 double abs_delta_s(fabs(delta_s));
382 double integration_step(0.0);
386 path_with_covariance.push_back(state_curr);
388 for (
int i = 0, n = ceil(abs_delta_s / discretization_); i < n; ++i)
391 s_seg += discretization_;
392 if (s_seg > abs_delta_s)
394 integration_step = discretization_ - (s_seg - abs_delta_s);
399 integration_step = discretization_;
403 ekf_.predict(state_curr, control, integration_step, state_pred);
406 ekf_.update(state_pred, state_next);
408 path_with_covariance.push_back(state_next);
410 for (
int i = 0; i < 16; i++)
418 return path_with_covariance;
421 State Dubins_State_Space::interpolate(
const State &state,
const vector<Control> &controls,
double t)
const
423 State state_curr, state_next;
425 state_curr.
x = state.
x;
426 state_curr.
y = state.
y;
431 for (
const auto &control : controls)
433 s_path += fabs(control.delta_s);
440 s_inter =
t * s_path;
443 bool interpolated =
false;
444 for (
const auto &control : controls)
449 double delta_s(control.delta_s);
450 double abs_delta_s(fabs(delta_s));
452 double integration_step(0.0);
454 state_curr.
kappa = control.kappa;
455 state_curr.
d =
sgn(delta_s);
460 abs_delta_s = abs_delta_s - (
s - s_inter);
464 for (
int i = 0, n = ceil(abs_delta_s / discretization_); i < n; ++i)
467 s_seg += discretization_;
468 if (s_seg > abs_delta_s)
470 integration_step = discretization_ - (s_seg - abs_delta_s);
475 integration_step = discretization_;
477 state_next =
integrate_ODE(state_curr, control, integration_step);
478 state_curr = state_next;
485 double integration_step)
const