Apollo 10.0
自动驾驶开放平台
sensor_manager.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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 *****************************************************************************/
17
18#include <utility>
19
20#include "modules/perception/common/proto/sensor_meta_schema.pb.h"
21
22#include "cyber/common/file.h"
23#include "cyber/common/log.h"
26
27namespace apollo {
28namespace perception {
29namespace algorithm {
30
36
37SensorManager::SensorManager() { CHECK_EQ(this->Init(), true); }
38
40 std::lock_guard<std::mutex> lock(mutex_);
41 if (inited_) {
42 return true;
43 }
44
45 sensor_info_map_.clear();
46 distort_model_map_.clear();
47 undistort_model_map_.clear();
48
49 std::string config_file = GetCommonConfigFile(FLAGS_obs_sensor_meta_file);
50
51 MultiSensorMeta sensor_list_proto;
52 if (!GetProtoFromASCIIFile(config_file, &sensor_list_proto)) {
53 AERROR << "Invalid MultiSensorMeta file: " << config_file;
54 return false;
55 }
56
57 auto AddSensorInfo = [this](const SensorMeta& sensor_meta_proto) {
58 SensorInfo sensor_info;
59 sensor_info.name = sensor_meta_proto.name();
60 sensor_info.type = static_cast<SensorType>(sensor_meta_proto.type());
61 sensor_info.orientation =
62 static_cast<SensorOrientation>(sensor_meta_proto.orientation());
63 sensor_info.frame_id = sensor_meta_proto.name();
64 sensor_info.is_main_sensor = sensor_meta_proto.is_main_sensor();
65
66 auto pair = sensor_info_map_.insert(
67 make_pair(sensor_meta_proto.name(), sensor_info));
68 if (!pair.second) {
69 AERROR << "Duplicate sensor name error.";
70 return false;
71 }
72
73 if (this->IsCamera(sensor_info.type)) {
74 std::shared_ptr<BrownCameraDistortionModel> distort_model(
76 // Todo(zero): Need change to GetCommonConfigFile
77 auto intrinsic_file = IntrinsicPath(sensor_info.frame_id);
78 if (!LoadBrownCameraIntrinsic(intrinsic_file, distort_model.get())) {
79 AERROR << "Failed to load camera intrinsic:" << intrinsic_file;
80 return false;
81 }
82 distort_model_map_.insert(make_pair(
83 sensor_meta_proto.name(),
84 std::dynamic_pointer_cast<BaseCameraDistortionModel>(distort_model)));
85 undistort_model_map_.insert(make_pair(sensor_meta_proto.name(),
86 distort_model->get_camera_model()));
87 }
88 return true;
89 };
90
91 for (const SensorMeta& sensor_meta_proto : sensor_list_proto.sensor_meta()) {
92 if (!AddSensorInfo(sensor_meta_proto)) {
93 AERROR << "Failed to add sensor_info: " << sensor_meta_proto.name();
94 return false;
95 }
96 }
97
98 inited_ = true;
99 AINFO << "Init sensor_manager success.";
100 return true;
101}
102
103bool SensorManager::IsSensorExist(const std::string& name) const {
104 return sensor_info_map_.find(name) != sensor_info_map_.end();
105}
106
107bool SensorManager::GetSensorInfo(const std::string& name,
108 SensorInfo* sensor_info) const {
109 if (sensor_info == nullptr) {
110 AERROR << "Nullptr error.";
111 return false;
112 }
113
114 const auto& itr = sensor_info_map_.find(name);
115 if (itr == sensor_info_map_.end()) {
116 return false;
117 }
118
119 *sensor_info = itr->second;
120 return true;
121}
122
123std::shared_ptr<BaseCameraDistortionModel> SensorManager::GetDistortCameraModel(
124 const std::string& name) const {
125 const auto& itr = distort_model_map_.find(name);
126
127 return itr == distort_model_map_.end() ? nullptr : itr->second;
128}
129
130std::shared_ptr<BaseCameraModel> SensorManager::GetUndistortCameraModel(
131 const std::string& name) const {
132 const auto& itr = undistort_model_map_.find(name);
133
134 return itr == undistort_model_map_.end() ? nullptr : itr->second;
135}
136
137bool SensorManager::IsHdLidar(const std::string& name) const {
138 const auto& itr = sensor_info_map_.find(name);
139 if (itr == sensor_info_map_.end()) {
140 return false;
141 }
142
143 SensorType type = itr->second.type;
144 return this->IsHdLidar(type);
145}
146
147bool SensorManager::IsHdLidar(const SensorType& type) const {
148 return type == SensorType::VELODYNE_128 || type == SensorType::VELODYNE_64 ||
149 type == SensorType::VELODYNE_32 || type == SensorType::VELODYNE_16;
150}
151
152bool SensorManager::IsLdLidar(const std::string& name) const {
153 const auto& itr = sensor_info_map_.find(name);
154 if (itr == sensor_info_map_.end()) {
155 return false;
156 }
157
158 SensorType type = itr->second.type;
159 return this->IsLdLidar(type);
160}
161
162bool SensorManager::IsLdLidar(const SensorType& type) const {
163 return type == SensorType::LDLIDAR_4 || type == SensorType::LDLIDAR_1;
164}
165
166bool SensorManager::IsLidar(const std::string& name) const {
167 const auto& itr = sensor_info_map_.find(name);
168 if (itr == sensor_info_map_.end()) {
169 return false;
170 }
171
172 SensorType type = itr->second.type;
173 return this->IsLidar(type);
174}
175
176bool SensorManager::IsLidar(const SensorType& type) const {
177 return this->IsHdLidar(type) || this->IsLdLidar(type);
178}
179
180bool SensorManager::IsRadar(const std::string& name) const {
181 const auto& itr = sensor_info_map_.find(name);
182 if (itr == sensor_info_map_.end()) {
183 return false;
184 }
185
186 SensorType type = itr->second.type;
187 return this->IsRadar(type);
188}
189
190bool SensorManager::IsRadar(const SensorType& type) const {
191 return type == SensorType::SHORT_RANGE_RADAR ||
192 type == SensorType::LONG_RANGE_RADAR;
193}
194
195bool SensorManager::IsCamera(const std::string& name) const {
196 const auto& itr = sensor_info_map_.find(name);
197 if (itr == sensor_info_map_.end()) {
198 return false;
199 }
200
201 SensorType type = itr->second.type;
202 return this->IsCamera(type);
203}
204
205bool SensorManager::IsCamera(const SensorType& type) const {
206 return type == SensorType::MONOCULAR_CAMERA ||
207 type == SensorType::STEREO_CAMERA;
208}
209
210bool SensorManager::IsUltrasonic(const std::string& name) const {
211 const auto& itr = sensor_info_map_.find(name);
212 if (itr == sensor_info_map_.end()) {
213 return false;
214 }
215
216 SensorType type = itr->second.type;
217 return this->IsUltrasonic(type);
218}
219
220bool SensorManager::IsUltrasonic(const SensorType& type) const {
221 return type == SensorType::ULTRASONIC;
222}
223
224bool SensorManager::IsMainSensor(const std::string& name) const {
225 const auto& itr = sensor_info_map_.find(name);
226 if (itr == sensor_info_map_.end()) {
227 return false;
228 }
229
230 return itr->second.is_main_sensor;
231}
232
233std::string SensorManager::GetFrameId(const std::string& name) const {
234 const auto& itr = sensor_info_map_.find(name);
235 return itr == sensor_info_map_.end() ? std::string("") : itr->second.frame_id;
236}
237
238} // namespace algorithm
239} // namespace perception
240} // namespace apollo
bool IsRadar(const std::string &name) const
bool IsCamera(const std::string &name) const
bool GetSensorInfo(const std::string &name, apollo::perception::base::SensorInfo *sensor_info) const
bool IsSensorExist(const std::string &name) const
std::shared_ptr< BaseCameraModel > GetUndistortCameraModel(const std::string &name) const
bool IsUltrasonic(const std::string &name) const
std::string GetFrameId(const std::string &name) const
std::shared_ptr< BaseCameraDistortionModel > GetDistortCameraModel(const std::string &name) const
bool IsLdLidar(const std::string &name) const
bool IsHdLidar(const std::string &name) const
bool IsMainSensor(const std::string &name) const
bool IsLidar(const std::string &name) const
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool GetProtoFromASCIIFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as ascii representation of protobufs,...
Definition file.cc:89
bool LoadBrownCameraIntrinsic(const std::string &yaml_file, base::BrownCameraDistortionModel *model)
Definition io_util.cc:59
SensorType
Sensor types are set in the order of lidar, radar, camera, ultrasonic Please make sure SensorType has...
Definition sensor_meta.h:29
std::string GetCommonConfigFile(const std::string &config_file)
Get the perception common config path
Definition util.cc:28
class register implement
Definition arena_queue.h:37