Apollo 10.0
自动驾驶开放平台
calibration.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
31
32#include "yaml-cpp/yaml.h"
33
34namespace YAML {
35// The >> operator disappeared in yaml-cpp 0.5, so this function is
36// added to provide support for code written under the yaml-cpp 0.3 API.
37template <typename T>
38void operator>>(const YAML::Node &node, T &i) {
39 i = node.as<T>();
40}
41} // namespace YAML
42
43namespace apollo {
44namespace drivers {
45namespace lslidar {
46namespace parser {
47
48const char *NUM_LASERS = "num_lasers";
49const char *LASERS = "lasers";
50const char *LASER_ID = "laser_id";
51const char *ROT_CORRECTION = "rot_correction";
52const char *VERT_CORRECTION = "vert_correction";
53const char *DIST_CORRECTION = "dist_correction";
54const char *TWO_PT_CORRECTION_AVAILABLE = "two_pt_correction_available";
55const char *DIST_CORRECTION_X = "dist_correction_x";
56const char *DIST_CORRECTION_Y = "dist_correction_y";
57const char *VERT_OFFSET_CORRECTION = "vert_offset_correction";
58const char *HORIZ_OFFSET_CORRECTION = "horiz_offset_correction";
59const char *MAX_INTENSITY = "max_intensity";
60const char *MIN_INTENSITY = "min_intensity";
61const char *FOCAL_DISTANCE = "focal_distance";
62const char *FOCAL_SLOPE = "focal_slope";
63
65 const YAML::Node &node,
66 std::pair<int, LaserCorrection> &correction) {
67 node[LASER_ID] >> correction.first;
68 node[ROT_CORRECTION] >> correction.second.rot_correction;
69 node[VERT_CORRECTION] >> correction.second.vert_correction;
70 node[DIST_CORRECTION] >> correction.second.dist_correction;
71 node[DIST_CORRECTION_X] >> correction.second.dist_correction_x;
72 node[DIST_CORRECTION_Y] >> correction.second.dist_correction_y;
73 node[VERT_OFFSET_CORRECTION] >> correction.second.vert_offset_correction;
74 if (node[HORIZ_OFFSET_CORRECTION]) {
76 >> correction.second.horiz_offset_correction;
77 } else {
78 correction.second.horiz_offset_correction = 0.0;
79 }
80
81 if (node[MAX_INTENSITY]) {
82 node[MAX_INTENSITY] >> correction.second.max_intensity;
83 } else {
84 correction.second.max_intensity = 255;
85 }
86
87 if (node[MIN_INTENSITY]) {
88 node[MIN_INTENSITY] >> correction.second.min_intensity;
89 } else {
90 correction.second.min_intensity = 0;
91 }
92
93 node[FOCAL_DISTANCE] >> correction.second.focal_distance;
94 node[FOCAL_SLOPE] >> correction.second.focal_slope;
95
96 // Calculate cached values
97 correction.second.cos_rot_correction
98 = cosf(correction.second.rot_correction);
99 correction.second.sin_rot_correction
100 = sinf(correction.second.rot_correction);
101 correction.second.cos_vert_correction
102 = cosf(correction.second.vert_correction);
103 correction.second.sin_vert_correction
104 = sinf(correction.second.vert_correction);
105 correction.second.focal_offset = 256.0f
106 * static_cast<float>(std::pow(
107 1 - correction.second.focal_distance / 13100.0f, 2));
108 correction.second.laser_ring = 0; // clear initially (set later)
109}
110
111void operator>>(const YAML::Node &node, Calibration &calibration) {
112 int num_lasers = 0;
113 node[NUM_LASERS] >> num_lasers;
114 const YAML::Node &lasers = node[LASERS];
115 calibration.laser_corrections_.clear();
116 calibration.num_lasers_ = num_lasers;
117
118 for (int i = 0; i < num_lasers; i++) {
119 std::pair<int, LaserCorrection> correction;
120 lasers[i] >> correction;
121 calibration.laser_corrections_.insert(correction);
122 }
123
124 // For each laser ring, find the next-smallest vertical angle.
125 //
126 // This implementation is simple, but not efficient. That is OK,
127 // since it only runs while starting up.
128 double next_angle = -std::numeric_limits<double>::infinity();
129
130 for (int ring = 0; ring < num_lasers; ++ring) {
131 // find minimum remaining vertical offset correction
132 double min_seen = std::numeric_limits<double>::infinity();
133 int next_index = num_lasers;
134
135 for (int j = 0; j < num_lasers; ++j) {
136 double angle = calibration.laser_corrections_[j].vert_correction;
137
138 if (next_angle < angle && angle < min_seen) {
139 min_seen = angle;
140 next_index = j;
141 }
142 }
143
144 if (next_index < num_lasers) { // anything found in this ring?
145 // store this ring number with its corresponding laser number
146 calibration.laser_corrections_[next_index].laser_ring = ring;
147 next_angle = min_seen;
148 }
149 }
150}
151
152YAML::Emitter &operator<<(
153 YAML::Emitter &out,
154 const std::pair<int, LaserCorrection> &correction) {
155 out << YAML::BeginMap;
156 out << YAML::Key << LASER_ID << YAML::Value << correction.first;
157 out << YAML::Key << ROT_CORRECTION << YAML::Value
158 << correction.second.rot_correction;
159 out << YAML::Key << VERT_CORRECTION << YAML::Value
160 << correction.second.vert_correction;
161 out << YAML::Key << DIST_CORRECTION << YAML::Value
162 << correction.second.dist_correction;
163 out << YAML::Key << DIST_CORRECTION_X << YAML::Value
164 << correction.second.dist_correction_x;
165 out << YAML::Key << DIST_CORRECTION_Y << YAML::Value
166 << correction.second.dist_correction_y;
167 out << YAML::Key << VERT_OFFSET_CORRECTION << YAML::Value
168 << correction.second.vert_offset_correction;
169 out << YAML::Key << HORIZ_OFFSET_CORRECTION << YAML::Value
170 << correction.second.horiz_offset_correction;
171 out << YAML::Key << MAX_INTENSITY << YAML::Value
172 << correction.second.max_intensity;
173 out << YAML::Key << MIN_INTENSITY << YAML::Value
174 << correction.second.min_intensity;
175 out << YAML::Key << FOCAL_DISTANCE << YAML::Value
176 << correction.second.focal_distance;
177 out << YAML::Key << FOCAL_SLOPE << YAML::Value
178 << correction.second.focal_slope;
179 out << YAML::EndMap;
180 return out;
181}
182
183YAML::Emitter &operator<<(YAML::Emitter &out, const Calibration &calibration) {
184 out << YAML::BeginMap;
185 out << YAML::Key << NUM_LASERS << YAML::Value
186 << calibration.laser_corrections_.size();
187 out << YAML::Key << LASERS << YAML::Value << YAML::BeginSeq;
188
189 for (std::map<int, LaserCorrection>::const_iterator it
190 = calibration.laser_corrections_.begin();
191 it != calibration.laser_corrections_.end();
192 ++it) {
193 out << *it;
194 }
195
196 out << YAML::EndSeq;
197 out << YAML::EndMap;
198 return out;
199}
200
201void Calibration::read(const std::string &calibration_file) {
202 std::ifstream fin(calibration_file.c_str());
203
204 if (!fin.is_open()) {
205 initialized_ = false;
206 return;
207 }
208
209 initialized_ = true;
210
211 try {
212 YAML::Node doc;
213 fin.close();
214 doc = YAML::LoadFile(calibration_file);
215 doc >> *this;
216 } catch (YAML::Exception &e) {
217 std::cerr << "YAML Exception: " << e.what() << std::endl;
218 initialized_ = false;
219 }
220
221 fin.close();
222}
223
224void Calibration::write(const std::string &calibration_file) {
225 std::ofstream fout(calibration_file.c_str());
226 YAML::Emitter out;
227 out << *this;
228 fout << out.c_str();
229 fout.close();
230}
231
232} // namespace parser
233} // namespace lslidar
234} // namespace drivers
235} // namespace apollo
Calibration class storing entire configuration for the Lslidar
Definition calibration.h:70
std::map< int, LaserCorrection > laser_corrections_
Definition calibration.h:72
void write(const std::string &calibration_file)
void read(const std::string &calibration_file)
void operator>>(const YAML::Node &node, T &i)
void operator>>(const YAML::Node &node, std::pair< int, LaserCorrection > &correction)
YAML::Emitter & operator<<(YAML::Emitter &out, const std::pair< int, LaserCorrection > &correction)
class register implement
Definition arena_queue.h:37