mpctest.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef MPCTEST_H
  2. #define MPCTEST_H
  3. #include "math.h"
  4. #include "mpc_lateral_controller/mpc.hpp"
  5. #include "mpc_lateral_controller/qp_solver/qp_solver_osqp.hpp"
  6. #include "mpc_lateral_controller/qp_solver/qp_solver_unconstraint_fast.hpp"
  7. #include "mpc_lateral_controller/vehicle_model/vehicle_model_bicycle_dynamics.hpp"
  8. #include "mpc_lateral_controller/vehicle_model/vehicle_model_bicycle_kinematics.hpp"
  9. #include "mpc_lateral_controller/vehicle_model/vehicle_model_bicycle_kinematics_no_delay.hpp"
  10. using namespace autoware::motion::control::mpc_lateral_controller;
  11. class MPCTest
  12. {
  13. public:
  14. MPCTest();
  15. protected:
  16. MPCParam param;
  17. // Test inputs
  18. MPCTrajectory dummy_straight_trajectory;
  19. MPCTrajectory dummy_right_turn_trajectory;
  20. iv::ADCSteeringReport neutral_steer;
  21. iv::ADCPose pose_zero;
  22. double default_velocity = 1.0;
  23. // rclcpp::Logger logger = rclcpp::get_logger("mpc_test_logger");
  24. // Vehicle model parameters
  25. double wheelbase = 2.7;
  26. double steer_limit = 1.0;
  27. double steer_tau = 0.1;
  28. double mass_fl = 600.0;
  29. double mass_fr = 600.0;
  30. double mass_rl = 600.0;
  31. double mass_rr = 600.0;
  32. double cf = 155494.663;
  33. double cr = 155494.663;
  34. // Filters parameter
  35. double steering_lpf_cutoff_hz = 3.0;
  36. double error_deriv_lpf_cutoff_hz = 5.0;
  37. // Test Parameters
  38. double admissible_position_error = 5.0;
  39. double admissible_yaw_error_rad = M_PI_2;
  40. double steer_lim = 0.610865; // 35 degrees
  41. double steer_rate_lim = 2.61799; // 150 degrees
  42. double ctrl_period = 0.03;
  43. bool use_steer_prediction = true;
  44. TrajectoryFilteringParam trajectory_param;
  45. MPCTrajectoryPoint makePoint(const double x, const double y, const float vx)
  46. {
  47. MPCTrajectoryPoint p;
  48. p.x = x;
  49. p.y = y;
  50. p.vx = vx;
  51. return p;
  52. }
  53. void SetUp()
  54. {
  55. param.prediction_horizon = 50;
  56. param.prediction_dt = 0.1;
  57. param.zero_ff_steer_deg = 0.5;
  58. param.input_delay = 0.0;
  59. param.acceleration_limit = 2.0;
  60. param.velocity_time_constant = 0.3;
  61. param.min_prediction_length = 5.0;
  62. param.steer_tau = 0.1;
  63. param.nominal_weight.lat_error = 1.0;
  64. param.nominal_weight.heading_error = 1.0;
  65. param.nominal_weight.heading_error_squared_vel = 1.0;
  66. param.nominal_weight.terminal_lat_error = 1.0;
  67. param.nominal_weight.terminal_heading_error = 0.1;
  68. param.low_curvature_weight.lat_error = 0.1;
  69. param.low_curvature_weight.heading_error = 0.0;
  70. param.low_curvature_weight.heading_error_squared_vel = 0.3;
  71. param.nominal_weight.steering_input = 1.0;
  72. param.nominal_weight.steering_input_squared_vel = 0.25;
  73. param.nominal_weight.lat_jerk = 0.0;
  74. param.nominal_weight.steer_rate = 0.0;
  75. param.nominal_weight.steer_acc = 0.000001;
  76. param.low_curvature_weight.steering_input = 1.0;
  77. param.low_curvature_weight.steering_input_squared_vel = 0.25;
  78. param.low_curvature_weight.lat_jerk = 0.0;
  79. param.low_curvature_weight.steer_rate = 0.0;
  80. param.low_curvature_weight.steer_acc = 0.000001;
  81. param.low_curvature_thresh_curvature = 0.0;
  82. trajectory_param.traj_resample_dist = 0.1;
  83. trajectory_param.path_filter_moving_ave_num = 35;
  84. trajectory_param.curvature_smoothing_num_traj = 1;
  85. trajectory_param.curvature_smoothing_num_ref_steer = 35;
  86. trajectory_param.enable_path_smoothing = true;
  87. trajectory_param.extend_trajectory_for_end_yaw_control = true;
  88. // dummy_straight_trajectory.push_back(0,0.0,0.0,0.0,1.0,0,0,0);
  89. // dummy_straight_trajectory.push_back(1,0.0,0.0,0.0,1.0,0,0,0);
  90. // dummy_straight_trajectory.push_back(2,0.0,0.0,0.0,1.0,0,0,0);
  91. // dummy_straight_trajectory.push_back(3,0.0,0.0,0.0,1.0,0,0,0);
  92. // dummy_straight_trajectory.push_back(36,30.9,0.0,0.0,1.0,0,0,0);
  93. dummy_straight_trajectory.push_back(0,0.0,0.0,0.0,1.0,0,0,0);
  94. dummy_straight_trajectory.push_back(1,0.1,0.0,0.0,1.0,0,0,0);
  95. dummy_straight_trajectory.push_back(2,0.2,0.0,0.0,1.0,0,0,0);
  96. dummy_straight_trajectory.push_back(3,0.3,0.0,0.0,1.0,0,0,0);
  97. dummy_straight_trajectory.push_back(4,0.4,0.0,0.0,1.0,0,0,0);
  98. dummy_right_turn_trajectory.push_back(-1.0,-1.0,0.0,0.0,1.0,0,0,0);
  99. dummy_right_turn_trajectory.push_back(0.0,0.0,0.0,0.0,1.0,0,0,0);
  100. dummy_right_turn_trajectory.push_back(1.0,-1.0,0.0,0.0,1.0,0,0,0);
  101. dummy_right_turn_trajectory.push_back(2.0,-2.0,0.0,0.0,1.0,0,0,0);
  102. // dummy_straight_trajectory.points.push_back(makePoint(0.0, 0.0, 1.0f));
  103. // dummy_straight_trajectory.points.push_back(makePoint(1.0, 0.0, 1.0f));
  104. // dummy_straight_trajectory.points.push_back(makePoint(2.0, 0.0, 1.0f));
  105. // dummy_straight_trajectory.points.push_back(makePoint(3.0, 0.0, 1.0f));
  106. // dummy_straight_trajectory.points.push_back(makePoint(4.0, 0.0, 1.0f));
  107. // dummy_right_turn_trajectory.points.push_back(makePoint(-1.0, -1.0, 1.0f));
  108. // dummy_right_turn_trajectory.points.push_back(makePoint(0.0, 0.0, 1.0f));
  109. // dummy_right_turn_trajectory.points.push_back(makePoint(1.0, -1.0, 1.0f));
  110. // dummy_right_turn_trajectory.points.push_back(makePoint(2.0, -2.0, 1.0f));
  111. neutral_steer.steering_tire_angle = 0.0;
  112. pose_zero.position.x = 0.0;
  113. pose_zero.position.y = 0.0;
  114. }
  115. void initializeMPC(MPC & mpc)
  116. {
  117. mpc.m_param = param;
  118. mpc.m_admissible_position_error = admissible_position_error;
  119. mpc.m_admissible_yaw_error_rad = admissible_yaw_error_rad;
  120. mpc.m_steer_lim = steer_lim;
  121. mpc.m_steer_rate_lim_map_by_curvature.emplace_back(0.0, steer_rate_lim);
  122. mpc.m_steer_rate_lim_map_by_velocity.emplace_back(0.0, steer_rate_lim);
  123. mpc.m_ctrl_period = ctrl_period;
  124. mpc.m_use_steer_prediction = use_steer_prediction;
  125. mpc.initializeLowPassFilters(steering_lpf_cutoff_hz, error_deriv_lpf_cutoff_hz);
  126. mpc.initializeSteeringPredictor();
  127. // const auto current_kinematics =
  128. // makeOdometry(pose_zero, 0.0);
  129. // mpc.setReferenceTrajectory(dummy_straight_trajectory, trajectory_param, current_kinematics);
  130. // Init trajectory
  131. }
  132. iv::ADCOdometry makeOdometry(const iv::ADCPose & pose, const double velocity)
  133. {
  134. iv::ADCOdometry odometry;
  135. odometry.pose = pose;
  136. odometry.twist.linear_x = velocity;
  137. return odometry;
  138. }
  139. public:
  140. void InitializeAndCalculate();
  141. };
  142. #endif // MPCTEST_H