Apollo 11.0
自动驾驶开放平台
discrete_points_math.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
22
23#include <cmath>
24
25#include "cyber/common/log.h"
26
27namespace apollo {
28namespace planning {
29
31 const std::vector<std::pair<double, double>>& xy_points,
32 std::vector<double>* headings, std::vector<double>* accumulated_s,
33 std::vector<double>* kappas, std::vector<double>* dkappas) {
34 CHECK_NOTNULL(headings);
35 CHECK_NOTNULL(kappas);
36 CHECK_NOTNULL(dkappas);
37 headings->clear();
38 kappas->clear();
39 dkappas->clear();
40
41 if (xy_points.size() < 2) {
42 return false;
43 }
44 std::vector<double> dxs;
45 std::vector<double> dys;
46 std::vector<double> y_over_s_first_derivatives;
47 std::vector<double> x_over_s_first_derivatives;
48 std::vector<double> y_over_s_second_derivatives;
49 std::vector<double> x_over_s_second_derivatives;
50
51 // Get finite difference approximated dx and dy for heading and kappa
52 // calculation
53 std::size_t points_size = xy_points.size();
54 for (std::size_t i = 0; i < points_size; ++i) {
55 double x_delta = 0.0;
56 double y_delta = 0.0;
57 if (i == 0) {
58 x_delta = (xy_points[i + 1].first - xy_points[i].first);
59 y_delta = (xy_points[i + 1].second - xy_points[i].second);
60 } else if (i == points_size - 1) {
61 x_delta = (xy_points[i].first - xy_points[i - 1].first);
62 y_delta = (xy_points[i].second - xy_points[i - 1].second);
63 } else {
64 x_delta = 0.5 * (xy_points[i + 1].first - xy_points[i - 1].first);
65 y_delta = 0.5 * (xy_points[i + 1].second - xy_points[i - 1].second);
66 }
67 dxs.push_back(x_delta);
68 dys.push_back(y_delta);
69 }
70
71 // Heading calculation
72 for (std::size_t i = 0; i < points_size; ++i) {
73 headings->push_back(std::atan2(dys[i], dxs[i]));
74 }
75
76 // Get linear interpolated s for dkappa calculation
77 double distance = 0.0;
78 accumulated_s->push_back(distance);
79 double fx = xy_points[0].first;
80 double fy = xy_points[0].second;
81 double nx = 0.0;
82 double ny = 0.0;
83 for (std::size_t i = 1; i < points_size; ++i) {
84 nx = xy_points[i].first;
85 ny = xy_points[i].second;
86 double end_segment_s =
87 std::sqrt((fx - nx) * (fx - nx) + (fy - ny) * (fy - ny));
88 accumulated_s->push_back(end_segment_s + distance);
89 distance += end_segment_s;
90 fx = nx;
91 fy = ny;
92 }
93
94 // Get finite difference approximated first derivative of y and x respective
95 // to s for kappa calculation
96 for (std::size_t i = 0; i < points_size; ++i) {
97 double xds = 0.0;
98 double yds = 0.0;
99 if (i == 0) {
100 xds = (xy_points[i + 1].first - xy_points[i].first) /
101 (accumulated_s->at(i + 1) - accumulated_s->at(i));
102 yds = (xy_points[i + 1].second - xy_points[i].second) /
103 (accumulated_s->at(i + 1) - accumulated_s->at(i));
104 } else if (i == points_size - 1) {
105 xds = (xy_points[i].first - xy_points[i - 1].first) /
106 (accumulated_s->at(i) - accumulated_s->at(i - 1));
107 yds = (xy_points[i].second - xy_points[i - 1].second) /
108 (accumulated_s->at(i) - accumulated_s->at(i - 1));
109 } else {
110 xds = (xy_points[i + 1].first - xy_points[i - 1].first) /
111 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
112 yds = (xy_points[i + 1].second - xy_points[i - 1].second) /
113 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
114 }
115 x_over_s_first_derivatives.push_back(xds);
116 y_over_s_first_derivatives.push_back(yds);
117 }
118
119 // Get finite difference approximated second derivative of y and x respective
120 // to s for kappa calculation
121 for (std::size_t i = 0; i < points_size; ++i) {
122 double xdds = 0.0;
123 double ydds = 0.0;
124 if (i == 0) {
125 xdds =
126 (x_over_s_first_derivatives[i + 1] - x_over_s_first_derivatives[i]) /
127 (accumulated_s->at(i + 1) - accumulated_s->at(i));
128 ydds =
129 (y_over_s_first_derivatives[i + 1] - y_over_s_first_derivatives[i]) /
130 (accumulated_s->at(i + 1) - accumulated_s->at(i));
131 } else if (i == points_size - 1) {
132 xdds =
133 (x_over_s_first_derivatives[i] - x_over_s_first_derivatives[i - 1]) /
134 (accumulated_s->at(i) - accumulated_s->at(i - 1));
135 ydds =
136 (y_over_s_first_derivatives[i] - y_over_s_first_derivatives[i - 1]) /
137 (accumulated_s->at(i) - accumulated_s->at(i - 1));
138 } else {
139 xdds = (x_over_s_first_derivatives[i + 1] -
140 x_over_s_first_derivatives[i - 1]) /
141 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
142 ydds = (y_over_s_first_derivatives[i + 1] -
143 y_over_s_first_derivatives[i - 1]) /
144 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
145 }
146 x_over_s_second_derivatives.push_back(xdds);
147 y_over_s_second_derivatives.push_back(ydds);
148 }
149
150 for (std::size_t i = 0; i < points_size; ++i) {
151 double xds = x_over_s_first_derivatives[i];
152 double yds = y_over_s_first_derivatives[i];
153 double xdds = x_over_s_second_derivatives[i];
154 double ydds = y_over_s_second_derivatives[i];
155 double kappa =
156 (xds * ydds - yds * xdds) /
157 (std::sqrt(xds * xds + yds * yds) * (xds * xds + yds * yds) + 1e-6);
158 kappas->push_back(kappa);
159 }
160
161 // Dkappa calculation
162 for (std::size_t i = 0; i < points_size; ++i) {
163 double dkappa = 0.0;
164 if (i == 0) {
165 dkappa = (kappas->at(i + 1) - kappas->at(i)) /
166 (accumulated_s->at(i + 1) - accumulated_s->at(i));
167 } else if (i == points_size - 1) {
168 dkappa = (kappas->at(i) - kappas->at(i - 1)) /
169 (accumulated_s->at(i) - accumulated_s->at(i - 1));
170 } else {
171 dkappa = (kappas->at(i + 1) - kappas->at(i - 1)) /
172 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
173 }
174 dkappas->push_back(dkappa);
175 }
176 return true;
177}
178
179} // namespace planning
180} // namespace apollo
static bool ComputePathProfile(const std::vector< std::pair< double, double > > &xy_points, std::vector< double > *headings, std::vector< double > *accumulated_s, std::vector< double > *kappas, std::vector< double > *dkappas)
Planning module main class.
class register implement
Definition arena_queue.h:37