Apollo 11.0
自动驾驶开放平台
static_transform_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 "yaml-cpp/yaml.h"
20
23
24namespace apollo {
25namespace transform {
26
28 if (!GetProtoConfig(&conf_)) {
29 AERROR << "Parse conf file failed, " << ConfigFilePath();
30 return false;
31 }
33 attr.set_channel_name(FLAGS_tf_static_topic);
34 attr.mutable_qos_profile()->CopyFrom(
36 writer_ = node_->CreateWriter<TransformStampeds>(attr);
37 SendTransforms();
38 return true;
39}
40
41void StaticTransformComponent::SendTransforms() {
42 std::vector<TransformStamped> tranform_stamped_vec;
43 for (auto& extrinsic_file : conf_.extrinsic_file()) {
44 if (extrinsic_file.enable()) {
45 AINFO << "Broadcast static transform, frame id ["
46 << extrinsic_file.frame_id() << "], child frame id ["
47 << extrinsic_file.child_frame_id() << "]";
48 TransformStamped transform;
49 if (ParseFromYaml(extrinsic_file.file_path(), &transform)) {
50 tranform_stamped_vec.emplace_back(transform);
51 }
52 }
53 }
54 SendTransform(tranform_stamped_vec);
55}
56
57bool StaticTransformComponent::ParseFromYaml(
58 const std::string& file_path, TransformStamped* transform_stamped) {
59 if (!cyber::common::PathExists(file_path)) {
60 AERROR << "Extrinsic yaml file does not exist: " << file_path;
61 return false;
62 }
63 YAML::Node tf = YAML::LoadFile(file_path);
64 try {
65 transform_stamped->mutable_header()->set_frame_id(
66 tf["header"]["frame_id"].as<std::string>());
67 transform_stamped->set_child_frame_id(
68 tf["child_frame_id"].as<std::string>());
69 // translation
70 auto translation =
71 transform_stamped->mutable_transform()->mutable_translation();
72 translation->set_x(tf["transform"]["translation"]["x"].as<double>());
73 translation->set_y(tf["transform"]["translation"]["y"].as<double>());
74 translation->set_z(tf["transform"]["translation"]["z"].as<double>());
75 // rotation
76 auto rotation = transform_stamped->mutable_transform()->mutable_rotation();
77 rotation->set_qx(tf["transform"]["rotation"]["x"].as<double>());
78 rotation->set_qy(tf["transform"]["rotation"]["y"].as<double>());
79 rotation->set_qz(tf["transform"]["rotation"]["z"].as<double>());
80 rotation->set_qw(tf["transform"]["rotation"]["w"].as<double>());
81 } catch (...) {
82 AERROR << "Extrinsic yaml file parse failed: " << file_path;
83 return false;
84 }
85 return true;
86}
87
88void StaticTransformComponent::SendTransform(
89 const std::vector<TransformStamped>& msgtf) {
90 for (auto it_in = msgtf.begin(); it_in != msgtf.end(); ++it_in) {
91 bool match_found = false;
92 for (auto& it_msg : *transform_stampeds_.mutable_transforms()) {
93 if (it_in->child_frame_id() == it_msg.child_frame_id()) {
94 it_msg = *it_in;
95 match_found = true;
96 break;
97 }
98 }
99 if (!match_found) {
100 *transform_stampeds_.add_transforms() = *it_in;
101 }
102 }
103
104 common::util::FillHeader(node_->Name(), &transform_stampeds_);
105 writer_->Write(transform_stampeds_);
106}
107
108} // namespace transform
109} // namespace apollo
bool GetProtoConfig(T *config) const
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
static const QosProfile QOS_PROFILE_TF_STATIC
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some string util functions.
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195
class register implement
Definition arena_queue.h:37