rotate_mpc.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 = 20; //优化考虑后面多少步
  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] - angular_velocity * 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 = 15.0 / 180 * M_PI;
  104. double min_angular_velocity = 0.01 / 180 * M_PI;
  105. double max_angular_acceleration_velocity = 30.0 / 180 * M_PI;//mpc_param.acc_angular; //
  106. /// 调整输入
  107. if (angular_velocity > max_angular_velocity) angular_velocity = max_angular_velocity;
  108. if (angular_velocity < -max_angular_velocity) angular_velocity = -max_angular_velocity;
  109. // 优化
  110. typedef CPPAD_TESTVECTOR(double) Dvector;
  111. /// 步长
  112. double dt = mpc_param.dt;
  113. /// 初始化参数
  114. size_t n_vars = N * 2; // 0-N为角度,N-2N为角速度
  115. Dvector vars(n_vars); // 0-N为角度,N-2N为角速度
  116. for (int i = 0; i < n_vars; i++) vars[i] = 0.0;
  117. // 参数上下限
  118. Dvector vars_lowerbound(n_vars);
  119. Dvector vars_upperbound(n_vars);
  120. for (int i = 0; i < n_vars; ++i) {
  121. vars_lowerbound[i] = -1.0e19;
  122. vars_upperbound[i] = 1.0e19;
  123. }
  124. /// limit of angular velocity
  125. for (auto i = N; i < 2 * N; ++i) {
  126. vars_lowerbound[i] = -max_angular_velocity;
  127. vars_upperbound[i] = max_angular_velocity;
  128. }
  129. // Lower and upper limits for the constraints
  130. /// 障碍物是否进入 碰撞检测范围内
  131. // float distance = Pose2d::distance(obs_relative_pose_, Pose2d(0, 0, 0));
  132. float distance = 1000;//屏蔽障碍物
  133. bool find_obs = distance < 5;
  134. if (find_obs) printf(" find obs ..............................\n");
  135. size_t n_constraints = 2 * N + find_obs * N;// 0~N角度,N~2N角加速度, 2N~3N障碍物约束
  136. Dvector constraints_lowerbound(n_constraints);
  137. Dvector constraints_upperbound(n_constraints);
  138. for (auto i = 0; i < n_constraints; ++i) {
  139. constraints_lowerbound[i] = 0;
  140. constraints_upperbound[i] = 0;
  141. }
  142. /// limit of angular acceleration
  143. for (auto i = N; i < 2 * N; ++i) {
  144. constraints_lowerbound[i] = -max_angular_acceleration_velocity;
  145. constraints_upperbound[i] = max_angular_acceleration_velocity;
  146. }
  147. /// limit of obs distance
  148. if (find_obs)
  149. for (auto i = 2 * N; i < 3 * N; ++i) {
  150. constraints_lowerbound[i] = 1.0;
  151. constraints_upperbound[i] = 1e19;
  152. }
  153. // fg_eval for IPOPT solver
  154. size_t n_fg_status = 1 + 3 * find_obs;
  155. Eigen::VectorXd fg_status(n_fg_status);
  156. fg_status[0] = angular_velocity;
  157. if (find_obs) {
  158. fg_status[1] = obs_relative_pose_.x();
  159. fg_status[2] = obs_relative_pose_.y();
  160. fg_status[3] = obs_relative_pose_.theta();
  161. }
  162. // std::cout << " size of fg_status: " << fg_status.size() << " | __LINE__: " << __LINE__ << std::endl;
  163. Pose2d targetAngularInAGV = current_to_target;
  164. /// 调整到适合的目标角度
  165. // if (targetAngularInAGV.theta() > 0) {
  166. //// double acc_time = (max_angular_velocity - angular_velocity) / max_angular_acceleration_velocity;
  167. //// double max_angular_once = max_angular_velocity * N * dt - acc_time * (max_angular_velocity - angular_velocity);
  168. // double max_angular_once = 9.0 / 180 * M_PI;
  169. // if (targetAngularInAGV.theta() > max_angular_once ) {
  170. // targetAngularInAGV.set_theta(max_angular_once);
  171. // }
  172. // }
  173. // else if (targetAngularInAGV.theta() < 0) {
  174. //// double acc_time = fabs((-max_angular_velocity - angular_velocity) / -max_angular_acceleration_velocity);
  175. //// double max_angular_once = -max_angular_velocity * N * dt + acc_time * (-max_angular_velocity - angular_velocity);
  176. // double max_angular_once = -9.0 / 180 * M_PI;
  177. // if (targetAngularInAGV.theta() < max_angular_once ) {
  178. // targetAngularInAGV.set_theta(max_angular_once);
  179. // }
  180. // } else{
  181. //
  182. // }
  183. // printf("target: %f\n",targetAngularInAGV.theta()/M_PI*180);
  184. Eigen::VectorXd condition(4);
  185. condition << dt, targetAngularInAGV.theta(), obs_w_, obs_h_;
  186. FG_eval_rotate fg_eval(fg_status, condition);
  187. // options for IPOPT solver
  188. std::string options;
  189. /// Uncomment this if you'd like more print information
  190. options += "Integer print_level 0\n";
  191. options += "Sparse true forward\n";
  192. options += "Sparse true reverse\n";
  193. options += "Numeric max_cpu_time 0.5\n";
  194. // place to return solution
  195. CppAD::ipopt::solve_result<Dvector> solution;
  196. // solve the problem
  197. CppAD::ipopt::solve<Dvector, FG_eval_rotate>(
  198. options, vars, vars_lowerbound, vars_upperbound, constraints_lowerbound,
  199. constraints_upperbound, fg_eval, solution);
  200. auto now = std::chrono::steady_clock::now();
  201. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - start);
  202. double time =
  203. double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  204. // invalid_number_detected
  205. if (solution.status != CppAD::ipopt::solve_result<Dvector>::success) {
  206. printf(" mpc failed status : %d input: %.5f max_wmg:%.5f\n", solution.status,
  207. angular_velocity, max_angular_velocity);
  208. if (solution.status == CppAD::ipopt::solve_result<Dvector>::local_infeasibility)
  209. return no_solution;
  210. return failed;
  211. }
  212. // Cost
  213. auto cost = solution.obj_value;
  214. // 输出第一步的角速度
  215. out.clear();
  216. double solve_angular_velocity = solution.x[N];
  217. out.push_back(solve_angular_velocity);
  218. return success;
  219. }