Apollo 11.0
自动驾驶开放平台
online_visualizer_component.cc
浏览该文件的文档.
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
18
21
22#include "cyber/time/clock.h"
28
29namespace apollo {
30namespace localization {
31namespace msf {
32
34
38
40 if (!InitConfig()) {
41 AERROR << "InitParams failed.";
42 return false;
43 }
44
45 if (!InitIO()) {
46 AERROR << "InitIO failed.";
47 return false;
48 }
49
51
52 return true;
53}
54
55bool OnlineVisualizerComponent::InitConfig() {
56 map_folder_ = FLAGS_map_dir + "/" + FLAGS_local_map_name;
57 map_visual_folder_ = FLAGS_map_visual_dir;
58 lidar_extrinsic_file_ = FLAGS_lidar_extrinsics_file;
59
60 lidar_local_topic_ = FLAGS_localization_lidar_topic;
61 gnss_local_topic_ = FLAGS_localization_gnss_topic;
62 fusion_local_topic_ = FLAGS_localization_topic;
63 ndt_local_topic_ = FLAGS_localization_ndt_topic;
64
65 Eigen::Affine3d velodyne_extrinsic;
66 bool success =
67 velodyne::LoadExtrinsic(lidar_extrinsic_file_, &velodyne_extrinsic);
68 if (!success) {
69 AERROR << "Load lidar extrinsic failed." << std::endl;
70 return false;
71 }
72 std::cout << "Load lidar extrinsic succeed." << std::endl;
73
74 pyramid_map::BaseMapConfig map_config;
75 std::string config_file = map_folder_ + "/config.xml";
76 map_config.map_version_ = "lossy_map";
77 success = map_config.Load(config_file);
78 if (!success) {
79 AERROR << "Load map config failed." << std::endl;
80 return false;
81 }
82 std::cout << "Load map config succeed." << std::endl;
83
84 VisualMapParam map_param;
85 map_param.set(map_config.map_resolutions_, map_config.map_node_size_x_,
86 map_config.map_node_size_y_, map_config.map_range_.GetMinX(),
87 map_config.map_range_.GetMinY(),
88 map_config.map_range_.GetMaxX(),
89 map_config.map_range_.GetMaxY());
91 map_folder_, map_visual_folder_, velodyne_extrinsic, map_param);
92 if (!success) {
93 return false;
94 }
95 return true;
96}
97
98bool OnlineVisualizerComponent::InitIO() {
99 // Lidar localization
100 std::function<void(const std::shared_ptr<LocalizationEstimate> &)>
101 lidar_local_call =
102 std::bind(&OnlineVisualizerComponent::OnLidarLocalization, this,
103 std::placeholders::_1);
104 lidar_local_listener_ = this->node_->CreateReader<LocalizationEstimate>(
105 lidar_local_topic_, lidar_local_call);
106
107 // GNSS localization
108 std::function<void(const std::shared_ptr<LocalizationEstimate> &)>
109 gnss_local_call =
110 std::bind(&OnlineVisualizerComponent::OnGNSSLocalization, this,
111 std::placeholders::_1);
112 gnss_local_listener_ = this->node_->CreateReader<LocalizationEstimate>(
113 gnss_local_topic_, gnss_local_call);
114
115 // Fusion localization
116 std::function<void(const std::shared_ptr<LocalizationEstimate> &)>
117 fusion_local_call =
118 std::bind(&OnlineVisualizerComponent::OnFusionLocalization, this,
119 std::placeholders::_1);
120 fusion_local_listener_ = this->node_->CreateReader<LocalizationEstimate>(
121 fusion_local_topic_, fusion_local_call);
122
123 // Ndt Localization
124 std::function<void(const std::shared_ptr<LocalizationEstimate> &)>
125 ndt_local_call =
126 std::bind(&OnlineVisualizerComponent::OnLidarLocalization, this,
127 std::placeholders::_1);
128 lidar_local_listener_ = this->node_->CreateReader<LocalizationEstimate>(
129 ndt_local_topic_, ndt_local_call);
130
131 return true;
132}
133
135 const std::shared_ptr<drivers::PointCloud> &msg) {
136 LidarVisFrame lidar_vis_frame;
137 lidar_vis_frame.timestamp = cyber::Time(msg->measurement_time()).ToSecond();
138
139 std::vector<unsigned char> intensities;
140 ParsePointCloudMessage(msg, &lidar_vis_frame.pt3ds, &intensities);
141
142 static unsigned int id = 1;
143 lidar_vis_frame.frame_id = id;
145
146 id++;
147 return true;
148}
149
150void OnlineVisualizerComponent::OnLidarLocalization(
151 const std::shared_ptr<LocalizationEstimate> &msg) {
152 LocalizationMsg lidar_loc_msg;
153
154 lidar_loc_msg.timestamp = msg->measurement_time();
155 lidar_loc_msg.x = msg->pose().position().x();
156 lidar_loc_msg.y = msg->pose().position().y();
157 lidar_loc_msg.z = msg->pose().position().z();
158
159 lidar_loc_msg.qx = msg->pose().orientation().qx();
160 lidar_loc_msg.qy = msg->pose().orientation().qy();
161 lidar_loc_msg.qz = msg->pose().orientation().qz();
162 lidar_loc_msg.qw = msg->pose().orientation().qw();
163
164 if (msg->has_uncertainty() &&
165 !std::isnan(msg->uncertainty().position_std_dev().x()) &&
166 !std::isnan(msg->uncertainty().position_std_dev().y()) &&
167 msg->uncertainty().position_std_dev().x() > 0 &&
168 msg->uncertainty().position_std_dev().y() > 0) {
169 lidar_loc_msg.std_x = msg->uncertainty().position_std_dev().x();
170 lidar_loc_msg.std_y = msg->uncertainty().position_std_dev().y();
171 lidar_loc_msg.std_z = msg->uncertainty().position_std_dev().z();
172 }
173
175}
176
177void OnlineVisualizerComponent::OnGNSSLocalization(
178 const std::shared_ptr<LocalizationEstimate> &msg) {
179 LocalizationMsg gnss_loc_msg;
180
181 gnss_loc_msg.timestamp = msg->measurement_time();
182 gnss_loc_msg.x = msg->pose().position().x();
183 gnss_loc_msg.y = msg->pose().position().y();
184 gnss_loc_msg.z = msg->pose().position().z();
185
186 gnss_loc_msg.qx = msg->pose().orientation().qx();
187 gnss_loc_msg.qy = msg->pose().orientation().qy();
188 gnss_loc_msg.qz = msg->pose().orientation().qz();
189 gnss_loc_msg.qw = msg->pose().orientation().qw();
190
191 if (msg->has_uncertainty() &&
192 !std::isnan(msg->uncertainty().position_std_dev().x()) &&
193 !std::isnan(msg->uncertainty().position_std_dev().y()) &&
194 msg->uncertainty().position_std_dev().x() > 0 &&
195 msg->uncertainty().position_std_dev().y() > 0) {
196 gnss_loc_msg.std_x = msg->uncertainty().position_std_dev().x();
197 gnss_loc_msg.std_y = msg->uncertainty().position_std_dev().y();
198 gnss_loc_msg.std_z = msg->uncertainty().position_std_dev().z();
199 }
200
202}
203
204void OnlineVisualizerComponent::OnFusionLocalization(
205 const std::shared_ptr<LocalizationEstimate> &msg) {
206 LocalizationMsg fusion_loc_msg;
207
208 fusion_loc_msg.timestamp = msg->measurement_time();
209 fusion_loc_msg.x = msg->pose().position().x();
210 fusion_loc_msg.y = msg->pose().position().y();
211 fusion_loc_msg.z = msg->pose().position().z();
212
213 fusion_loc_msg.qx = msg->pose().orientation().qx();
214 fusion_loc_msg.qy = msg->pose().orientation().qy();
215 fusion_loc_msg.qz = msg->pose().orientation().qz();
216 fusion_loc_msg.qw = msg->pose().orientation().qw();
217
218 if (msg->has_uncertainty() &&
219 !std::isnan(msg->uncertainty().position_std_dev().x()) &&
220 !std::isnan(msg->uncertainty().position_std_dev().y()) &&
221 msg->uncertainty().position_std_dev().x() > 0 &&
222 msg->uncertainty().position_std_dev().y() > 0) {
223 fusion_loc_msg.std_x = msg->uncertainty().position_std_dev().x();
224 fusion_loc_msg.std_y = msg->uncertainty().position_std_dev().y();
225 fusion_loc_msg.std_z = msg->uncertainty().position_std_dev().z();
226 }
227
229}
230
231void OnlineVisualizerComponent::ParsePointCloudMessage(
232 const std::shared_ptr<drivers::PointCloud> &msg,
234 std::vector<unsigned char> *intensities) {
235 CHECK_NOTNULL(pt3ds);
236 CHECK_NOTNULL(intensities);
237
238 if (msg->height() > 1 && msg->width() > 1) {
239 for (unsigned int i = 0; i < msg->height(); ++i) {
240 for (unsigned int j = 0; j < msg->width(); ++j) {
241 Eigen::Vector3d pt3d;
242 pt3d[0] = static_cast<double>(msg->point(i * msg->width() + j).x());
243 pt3d[1] = static_cast<double>(msg->point(i * msg->width() + j).y());
244 pt3d[2] = static_cast<double>(msg->point(i * msg->width() + j).z());
245 if (!std::isnan(pt3d[0])) {
246 unsigned char intensity = static_cast<unsigned char>(
247 msg->point(i * msg->width() + j).intensity());
248 pt3ds->push_back(pt3d);
249 intensities->push_back(intensity);
250 }
251 }
252 }
253 } else {
254 AINFO << "Receiving un-organized-point-cloud, width " << msg->width()
255 << " height " << msg->height() << "size " << msg->point_size();
256 for (int i = 0; i < msg->point_size(); ++i) {
257 Eigen::Vector3d pt3d;
258 pt3d[0] = static_cast<double>(msg->point(i).x());
259 pt3d[1] = static_cast<double>(msg->point(i).y());
260 pt3d[2] = static_cast<double>(msg->point(i).z());
261 if (!std::isnan(pt3d[0])) {
262 unsigned char intensity =
263 static_cast<unsigned char>(msg->point(i).intensity());
264 pt3ds->push_back(pt3d);
265 intensities->push_back(intensity);
266 }
267 }
268 }
269}
270
271} // namespace msf
272} // namespace localization
273} // namespace apollo
std::shared_ptr< Node > node_
Cyber has builtin time type Time.
Definition time.h:31
double ToSecond() const
convert time to second.
Definition time.cc:77
bool Proc(const std::shared_ptr< drivers::PointCloud > &msg) override
bool Init() override
module initialization function
void AddLidarLocMessage(const LocalizationMsg &lidar_loc_msg)
void AddFusionLocMessage(const LocalizationMsg &fusion_loc_msg)
void AddLidarFrame(const LidarVisFrame &lidar_frame)
bool Init(const std::string &map_folder, const std::string &map_visual_folder, const Eigen::Affine3d &velodyne_extrinsic, const VisualMapParam &map_param)
void AddGNSSLocMessage(const LocalizationMsg &gnss_loc_msg)
The gflags used by localization module
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
EigenVector< Eigen::Vector3d > EigenVector3dVec
Definition eigen_defs.h:46
bool LoadExtrinsic(const std::string &file_path, Eigen::Affine3d *extrinsic)
Load the velodyne extrinsic from a YAML file.
class register implement
Definition arena_queue.h:37
The pcl types.
Contains a number of helper functions related to quaternions.
::apollo::common::EigenVector3dVec pt3ds
The 3D point cloud in this frame.
The utilities of velodyne.