Apollo 10.0
自动驾驶开放平台
rtk_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 "modules/common_msgs/transform_msgs/transform.pb.h"
20
21#include "cyber/time/clock.h"
22#include "cyber/time/duration.h"
27
28namespace apollo {
29namespace localization {
30
33
35 tf2_broadcaster_.reset(new apollo::transform::TransformBroadcaster(node_));
36 if (!InitConfig()) {
37 AERROR << "Init Config falseed.";
38 return false;
39 }
40
41 if (!InitIO()) {
42 AERROR << "Init Interval falseed.";
43 return false;
44 }
45
46 // get imu to localizaiton transform
47 if (!GetLocalizationToImuTF()) {
48 AERROR << "Get IMU to Localization tranform failed.";
49 return false;
50 }
51
52 return true;
53}
54
55bool RTKLocalizationComponent::InitConfig() {
56 rtk_config::Config rtk_config;
58 &rtk_config)) {
59 return false;
60 }
61 AINFO << "Rtk localization config: " << rtk_config.DebugString();
62
63 localization_topic_ = rtk_config.localization_topic();
64 localization_status_topic_ = rtk_config.localization_status_topic();
65 imu_topic_ = rtk_config.imu_topic();
66 gps_topic_ = rtk_config.gps_topic();
67 gps_status_topic_ = rtk_config.gps_status_topic();
68 broadcast_tf_frame_id_ = rtk_config.broadcast_tf_frame_id();
69 broadcast_tf_child_frame_id_ = rtk_config.broadcast_tf_child_frame_id();
70 imu_frame_id_ = rtk_config.imu_frame_id();
71
72 localization_->InitConfig(rtk_config);
73
74 return true;
75}
76
77bool RTKLocalizationComponent::InitIO() {
78 corrected_imu_listener_ = node_->CreateReader<localization::CorrectedImu>(
79 imu_topic_, std::bind(&RTKLocalization::ImuCallback, localization_.get(),
80 std::placeholders::_1));
81 ACHECK(corrected_imu_listener_);
82
83 gps_status_listener_ = node_->CreateReader<drivers::gnss::InsStat>(
84 gps_status_topic_, std::bind(&RTKLocalization::GpsStatusCallback,
85 localization_.get(), std::placeholders::_1));
86 ACHECK(gps_status_listener_);
87
88 localization_talker_ =
89 node_->CreateWriter<LocalizationEstimate>(localization_topic_);
90 ACHECK(localization_talker_);
91
92 localization_status_talker_ =
93 node_->CreateWriter<LocalizationStatus>(localization_status_topic_);
94 ACHECK(localization_status_talker_);
95 return true;
96}
97
98bool RTKLocalizationComponent::GetLocalizationToImuTF() {
99 transform::Buffer* tf2_buffer = transform::Buffer::Instance();
100 transform::TransformStamped tf;
101 cyber::Duration duration(1.0);
102 for (uint8_t i = 0; i < 10; ++i) {
103 try {
104 tf = tf2_buffer->lookupTransform(
105 imu_frame_id_, broadcast_tf_child_frame_id_, cyber::Time(0));
106 } catch (std::exception& ex) {
107 AERROR << ex.what();
108 duration.Sleep();
109 continue;
110 }
111 AINFO << "read localization to imu transform: " << tf.DebugString();
112 auto& rotation = tf.transform().rotation();
113 imu_localization_quat_.reset(new Eigen::Quaterniond(
114 rotation.qw(), rotation.qx(), rotation.qy(), rotation.qz()));
115 auto& translation = tf.transform().translation();
116 imu_localization_translation_.reset(
117 new Eigen::Vector3d(translation.x(), translation.y(), translation.z()));
118 return true;
119 }
120 return false;
121}
122
123void RTKLocalizationComponent::CompensateImuLocalizationExtrinsic(
124 LocalizationEstimate* localization) {
125 CHECK_NOTNULL(localization);
126 // calculate orientation_vehicle_world
127 apollo::localization::Pose* posepb_loc = localization->mutable_pose();
128 const apollo::common::Quaternion& orientation = posepb_loc->orientation();
129 const Eigen::Quaterniond quaternion(orientation.qw(), orientation.qx(),
130 orientation.qy(), orientation.qz());
131 Eigen::Quaterniond quat_vehicle_world =
132 quaternion * (*imu_localization_quat_);
133
134 // set heading according to rotation of vehicle
135 posepb_loc->set_heading(common::math::QuaternionToHeading(
136 quat_vehicle_world.w(), quat_vehicle_world.x(), quat_vehicle_world.y(),
137 quat_vehicle_world.z()));
138
139 // set euler angles according to rotation of vehicle
140 apollo::common::Point3D* eulerangles = posepb_loc->mutable_euler_angles();
142 quat_vehicle_world.w(), quat_vehicle_world.x(), quat_vehicle_world.y(),
143 quat_vehicle_world.z());
144 eulerangles->set_x(euler_angle.pitch());
145 eulerangles->set_y(euler_angle.roll());
146 eulerangles->set_z(euler_angle.yaw());
147
148 // Compensate the translation between imu and vehicle center.
149 apollo::common::PointENU* position = posepb_loc->mutable_position();
150 Eigen::Vector3d compensated_position =
151 quat_vehicle_world.toRotationMatrix() * (*imu_localization_translation_);
152 position->set_x(position->x() + compensated_position[0]);
153 position->set_y(position->y() + compensated_position[1]);
154 position->set_z(position->z() + compensated_position[2]);
155}
156
158 const std::shared_ptr<localization::Gps>& gps_msg) {
159 localization_->GpsCallback(gps_msg);
160
161 if (localization_->IsServiceStarted()) {
162 LocalizationEstimate localization;
163 localization_->GetLocalization(&localization);
164 LocalizationStatus localization_status;
165 localization_->GetLocalizationStatus(&localization_status);
166
167 // set localization pose at rear axle center
168 CompensateImuLocalizationExtrinsic(&localization);
169
170 // publish localization messages
171 PublishPoseBroadcastTopic(localization);
172 PublishPoseBroadcastTF(localization);
173 PublishLocalizationStatus(localization_status);
174 ADEBUG << "[OnTimer]: Localization message publish success!";
175 }
176
177 return true;
178}
179
180void RTKLocalizationComponent::PublishPoseBroadcastTF(
181 const LocalizationEstimate& localization) {
182 // broadcast tf message
184
185 auto mutable_head = tf2_msg.mutable_header();
186 mutable_head->set_timestamp_sec(localization.measurement_time());
187 mutable_head->set_frame_id(broadcast_tf_frame_id_);
188 tf2_msg.set_child_frame_id(broadcast_tf_child_frame_id_);
189
190 auto mutable_translation = tf2_msg.mutable_transform()->mutable_translation();
191 mutable_translation->set_x(localization.pose().position().x());
192 mutable_translation->set_y(localization.pose().position().y());
193 mutable_translation->set_z(localization.pose().position().z());
194
195 auto mutable_rotation = tf2_msg.mutable_transform()->mutable_rotation();
196 mutable_rotation->set_qx(localization.pose().orientation().qx());
197 mutable_rotation->set_qy(localization.pose().orientation().qy());
198 mutable_rotation->set_qz(localization.pose().orientation().qz());
199 mutable_rotation->set_qw(localization.pose().orientation().qw());
200
201 tf2_broadcaster_->SendTransform(tf2_msg);
202}
203
204void RTKLocalizationComponent::PublishPoseBroadcastTopic(
205 const LocalizationEstimate& localization) {
206 localization_talker_->Write(localization);
207}
208
209void RTKLocalizationComponent::PublishLocalizationStatus(
210 const LocalizationStatus& localization_status) {
211 localization_status_talker_->Write(localization_status);
212}
213
214} // namespace localization
215} // namespace apollo
std::shared_ptr< Node > node_
bool Proc(const std::shared_ptr< localization::Gps > &gps_msg) override
void GpsStatusCallback(const std::shared_ptr< drivers::gnss::InsStat > &status_msg)
void ImuCallback(const std::shared_ptr< localization::CorrectedImu > &imu_msg)
This class provides an easy way to publish coordinate frame transform information.
Defines the EulerAnglesZXY class.
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Math-related util functions.
EulerAnglesZXY< double > EulerAnglesZXYd
double QuaternionToHeading(const double qw, const double qx, const double qy, const double qz)
Definition quaternion.h:56
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
class register implement
Definition arena_queue.h:37
Contains a number of helper functions related to quaternions.
optional apollo::localization::Pose pose
optional apollo::common::Quaternion orientation
Definition pose.proto:31
optional apollo::common::PointENU position
Definition pose.proto:26