Apollo 10.0
自动驾驶开放平台
ndt_localization_component.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 *****************************************************************************/
16
18
19#include "cyber/time/clock.h"
23
24namespace apollo {
25namespace localization {
26namespace ndt {
27
29
32
34 tf2_broadcaster_.reset(new apollo::transform::TransformBroadcaster(node_));
35 if (!InitConfig()) {
36 AERROR << "Init Config false.";
37 return false;
38 }
39
40 if (!InitIO()) {
41 AERROR << "Init Interval false.";
42 return false;
43 }
44
45 return true;
46}
47
48bool NDTLocalizationComponent::InitConfig() {
49 localization_topic_ = FLAGS_localization_topic;
50 lidar_topic_ = FLAGS_lidar_topic;
51 lidar_pose_topic_ = FLAGS_localization_ndt_topic;
52 broadcast_tf_frame_id_ = FLAGS_broadcast_tf_frame_id;
53 broadcast_tf_child_frame_id_ = FLAGS_broadcast_tf_child_frame_id;
54 odometry_status_topic_ = FLAGS_ins_stat_topic;
55 localization_status_topic_ = FLAGS_localization_msf_status;
56
57 localization_->Init();
58
59 return true;
60}
61
62bool NDTLocalizationComponent::InitIO() {
63 cyber::ReaderConfig reader_config;
64 reader_config.channel_name = lidar_topic_;
65 reader_config.pending_queue_size = 1;
66
67 std::function<void(const std::shared_ptr<drivers::PointCloud>&)>
68 lidar_register_call = std::bind(&NDTLocalizationComponent::LidarCallback,
69 this, std::placeholders::_1);
70 lidar_listener_ = this->node_->CreateReader<drivers::PointCloud>(
71 reader_config, lidar_register_call);
72
73 reader_config.channel_name = odometry_status_topic_;
74 reader_config.pending_queue_size = 1;
75 std::function<void(const std::shared_ptr<drivers::gnss::InsStat>&)>
76 odometry_status_call =
77 std::bind(&NDTLocalizationComponent::OdometryStatusCallback, this,
78 std::placeholders::_1);
79 odometry_status_listener_ = this->node_->CreateReader<drivers::gnss::InsStat>(
80 reader_config, odometry_status_call);
81
82 localization_talker_ =
83 this->node_->CreateWriter<LocalizationEstimate>(localization_topic_);
84
85 lidar_pose_talker_ =
86 this->node_->CreateWriter<LocalizationEstimate>(lidar_pose_topic_);
87
88 localization_status_talker_ =
89 this->node_->CreateWriter<LocalizationStatus>(localization_status_topic_);
90
91 return true;
92}
93
95 const std::shared_ptr<localization::Gps>& gps_msg) {
96 localization_->OdometryCallback(gps_msg);
97
98 if (localization_->IsServiceStarted()) {
99 LocalizationEstimate localization;
100 localization_->GetLocalization(&localization);
101
102 LocalizationStatus localization_status;
103 localization_->GetLocalizationStatus(&localization_status);
104
105 // publish localization messages
106 PublishPoseBroadcastTopic(localization);
107 PublishPoseBroadcastTF(localization);
108 PublishLocalizationStatusTopic(localization_status);
109 ADEBUG << "[OnTimer]: Localization message publish success!";
110 }
111
112 return true;
113}
114
115void NDTLocalizationComponent::LidarCallback(
116 const std::shared_ptr<drivers::PointCloud>& lidar_msg) {
117 localization_->LidarCallback(lidar_msg);
118 // for test to output lidar pose
119 if (localization_->IsServiceStarted()) {
120 LocalizationEstimate localization;
121 localization_->GetLidarLocalization(&localization);
122 // publish localization messages
123 PublishLidarPoseBroadcastTopic(localization);
124 }
125}
126
127void NDTLocalizationComponent::OdometryStatusCallback(
128 const std::shared_ptr<drivers::gnss::InsStat>& status_msg) {
129 localization_->OdometryStatusCallback(status_msg);
130}
131
132void NDTLocalizationComponent::PublishPoseBroadcastTF(
133 const LocalizationEstimate& localization) {
134 // broadcast tf message
136
137 auto mutable_head = tf2_msg.mutable_header();
138 mutable_head->set_timestamp_sec(localization.measurement_time());
139 mutable_head->set_frame_id(broadcast_tf_frame_id_);
140 tf2_msg.set_child_frame_id(broadcast_tf_child_frame_id_);
141
142 auto mutable_translation = tf2_msg.mutable_transform()->mutable_translation();
143 mutable_translation->set_x(localization.pose().position().x());
144 mutable_translation->set_y(localization.pose().position().y());
145 mutable_translation->set_z(localization.pose().position().z());
146
147 auto mutable_rotation = tf2_msg.mutable_transform()->mutable_rotation();
148 mutable_rotation->set_qx(localization.pose().orientation().qx());
149 mutable_rotation->set_qy(localization.pose().orientation().qy());
150 mutable_rotation->set_qz(localization.pose().orientation().qz());
151 mutable_rotation->set_qw(localization.pose().orientation().qw());
152
153 tf2_broadcaster_->SendTransform(tf2_msg);
154}
155
156void NDTLocalizationComponent::PublishPoseBroadcastTopic(
157 const LocalizationEstimate& localization) {
158 localization_talker_->Write(localization);
159}
160
161void NDTLocalizationComponent::PublishLidarPoseBroadcastTopic(
162 const LocalizationEstimate& localization) {
163 lidar_pose_talker_->Write(localization);
164}
165
166void NDTLocalizationComponent::PublishLocalizationStatusTopic(
167 const LocalizationStatus& localization_status) {
168 localization_status_talker_->Write(localization_status);
169}
170
171} // namespace ndt
172} // namespace localization
173} // namespace apollo
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
std::shared_ptr< Node > node_
bool Proc(const std::shared_ptr< localization::Gps > &odometry_msg) override
This class provides an easy way to publish coordinate frame transform information.
The gflags used by localization module
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
Contains a number of helper functions related to quaternions.
uint32_t pending_queue_size
configuration for responding ChannelBuffer.