rs_path.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*******************************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2022 Zhang Zhimeng
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  20. * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  22. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  25. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ******************************************************************************/
  27. #include "hybrid_A_star/rs_path.h"
  28. #include <vector>
  29. #include <Eigen/Core>
  30. #include <glog/logging.h>
  31. double turning_radius_;
  32. void setTurningRadius(double R){
  33. turning_radius_ = R;
  34. }
  35. // P 371: TABLE 1
  36. const RSPath::RSPathSegmentType RSPath::RS_path_segment_type[18][5] = {
  37. {L, R, L, N, N},
  38. {R, L, R, N, N},
  39. {L, R, L, R, N},
  40. {R, L, R, L, N},
  41. {L, R, S, L, N},
  42. {R, L, S, R, N},
  43. {L, S, R, L, N},
  44. {R, S, L, R, N},
  45. {L, R, S, R, N},
  46. {R, L, S, L, N},
  47. {R, S, R, L, N},
  48. {L, S, L, R, N},
  49. {L, S, R, N, N},
  50. {R, S, L, N, N},
  51. {L, S, L, N, N},
  52. {R, S, R, N, N},
  53. {L, R, S, L, R},
  54. {R, L, S, R, L}
  55. };
  56. // RSPath::RSPath(double turning_radius) : turning_radius_(turning_radius) {}
  57. double RSPath::Mod2Pi(double x) {
  58. double v = fmod(x, 2 * M_PI);
  59. if (v < -M_PI) {
  60. v += 2.0 * M_PI;
  61. } else if (v > M_PI) {
  62. v -= 2.0 * M_PI;
  63. }
  64. return v;
  65. }
  66. void RSPath::Polar(double x, double y, double &r, double &theta) {
  67. r = std::sqrt(x * x + y * y);
  68. theta = std::atan2(y, x);
  69. }
  70. void RSPath::TauOmega(double u, double v, double xi, double eta, double phi, double &tau, double &omega) {
  71. double delta = Mod2Pi(u - v);
  72. double A = std::sin(u) - std::sin(delta);
  73. double B = std::cos(u) - std::cos(delta) - 1.0;
  74. double t1 = std::atan2(eta * A - xi * B, xi * A + eta * B);
  75. double t2 = 2.0 * (std::cos(delta) - std::cos(v) - std::cos(u)) + 3.0;
  76. tau = (t2 < 0.0) ? Mod2Pi(t1 + M_PI) : Mod2Pi(t1);
  77. omega = Mod2Pi(tau - u + v - phi);
  78. }
  79. bool RSPath::LpSpLp(double x, double y, double phi, double &t, double &u, double &v) {
  80. Polar(x - std::sin(phi), y - 1.0 + std::cos(phi), u, t);
  81. if (t >= 0.0) {
  82. v = Mod2Pi(phi - t);
  83. if (v >= 0.0) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. bool RSPath::LpSpRp(double x, double y, double phi, double &t, double &u, double &v) {
  90. double t1, u1;
  91. Polar(x + std::sin(phi), y - 1 - std::cos(phi), u1, t1);
  92. u1 = std::pow(u1, 2);
  93. if (u1 < 4.0) {
  94. return false;
  95. }
  96. double theta;
  97. u = std::sqrt(u1 - 4.0);
  98. theta = std::atan2(2.0, u);
  99. t = Mod2Pi(t1 + theta);
  100. v = Mod2Pi(t - phi);
  101. return true;
  102. }
  103. RSPath::RSPathData RSPath::GetRSPath(const double x_0, const double y_0, const double yaw_0,
  104. const double x_1, const double y_1, const double yaw_1) {
  105. // translation
  106. double dx = x_1 - x_0;
  107. double dy = y_1 - y_0;
  108. // rotate
  109. double c = std::cos(yaw_0);// 2d rotation matrix
  110. double s = std::sin(yaw_0);
  111. double x = c * dx + s * dy;
  112. double y = -s * dx + c * dy;
  113. double phi = yaw_1 - yaw_0;
  114. return GetRSPath(x / turning_radius_, y / turning_radius_, phi);
  115. }
  116. RSPath::RSPathData RSPath::GetRSPath(const double x, const double y, const double phi) {
  117. RSPathData path;
  118. CSC(x, y, phi, path);
  119. CCC(x, y, phi, path);
  120. CCCC(x, y, phi, path);
  121. CCSC(x, y, phi, path);
  122. CCSCC(x, y, phi, path);
  123. return path;
  124. }
  125. double RSPath::Distance(const double x_0, const double y_0, const double yaw_0,
  126. const double x_1, const double y_1, const double yaw_1) {
  127. return turning_radius_ * GetRSPath(x_0, y_0, yaw_0, x_1, y_1, yaw_1).Length();
  128. }
  129. void RSPath::CSC(double x, double y, double phi, RSPathData &path) {
  130. double t, u, v, length_min = path.Length(), L;
  131. if (LpSpLp(x, y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  132. path = RSPathData(RS_path_segment_type[14], t, u, v);
  133. length_min = L;
  134. }
  135. if (LpSpLp(-x, y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  136. path = RSPathData(RS_path_segment_type[14], -t, -u, -v);
  137. length_min = L;
  138. }
  139. if (LpSpLp(x, -y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  140. path = RSPathData(RS_path_segment_type[15], t, u, v);
  141. length_min = L;
  142. }
  143. if (LpSpLp(-x, -y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  144. path = RSPathData(RS_path_segment_type[15], -t, -u, -v);
  145. length_min = L;
  146. }
  147. if (LpSpRp(x, y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  148. path = RSPathData(RS_path_segment_type[12], t, u, v);
  149. length_min = L;
  150. }
  151. if (LpSpRp(-x, y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  152. path = RSPathData(RS_path_segment_type[12], -t, -u, -v);
  153. length_min = L;
  154. }
  155. if (LpSpRp(x, -y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  156. path = RSPathData(RS_path_segment_type[13], t, u, v);
  157. length_min = L;
  158. }
  159. if (LpSpRp(-x, -y, phi, t, u, v) && length_min > std::fabs(t) + std::fabs(u) + std::fabs(v)) {
  160. path = RSPathData(RS_path_segment_type[13], -t, -u, -v);
  161. }
  162. }
  163. bool RSPath::LpRmL(double x, double y, double phi, double &t, double &u, double &v) {
  164. double xi = x - std::sin(phi);
  165. double eta = y - 1.0 + std::cos(phi);
  166. double u1, theta;
  167. Polar(xi, eta, u1, theta);
  168. if (u1 > 4.0) {
  169. return false;
  170. }
  171. u = -2.0 * std::asin(0.25 * u1);
  172. t = Mod2Pi(theta + 0.5 * u + M_PI);
  173. v = Mod2Pi(phi - t + u);
  174. return true;
  175. }
  176. void RSPath::CCC(double x, double y, double phi, RSPathData &path) {
  177. double t, u, v, L;
  178. double length_min = path.Length();
  179. if (LpRmL(x, y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  180. path = RSPathData(RS_path_segment_type[0], t, u, v);
  181. length_min = L;
  182. }
  183. if (LpRmL(-x, y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  184. path = RSPathData(RS_path_segment_type[0], -t, -u, -v);
  185. length_min = L;
  186. }
  187. if (LpRmL(x, -y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  188. path = RSPathData(RS_path_segment_type[1], t, u, v);
  189. length_min = L;
  190. }
  191. if (LpRmL(-x, -y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  192. path = RSPathData(RS_path_segment_type[1], -t, -u, -v);
  193. length_min = L;
  194. }
  195. double xb = x * std::cos(phi) + y * std::sin(phi);
  196. double yb = x * std::sin(phi) - y * std::cos(phi);
  197. if (LpRmL(xb, yb, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  198. path = RSPathData(RS_path_segment_type[0], v, u, t);
  199. length_min = L;
  200. }
  201. if (LpRmL(-xb, yb, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  202. path = RSPathData(RS_path_segment_type[0], -v, -u, -t);
  203. length_min = L;
  204. }
  205. if (LpRmL(xb, -yb, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  206. path = RSPathData(RS_path_segment_type[1], v, u, t);
  207. length_min = L;
  208. }
  209. if (LpRmL(-xb, -yb, phi, t, u, v) && length_min > std::fabs(t) + std::fabs(u) + std::fabs(v)) {
  210. path = RSPathData(RS_path_segment_type[1], -v, -u, -t);
  211. }
  212. }
  213. bool RSPath::LpRupLumRm(double x, double y, double phi, double &t, double &u, double &v) {
  214. double xi = x + std::sin(phi);
  215. double eta = y - 1.0 - std::cos(phi);
  216. double rho = 0.25 * (2.0 + std::sqrt(xi * xi + eta * eta));
  217. if (rho > 1.0) {
  218. return false;
  219. }
  220. u = std::acos(rho);
  221. TauOmega(u, -u, xi, eta, phi, t, v);
  222. return true;
  223. }
  224. bool RSPath::LpRumLumRp(double x, double y, double phi, double &t, double &u, double &v) {
  225. double xi = x + std::sin(phi);
  226. double eta = y - 1.0 - std::cos(phi);
  227. double rho = (20.0 - xi * xi - eta * eta) / 16.0;
  228. if (rho >= 0.0 && rho <= 1.0) {
  229. u = -std::acos(rho);
  230. if (u >= -M_PI_2) {
  231. TauOmega(u, u, xi, eta, phi, t, v);
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. void RSPath::CCCC(double x, double y, double phi, RSPathData &path) {
  238. double t, u, v, L;
  239. double length_min = path.Length();
  240. if (LpRupLumRm(x, y, phi, t, u, v) && length_min > (L = std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v))) {
  241. path = RSPathData(RS_path_segment_type[2], t, u, -u, v);
  242. length_min = L;
  243. }
  244. if (LpRupLumRm(-x, y, -phi, t, u, v) &&
  245. length_min > (L = std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v))
  246. ) {
  247. path = RSPathData(RS_path_segment_type[2], -t, -u, u, -v);
  248. length_min = L;
  249. }
  250. if (LpRupLumRm(x, -y, -phi, t, u, v) &&
  251. length_min > (L = std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v))
  252. ) {
  253. path = RSPathData(RS_path_segment_type[3], t, u, -u, v);
  254. length_min = L;
  255. }
  256. if (LpRupLumRm(-x, -y, phi, t, u, v) &&
  257. length_min > (L = std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v))
  258. ) {
  259. path = RSPathData(RS_path_segment_type[3], -t, -u, u, -v);
  260. length_min = L;
  261. }
  262. if (LpRumLumRp(x, y, phi, t, u, v) &&
  263. length_min > (L = std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v))
  264. ) {
  265. path = RSPathData(RS_path_segment_type[2], t, u, u, v);
  266. length_min = L;
  267. }
  268. if (LpRumLumRp(-x, y, -phi, t, u, v) &&
  269. length_min > (L = std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v))
  270. ) {
  271. path = RSPathData(RS_path_segment_type[2], -t, -u, -u, -v);
  272. length_min = L;
  273. }
  274. if (LpRumLumRp(x, -y, -phi, t, u, v) &&
  275. length_min > (L = std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v))
  276. ) {
  277. path = RSPathData(RS_path_segment_type[3], t, u, u, v);
  278. length_min = L;
  279. }
  280. if (LpRumLumRp(-x, -y, phi, t, u, v) && length_min > std::fabs(t) + 2.0 * std::fabs(u) + std::fabs(v)) {
  281. path = RSPathData(RS_path_segment_type[3], -t, -u, -u, -v);
  282. }
  283. }
  284. bool RSPath::LpRmSmLm(double x, double y, double phi, double &t, double &u, double &v) {
  285. double xi = x - std::sin(phi);
  286. double eta = y - 1.0 + std::cos(phi);
  287. double rho, theta;
  288. Polar(xi, eta, rho, theta);
  289. if (rho < 2.0) {
  290. return false;
  291. }
  292. double r = std::sqrt(rho * rho - 4.0);
  293. u = 2.0 - r;
  294. t = Mod2Pi(theta + std::atan2(r, -2.0));
  295. v = Mod2Pi(phi - M_PI_2 - t);
  296. return true;
  297. }
  298. bool RSPath::LpRmSmRm(double x, double y, double phi, double &t, double &u, double &v) {
  299. double xi = x + std::sin(phi);
  300. double eta = y - 1.0 - std::cos(phi);
  301. double rho, theta;
  302. Polar(-eta, xi, rho, theta);
  303. if (rho < 2.0) {
  304. return false;
  305. }
  306. t = theta;
  307. u = 2.0 - rho;
  308. v = Mod2Pi(t + M_PI_2 - phi);
  309. return true;
  310. }
  311. void RSPath::CCSC(double x, double y, double phi, RSPathData &path) {
  312. double t, u, v, L;
  313. double length_min = path.Length() - M_PI_2;
  314. if (LpRmSmLm(x, y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  315. path = RSPathData(RS_path_segment_type[4], t, -M_PI_2, u, v);
  316. length_min = L;
  317. }
  318. if (LpRmSmLm(-x, y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  319. path = RSPathData(RS_path_segment_type[4], -t, M_PI_2, -u, -v);
  320. length_min = L;
  321. }
  322. if (LpRmSmLm(x, -y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  323. path = RSPathData(RS_path_segment_type[5], t, -M_PI_2, u, v);
  324. length_min = L;
  325. }
  326. if (LpRmSmLm(-x, -y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  327. path = RSPathData(RS_path_segment_type[5], -t, M_PI_2, -u, -v);
  328. length_min = L;
  329. }
  330. if (LpRmSmRm(x, y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  331. path = RSPathData(RS_path_segment_type[8], t, -M_PI_2, u, v);
  332. length_min = L;
  333. }
  334. if (LpRmSmRm(-x, y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  335. path = RSPathData(RS_path_segment_type[8], -t, M_PI_2, -u, -v);
  336. length_min = L;
  337. }
  338. if (LpRmSmRm(x, -y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  339. path = RSPathData(RS_path_segment_type[9], t, -M_PI_2, u, v);
  340. length_min = L;
  341. }
  342. if (LpRmSmRm(-x, -y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  343. path = RSPathData(RS_path_segment_type[9], -t, M_PI_2, -u, -v);
  344. length_min = L;
  345. }
  346. double xb = x * std::cos(phi) + y * std::sin(phi);
  347. double yb = x * std::sin(phi) - y * std::cos(phi);
  348. if (LpRmSmLm(xb, yb, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  349. path = RSPathData(RS_path_segment_type[6], v, u, -M_PI_2, t);
  350. length_min = L;
  351. }
  352. if (LpRmSmLm(-xb, yb, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  353. path = RSPathData(RS_path_segment_type[6], -v, -u, M_PI_2, -t);
  354. length_min = L;
  355. }
  356. if (LpRmSmLm(xb, -yb, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  357. path = RSPathData(RS_path_segment_type[7], v, u, -M_PI_2, t);
  358. length_min = L;
  359. }
  360. if (LpRmSmLm(-xb, -yb, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  361. path = RSPathData(RS_path_segment_type[7], -v, -u, M_PI_2, -t);
  362. length_min = L;
  363. }
  364. if (LpRmSmRm(xb, yb, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  365. path = RSPathData(RS_path_segment_type[10], v, u, -M_PI_2, t);
  366. length_min = L;
  367. }
  368. if (LpRmSmRm(-xb, yb, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  369. path = RSPathData(RS_path_segment_type[10], -v, -u, M_PI_2, -t);
  370. length_min = L;
  371. }
  372. if (LpRmSmRm(xb, -yb, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  373. path = RSPathData(RS_path_segment_type[11], v, u, -M_PI_2, t);
  374. length_min = L;
  375. }
  376. if (LpRmSmRm(-xb, -yb, phi, t, u, v) && length_min > std::fabs(t) + std::fabs(u) + std::fabs(v)) {
  377. path = RSPathData(RS_path_segment_type[11], -v, -u, M_PI_2, -t);
  378. }
  379. }
  380. bool RSPath::LpRmSLmRp(double x, double y, double phi, double &t, double &u, double &v) {
  381. double xi = x + std::sin(phi);
  382. double eta = y - 1.0 - std::cos(phi);
  383. double rho, theta;
  384. Polar(xi, eta, rho, theta);
  385. if (rho >= 2.0) {
  386. u = 4.0 - std::sqrt(rho * rho - 4.0);
  387. if (u <= 0.0) {
  388. t = Mod2Pi(std::atan2((4.0 - u) * xi - 2.0 * eta, -2.0 * xi + (u - 4.0) * eta));
  389. v = Mod2Pi(t - phi);
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. void RSPath::CCSCC(double x, double y, double phi, RSPathData &path) {
  396. double t, u, v, L;
  397. double length_min = path.Length() - M_PI;
  398. if (LpRmSLmRp(x, y, phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  399. path = RSPathData(RS_path_segment_type[16], t, -M_PI_2, u, -M_PI_2, v);
  400. length_min = L;
  401. }
  402. if (LpRmSLmRp(-x, y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  403. path = RSPathData(RS_path_segment_type[16], -t, M_PI_2, -u, M_PI_2, -v);
  404. length_min = L;
  405. }
  406. if (LpRmSLmRp(x, -y, -phi, t, u, v) && length_min > (L = std::fabs(t) + std::fabs(u) + std::fabs(v))) {
  407. path = RSPathData(RS_path_segment_type[17], t, -M_PI_2, u, -M_PI_2, v);
  408. length_min = L;
  409. }
  410. if (LpRmSLmRp(-x, -y, phi, t, u, v) && length_min > std::fabs(t) + std::fabs(u) + std::fabs(v)) {
  411. path = RSPathData(RS_path_segment_type[17], -t, M_PI_2, -u, M_PI_2, -v);
  412. }
  413. }
  414. TypeVectorVecd<3> RSPath::GetRSPath(const Eigen::Vector3d &start_state, const Eigen::Vector3d &goal_state,
  415. const double step_size, double &length) {
  416. RSPathData rs_path = GetRSPath(start_state.x(), start_state.y(), start_state.z(),
  417. goal_state.x(), goal_state.y(), goal_state.z());
  418. length = rs_path.Length() * turning_radius_;
  419. // Debug info
  420. // std::cout << "rs length: " << rs_path.Length() << " | "
  421. // << rs_path.length_[0] << " " << rs_path.length_[1] << " "
  422. // << rs_path.length_[2] << " " << rs_path.length_[3] << " "
  423. // << rs_path.length_[4] << std::endl;
  424. // std::cout << "type: "
  425. // << rs_path.type_[0] << " " << rs_path.type_[1] << " "
  426. // << rs_path.type_[2] << " " << rs_path.type_[3] << " "
  427. // << rs_path.type_[4] << std::endl;
  428. const double path_length = rs_path.Length() * turning_radius_;
  429. const auto interpolation_number = static_cast<unsigned int> (path_length / step_size);
  430. double phi;
  431. TypeVectorVecd<3> path_poses;
  432. for (unsigned int i = 0; i <= interpolation_number; ++i) {
  433. double v;
  434. double t = i * 1.0 / interpolation_number;
  435. double seg = t * rs_path.Length();
  436. Eigen::Vector3d temp_pose(0.0, 0.0, start_state.z());
  437. for (unsigned int j = 0; j < 5u && seg > 0; ++j) {
  438. if (rs_path.length_[j] < 0.0) {
  439. v = std::max(-seg, rs_path.length_[j]);
  440. seg += v;
  441. } else {
  442. v = std::min(seg, rs_path.length_[j]);
  443. seg -= v;
  444. }
  445. phi = temp_pose.z();
  446. switch (rs_path.type_[j]) {
  447. case L:
  448. temp_pose.x() = std::sin(phi + v) - std::sin(phi) + temp_pose.x();
  449. temp_pose.y() = -std::cos(phi + v) + std::cos(phi) + temp_pose.y();
  450. temp_pose.z() = phi + v;
  451. break;
  452. case R:
  453. temp_pose.x() = -std::sin(phi - v) + std::sin(phi) + temp_pose.x();
  454. temp_pose.y() = std::cos(phi - v) - std::cos(phi) + temp_pose.y();
  455. temp_pose.z() = phi - v;
  456. break;
  457. case S:
  458. temp_pose.x() = v * std::cos(phi) + temp_pose.x();
  459. temp_pose.y() = v * std::sin(phi) + temp_pose.y();
  460. temp_pose.z() = phi;
  461. break;
  462. case N:
  463. break;
  464. }
  465. }
  466. Eigen::Vector3d pose;
  467. pose.block<2, 1>(0, 0) = temp_pose.block<2, 1>(0, 0) * turning_radius_
  468. + start_state.block<2, 1>(0, 0);
  469. pose.z() = temp_pose.z();
  470. path_poses.emplace_back(pose);
  471. }
  472. return path_poses;
  473. }