Apollo 10.0
自动驾驶开放平台
msg_serializer.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 <limits>
19
20#include "cyber/common/log.h"
21
22#include "cyber/time/clock.h"
24#include "modules/common_msgs/prediction_msgs/feature.pb.h"
25
27
28namespace {
29constexpr float kFloatMax = std::numeric_limits<float>::max();
30} // namespace
31
32namespace apollo {
33namespace perception {
34namespace onboard {
35
36bool MsgSerializer::SerializeMsg(double timestamp, uint64_t lidar_timestamp,
37 int seq_num,
38 const std::vector<base::ObjectPtr> &objects,
39 const apollo::common::ErrorCode &error_code,
40 PerceptionObstacles *obstacles) {
41 double publish_time = Clock::NowInSeconds();
42 ::apollo::common::Header *header = obstacles->mutable_header();
43 header->set_timestamp_sec(publish_time);
44 header->set_module_name("perception_obstacle");
45 header->set_sequence_num(seq_num);
46 header->set_lidar_timestamp(lidar_timestamp);
47 header->set_camera_timestamp(0);
48 header->set_radar_timestamp(0);
49
50 obstacles->set_error_code(error_code);
51 for (const auto &obj : objects) {
52 PerceptionObstacle *obstacle = obstacles->add_perception_obstacle();
53 if (!ConvertObjectToPb(obj, obstacle)) {
54 AERROR << "ConvertObjectToPb failed, Object:" << obj->ToString();
55 return false;
56 }
57 }
58 return true;
59}
60
62 double timestamp, uint64_t lidar_timestamp,
63 int seq_num,
64 const std::vector<base::ObjectPtr>& objects,
65 PerceptionBenchmarkFrame* benchmark_frame) {
66 // double publish_time = Clock::NowInSeconds();
67 ::apollo::common::Header *header = benchmark_frame->mutable_header();
68 header->set_timestamp_sec(timestamp);
69 header->set_module_name("perception_benchmark");
70 header->set_sequence_num(seq_num);
71 header->set_lidar_timestamp(lidar_timestamp);
72 header->set_camera_timestamp(0);
73 header->set_radar_timestamp(0);
74
75 for (const auto &obj : objects) {
76 PerceptionObstacle *obstacle = benchmark_frame->add_perception_obstacle();
77 if (!ConvertObjectToPb(obj, obstacle)) {
78 AERROR << "ConvertObjectToPb failed, Object:" << obj->ToString();
79 return false;
80 }
81 }
82 return true;
83}
84
86 double timestamp, uint64_t lidar_timestamp, int seq_num,
87 const Eigen::Affine3d &pose, const std::vector<base::ObjectPtr> &objects,
88 PerceptionBenchmarkFrame *obstacles, bool use_lidar_cooridinate) {
89 ::apollo::common::Header *header = obstacles->mutable_header();
90 header->set_timestamp_sec(timestamp);
91 header->set_module_name("perception_LidarFrame_benchmark");
92 header->set_sequence_num(seq_num);
93 header->set_lidar_timestamp(lidar_timestamp);
94 header->set_camera_timestamp(0);
95 header->set_radar_timestamp(0);
96
97 for (const auto &obj : objects) {
98 PerceptionObstacle *obstacle = obstacles->add_perception_obstacle();
99 if (!ConvertSegmentedObjectToPb(obj, obstacle,
100 pose, use_lidar_cooridinate)) {
101 AERROR << "ConvertSegmentedObjectToPb failed, Object:"
102 << obj->ToString();
103 return false;
104 }
105 }
106 return true;
107}
108
109bool MsgSerializer::ConvertObjectToPb(const base::ObjectPtr &object_ptr,
110 PerceptionObstacle *pb_msg) {
111 if (object_ptr == nullptr || pb_msg == nullptr) {
112 return false;
113 }
114
115 pb_msg->set_id(object_ptr->track_id);
116 pb_msg->set_theta(object_ptr->theta);
117 pb_msg->set_confidence(object_ptr->confidence);
118
119 apollo::common::Point3D *obj_center = pb_msg->mutable_position();
120 obj_center->set_x(object_ptr->center(0));
121 obj_center->set_y(object_ptr->center(1));
122 obj_center->set_z(object_ptr->center(2));
123
124 apollo::common::Point3D *obj_velocity = pb_msg->mutable_velocity();
125 obj_velocity->set_x(object_ptr->velocity(0));
126 obj_velocity->set_y(object_ptr->velocity(1));
127 obj_velocity->set_z(object_ptr->velocity(2));
128
129 apollo::common::Point3D *obj_acceleration = pb_msg->mutable_acceleration();
130 obj_acceleration->set_x(object_ptr->acceleration(0));
131 obj_acceleration->set_y(object_ptr->acceleration(1));
132 obj_acceleration->set_z(object_ptr->acceleration(2));
133
134 pb_msg->set_length(object_ptr->size(0));
135 pb_msg->set_width(object_ptr->size(1));
136 pb_msg->set_height(object_ptr->size(2));
137
138 for (size_t i = 0; i < object_ptr->polygon.size(); ++i) {
139 auto &pt = object_ptr->polygon.at(i);
140 apollo::common::Point3D *p = pb_msg->add_polygon_point();
141 p->set_x(pt.x);
142 p->set_y(pt.y);
143 p->set_z(pt.z);
144 }
145
146 if (FLAGS_obs_benchmark_mode) {
147 for (auto &point : object_ptr->lidar_supplement.cloud.points()) {
148 pb_msg->add_point_cloud(point.x);
149 pb_msg->add_point_cloud(point.y);
150 pb_msg->add_point_cloud(point.z);
151 }
152 }
153
154 apollo::common::Point3D *obj_anchor_point = pb_msg->mutable_anchor_point();
155 obj_anchor_point->set_x(object_ptr->anchor_point(0));
156 obj_anchor_point->set_y(object_ptr->anchor_point(1));
157 obj_anchor_point->set_z(object_ptr->anchor_point(2));
158
159 BBox2D *obj_bbox2d = pb_msg->mutable_bbox2d();
160 const base::BBox2DF &box = object_ptr->camera_supplement.box;
161 obj_bbox2d->set_xmin(box.xmin);
162 obj_bbox2d->set_ymin(box.ymin);
163 obj_bbox2d->set_xmax(box.xmax);
164 obj_bbox2d->set_ymax(box.ymax);
165
166 for (size_t i = 0; i < 3; i++) {
167 for (size_t j = 0; j < 3; j++) {
168 pb_msg->add_position_covariance(object_ptr->center_uncertainty(i, j));
169 pb_msg->add_velocity_covariance(object_ptr->velocity_uncertainty(i, j));
170 pb_msg->add_acceleration_covariance(
171 object_ptr->acceleration_uncertainty(i, j));
172 }
173 }
174
175 pb_msg->set_tracking_time(object_ptr->tracking_time);
176 pb_msg->set_type(static_cast<PerceptionObstacle::Type>(object_ptr->type));
177 pb_msg->set_sub_type(
178 static_cast<PerceptionObstacle::SubType>(object_ptr->sub_type));
179 pb_msg->set_timestamp(object_ptr->latest_tracked_time); // in seconds.
180 pb_msg->set_semantic_type(static_cast<PerceptionObstacle::SemanticType>(
181 object_ptr->lidar_supplement.semantic_type));
182
183 if (object_ptr->lidar_supplement.height_above_ground != kFloatMax) {
184 pb_msg->set_height_above_ground(
185 object_ptr->lidar_supplement.height_above_ground);
186 } else {
187 pb_msg->set_height_above_ground(std::numeric_limits<double>::quiet_NaN());
188 }
189
190 if (object_ptr->type == base::ObjectType::VEHICLE) {
191 LightStatus *light_status = pb_msg->mutable_light_status();
192 const base::CarLight &car_light = object_ptr->car_light;
193 light_status->set_brake_visible(car_light.brake_visible);
194 light_status->set_brake_switch_on(car_light.brake_switch_on);
195
196 light_status->set_left_turn_visible(car_light.left_turn_visible);
197 light_status->set_left_turn_switch_on(car_light.left_turn_switch_on);
198
199 light_status->set_right_turn_visible(car_light.right_turn_visible);
200 light_status->set_right_turn_switch_on(car_light.right_turn_switch_on);
201 }
202
203 if (FLAGS_obs_save_fusion_supplement &&
204 object_ptr->fusion_supplement.on_use) {
205 for (const auto &measurement : object_ptr->fusion_supplement.measurements) {
206 SensorMeasurement *pb_measurement = pb_msg->add_measurements();
207 pb_measurement->set_sensor_id(measurement.sensor_id);
208 pb_measurement->set_id(measurement.track_id);
209
210 apollo::common::Point3D *pb_position = pb_measurement->mutable_position();
211 pb_position->set_x(measurement.center(0));
212 pb_position->set_y(measurement.center(1));
213 pb_position->set_z(measurement.center(2));
214
215 pb_measurement->set_theta(measurement.theta);
216 pb_measurement->set_length(measurement.size(0));
217 pb_measurement->set_width(measurement.size(1));
218 pb_measurement->set_height(measurement.size(2));
219
220 apollo::common::Point3D *pb_velocity = pb_measurement->mutable_velocity();
221 pb_velocity->set_x(measurement.velocity(0));
222 pb_velocity->set_y(measurement.velocity(1));
223 pb_velocity->set_z(measurement.velocity(2));
224
225 pb_measurement->set_type(
226 static_cast<PerceptionObstacle::Type>(measurement.type));
227 // pb_measurement->set_sub_type();
228 pb_measurement->set_timestamp(measurement.timestamp);
229
230 BBox2D *pb_box = pb_measurement->mutable_box();
231 pb_box->set_xmin(measurement.box.xmin);
232 pb_box->set_ymin(measurement.box.ymin);
233 pb_box->set_xmax(measurement.box.xmax);
234 pb_box->set_ymax(measurement.box.ymax);
235 }
236 }
237
238// TODO(all): semantic map related, for debugging
239// // record the best prediction trajectory
240// if (object_ptr->feature.get() &&
241// object_ptr->feature->predicted_trajectory_size() > 0) {
242// apollo::perception::DebugMessage *dmsg = pb_msg->mutable_msg();
243// apollo::perception::Trajectory *target_traj = dmsg->add_trajectory();
244// const apollo::prediction::Trajectory &src_traj =
245// object_ptr->feature->predicted_trajectory(0);
246// (*target_traj->mutable_trajectory_point()) = (src_traj.trajectory_point());
247// ADEBUG << "Inserting Trajectores in PB with point size "
248// << src_traj.trajectory_point_size();
249// }
250
251 return true;
252}
253
254bool MsgSerializer::ConvertSegmentedObjectToPb(
255 const base::ObjectPtr &object_ptr, PerceptionObstacle *pb_msg,
256 const Eigen::Affine3d &pose, bool use_lidar_cooridinate) {
257 if (object_ptr == nullptr || pb_msg == nullptr) {
258 return false;
259 }
260
261 pb_msg->set_id(object_ptr->id);
262 pb_msg->set_confidence(object_ptr->confidence);
263
264 pb_msg->set_length(object_ptr->size(0));
265 pb_msg->set_width(object_ptr->size(1));
266 pb_msg->set_height(object_ptr->size(2));
267
268 pb_msg->set_type(static_cast<PerceptionObstacle::Type>(object_ptr->type));
269 pb_msg->set_sub_type(static_cast<PerceptionObstacle::SubType>(
270 object_ptr->sub_type));
271 pb_msg->set_semantic_type(static_cast<PerceptionObstacle::SemanticType>(
272 object_ptr->lidar_supplement.semantic_type));
273
274 if (use_lidar_cooridinate) {
275 pb_msg->set_theta(object_ptr->theta);
276 pb_msg->mutable_position()->set_x(object_ptr->center(0));
277 pb_msg->mutable_position()->set_y(object_ptr->center(1));
278 pb_msg->mutable_position()->set_z(object_ptr->center(2));
279
280 for (size_t i = 0; i < object_ptr->polygon.size(); ++i) {
281 auto &pt = object_ptr->polygon.at(i);
282 apollo::common::Point3D *p = pb_msg->add_polygon_point();
283 p->set_x(pt.x);
284 p->set_y(pt.y);
285 p->set_z(pt.z);
286 }
287 if (FLAGS_obs_benchmark_mode) {
288 for (auto &point : object_ptr->lidar_supplement.cloud.points()) {
289 pb_msg->add_point_cloud(point.x);
290 pb_msg->add_point_cloud(point.y);
291 pb_msg->add_point_cloud(point.z);
292 }
293 }
294 } else {
295 // theta
296 Eigen::Vector3d dir = pose.rotation() *
297 object_ptr->direction.cast<double>();
298 double world_theta = std::atan2(dir(1), dir(0));
299 pb_msg->set_theta(world_theta);
300
301 // center [Attention: Z is CENTER-Z, but tracking is BOTTOM-Z]
302 Eigen::Vector3d lidar_center(object_ptr->center(0),
303 object_ptr->center(1), object_ptr->center(2));
304 lidar_center = pose * lidar_center;
305 pb_msg->mutable_position()->set_x(lidar_center[0]);
306 pb_msg->mutable_position()->set_y(lidar_center[1]);
307 pb_msg->mutable_position()->set_z(lidar_center[2]);
308
309 // polygon
310 for (size_t i = 0; i < object_ptr->polygon.size(); ++i) {
311 auto &pt = object_ptr->polygon.at(i);
312 Eigen::Vector3d trans_point_polygon(pt.x, pt.y, pt.z);
313 trans_point_polygon = pose * trans_point_polygon;
314
315 apollo::common::Point3D *p = pb_msg->add_polygon_point();
316 p->set_x(trans_point_polygon[0]);
317 p->set_y(trans_point_polygon[1]);
318 p->set_z(trans_point_polygon[2]);
319 }
320 if (FLAGS_obs_benchmark_mode) {
321 auto cloud = object_ptr->lidar_supplement.cloud_world;
322 for (auto &point : cloud.points()) {
323 pb_msg->add_point_cloud(point.x);
324 pb_msg->add_point_cloud(point.y);
325 pb_msg->add_point_cloud(point.z);
326 }
327 }
328 }
329
330 // valued in tracking, this NOT-care
331 apollo::common::Point3D *obj_anchor_point = pb_msg->mutable_anchor_point();
332 obj_anchor_point->set_x(object_ptr->anchor_point(0));
333 obj_anchor_point->set_y(object_ptr->anchor_point(1));
334 obj_anchor_point->set_z(object_ptr->anchor_point(2));
335
336 BBox2D *obj_bbox2d = pb_msg->mutable_bbox2d();
337 const base::BBox2DF &box = object_ptr->camera_supplement.box;
338 obj_bbox2d->set_xmin(box.xmin);
339 obj_bbox2d->set_ymin(box.ymin);
340 obj_bbox2d->set_xmax(box.xmax);
341 obj_bbox2d->set_ymax(box.ymax);
342
343 if (object_ptr->lidar_supplement.height_above_ground != kFloatMax) {
344 pb_msg->set_height_above_ground(
345 object_ptr->lidar_supplement.height_above_ground);
346 } else {
347 pb_msg->set_height_above_ground(
348 std::numeric_limits<double>::quiet_NaN());
349 }
350
351 if (object_ptr->type == base::ObjectType::VEHICLE) {
352 LightStatus *light_status = pb_msg->mutable_light_status();
353 const base::CarLight &car_light = object_ptr->car_light;
354 light_status->set_brake_visible(car_light.brake_visible);
355 light_status->set_brake_switch_on(car_light.brake_switch_on);
356
357 light_status->set_left_turn_visible(car_light.left_turn_visible);
358 light_status->set_left_turn_switch_on(car_light.left_turn_switch_on);
359
360 light_status->set_right_turn_visible(car_light.right_turn_visible);
361 light_status->set_right_turn_switch_on(car_light.right_turn_switch_on);
362 }
363 return true;
364}
365
366} // namespace onboard
367} // namespace perception
368} // 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
static bool SerializeMsg(double timestamp, uint64_t lidar_timestamp, int seq_num, const std::vector< base::ObjectPtr > &objects, const apollo::common::ErrorCode &error_code, PerceptionObstacles *obstacles)
static bool SerializeLidarFrameMsg(double timestamp, uint64_t lidar_timestamp, int seq_num, const Eigen::Affine3d &pose, const std::vector< base::ObjectPtr > &objects, PerceptionBenchmarkFrame *obstacles, bool use_lidar_cooridinate=true)
static bool SerializeBenchmarkMsg(double timestamp, uint64_t lidar_timestamp, int seq_num, const std::vector< base::ObjectPtr > &objects, PerceptionBenchmarkFrame *obstacles)
#define AERROR
Definition log.h:44
std::shared_ptr< Object > ObjectPtr
Definition object.h:127
BBox2D< float > BBox2DF
Definition box.h:164
class register implement
Definition arena_queue.h:37