sensor_manager.cc 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/******************************************************************************
 * Copyright 2018 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.
 *****************************************************************************/
#include "modules/perception/common/sensor_manager/sensor_manager.h"

#include <string>
19
#include <utility>
20 21
#include "google/protobuf/text_format.h"

22
#include "cybertron/common/log.h"
23 24
#include "modules/perception/lib/config_manager/config_manager.h"
#include "modules/perception/lib/io/file_util.h"
25 26
#include "modules/perception/common/io/io_util.h"
#include "modules/perception/proto/sensor_meta_schema.pb.h"
27 28 29 30 31

namespace apollo {
namespace perception {
namespace common {

32 33 34 35
using apollo::perception::base::BrownCameraDistortionModel;
using apollo::perception::base::SensorInfo;
using apollo::perception::base::SensorOrientation;
using apollo::perception::base::SensorType;
36

37
SensorManager::SensorManager() { CHECK_EQ(this->Init(), true); }
38 39 40 41 42 43 44 45 46 47 48 49 50

bool SensorManager::Init() {
  std::lock_guard<std::mutex> lock(mutex_);
  if (inited_) {
    return true;
  }

  sensor_info_map_.clear();
  distort_model_map_.clear();
  undistort_model_map_.clear();

  lib::ConfigManager* config_manager =
      lib::Singleton<lib::ConfigManager>::get_instance();
D
Dong Li 已提交
51
  CHECK_NOTNULL(config_manager);
52 53 54 55 56
  std::string file_path = lib::FileUtil::GetAbsolutePath(
      config_manager->work_root(), FLAGS_obs_sensor_meta_path);

  std::string content;
  if (!lib::FileUtil::GetFileContent(file_path, &content)) {
57
    AERROR << "Failed to get SensorManager config path: "
58
           << FLAGS_obs_sensor_meta_path;
59 60 61 62 63 64
    return false;
  }

  MultiSensorMeta sensor_list_proto;
  if (!google::protobuf::TextFormat::ParseFromString(content,
                                                     &sensor_list_proto)) {
65
    AERROR << "Invalid MultiSensorMeta file!";
66 67 68 69
    return false;
  }

  auto AddSensorInfo = [this](const SensorMeta& sensor_meta_proto) {
70
    SensorInfo sensor_info;
71
    sensor_info.name = sensor_meta_proto.name();
72
    sensor_info.type = static_cast<SensorType>(sensor_meta_proto.type());
73
    sensor_info.orientation =
74
        static_cast<SensorOrientation>(sensor_meta_proto.orientation());
75 76 77 78 79
    sensor_info.frame_id = sensor_meta_proto.name();

    auto pair = sensor_info_map_.insert(
        make_pair(sensor_meta_proto.name(), sensor_info));
    if (!pair.second) {
80
      AERROR << "Duplicate sensor name error.";
81 82 83 84
      return false;
    }

    if (this->IsCamera(sensor_info.type)) {
85 86
      std::shared_ptr<BrownCameraDistortionModel> distort_model(
          new BrownCameraDistortionModel());
87 88
      if (!LoadBrownCameraIntrinsic(IntrinsicPath(sensor_info.frame_id),
                                    distort_model.get())) {
89
        AERROR << "Failed to load camera intrinsic";
90 91
        return false;
      }
92 93 94
      distort_model_map_.insert(make_pair(
          sensor_meta_proto.name(),
          std::dynamic_pointer_cast<BaseCameraDistortionModel>(distort_model)));
95 96 97 98 99 100 101 102
      undistort_model_map_.insert(make_pair(sensor_meta_proto.name(),
                                            distort_model->get_camera_model()));
    }
    return true;
  };

  for (const SensorMeta& sensor_meta_proto : sensor_list_proto.sensor_meta()) {
    if (!AddSensorInfo(sensor_meta_proto)) {
103
      AERROR << "Failed to add sensor_info: " << sensor_meta_proto.name();
104 105 106 107 108
      return false;
    }
  }

  inited_ = true;
109
  AINFO << "Init sensor_manager success.";
110 111 112 113 114 115 116 117 118 119 120 121 122
  return true;
}

bool SensorManager::IsSensorExist(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
    return true;
  }
}

bool SensorManager::GetSensorInfo(const std::string& name,
123
                                  SensorInfo* sensor_info) const {
124
  if (sensor_info == nullptr) {
125
    AERROR << "Nullptr error.";
126 127 128 129 130 131 132 133 134 135 136 137
    return false;
  }

  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
    *sensor_info = itr->second;
  }
  return true;
}

138
std::shared_ptr<BaseCameraDistortionModel> SensorManager::GetDistortCameraModel(
139 140 141 142 143 144 145 146 147
    const std::string& name) const {
  const auto& itr = distort_model_map_.find(name);
  if (itr == distort_model_map_.end()) {
    return nullptr;
  } else {
    return itr->second;
  }
}

148
std::shared_ptr<BaseCameraModel> SensorManager::GetUndistortCameraModel(
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    const std::string& name) const {
  const auto& itr = undistort_model_map_.find(name);
  if (itr == undistort_model_map_.end()) {
    return nullptr;
  } else {
    return itr->second;
  }
}

bool SensorManager::IsHdLidar(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
163
    SensorType type = itr->second.type;
164 165 166 167
    return this->IsHdLidar(type);
  }
}

168
bool SensorManager::IsHdLidar(const SensorType& type) const {
Z
ZhaozhengPlus 已提交
169 170 171
  return type == SensorType::VELODYNE_128 ||
         type == SensorType::VELODYNE_64 ||
         type == SensorType::VELODYNE_32 ||
172
         type == SensorType::VELODYNE_16;
173 174 175 176 177 178 179
}

bool SensorManager::IsLdLidar(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
180
    SensorType type = itr->second.type;
181 182 183 184
    return this->IsLdLidar(type);
  }
}

185
bool SensorManager::IsLdLidar(const SensorType& type) const {
186
  return type == SensorType::LDLIDAR_4 || type == SensorType::LDLIDAR_1;
187 188 189 190 191 192 193
}

bool SensorManager::IsLidar(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
194
    SensorType type = itr->second.type;
195 196 197 198
    return this->IsLidar(type);
  }
}

199
bool SensorManager::IsLidar(const SensorType& type) const {
200 201 202 203 204 205 206 207
  return this->IsHdLidar(type) || this->IsLdLidar(type);
}

bool SensorManager::IsRadar(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
208
    SensorType type = itr->second.type;
209 210 211 212
    return this->IsRadar(type);
  }
}

213 214 215
bool SensorManager::IsRadar(const SensorType& type) const {
  return type == SensorType::SHORT_RANGE_RADAR ||
         type == SensorType::LONG_RANGE_RADAR;
216 217 218 219 220 221 222
}

bool SensorManager::IsCamera(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
223
    SensorType type = itr->second.type;
224 225 226 227
    return this->IsCamera(type);
  }
}

228 229 230
bool SensorManager::IsCamera(const SensorType& type) const {
  return type == SensorType::MONOCULAR_CAMERA ||
         type == SensorType::STEREO_CAMERA;
231 232 233 234 235 236 237
}

bool SensorManager::IsUltrasonic(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return false;
  } else {
238
    SensorType type = itr->second.type;
239 240 241 242
    return this->IsUltrasonic(type);
  }
}

243 244
bool SensorManager::IsUltrasonic(const SensorType& type) const {
  return type == SensorType::ULTRASONIC;
245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

std::string SensorManager::GetFrameId(const std::string& name) const {
  const auto& itr = sensor_info_map_.find(name);
  if (itr == sensor_info_map_.end()) {
    return std::string("");
  } else {
    return itr->second.frame_id;
  }
}

}  // namespace common
}  // namespace perception
}  // namespace apollo