mpc_controller_test.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /******************************************************************************
  2. * Copyright 2017 The Apollo Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *****************************************************************************/
  16. #include "modules/control/controller/mpc_controller.h"
  17. #include "cyber/common/file.h"
  18. #include "cyber/common/log.h"
  19. #include "gmock/gmock.h"
  20. #include "gtest/gtest.h"
  21. #include "cyber/time/clock.h"
  22. #include "modules/common/vehicle_state/vehicle_state_provider.h"
  23. #include "modules/control/common/control_gflags.h"
  24. #include "modules/control/proto/control_conf.pb.h"
  25. #include "modules/localization/common/localization_gflags.h"
  26. #include "modules/planning/proto/planning.pb.h"
  27. namespace apollo {
  28. namespace control {
  29. using apollo::cyber::Clock;
  30. using PlanningTrajectoryPb = planning::ADCTrajectory;
  31. using LocalizationPb = localization::LocalizationEstimate;
  32. using ChassisPb = canbus::Chassis;
  33. using apollo::common::VehicleStateProvider;
  34. class MPCControllerTest : public ::testing::Test, MPCController {
  35. public:
  36. virtual void SetupTestCase() {
  37. FLAGS_v = 4;
  38. FLAGS_control_conf_file =
  39. "/apollo/modules//control/testdata/mpc_controller_test/"
  40. "control_conf.pb.txt ";
  41. ControlConf control_conf;
  42. ACHECK(cyber::common::GetProtoFromFile(FLAGS_control_conf_file,
  43. &control_conf));
  44. mpc_conf_ = control_conf.mpc_controller_conf();
  45. timestamp_ = Clock::NowInSeconds();
  46. }
  47. void ComputeLateralErrors(const double x, const double y, const double theta,
  48. const double linear_v, const double angular_v,
  49. const double linear_a,
  50. const TrajectoryAnalyzer &trajectory_analyzer,
  51. SimpleMPCDebug *debug) {
  52. MPCController::ComputeLateralErrors(x, y, theta, linear_v, angular_v,
  53. linear_a, trajectory_analyzer, debug);
  54. }
  55. protected:
  56. LocalizationPb LoadLocalizaionPb(const std::string &filename) {
  57. LocalizationPb localization_pb;
  58. ACHECK(cyber::common::GetProtoFromFile(filename, &localization_pb))
  59. << "Failed to open file " << filename;
  60. localization_pb.mutable_header()->set_timestamp_sec(timestamp_);
  61. return localization_pb;
  62. }
  63. ChassisPb LoadChassisPb(const std::string &filename) {
  64. ChassisPb chassis_pb;
  65. ACHECK(cyber::common::GetProtoFromFile(filename, &chassis_pb))
  66. << "Failed to open file " << filename;
  67. chassis_pb.mutable_header()->set_timestamp_sec(timestamp_);
  68. return chassis_pb;
  69. }
  70. PlanningTrajectoryPb LoadPlanningTrajectoryPb(const std::string &filename) {
  71. PlanningTrajectoryPb planning_trajectory_pb;
  72. ACHECK(cyber::common::GetProtoFromFile(filename, &planning_trajectory_pb))
  73. << "Failed to open file " << filename;
  74. planning_trajectory_pb.mutable_header()->set_timestamp_sec(timestamp_);
  75. return planning_trajectory_pb;
  76. }
  77. MPCControllerConf mpc_conf_;
  78. double timestamp_ = 0.0;
  79. };
  80. TEST_F(MPCControllerTest, ComputeLateralErrors) {
  81. auto localization_pb = LoadLocalizaionPb(
  82. "/apollo/modules//control/testdata/mpc_controller_test/"
  83. "1_localization.pb.txt");
  84. auto chassis_pb = LoadChassisPb(
  85. "/apollo/modules/control/testdata/mpc_controller_test/1_chassis.pb.txt");
  86. FLAGS_enable_map_reference_unify = false;
  87. auto injector = std::make_shared<DependencyInjector>();
  88. auto vehicle_state = injector->vehicle_state();
  89. vehicle_state->Update(localization_pb, chassis_pb);
  90. auto planning_trajectory_pb = LoadPlanningTrajectoryPb(
  91. "/apollo/modules//control/testdata/mpc_controller_test/"
  92. "1_planning.pb.txt");
  93. TrajectoryAnalyzer trajectory_analyzer(&planning_trajectory_pb);
  94. ControlCommand cmd;
  95. SimpleMPCDebug *debug = cmd.mutable_debug()->mutable_simple_mpc_debug();
  96. ComputeLateralErrors(
  97. vehicle_state->x(), vehicle_state->y(), vehicle_state->heading(),
  98. vehicle_state->linear_velocity(), vehicle_state->angular_velocity(),
  99. vehicle_state->linear_acceleration(), trajectory_analyzer, debug);
  100. double theta_error_expected = -0.03549;
  101. double theta_error_dot_expected = 0.0044552856731;
  102. double d_error_expected = 1.30917375441;
  103. double d_error_dot_expected = 0.0;
  104. double matched_theta_expected = -1.81266;
  105. double matched_kappa_expected = -0.00237307;
  106. EXPECT_NEAR(debug->heading_error(), theta_error_expected, 0.001);
  107. EXPECT_NEAR(debug->heading_error_rate(), theta_error_dot_expected, 0.001);
  108. EXPECT_NEAR(debug->lateral_error(), d_error_expected, 0.001);
  109. EXPECT_NEAR(debug->lateral_error_rate(), d_error_dot_expected, 0.001);
  110. EXPECT_NEAR(debug->ref_heading(), matched_theta_expected, 0.001);
  111. EXPECT_NEAR(debug->curvature(), matched_kappa_expected, 0.001);
  112. }
  113. } // namespace control
  114. } // namespace apollo