Apollo 10.0
自动驾驶开放平台
msf_localization_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
19#include "cyber/time/clock.h"
21
24
25namespace apollo {
26namespace localization {
27
29
31
33 publisher_.reset(new LocalizationMsgPublisher(this->node_));
34
35 if (!InitConfig()) {
36 AERROR << "Init Config failed.";
37 return false;
38 }
39
40 if (!InitIO()) {
41 AERROR << "Init IO failed.";
42 return false;
43 }
44
45 return true;
46}
47
48bool MSFLocalizationComponent::InitConfig() {
49 lidar_topic_ = FLAGS_lidar_topic;
50 bestgnsspos_topic_ = FLAGS_gnss_best_pose_topic;
51 gnss_heading_topic_ = FLAGS_heading_topic;
52
53 if (!publisher_->InitConfig()) {
54 AERROR << "Init publisher config failed.";
55 return false;
56 }
57
58 if (!localization_.Init().ok()) {
59 AERROR << "Init class MSFLocalization failed.";
60 return false;
61 }
62
63 return true;
64}
65
66bool MSFLocalizationComponent::InitIO() {
67 cyber::ReaderConfig reader_config;
68 reader_config.channel_name = lidar_topic_;
69 reader_config.pending_queue_size = 1;
70
71 std::function<void(const std::shared_ptr<drivers::PointCloud>&)>
72 lidar_register_call = std::bind(&MSFLocalization::OnPointCloud,
73 &localization_, std::placeholders::_1);
74
75 lidar_listener_ = this->node_->CreateReader<drivers::PointCloud>(
76 reader_config, lidar_register_call);
77
78 std::function<void(const std::shared_ptr<drivers::gnss::GnssBestPose>&)>
79 bestgnsspos_register_call =
80 std::bind(&MSFLocalization::OnGnssBestPose, &localization_,
81 std::placeholders::_1);
82 bestgnsspos_listener_ =
83 this->node_->CreateReader<drivers::gnss::GnssBestPose>(
84 bestgnsspos_topic_, bestgnsspos_register_call);
85
86 std::function<void(const std::shared_ptr<drivers::gnss::Heading>&)>
87 gnss_heading_call = std::bind(&MSFLocalization::OnGnssHeading,
88 &localization_, std::placeholders::_1);
89 gnss_heading_listener_ = this->node_->CreateReader<drivers::gnss::Heading>(
90 gnss_heading_topic_, gnss_heading_call);
91
92 // init writer
93 if (!publisher_->InitIO()) {
94 AERROR << "Init publisher io failed.";
95 return false;
96 }
97
98 localization_.SetPublisher(publisher_);
99
100 return true;
101}
102
104 const std::shared_ptr<drivers::gnss::Imu>& imu_msg) {
105 localization_.OnRawImuCache(imu_msg);
106 return true;
107}
108
110 const std::shared_ptr<cyber::Node>& node)
111 : node_(node), localization_gnss_compensator_(
112 LocalizationGnssCompensator::Instance()),
113 tf2_broadcaster_(node) {}
114
116 localization_topic_ = FLAGS_localization_topic;
117 broadcast_tf_frame_id_ = FLAGS_broadcast_tf_frame_id;
118 broadcast_tf_child_frame_id_ = FLAGS_broadcast_tf_child_frame_id;
119 lidar_local_topic_ = FLAGS_localization_lidar_topic;
120 gnss_local_topic_ = FLAGS_localization_gnss_topic;
121 localization_status_topic_ = FLAGS_localization_msf_status;
122
123 return true;
124}
125
127 localization_talker_ =
128 node_->CreateWriter<LocalizationEstimate>(localization_topic_);
129
130 lidar_local_talker_ =
131 node_->CreateWriter<LocalizationEstimate>(lidar_local_topic_);
132
133 gnss_local_talker_ =
134 node_->CreateWriter<LocalizationEstimate>(gnss_local_topic_);
135
136 localization_status_talker_ =
137 node_->CreateWriter<LocalizationStatus>(localization_status_topic_);
138 return true;
139}
140
142 const LocalizationEstimate& localization) {
143 // broadcast tf message
145 // uint64_t compensated_delta;
146 uint64_t current_send_tf_time = apollo::cyber::Clock::Now().ToNanosecond();
147 uint64_t measurement_time =
149
150 auto mutable_head = tf2_msg.mutable_header();
151
152 localization_gnss_compensator_ = LocalizationGnssCompensator::Instance();
153
154 localization_gnss_compensator_->ProcessCompensation(
155 current_send_tf_time, &measurement_time);
156
157 mutable_head->set_timestamp_sec(
158 apollo::cyber::Time(measurement_time).ToSecond());
159 mutable_head->set_frame_id(broadcast_tf_frame_id_);
160 tf2_msg.set_child_frame_id(broadcast_tf_child_frame_id_);
161
162 auto mutable_translation = tf2_msg.mutable_transform()->mutable_translation();
163 mutable_translation->set_x(localization.pose().position().x());
164 mutable_translation->set_y(localization.pose().position().y());
165 mutable_translation->set_z(localization.pose().position().z());
166
167 auto mutable_rotation = tf2_msg.mutable_transform()->mutable_rotation();
168 mutable_rotation->set_qx(localization.pose().orientation().qx());
169 mutable_rotation->set_qy(localization.pose().orientation().qy());
170 mutable_rotation->set_qz(localization.pose().orientation().qz());
171 mutable_rotation->set_qw(localization.pose().orientation().qw());
172
173 tf2_broadcaster_.SendTransform(tf2_msg);
174}
175
177 const LocalizationEstimate& localization) {
178 double cur_system_time = localization.header().timestamp_sec();
179 if (pre_system_time_ > 0.0 && cur_system_time - pre_system_time_ > 0.02) {
180 AERROR << std::setprecision(16)
181 << "the localization processing time enlonged more than 2 times "
182 "according to system time, "
183 << "the pre system time and current system time: "
184 << pre_system_time_ << " " << cur_system_time;
185 } else if (pre_system_time_ > 0.0 &&
186 cur_system_time - pre_system_time_ < 0.0) {
187 AERROR << std::setprecision(16)
188 << "published localization message's time is eary than last imu "
189 "message "
190 "according to system time, "
191 << "the pre system time and current system time: "
192 << pre_system_time_ << " " << cur_system_time;
193 }
194 pre_system_time_ = cur_system_time;
195 localization_talker_->Write(localization);
196}
197
199 const LocalizationEstimate& localization) {
200 gnss_local_talker_->Write(localization);
201}
202
204 const LocalizationEstimate& localization) {
205 lidar_local_talker_->Write(localization);
206}
207
209 const LocalizationStatus& localization_status) {
210 localization_status_talker_->Write(localization_status);
211}
212
213} // namespace localization
214} // namespace apollo
bool ok() const
check whether the return status is OK.
Definition status.h:67
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static Time Now()
PRECISION >= 1000000 means the precision is at least 1us.
Definition clock.cc:40
std::shared_ptr< Node > node_
Cyber has builtin time type Time.
Definition time.h:31
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
void ProcessCompensation(const uint64_t &current_send_tf_time, uint64_t *measurement_time)
compensate the time parsed from invalid GNSS data
void PublishLocalizationMsfGnss(const LocalizationEstimate &localization)
void PublishPoseBroadcastTopic(const LocalizationEstimate &localization)
void PublishLocalizationMsfLidar(const LocalizationEstimate &localization)
void PublishLocalizationStatus(const LocalizationStatus &localization_status)
LocalizationMsgPublisher(const std::shared_ptr< cyber::Node > &node)
void PublishPoseBroadcastTF(const LocalizationEstimate &localization)
bool Proc(const std::shared_ptr< drivers::gnss::Imu > &imu_msg) override
void SetPublisher(const std::shared_ptr< LocalizationMsgPublisher > &publisher)
void OnPointCloud(const std::shared_ptr< drivers::PointCloud > &message)
void OnGnssBestPose(const std::shared_ptr< drivers::gnss::GnssBestPose > &bestgnsspos_msg)
void OnRawImuCache(const std::shared_ptr< drivers::gnss::Imu > &imu_msg)
void OnGnssHeading(const std::shared_ptr< drivers::gnss::Heading > &gnss_heading_msg)
void SendTransform(const TransformStamped &transform)
Send a TransformStamped message The stamped data structure includes frame_id, and time,...
The gflags used by localization module
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
Contains a number of helper functions related to quaternions.
optional double timestamp_sec
Definition header.proto:9
optional apollo::localization::Pose pose
optional apollo::common::Header header
optional apollo::common::Quaternion orientation
Definition pose.proto:31
optional apollo::common::PointENU position
Definition pose.proto:26