Apollo 11.0
自动驾驶开放平台
radar_preprocessor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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 "modules/perception/radar4d_detection/proto/preprocessor_config.pb.h"
19
20#include "cyber/common/file.h"
21#include "cyber/common/log.h"
24
25namespace apollo {
26namespace perception {
27namespace radar4d {
28
29const float RadarPreprocessor::kPointInfThreshold = 1e3;
30int RadarPreprocessor::current_idx_ = 0;
31std::unordered_map<int, int> RadarPreprocessor::local2global_;
32
34 // load config file
35 std::string config_file =
36 GetConfigFile(options.config_path, options.config_file);
37
38 PreprocessorConfig config;
39 ACHECK(cyber::common::GetProtoFromFile(config_file, &config));
40
41 rcs_offset_ = config.rcs_offset();
42 return true;
43}
44
46 const std::shared_ptr<apollo::drivers::OculiiPointCloud const>& message,
47 const PreprocessorOptions& options,
48 RadarFrame* frame) {
49 if (frame == nullptr) {
50 return false;
51 }
52 if (frame->cloud == nullptr) {
53 frame->cloud = base::RadarPointFCloudPool::Instance().Get();
54 }
55 if (frame->world_cloud == nullptr) {
56 frame->world_cloud = base::RadarPointDCloudPool::Instance().Get();
57 }
58
59 frame->cloud->set_timestamp(message->measurement_time());
60 if (message->point_size() > 0) {
61 frame->cloud->reserve(message->point_size());
63 for (int i = 0; i < message->point_size(); ++i) {
64 // original point cloud from driver message
65 const apollo::drivers::OculiiPointXYZIV& pt = message->point(i);
67 message->raw_pointclouds(i);
68 // filter abnormal points
69 if (filter_naninf_points_) {
70 if (std::isnan(pt.x()) || std::isnan(pt.y()) || std::isnan(pt.z())) {
71 continue;
72 }
73 if (fabs(pt.x()) > kPointInfThreshold ||
74 fabs(pt.y()) > kPointInfThreshold ||
75 fabs(pt.z()) > kPointInfThreshold) {
76 continue;
77 }
78 }
79 // filter points higher than the threshold
80 if (filter_high_z_points_ && pt.z() > z_threshold_) {
81 continue;
82 }
83 point.x = pt.x();
84 point.y = pt.y();
85 point.z = pt.z();
86 // rcs offset is set for different radars used for training and testing
87 point.rcs = static_cast<float>(pt.intensity() + rcs_offset_);
88 // doppler velocity is returned by radar
89 point.velocity = static_cast<float>(raw_pt.doppler());
90 // compensated velocity of each point needs to be calculated
91 point.comp_vel = CalCompensatedVelocity(point, options);
92 frame->cloud->push_back(point);
93 }
94 // transform the cloud point from local to world
95 TransformCloud(frame->cloud, frame->radar2world_pose, frame->world_cloud);
96 }
97 return true;
98}
99
100bool RadarPreprocessor::TransformCloud(
101 const base::RadarPointFCloudPtr& local_cloud, const Eigen::Affine3d& pose,
102 base::RadarPointDCloudPtr world_cloud) const {
103 if (local_cloud == nullptr) {
104 return false;
105 }
106 world_cloud->clear();
107 world_cloud->reserve(local_cloud->size());
108 for (size_t i = 0; i < local_cloud->size(); ++i) {
109 auto& pt = local_cloud->at(i);
110 Eigen::Vector3d trans_point(pt.x, pt.y, pt.z);
111 trans_point = pose * trans_point;
112 base::RadarPointD world_point;
113 world_point.x = trans_point(0);
114 world_point.y = trans_point(1);
115 world_point.z = trans_point(2);
116 world_point.rcs = pt.rcs;
117 world_point.velocity = pt.velocity;
118 world_point.comp_vel = pt.comp_vel;
119 world_cloud->push_back(world_point);
120 }
121 return true;
122}
123
124float RadarPreprocessor::CalCompensatedVelocity(
125 const base::RadarPointF& point,
126 const PreprocessorOptions& options) {
127 // transfer the ego speed from world coor to radar coor
128 const Eigen::Matrix4d& radar2world = *(options.radar2world_pose);
129 Eigen::Matrix3d radar2world_rotate = radar2world.block<3, 3>(0, 0);
130 Eigen::Vector3d radar_speed =
131 static_cast<Eigen::Matrix<double, 3, 1, 0, 3, 1>>(
132 radar2world_rotate.inverse() *
133 options.car_linear_speed.cast<double>());
134
135 // calculate the direction of point
136 Eigen::Vector3f local_loc(point.x, point.y, point.z);
137 float azimuth = std::atan2(local_loc[1], local_loc[0]);
138 float xy_temp = std::sqrt(local_loc[0] * local_loc[0]
139 + local_loc[1] * local_loc[1]);
140 float elevation = std::atan2(local_loc[2], xy_temp);
141
142 // calculate the compensated velocity
143 // = doppler velocity + projection of ego velocity
144 float compensated_v = point.velocity +
145 radar_speed[0] * std::cos(azimuth) +
146 radar_speed[1] * std::sin(azimuth) +
147 radar_speed[2] * std::sin(elevation);
148 return compensated_v;
149}
150
152
153} // namespace radar4d
154} // namespace perception
155} // namespace apollo
bool Init(const PreprocessorInitOptions &options) override
Init RadarPreprocessor config
bool Preprocess(const std::shared_ptr< apollo::drivers::OculiiPointCloud const > &message, const PreprocessorOptions &options, RadarFrame *frame) override
Process radar point cloud.
#define ACHECK(cond)
Definition log.h:80
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::shared_ptr< RadarPointFCloud > RadarPointFCloudPtr
std::shared_ptr< RadarPointDCloud > RadarPointDCloudPtr
RadarPoint< double > RadarPointD
Definition point.h:114
RadarPoint< float > RadarPointF
Definition point.h:113
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
#define PERCEPTION_REGISTER_PREPROCESSOR(name)
std::shared_ptr< base::AttributeRadarPointCloud< base::RadarPointD > > world_cloud
Definition radar_frame.h:37
std::shared_ptr< base::AttributeRadarPointCloud< base::RadarPointF > > cloud
Definition radar_frame.h:34