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
/******************************************************************************
 * 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"

18
#include <google/protobuf/text_format.h>
19 20

#include <string>
21 22 23
#include <utility>

#include "cybertron/common/log.h"
24

25
// #include "modules/perception/common/io/io_util.h"
26 27
#include "modules/perception/lib/config_manager/config_manager.h"
#include "modules/perception/lib/io/file_util.h"
28
#include "modules/perception/proto/sensor_meta_schema.pb.h"
29 30 31 32 33

namespace apollo {
namespace perception {
namespace common {

34 35 36 37
using apollo::perception::base::BrownCameraDistortionModel;
using apollo::perception::base::SensorInfo;
using apollo::perception::base::SensorOrientation;
using apollo::perception::base::SensorType;
38

39 40 41
SensorManager::SensorManager() {
  CHECK_EQ(this->Init(), true);
}
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

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();
  assert(config_manager != nullptr);
  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)) {
61
    AERROR << "Failed to get SensorManager config path: "
62 63 64 65 66 67 68
              << FLAGS_obs_sensor_meta_path;
    return false;
  }

  MultiSensorMeta sensor_list_proto;
  if (!google::protobuf::TextFormat::ParseFromString(content,
                                                     &sensor_list_proto)) {
69
    AERROR << "Invalid MultiSensorMeta file!";
70 71 72 73
    return false;
  }

  auto AddSensorInfo = [this](const SensorMeta& sensor_meta_proto) {
74
    SensorInfo sensor_info;
75
    sensor_info.name = sensor_meta_proto.name();
76
    sensor_info.type = static_cast<SensorType>(sensor_meta_proto.type());
77
    sensor_info.orientation =
78
        static_cast<SensorOrientation>(sensor_meta_proto.orientation());
79 80 81 82 83
    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) {
84
      AERROR << "Duplicate sensor name error.";
85 86 87 88
      return false;
    }

    if (this->IsCamera(sensor_info.type)) {
89 90
      std::shared_ptr<BrownCameraDistortionModel> distort_model(
          new BrownCameraDistortionModel());
91
      /* TODO(jmtao): to add back ASAP once CTE is done
92 93
      if (!LoadBrownCameraIntrinsic(IntrinsicPath(sensor_info.frame_id),
                                    distort_model.get())) {
94
        AERROR << "Failed to load camera intrinsic";
95 96
        return false;
      }
97
      */
98 99
      distort_model_map_.insert(
          make_pair(sensor_meta_proto.name(),
100
                    std::dynamic_pointer_cast<BaseCameraDistortionModel>(
101 102 103 104 105 106 107 108 109
                        distort_model)));
      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)) {
110
      AERROR << "Failed to add sensor_info: " << sensor_meta_proto.name();
111 112 113 114 115
      return false;
    }
  }

  inited_ = true;
116
  AINFO << "Init sensor_manager success.";
117 118 119 120 121 122 123 124 125 126 127 128 129
  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,
130
                                  SensorInfo* sensor_info) const {
131
  if (sensor_info == nullptr) {
132
    AERROR << "Nullptr error.";
133 134 135 136 137 138 139 140 141 142 143 144
    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;
}

145
std::shared_ptr<BaseCameraDistortionModel> SensorManager::GetDistortCameraModel(
146 147 148 149 150 151 152 153 154
    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;
  }
}

155
std::shared_ptr<BaseCameraModel> SensorManager::GetUndistortCameraModel(
156 157 158 159 160 161 162 163 164 165 166 167 168 169
    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 {
170
    SensorType type = itr->second.type;
171 172 173 174
    return this->IsHdLidar(type);
  }
}

175 176 177 178
bool SensorManager::IsHdLidar(const SensorType& type) const {
  return type == SensorType::VELODYNE_64 ||
         type == SensorType::VELODYNE_32 ||
         type == SensorType::VELODYNE_16;
179 180 181 182 183 184 185
}

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 {
186
    SensorType type = itr->second.type;
187 188 189 190
    return this->IsLdLidar(type);
  }
}

191 192 193
bool SensorManager::IsLdLidar(const SensorType& type) const {
  return type == SensorType::LDLIDAR_4 ||
         type == SensorType::LDLIDAR_1;
194 195 196 197 198 199 200
}

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 {
201
    SensorType type = itr->second.type;
202 203 204 205
    return this->IsLidar(type);
  }
}

206
bool SensorManager::IsLidar(const SensorType& type) const {
207 208 209 210 211 212 213 214
  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 {
215
    SensorType type = itr->second.type;
216 217 218 219
    return this->IsRadar(type);
  }
}

220 221 222
bool SensorManager::IsRadar(const SensorType& type) const {
  return type == SensorType::SHORT_RANGE_RADAR ||
         type == SensorType::LONG_RANGE_RADAR;
223 224 225 226 227 228 229
}

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 {
230
    SensorType type = itr->second.type;
231 232 233 234
    return this->IsCamera(type);
  }
}

235 236 237
bool SensorManager::IsCamera(const SensorType& type) const {
  return type == SensorType::MONOCULAR_CAMERA ||
         type == SensorType::STEREO_CAMERA;
238 239 240 241 242 243 244
}

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 {
245
    SensorType type = itr->second.type;
246 247 248 249
    return this->IsUltrasonic(type);
  }
}

250 251
bool SensorManager::IsUltrasonic(const SensorType& type) const {
  return type == SensorType::ULTRASONIC;
252 253 254 255 256 257 258 259 260 261 262 263 264 265
}

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