rs_path.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef HYBRID_A_STAR_RS_PATH_H
  28. #define HYBRID_A_STAR_RS_PATH_H
  29. #include "hybrid_A_star/type.h"
  30. #include <cmath>
  31. #include <limits>
  32. #include <vector>
  33. #include <Eigen/Core>
  34. /*!
  35. * Refer to this paper:
  36. * 1990 Optimal paths for a car that goes both forwards and backwards. J. A. Reeds, L. A. Shepp. Pacific J.
  37. *
  38. * Notes: There are many notation errors in this paper, and there are many formula derivation errors.
  39. * When reading the paper, please note that the errors! Errors mainly concentrated in Section 8.
  40. */
  41. class RSPath {
  42. public:
  43. // double turning_radius_ = 1.0;
  44. // RSPath() = delete;
  45. // explicit RSPath(double turning_radius = 1.0);
  46. enum RSPathSegmentType {
  47. N = 0, L = 1, S = 2, R = 3
  48. };
  49. static const RSPathSegmentType RS_path_segment_type[18][5];
  50. struct RSPathData {
  51. public:
  52. explicit RSPathData(const RSPathSegmentType *type = RS_path_segment_type[0],
  53. double t = std::numeric_limits<double>::max(),
  54. double u = 0.0, double v = 0.0, double w = 0.0, double x = 0.0) : type_(type) {
  55. length_[0] = t;
  56. length_[1] = u;
  57. length_[2] = v;
  58. length_[3] = w;
  59. length_[4] = x;
  60. total_length_ = std::fabs(length_[0]) + std::fabs(length_[1]) + std::fabs(length_[2])
  61. + std::fabs(length_[3]) + std::fabs(length_[4]);
  62. }
  63. double Length() const {
  64. return total_length_;
  65. }
  66. public:
  67. double length_[5]{};
  68. const RSPathSegmentType *type_;
  69. private:
  70. double total_length_;
  71. };
  72. /*!
  73. * Calculate the actual distance from (x_0, y_0, yaw_0) to (x_1, y_1, yaw_1)
  74. * @param x_0
  75. * @param y_0
  76. * @param yaw_0
  77. * @param x_1
  78. * @param y_1
  79. * @param yaw_1
  80. * @return
  81. */
  82. double Distance(double x_0, double y_0, double yaw_0,
  83. double x_1, double y_1, double yaw_1);
  84. /*!
  85. * Get a reed shepp path
  86. * @param start_state start state including position and yaw
  87. * @param goal_state goal state including position and yaw
  88. * @param step_size actual step size for collision detection
  89. * @return Discrete points of reed shepp path, including position and yaw
  90. */
  91. TypeVectorVecd<3> GetRSPath(const Eigen::Vector3d &start_state, const Eigen::Vector3d &goal_state, double step_size, double &length);
  92. RSPathData GetRSPath(double x_0, double y_0, double yaw_0,
  93. double x_1, double y_1, double yaw_1);
  94. RSPathData GetRSPath(double x, double y, double phi);
  95. private:
  96. static void CSC(double x, double y, double phi, RSPathData &path);
  97. void CCC(double x, double y, double phi, RSPathData &path);
  98. static void CCCC(double x, double y, double phi, RSPathData &path);
  99. static void CCSC(double x, double y, double phi, RSPathData &path);
  100. static void CCSCC(double x, double y, double phi, RSPathData &path);
  101. static inline double Mod2Pi(double x);
  102. static inline void Polar(double x, double y, double &r, double &theta);
  103. static inline void TauOmega(double u, double v, double xi, double eta, double phi, double &tau, double &omega);
  104. // Paper P390, formula 8.1
  105. static inline bool LpSpLp(double x, double y, double phi, double &t, double &u, double &v);
  106. // Paper P390, formula 8.2
  107. static inline bool LpSpRp(double x, double y, double phi, double &t, double &u, double &v);
  108. // Paper P390 formula 8.3 and 8.4.
  109. // There is an error in the deduction of the formula in the paper.
  110. // It can be deduced by itself according to the inscribed circle.
  111. static inline bool LpRmL(double x, double y, double phi, double &t, double &u, double &v);
  112. // Paper P391 formula 8.7
  113. static inline bool LpRupLumRm(double x, double y, double phi, double &t, double &u, double &v);
  114. // Paper P391 formula 8.8
  115. static inline bool LpRumLumRp(double x, double y, double phi, double &t, double &u, double &v);
  116. // Paper P391 formula 8.9
  117. static inline bool LpRmSmLm(double x, double y, double phi, double &t, double &u, double &v);
  118. // Paper P391 formula 8.10
  119. static inline bool LpRmSmRm(double x, double y, double phi, double &t, double &u, double &v);
  120. // Paper P391 formula 8.11
  121. // There is an error in the deduction of the formula in the paper.
  122. // It can be deduced by itself according to the inscribed circle.
  123. static inline bool LpRmSLmRp(double x, double y, double phi, double &t, double &u, double &v);
  124. // private:
  125. // double turning_radius_ = 1.0;
  126. };
  127. #endif //HYBRID_A_STAR_RS_PATH_H