planning_test_base.cc 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

17
#include "modules/planning/integration_tests/planning_test_base.h"
18

D
Dong Li 已提交
19
#include "modules/common/log.h"
20
#include "modules/common/vehicle_state/vehicle_state.h"
D
Dong Li 已提交
21

22 23 24 25 26
namespace apollo {
namespace planning {

using common::adapter::AdapterManager;

27 28 29
DEFINE_string(test_data_dir, "", "the test data folder");
DEFINE_bool(test_update_golden_log, false,
            "true to update decision golden log file.");
30
DEFINE_string(test_routing_response_file,
31 32
              "modules/planning/testdata/garage_routing.pb.txt",
              "The routing file used in test");
33 34 35 36 37 38
DEFINE_string(test_localization_file,
              "modules/planning/testdata/garage_localization.pb.txt",
              "The localization test file");
DEFINE_string(test_chassis_file,
              "modules/planning/testdata/garage_chassis.pb.txt",
              "The chassis test file");
39
DEFINE_string(test_prediction_file, "", "The prediction module test file");
40

D
Dong Li 已提交
41
void PlanningTestBase::SetUpTestCase() {
42 43 44
  FLAGS_planning_config_file =
      "modules/planning/testdata/conf/planning_config.pb.txt";
  FLAGS_adapter_config_path = "modules/planning/testdata/conf/adapter.conf";
45 46
  FLAGS_map_dir = "modules/planning/testdata";
  FLAGS_base_map_filename = "base_map.txt";
47 48
  FLAGS_test_localization_file =
      "modules/planning/testdata/garage_localization.pb.txt";
49
  FLAGS_test_chassis_file = "modules/planning/testdata/garage_chassis.pb.txt";
50
  FLAGS_test_prediction_file =
51
      "modules/planning/testdata/garage_prediction.pb.txt";
52 53
}

54
bool PlanningTestBase::SetUpAdapters() {
55 56 57
  if (!AdapterManager::Initialized()) {
    AdapterManager::Init(FLAGS_adapter_config_path);
  }
58
  if (!AdapterManager::GetRoutingResponse()) {
59
    AERROR << "routing is not registered in adapter manager, check adapter "
60
              "config file."
61
           << FLAGS_adapter_config_path;
62 63
    return false;
  }
D
Dong Li 已提交
64
  if (!AdapterManager::FeedRoutingResponseFile(
65 66
          FLAGS_test_routing_response_file)) {
    AERROR << "failed to routing file: " << FLAGS_test_routing_response_file;
67 68
    return false;
  }
D
Dong Li 已提交
69
  AINFO << "Using Routing " << FLAGS_test_routing_response_file;
70 71 72 73 74
  if (!AdapterManager::FeedLocalizationFile(FLAGS_test_localization_file)) {
    AERROR << "Failed to load localization file: "
           << FLAGS_test_localization_file;
    return false;
  }
D
Dong Li 已提交
75
  AINFO << "Using Localization file: " << FLAGS_test_localization_file;
76 77 78
  if (!AdapterManager::FeedChassisFile(FLAGS_test_chassis_file)) {
    AERROR << "Failed to load chassis file: " << FLAGS_test_chassis_file;
    return false;
79
  }
D
Dong Li 已提交
80
  AINFO << "Using Chassis file: " << FLAGS_test_chassis_file;
81
  if (!FLAGS_test_prediction_file.empty() &&
D
Dong Li 已提交
82
      !AdapterManager::FeedPredictionFile(FLAGS_test_prediction_file)) {
83 84 85
    AERROR << "Failed to load prediction file: " << FLAGS_test_prediction_file;
    return false;
  }
J
Jiangtao Hu 已提交
86
  AINFO << "Using Prediction file: " << FLAGS_test_prediction_file;
87 88 89
  return true;
}

90
void PlanningTestBase::SetUp() {
91
  adc_trajectory_ = nullptr;
D
Dong Li 已提交
92
  CHECK(SetUpAdapters()) << "Failed to setup adapters";
93
  planning_.Init();
94 95
}

96
void PlanningTestBase::TrimPlanning(ADCTrajectory* origin) {
D
Dong Li 已提交
97 98
  origin->clear_latency_stats();
  origin->clear_debug();
99 100 101 102 103 104 105 106 107 108 109 110
  origin->mutable_header()->clear_radar_timestamp();
  origin->mutable_header()->clear_lidar_timestamp();
  origin->mutable_header()->clear_timestamp_sec();
  origin->mutable_header()->clear_camera_timestamp();
}

bool PlanningTestBase::RunPlanning(const std::string& test_case_name,
                                   int case_num) {
  const std::string golden_result_file = apollo::common::util::StrCat(
      "result_", test_case_name, "_", case_num, ".pb.txt");
  std::string tmp_golden_path = "/tmp/" + golden_result_file;
  std::string full_golden_path = FLAGS_test_data_dir + "/" + golden_result_file;
111 112
  planning_.RunOnce();
  adc_trajectory_ = AdapterManager::GetPlanning()->GetLatestPublished();
113 114 115 116 117 118
  if (!adc_trajectory_) {
    AERROR << " did not get latest adc trajectory";
    return false;
  }
  TrimPlanning(adc_trajectory_);
  if (FLAGS_test_update_golden_log) {
119
    AINFO << "The golden file is regenerated:"  << full_golden_path;
120
    ::apollo::common::util::SetProtoToASCIIFile(*adc_trajectory_,
121
                                                full_golden_path);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  } else {
    ADCTrajectory golden_result;
    bool load_success = ::apollo::common::util::GetProtoFromASCIIFile(
        full_golden_path, &golden_result);
    if (!load_success) {
      AERROR << "Failed to load golden file: " << full_golden_path;
      ::apollo::common::util::SetProtoToASCIIFile(*adc_trajectory_,
                                                  tmp_golden_path);
      AINFO << "Current result is written to " << tmp_golden_path;
      return false;
    }
    bool same_result =
        ::apollo::common::util::IsProtoEqual(golden_result, *adc_trajectory_);
    if (!same_result) {
      std::string tmp_planning_file = tmp_golden_path + ".tmp";
      ::apollo::common::util::SetProtoToASCIIFile(*adc_trajectory_,
                                                  tmp_planning_file);
      AERROR << "found diff " << tmp_planning_file << " " << full_golden_path;
      return false;
    }
  }
  return true;
144 145
}

146
void PlanningTestBase::export_sl_points(
A
Aaron Xiao 已提交
147
    const std::vector<std::vector<common::SLPoint>>& points,
148
    const std::string& filename) {
D
Dong Li 已提交
149
  AINFO << "Write sl_points to file " << filename;
150 151 152 153 154 155 156 157 158 159 160 161
  std::ofstream ofs(filename);
  ofs << "level, s, l" << std::endl;
  int level = 0;
  for (const auto& level_points : points) {
    for (const auto& point : level_points) {
      ofs << level << ", " << point.s() << ", " << point.l() << std::endl;
    }
    ++level;
  }
  ofs.close();
}

162 163
void PlanningTestBase::export_path_data(const PathData& path_data,
                                        const std::string& filename) {
D
Dong Li 已提交
164
  AINFO << "Write path_data to file " << filename;
165 166 167
  std::ofstream ofs(filename);
  ofs << "s, l, dl, ddl, x, y, z" << std::endl;
  const auto& frenet_path = path_data.frenet_frame_path();
168
  const auto& discrete_path = path_data.discretized_path();
169
  if (frenet_path.NumOfPoints() != discrete_path.NumOfPoints()) {
170 171 172
    AERROR << "frenet_path and discrete path have different number of points";
    return;
  }
173 174 175
  for (uint32_t i = 0; i < frenet_path.NumOfPoints(); ++i) {
    const auto& frenet_point = frenet_path.PointAt(i);
    const auto& discrete_point = discrete_path.PathPointAt(i);
176 177 178 179 180 181 182 183
    ofs << frenet_point.s() << ", " << frenet_point.l() << ", "
        << frenet_point.dl() << ", " << frenet_point.ddl() << discrete_point.x()
        << ", " << discrete_point.y() << ", " << discrete_point.z()
        << std::endl;
  }
  ofs.close();
}

184 185
}  // namespace planning
}  // namespace apollo