rotate_mpc.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // Created by gf on 23-7-21.
  3. //
  4. #include "rotate_mpc.h"
  5. #include <chrono>
  6. #include <cppad/cppad.hpp>
  7. #include <cppad/ipopt/solve.hpp>
  8. size_t mpc_rotate_steps = 25; //优化考虑后面多少步
  9. #define N mpc_rotate_steps
  10. class FG_eval_rotate {
  11. public:
  12. Eigen::VectorXd m_statu; //当前状态
  13. Eigen::VectorXd m_condition; //搜索条件参数
  14. FG_eval_rotate(Eigen::VectorXd status, Eigen::VectorXd condition) {
  15. m_statu = status;
  16. m_condition = condition;
  17. }
  18. typedef CPPAD_TESTVECTOR(CppAD::AD<double>) ADvector;
  19. void operator()(ADvector &fg, const ADvector &vars) {
  20. fg[0] = 0;
  21. double dt = m_condition[0];
  22. double target_theta = m_condition[1];
  23. double obs_w = m_condition[2];
  24. double obs_h = m_condition[3];
  25. double angular_velocity = m_statu[0];
  26. // Weights for how "important" each cost is - can be tuned
  27. const double y_cost_weight = 1000;
  28. const double angular_cost_weight = 4000;
  29. const double v_cost_weight = 5000;
  30. const double angular_velocity_cost_weight = 1000;
  31. const double acceleration_cost_weight = 1;
  32. const double angular_acceleration_cost_weight = 10;
  33. const double obs_distance_weight = 5000.0;
  34. // 代价函数计算
  35. for (int t = 0; t < N - 1; ++t) {
  36. /// 角速度,角加速度weight loss
  37. //fg[0] += angular_velocity_cost_weight * CppAD::pow(vars[N + t], 2);
  38. fg[0] += angular_acceleration_cost_weight * CppAD::pow(vars[N + t + 1] - vars[N + t], 2);
  39. /// 目标角度 loss
  40. fg[0] += angular_cost_weight * CppAD::pow(target_theta - vars[t], 2);
  41. }
  42. // 约束条件
  43. int n_angular = 1;
  44. int n_angular_acceleration = n_angular + N;
  45. int n_obs = n_angular_acceleration + N;
  46. /// 角度等式约束 1~N
  47. fg[n_angular] = vars[0] - vars[N] * dt;
  48. for (int t = 1; t < N; ++t) {
  49. // State at time t + 1
  50. CppAD::AD<double> theta1 = vars[t];
  51. // State at time t
  52. CppAD::AD<double> theta0 = vars[t - 1];
  53. CppAD::AD<double> angular_velocity0 = vars[N + t - 1];
  54. // Setting up the rest of the model constraints
  55. fg[n_angular + t] = theta1 - (theta0 + angular_velocity0 * dt);
  56. }
  57. /// 角加速度不等式约束 N+1~2N
  58. fg[n_angular_acceleration] = (vars[0 + N] - angular_velocity) / dt;
  59. for (int t = 1; t < N; ++t) {
  60. fg[n_angular_acceleration + t] = (vars[t + N] - vars[t + N - 1]) / dt;
  61. }
  62. /// 障碍物距离不等式约束 2N+1~3N
  63. if (m_statu.size() == 1 + 3) {
  64. float obs_x = m_statu[1];
  65. float obs_y = m_statu[2];
  66. float obs_theta = m_statu[3];
  67. Pose2d obs_relative_pose(obs_x, obs_y, obs_theta);
  68. std::vector<Pose2d> obs_vertexes = Pose2d::generate_rectangle_vertexs(obs_relative_pose, obs_w * 2,
  69. obs_h * 2);
  70. for (auto t = 0; t < N; ++t) {
  71. CppAD::AD<double> min_distance = 1e19;
  72. for (auto i = 0; i < 8; ++i) {
  73. CppAD::AD<double> ra = obs_vertexes[i].x() * CppAD::cos(vars[t]) -
  74. obs_vertexes[i].y() * CppAD::sin(vars[t]);
  75. CppAD::AD<double> rb = obs_vertexes[i].x() * CppAD::sin(vars[t]) +
  76. obs_vertexes[i].y() * CppAD::cos(vars[t]);
  77. CppAD::AD<double> distance = CppAD::pow(ra / obs_w, 8) + CppAD::pow(rb / obs_h, 8);
  78. if (distance < min_distance) min_distance = distance;
  79. }
  80. fg[n_obs + t] = min_distance;
  81. }
  82. // std::cout << " fg[n_obs]: " << fg[n_obs] << " | __LINE__:" << __LINE__ << std::endl;
  83. }
  84. }
  85. };
  86. RotateMPC::RotateMPC(const Pose2d &obs, double obs_w, double obs_h, double min_velocity, double max_velocity) {
  87. min_velocity_ = min_velocity;
  88. max_velocity_ = max_velocity;
  89. obs_relative_pose_ = obs;
  90. obs_w_ = obs_w;
  91. obs_h_ = obs_h;
  92. }
  93. RotateMPC::~RotateMPC() {}
  94. MpcError
  95. RotateMPC::solve(Pose2d current_to_target, Eigen::VectorXd statu, MPC_parameter mpc_param, std::vector<double> &out) {
  96. auto start = std::chrono::steady_clock::now();
  97. // State vector holds all current values needed for vars below
  98. Pose2d pose_agv = Pose2d(statu[0], statu[1], statu[2]);
  99. double line_velocity = statu[3];
  100. double angular_velocity = statu[4];
  101. /// 定义最大最小角度,角速度,角加速度(不区分方向)
  102. double max_angular = M_PI;
  103. double max_angular_velocity = 6.0 / 180 * M_PI;
  104. double min_angular_velocity = 0.1 / 180 * M_PI;
  105. double max_angular_acceleration_velocity = 3.0 / 180 * M_PI;//mpc_param.acc_angular; //
  106. // 优化
  107. typedef CPPAD_TESTVECTOR(double) Dvector;
  108. /// 步长
  109. double dt = mpc_param.dt;
  110. /// 初始化参数
  111. size_t n_vars = N * 2; // 0-N为角度,N-2N为角速度
  112. Dvector vars(n_vars); // 0-N为角度,N-2N为角速度
  113. for (int i = 0; i < n_vars; i++) vars[i] = 0.0;
  114. // 参数上下限
  115. Dvector vars_lowerbound(n_vars);
  116. Dvector vars_upperbound(n_vars);
  117. for (int i = 0; i < n_vars; ++i) {
  118. vars_lowerbound[i] = -1.0e19;
  119. vars_upperbound[i] = 1.0e19;
  120. }
  121. /// limit of angular velocity
  122. for (auto i = N; i < 2 * N; ++i) {
  123. vars_lowerbound[i] = -max_angular_velocity;
  124. vars_upperbound[i] = max_angular_velocity;
  125. }
  126. // Lower and upper limits for the constraints
  127. /// 障碍物是否进入 碰撞检测范围内
  128. float distance = Pose2d::distance(obs_relative_pose_, Pose2d(0, 0, 0));
  129. bool find_obs = distance < 20;
  130. if (find_obs) printf(" find obs ..............................\n");
  131. size_t n_constraints = 2 * N + find_obs * N;// 0~N角度,N~2N角加速度, 2N~3N障碍物约束
  132. Dvector constraints_lowerbound(n_constraints);
  133. Dvector constraints_upperbound(n_constraints);
  134. for (auto i = 0; i < n_constraints; ++i) {
  135. constraints_lowerbound[i] = 0;
  136. constraints_upperbound[i] = 0;
  137. }
  138. /// limit of angular acceleration
  139. for (auto i = N; i < 2 * N; ++i) {
  140. constraints_lowerbound[i] = -max_angular_acceleration_velocity;
  141. constraints_upperbound[i] = max_angular_acceleration_velocity;
  142. }
  143. /// limit of obs distance
  144. if (find_obs)
  145. for (auto i = 2 * N; i < 3 * N; ++i) {
  146. constraints_lowerbound[i] = 1.0;
  147. constraints_upperbound[i] = 1e19;
  148. }
  149. // fg_eval for IPOPT solver
  150. size_t n_fg_status = 1 + 3*find_obs;
  151. Eigen::VectorXd fg_status(n_fg_status);
  152. fg_status[0] = angular_velocity;
  153. if (find_obs){
  154. fg_status[1] = obs_relative_pose_.x();
  155. fg_status[2] = obs_relative_pose_.y();
  156. fg_status[3] = obs_relative_pose_.theta();
  157. }
  158. // std::cout << " size of fg_status: " << fg_status.size() << " | __LINE__: " << __LINE__ << std::endl;
  159. Pose2d targetAngularInAGV = current_to_target;
  160. /// 调整到适合的目标角度
  161. if (targetAngularInAGV.theta() > max_angular_velocity * N * dt)
  162. targetAngularInAGV.set_theta(max_angular_velocity * N * dt);
  163. Eigen::VectorXd condition(4);
  164. condition << dt, targetAngularInAGV.theta(), obs_w_, obs_h_;
  165. FG_eval_rotate fg_eval(fg_status, condition);
  166. // options for IPOPT solver
  167. std::string options;
  168. /// Uncomment this if you'd like more print information
  169. options += "Integer print_level 0\n";
  170. options += "Sparse true forward\n";
  171. options += "Sparse true reverse\n";
  172. options += "Numeric max_cpu_time 0.5\n";
  173. // place to return solution
  174. CppAD::ipopt::solve_result<Dvector> solution;
  175. // solve the problem
  176. CppAD::ipopt::solve<Dvector, FG_eval_rotate>(
  177. options, vars, vars_lowerbound, vars_upperbound, constraints_lowerbound,
  178. constraints_upperbound, fg_eval, solution);
  179. auto now = std::chrono::steady_clock::now();
  180. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - start);
  181. double time =
  182. double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  183. // invalid_number_detected
  184. if (solution.status != CppAD::ipopt::solve_result<Dvector>::success) {
  185. printf(" mpc failed status : %d input: %.5f max_wmg:%.5f\n", solution.status,
  186. angular_velocity, max_angular_velocity);
  187. if (solution.status == CppAD::ipopt::solve_result<Dvector>::local_infeasibility)
  188. return no_solution;
  189. return failed;
  190. }
  191. // Cost
  192. auto cost = solution.obj_value;
  193. // 输出第一步的角速度
  194. out.clear();
  195. double solve_angular_velocity = solution.x[N];
  196. out.push_back(solve_angular_velocity);
  197. return success;
  198. }