Apollo 11.0
自动驾驶开放平台
barrier_recognition_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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
22namespace apollo {
23namespace perception {
24namespace lidar {
25
27
28const std::string kBarrierId2Type[] = {
29 "UNKOWN", "ROD", "FENCE", "ADVERTISING", "TELESCOPIC", "OTHER"
30};
31
33 BarrierRecognitionComponentConfig barrier_comp_config;
34 if (!GetProtoConfig(&barrier_comp_config)) {
35 AERROR << "Get BarrierRecognitionComponent file failed";
36 return false;
37 }
38 AINFO << "BarrierRecognitionComponent Component Configs: " << barrier_comp_config.DebugString();
39
40 tf2_frame_id_ = barrier_comp_config.frame_id();
41 tf2_child_frame_id_ = barrier_comp_config.child_frame_id();
42
43 auto recognizer_param = barrier_comp_config.recognizer_param();
44 BarrierRecognizerInitOptions barrier_bank_init_options;
45 barrier_bank_init_options.config_path = recognizer_param.config_path();
46 barrier_bank_init_options.config_file = recognizer_param.config_file();
47 ACHECK(barrier_bank_.Init(barrier_bank_init_options));
48
49 auto tracker_param = barrier_comp_config.tracker_param();
50 StatusTrackerInitOptions status_tracker_init_options;
51 status_tracker_init_options.config_path = tracker_param.config_path();
52 status_tracker_init_options.config_file = tracker_param.config_file();
53 ACHECK(status_tracker_.Init(status_tracker_init_options));
54
55 // init hdmaps
56 hd_map_ = map::HDMapInput::Instance();
57 if (hd_map_ == nullptr) {
58 AERROR << "BarrierRecognitionComponent get hdmap failed.";
59 return false;
60 }
61
62 if (!hd_map_->Init()) {
63 AERROR << "BarrierRecognitionComponent init hd-map failed.";
64 return false;
65 }
66 AINFO << "Successfully init barrier recognition component.";
67
68 writer_ = node_->CreateWriter<PerceptionBarrierGate>(
69 barrier_comp_config.output_channel_name());
70
71 return true;
72}
73
74bool BarrierRecognitionComponent::Proc(const std::shared_ptr<LidarFrameMessage>& message) {
76 // internal proc
77 std::vector<BarrieGate> gates;
78 bool status = InternalProc(message, gates);
79 if (status) {
80 std::shared_ptr<PerceptionBarrierGate> out_message(new (std::nothrow)
82 if (!MakeProtobufMsg(message->timestamp_, seq_num_, gates,
83 out_message.get())) {
84 AERROR << "MakeProtobufMsg failed ts: " << message->timestamp_;
85 return false;
86 }
87 seq_num_++;
88 writer_->Write(out_message);
89 AINFO << "Send peception barrier gate message.";
90 }
91 return status;
92}
93
94bool BarrierRecognitionComponent::InternalProc(
95 const std::shared_ptr<LidarFrameMessage>& message,
96 std::vector<BarrieGate>& gates) {
97 const double timestamp = message->timestamp_;
98 auto frame = message->lidar_frame_.get();
99
100 std::vector<apollo::hdmap::BarrierGate> hdmap_gates;
101 // query pose and signals, if far, return false
102 QueryPoseAndGates(timestamp, frame->lidar2world_pose, &hdmap_gates);
103
104 PERF_BLOCK("barrier_recognition")
105 for (auto& hdmap_gate : hdmap_gates) {
106 std::vector<double> world_polygon;
107 GetGatesPolygon(hdmap_gate, world_polygon);
108 BarrierRecognizerOptions detector_options;
109 detector_options.name = "StraightPoleRecognizer";
110 detector_options.world_roi_polygon = world_polygon;
111
112 float open_percent = 0.;
113 if (!barrier_bank_.Recognize(detector_options, frame, open_percent)) {
114 AINFO << "barrier recognize error.";
115 return false;
116 }
117 int status_id = 0;
118 if (!status_tracker_.Track(timestamp, open_percent, status_id)) {
119 AINFO << "barrier track error.";
120 return false;
121 }
122
123 BarrieGate obj = {status_id, hdmap_gate.id().id(),
124 kBarrierId2Type[static_cast<int>(hdmap_gate.type())],
125 open_percent};
126 gates.emplace_back(obj);
127 }
129
130 return true;
131}
132
133bool BarrierRecognitionComponent::QueryPoseAndGates(
134 const double ts, const Eigen::Affine3d& pose,
135 std::vector<apollo::hdmap::BarrierGate>* gates) {
136 if (!hd_map_) {
137 AERROR << "hd_map_ not init.";
138 return false;
139 }
140
141 // get gates
142 Eigen::Vector3d global_tranlation = pose.translation();
143 if (!hd_map_->GetBarrierGates(global_tranlation,
144 forward_distance_to_queries_,
145 gates)) {
146 if (ts - last_gates_ts_ < valid_hdmap_interval_) {
147 *gates = last_gates_;
148 AWARN << "query pose and gates failed to get gates info. "
149 << "Now use last info. ts:" << ts
150 << " pose:" << global_tranlation
151 << " gates.size(): " << gates->size();
152 } else {
153 AERROR << "query pose and gates failed to get gates info. "
154 << "ts:" << ts << " pose:" << global_tranlation;
155 return true;
156 }
157 } else {
158 AINFO << "query pose and gates succeeded, gates.size(): "
159 << gates->size();
160 last_gates_ts_ = ts;
161 last_gates_ = *gates;
162 }
163 return true;
164}
165
166void BarrierRecognitionComponent::GetGatesPolygon(
167 const apollo::hdmap::BarrierGate& barrier_gate,
168 std::vector<double>& world_polygon) {
169 world_polygon.clear();
170 for (int i = 0; i < barrier_gate.polygon().point_size(); i+=2) {
171 world_polygon.push_back(barrier_gate.polygon().point(i).x());
172 world_polygon.push_back(barrier_gate.polygon().point(i).y());
173 }
174}
175
176bool BarrierRecognitionComponent::MakeProtobufMsg(
177 double msg_timestamp, int seq_num,
178 std::vector<BarrieGate> gates,
179 PerceptionBarrierGate *msg) {
180 double publish_time = apollo::cyber::Clock::NowInSeconds();
181 auto header = msg->mutable_header();
182 header->set_timestamp_sec(publish_time);
183 header->set_module_name("perception_lidar");
184 header->set_sequence_num(seq_num);
185 // in nanosecond
186 // PnC would use lidar timestamp to predict
187 header->set_lidar_timestamp(static_cast<uint64_t>(msg_timestamp * 1e9));
188 header->set_camera_timestamp(static_cast<uint64_t>(msg_timestamp * 1e9));
189
190 for (const auto &obj : gates) {
191 BarrierGate *gate = msg->add_barrier_gates();
192 gate->set_status(static_cast<BarrierGate::Status>(obj.status_id));
193 gate->set_id(obj.id);
194 gate->set_type(obj.type);
195 gate->set_is_useable(true);
196 gate->set_open_percent(obj.open_percent);
197 }
198
199 return true;
200}
201
202
203} // namespace lidar
204} // namespace perception
205} // namespace apollo
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
bool GetProtoConfig(T *config) const
std::shared_ptr< Node > node_
bool Proc(const std::shared_ptr< LidarFrameMessage > &message) override
Process of barrier recognition
bool Init() override
Init barrier recognition component
bool Init(const BarrierRecognizerInitOptions &options=BarrierRecognizerInitOptions())
Init of BarrierRecognizerBank object
bool Recognize(const BarrierRecognizerOptions &options, LidarFrame *frame, float &open_percent)
calculate the relative position of the vehicle based on the reflector
bool Track(double timestamp, const float open_percent, int &status_id)
Tracking status.
bool Init(const StatusTrackerInitOptions &options=StatusTrackerInitOptions())
Init BarrierStatusTracker config
bool GetBarrierGates(const Eigen::Vector3d &pointd, double forward_distance, std::vector< apollo::hdmap::BarrierGate > *barrier_gates)
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
Definition file.cc:179
class register implement
Definition arena_queue.h:37
#define PERF_FUNCTION(...)
Definition profiler.h:54
#define PERF_BLOCK_END
Definition profiler.h:53
#define PERF_BLOCK(...)
Definition profiler.h:52
repeated apollo::common::PointENU point