Apollo 11.0
自动驾驶开放平台
radar_detection_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 *****************************************************************************/
17
18#include "cyber/time/clock.h"
22
24
25namespace apollo {
26namespace perception {
27namespace radar {
28
30 RadarComponentConfig comp_config;
31 if (!GetProtoConfig(&comp_config)) {
32 return false;
33 }
34 AINFO << "Radar component config: " << comp_config.DebugString();
35
36 // To load component configs
37 tf_child_frame_id_ = comp_config.tf_child_frame_id();
38 radar_forward_distance_ = comp_config.radar_forward_distance();
39 odometry_channel_name_ = comp_config.odometry_channel_name();
40
41 if (!algorithm::SensorManager::Instance()->GetSensorInfo(
42 comp_config.radar_name(), &radar_info_)) {
43 AERROR << "Failed to get sensor info, sensor name: "
44 << comp_config.radar_name();
45 return false;
46 }
47
48 writer_ = node_->CreateWriter<onboard::SensorFrameMessage>(
49 comp_config.output_channel_name());
50
51 // Init algorithm plugin
52 ACHECK(InitAlgorithmPlugin(comp_config))
53 << "Failed to init algorithm plugin.";
54
55 radar2world_trans_.Init(tf_child_frame_id_);
56 radar2novatel_trans_.Init(tf_child_frame_id_);
57 localization_subscriber_.Init(
58 odometry_channel_name_,
59 odometry_channel_name_ + '_' + comp_config.radar_name());
60 return true;
61}
62
63bool RadarDetectionComponent::Proc(const std::shared_ptr<ContiRadar>& message) {
64 AINFO << "Enter radar preprocess, message timestamp: "
65 << message->header().timestamp_sec() << " current timestamp "
67 auto out_message = std::make_shared<onboard::SensorFrameMessage>();
68
69 if (!InternalProc(message, out_message)) {
70 return false;
71 }
72 writer_->Write(out_message);
73 return true;
74}
75
76bool RadarDetectionComponent::InitAlgorithmPlugin(
77 const RadarComponentConfig& config) {
78 if (onboard::FLAGS_obs_enable_hdmap_input) {
79 hdmap_input_ = map::HDMapInput::Instance();
80 ACHECK(hdmap_input_->Init()) << "Failed to init hdmap input.";
81 }
82
83 auto preprocessor_param = config.preprocessor_param();
84 PreprocessorInitOptions preprocessor_init_options;
85 preprocessor_init_options.config_path = preprocessor_param.config_path();
86 preprocessor_init_options.config_file = preprocessor_param.config_file();
87 BasePreprocessor* radar_preprocessor =
88 BasePreprocessorRegisterer::GetInstanceByName(preprocessor_param.name());
89 CHECK_NOTNULL(radar_preprocessor);
90 radar_preprocessor_.reset(radar_preprocessor);
91 ACHECK(radar_preprocessor_->Init(preprocessor_init_options))
92 << "Failed to init radar preprocessor.";
93
94 auto perception_param = config.perception_param();
95 PerceptionInitOptions perception_init_options;
96 perception_init_options.config_path = perception_param.config_path();
97 perception_init_options.config_file = perception_param.config_file();
98 BaseRadarObstaclePerception* radar_perception =
99 BaseRadarObstaclePerceptionRegisterer::GetInstanceByName(
100 perception_param.name());
101 CHECK_NOTNULL(radar_perception);
102 radar_perception_.reset(radar_perception);
103 ACHECK(radar_perception_->Init(perception_init_options))
104 << "Failed to init radar perception.";
105 return true;
106}
107
108bool RadarDetectionComponent::InternalProc(
109 const std::shared_ptr<ContiRadar>& in_message,
110 std::shared_ptr<onboard::SensorFrameMessage> out_message) {
112 ContiRadar raw_obstacles = *in_message;
113 {
114 std::unique_lock<std::mutex> lock(_mutex);
115 ++seq_num_;
116 }
117 double timestamp = in_message->header().timestamp_sec();
119 // Init preprocessor_options
120 PreprocessorOptions preprocessor_options;
121 ContiRadar corrected_obstacles;
122 radar_preprocessor_->Preprocess(raw_obstacles, preprocessor_options,
123 &corrected_obstacles);
124 PERF_BLOCK_END_WITH_INDICATOR(radar_info_.name, "radar_preprocessor");
125 timestamp = corrected_obstacles.header().timestamp_sec();
126
127 out_message->timestamp_ = timestamp;
128 out_message->seq_num_ = seq_num_;
129 out_message->process_stage_ =
131 out_message->sensor_id_ = radar_info_.name;
132
133 // Init radar perception options
134 RadarPerceptionOptions options;
135 options.sensor_name = radar_info_.name;
136 // Init detector_options
137 Eigen::Affine3d radar_trans;
138 if (!radar2world_trans_.GetSensor2worldTrans(timestamp, &radar_trans)) {
139 out_message->error_code_ = apollo::common::ErrorCode::PERCEPTION_ERROR_TF;
140 AERROR << "Failed to get pose at time: " << timestamp;
141 return true;
142 }
143 Eigen::Affine3d radar2novatel_trans;
144 if (!radar2novatel_trans_.GetTrans(timestamp, &radar2novatel_trans, "novatel",
145 tf_child_frame_id_)) {
146 out_message->error_code_ = apollo::common::ErrorCode::PERCEPTION_ERROR_TF;
147 AERROR << "Failed to get radar2novatel trans at time: " << timestamp;
148 return true;
149 }
150 PERF_BLOCK_END_WITH_INDICATOR(radar_info_.name, "GetSensor2worldTrans");
151 Eigen::Matrix4d radar2world_pose = radar_trans.matrix();
152 options.detector_options.radar2world_pose = &radar2world_pose;
153 Eigen::Matrix4d radar2novatel_trans_m = radar2novatel_trans.matrix();
154 options.detector_options.radar2novatel_trans = &radar2novatel_trans_m;
155 if (!GetCarLocalizationSpeed(timestamp,
156 &(options.detector_options.car_linear_speed),
157 &(options.detector_options.car_angular_speed))) {
158 AERROR << "Failed to call get_car_speed. [timestamp: " << timestamp;
159 // return false;
160 }
161 PERF_BLOCK_END_WITH_INDICATOR(radar_info_.name, "GetCarSpeed");
162 // Init roi_filter_options
163 base::PointD position;
164 position.x = radar_trans(0, 3);
165 position.y = radar_trans(1, 3);
166 position.z = radar_trans(2, 3);
167 options.roi_filter_options.roi.reset(new base::HdmapStruct());
168 if (onboard::FLAGS_obs_enable_hdmap_input) {
169 hdmap_input_->GetRoiHDMapStruct(position, radar_forward_distance_,
170 options.roi_filter_options.roi);
171 }
172 PERF_BLOCK_END_WITH_INDICATOR(radar_info_.name, "GetRoiHDMapStruct");
173 // Radar perception
174 std::vector<base::ObjectPtr> radar_objects;
175 if (!radar_perception_->Perceive(corrected_obstacles, options,
176 &radar_objects)) {
177 out_message->error_code_ =
179 AERROR << "RadarDetector Proc failed.";
180 return true;
181 }
182 out_message->frame_.reset(new base::Frame());
183 out_message->frame_->sensor_info = radar_info_;
184 out_message->frame_->timestamp = timestamp;
185 out_message->frame_->sensor2world_pose = radar_trans;
186 out_message->frame_->objects = radar_objects;
187
188 PERF_BLOCK_END_WITH_INDICATOR(radar_info_.name, "radar_perception");
189 return true;
190}
191
192bool RadarDetectionComponent::GetCarLocalizationSpeed(
193 double timestamp, Eigen::Vector3f* car_linear_speed,
194 Eigen::Vector3f* car_angular_speed) {
195 if (car_linear_speed == nullptr) {
196 AERROR << "car_linear_speed is not available";
197 return false;
198 }
199 (*car_linear_speed) = Eigen::Vector3f::Zero();
200
201 if (car_angular_speed == nullptr) {
202 AERROR << "car_angular_speed is not available";
203 return false;
204 }
205 (*car_angular_speed) = Eigen::Vector3f::Zero();
206 std::shared_ptr<LocalizationEstimate const> loct_ptr;
207 if (!localization_subscriber_.LookupNearest(timestamp, &loct_ptr)) {
208 AERROR << "Cannot get car speed.";
209 return false;
210 }
211 (*car_linear_speed)[0] =
212 static_cast<float>(loct_ptr->pose().linear_velocity().x());
213 (*car_linear_speed)[1] =
214 static_cast<float>(loct_ptr->pose().linear_velocity().y());
215 (*car_linear_speed)[2] =
216 static_cast<float>(loct_ptr->pose().linear_velocity().z());
217 (*car_angular_speed)[0] =
218 static_cast<float>(loct_ptr->pose().angular_velocity().x());
219 (*car_angular_speed)[1] =
220 static_cast<float>(loct_ptr->pose().angular_velocity().y());
221 (*car_angular_speed)[2] =
222 static_cast<float>(loct_ptr->pose().angular_velocity().z());
223
224 return true;
225}
226
227} // namespace radar
228} // namespace perception
229} // namespace apollo
Set the behavior of the
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
bool GetProtoConfig(T *config) const
std::shared_ptr< Node > node_
bool GetRoiHDMapStruct(const base::PointD &pointd, const double distance, std::shared_ptr< base::HdmapStruct > hdmap_struct_prt)
bool GetTrans(double timestamp, Eigen::Affine3d *trans, const std::string &frame_id, const std::string &child_frame_id)
bool GetSensor2worldTrans(double timestamp, Eigen::Affine3d *sensor2world_trans, Eigen::Affine3d *novatel2world_trans=nullptr)
void Init(const std::string &sensor2novatel_tf2_child_frame_id)
bool Proc(const std::shared_ptr< ContiRadar > &message) override
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Point< double > PointD
Definition point.h:57
class register implement
Definition arena_queue.h:37
#define PERF_BLOCK_END_WITH_INDICATOR(indicator, msg)
Definition perf_util.h:128
#define PERF_FUNCTION_WITH_INDICATOR(indicator)
Definition perf_util.h:125
#define PERF_BLOCK_START()
Definition perf_util.h:126