Apollo 11.0
自动驾驶开放平台
pbf_gatekeeper.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 *****************************************************************************/
17
18#include <algorithm>
19#include <limits>
20
21#include "modules/perception/multi_sensor_fusion/proto/pbf_gatekeeper_config.pb.h"
22
23#include "cyber/common/file.h"
26
27namespace apollo {
28namespace perception {
29namespace fusion {
30
32
34
36
38 std::string config_file =
39 GetConfigFile(options.config_path, options.config_file);
40
42 if (!cyber::common::GetProtoFromFile(config_file, &params)) {
43 AERROR << "Read config failed: " << config_file;
44 return false;
45 }
46
50 params_.use_camera_3d = params.use_camera_3d();
57 params_.toic_threshold = params.toic_threshold();
60 return true;
61}
62
64 bool invisible_in_lidar = !(track->IsLidarVisible());
65 bool invisible_in_radar = !(track->IsRadarVisible());
66 bool invisible_in_camera = !(track->IsCameraVisible());
67 if (invisible_in_lidar && invisible_in_radar &&
68 (!params_.use_camera_3d || invisible_in_camera)) {
69 auto sensor_obj = track->GetFusedObject();
70 if (sensor_obj != nullptr && sensor_obj->GetBaseObject()->sub_type !=
72 return false;
73 }
74 }
75 time_t rawtime = static_cast<time_t>(track->GetFusedObject()->GetTimestamp());
76
77 // use thread-safe localtime_r instead of localtime
78 struct tm timeinfo;
79 localtime_r(&rawtime, &timeinfo);
80 bool is_night = (timeinfo.tm_hour >= 23);
81 if (!LidarAbleToPublish(track) && !RadarAbleToPublish(track, is_night) &&
82 !CameraAbleToPublish(track, is_night)) {
83 return false;
84 }
85
86 track->AddTrackedTimes();
87 if (params_.use_track_time_pub_strategy &&
88 track->GetTrackedTimes() <=
89 static_cast<size_t>(params_.pub_track_time_thresh)) {
90 return false;
91 }
92 return true;
93}
94
95bool PbfGatekeeper::LidarAbleToPublish(const TrackPtr &track) {
96 bool visible_in_lidar = track->IsLidarVisible();
97 if (params_.publish_if_has_lidar && visible_in_lidar) {
98 return true;
99 }
100 return false;
101}
102
103bool PbfGatekeeper::RadarAbleToPublish(const TrackPtr &track, bool is_night) {
104 if (!params_.publish_if_has_radar) {
105 return false;
106 }
107
108 bool visible_in_radar = track->IsRadarVisible();
109 SensorObjectConstPtr radar_object = track->GetLatestRadarObject();
110 if (!visible_in_radar || radar_object == nullptr) {
111 return false;
112 }
113
114 if (radar_object->GetSensorId() == "radar_front") {
115 if (radar_object->GetBaseObject()->radar_supplement.range >
117 radar_object->GetBaseObject()->radar_supplement.angle <
119 auto base_obj = track->GetFusedObject()->GetBaseObject();
120 double heading_v = std::abs(base_obj->velocity.dot(base_obj->direction));
121 auto set_velocity_to_zero = [heading_v, track]() {
122 if (heading_v < 0.3) {
123 track->GetFusedObject()->GetBaseObject()->velocity.setZero();
124 }
125 };
126
127 double toic_p = track->GetToicProb();
128 if (!is_night) {
129 if (toic_p > params_.toic_threshold) {
130 set_velocity_to_zero();
131 return true;
132 }
133 } else {
134 // the velocity buffer is [-3, +3] m/s
135 double v_ct = 4.0;
136 double v_slope = 1.0;
137 auto heading_v_decision = [](double x, double c, double k) {
138 x = x - c;
139 return 0.5 + 0.5 * x * k / std::sqrt(1 + x * x * k * k);
140 };
141 auto fuse_two_probabilities = [](double p1, double p2) {
142 double p = (p1 * p2) / (2 * p1 * p2 + 1 - p1 - p2);
143 p = std::min(1.0 - std::numeric_limits<float>::epsilon(), p);
144 return p;
145 };
146
147 double min_toic_p = 0.2;
148 toic_p = std::max(min_toic_p, toic_p);
149 double v_p = heading_v_decision(heading_v, v_ct, v_slope);
150 double p = fuse_two_probabilities(toic_p, v_p);
151 if (p > 0.5) {
152 set_velocity_to_zero();
153 return true;
154 }
155 }
156 }
157 } else if (radar_object->GetSensorId() == "radar_rear") {
158 ADEBUG << "radar_rear: min_dis: " << params_.min_radar_confident_distance
159 << " obj dist: "
160 << radar_object->GetBaseObject()->radar_supplement.range
161 << " track_id: " << track->GetTrackId()
162 << " exist_prob: " << track->GetExistenceProb();
163 if (radar_object->GetBaseObject()->radar_supplement.range >
165 (radar_object->GetBaseObject()->velocity.norm() > 4.0 ||
166 track->GetExistenceProb() > params_.radar_existence_threshold)) {
167 return true;
168 }
169 }
170
171 return false;
172}
173
174bool PbfGatekeeper::CameraAbleToPublish(const TrackPtr &track, bool is_night) {
175 bool visible_in_camera = track->IsCameraVisible();
176 SensorId2ObjectMap &camera_objects = track->GetCameraObjects();
177 auto iter = camera_objects.find("front_6mm");
178 auto iter_narrow = camera_objects.find("front_12mm");
179 iter = iter != camera_objects.end() ? iter : iter_narrow;
180 if (camera_objects.find("camera_rear") != camera_objects.end()) {
181 iter = camera_objects.find("camera_rear");
182 } else if (camera_objects.find("camera_front") != camera_objects.end()) {
183 iter = camera_objects.find("camera_front");
184 }
185
186 if (params_.publish_if_has_camera && visible_in_camera &&
187 iter != camera_objects.end() && params_.use_camera_3d && !is_night) {
188 SensorObjectConstPtr camera_object = iter->second;
189 double range =
190 camera_object->GetBaseObject()->camera_supplement.local_center.norm();
191 // If sub_type of object is traffic cone publish it regardless of range
192 if ((camera_object->GetBaseObject()->sub_type ==
194 (range >= params_.min_camera_publish_distance)) {
195 double exist_prob = track->GetExistenceProb();
196 if (exist_prob > params_.existence_threshold) {
197 static int cnt_cam = 1;
198 AINFO << "publish camera only object : cnt = " << cnt_cam;
199 cnt_cam++;
200 return true;
201 }
202 }
203 }
204 return false;
205}
206
208
209} // namespace fusion
210} // namespace perception
211} // namespace apollo
#define PERCEPTION_REGISTER_GATEKEEPER(name)
bool Init(const GatekeeperInitOptions &options) override
Initialize
bool AbleToPublish(const TrackPtr &track) override
whether able to publish
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
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
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
std::shared_ptr< const SensorObject > SensorObjectConstPtr
std::shared_ptr< Track > TrackPtr
Definition track.h:397
std::map< std::string, SensorObjectPtr > SensorId2ObjectMap
Definition track.h:30
std::string GetConfigFile(const std::string &config_path, const std::string &config_file)
Definition util.cc:80
class register implement
Definition arena_queue.h:37