Apollo 10.0
自动驾驶开放平台
lslidarCH16_parser.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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
19namespace apollo {
20namespace drivers {
21namespace lslidar {
22namespace parser {
23
25 LslidarParser(config), previous_packet_stamp_(0), gps_base_usec_(0) {}
26
28 const std::shared_ptr<LslidarScan>& scan_msg,
29 const std::shared_ptr<PointCloud>& out_msg) {
30 // allocate a point cloud with same time and frame ID as raw data
31 out_msg->mutable_header()->set_timestamp_sec(
32 scan_msg->basetime() / 1000000000.0);
33 out_msg->mutable_header()->set_module_name(
34 scan_msg->header().module_name());
35 out_msg->mutable_header()->set_frame_id(scan_msg->header().frame_id());
36 out_msg->set_height(1);
37 out_msg->set_measurement_time(scan_msg->basetime() / 1000000000.0);
38 out_msg->mutable_header()->set_sequence_num(
39 scan_msg->header().sequence_num());
40 gps_base_usec_ = scan_msg->basetime();
41
42 const unsigned char* difop_ptr
43 = (const unsigned char*)scan_msg->difop_pkts(0).data().c_str();
44
45 two_bytes angle_1, angle_2, angle_3, angle_4;
46 angle_1.bytes[0] = difop_ptr[663];
47 angle_1.bytes[1] = difop_ptr[662];
48 prism_angle[0] = angle_1.uint * 0.01;
49
50 angle_2.bytes[0] = difop_ptr[665];
51 angle_2.bytes[1] = difop_ptr[664];
52 prism_angle[1] = angle_2.uint * 0.01;
53
54 angle_3.bytes[0] = difop_ptr[667];
55 angle_3.bytes[1] = difop_ptr[666];
56 prism_angle[2] = angle_3.uint * 0.01;
57
58 angle_4.bytes[0] = difop_ptr[669];
59 angle_4.bytes[1] = difop_ptr[668];
60 prism_angle[3] = angle_4.uint * 0.01;
61 packets_size = scan_msg->firing_pkts_size();
62
63 for (size_t i = 0; i < packets_size; ++i) {
64 Unpack(static_cast<int>(i),
65 scan_msg->firing_pkts(static_cast<int>(i)),
66 out_msg);
67 last_time_stamp_ = out_msg->measurement_time();
68 ADEBUG << "stamp: " << std::fixed << last_time_stamp_;
69 }
70
71 if (out_msg->point().empty()) {
72 // we discard this pointcloud if empty
73 AERROR << "All points is NAN!Please check lslidar:" << config_.model();
74 }
75
76 // set default width
77 out_msg->set_width(out_msg->point_size());
78}
79
84void LslidarCH16Parser::Unpack(
85 int num,
86 const LslidarPacket& pkt,
87 std::shared_ptr<PointCloud> pc) {
88 float x, y, z;
89 uint64_t packet_end_time;
90 double z_sin_altitude = 0.0;
91 double z_cos_altitude = 0.0;
92 time_last = 0;
93 const RawPacket* raw = (const RawPacket*)pkt.data().c_str();
94
95 packet_end_time = pkt.stamp();
96
97 for (size_t point_idx = 0; point_idx < POINTS_PER_PACKET; point_idx++) {
98 firings[point_idx].vertical_line = raw->points[point_idx].vertical_line;
99 two_bytes point_amuzith;
100 point_amuzith.bytes[0] = raw->points[point_idx].azimuth_2;
101 point_amuzith.bytes[1] = raw->points[point_idx].azimuth_1;
102 firings[point_idx].azimuth
103 = static_cast<double>(point_amuzith.uint) * 0.01 * DEG_TO_RAD;
104 four_bytes point_distance;
105 point_distance.bytes[0] = raw->points[point_idx].distance_3;
106 point_distance.bytes[1] = raw->points[point_idx].distance_2;
107 point_distance.bytes[2] = raw->points[point_idx].distance_1;
108 point_distance.bytes[3] = 0;
109 firings[point_idx].distance = static_cast<double>(point_distance.uint)
110 / 256.0 * DISTANCE_RESOLUTION;
111 firings[point_idx].intensity = raw->points[point_idx].intensity;
112 }
113
114 for (size_t point_idx = 0; point_idx < POINTS_PER_PACKET; point_idx++) {
115 LaserCorrection& corrections
117 .laser_corrections_[firings[point_idx].vertical_line];
118
119 if (config_.calibration())
120 firings[point_idx].distance
121 = firings[point_idx].distance + corrections.dist_correction;
122
123 if (firings[point_idx].distance > config_.max_range()
124 || firings[point_idx].distance < config_.min_range())
125 continue;
126
127 // Convert the point to xyz coordinate
128 double cos_azimuth_half = cos(firings[point_idx].azimuth * 0.5);
129
130 if (abs(prism_angle[0]) < 1e-6 && abs(prism_angle[1]) < 1e-6
131 && abs(prism_angle[2]) < 1e-6 && abs(prism_angle[3]) < 1e-6) {
132 z_sin_altitude = sin_scan_laser_altitude
133 [firings[point_idx].vertical_line / 4 + 2]
134 + 2 * cos_azimuth_half
135 * sin_scan_mirror_altitude
136 [firings[point_idx].vertical_line % 4];
137 } else {
138 z_sin_altitude = sin_scan_laser_altitude
139 [firings[point_idx].vertical_line / 4 + 2]
140 + 2 * cos_azimuth_half
141 * sin(prism_angle
142 [firings[point_idx].vertical_line % 4]
143 * M_PI / 180);
144 }
145
146 z_cos_altitude = sqrt(1 - z_sin_altitude * z_sin_altitude);
147
148 x = firings[point_idx].distance * z_cos_altitude
149 * cos(firings[point_idx].azimuth);
150 y = firings[point_idx].distance * z_cos_altitude
151 * sin(firings[point_idx].azimuth);
152 z = firings[point_idx].distance * z_sin_altitude;
153
154 // Compute the time of the point
155 uint64_t point_time
156 = packet_end_time - 1665 * (POINTS_PER_PACKET - point_idx - 1);
157 if (time_last < point_time && time_last > 0) {
158 point_time = time_last + 1665;
159 }
160 time_last = point_time;
161
162 PointXYZIT* point = pc->add_point();
163 point->set_timestamp(point_time / 1000000000.0);
164 point->set_intensity(firings[point_idx].intensity);
165
166 if (config_.calibration()) {
168 firings[point_idx].vertical_line,
169 CH16,
170 firings[point_idx].distance,
171 &corrections,
172 firings[point_idx].azimuth,
173 point);
174 } else {
175 if ((y >= config_.bottom_left_x() && y <= config_.top_right_x())
176 && (-x >= config_.bottom_left_y()
177 && -x <= config_.top_right_y())) {
178 point->set_x(nan);
179 point->set_y(nan);
180 point->set_z(nan);
181 point->set_timestamp(point_time);
182 point->set_intensity(0);
183 } else {
184 point->set_x(y);
185 point->set_y(-x);
186 point->set_z(z);
187 }
188 }
189 }
190}
191
192void LslidarCH16Parser::Order(std::shared_ptr<PointCloud> cloud) {
193 int width = 16;
194 cloud->set_width(width);
195 int height = cloud->point_size() / cloud->width();
196 cloud->set_height(height);
197
198 std::shared_ptr<PointCloud> cloud_origin = std::make_shared<PointCloud>();
199 cloud_origin->CopyFrom(*cloud);
200
201 for (int i = 0; i < width; ++i) {
202 int col = ORDER_32[i];
203
204 for (int j = 0; j < height; ++j) {
205 // make sure offset is initialized, should be init at setup() just
206 // once
207 int target_index = j * width + i;
208 int origin_index = j * width + col;
209 cloud->mutable_point(target_index)
210 ->CopyFrom(cloud_origin->point(origin_index));
211 }
212 }
213}
214
215} // namespace parser
216} // namespace lslidar
217} // namespace drivers
218} // namespace apollo
std::map< int, LaserCorrection > laser_corrections_
Definition calibration.h:72
void Order(std::shared_ptr< apollo::drivers::PointCloud > cloud)
void GeneratePointcloud(const std::shared_ptr< LslidarScan > &scan_msg, const std::shared_ptr< apollo::drivers::PointCloud > &out_msg)
Set up for data processing.
void ComputeCoords2(int Laser_ring, int Type, const float &raw_distance, LaserCorrection *corrections, const double rotation, PointXYZIT *point)
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
These classes Unpack raw Lslidar LIDAR packets into several useful formats.
#define DEG_TO_RAD
#define CH16
class register implement
Definition arena_queue.h:37
used for unpacking the first two data bytes in a block