Apollo 11.0
自动驾驶开放平台
prediction_util.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
19#include <algorithm>
20
23
24namespace apollo {
25namespace prediction {
26namespace math_util {
27
28double Normalize(const double value, const double mean, const double std) {
29 const double eps = 1e-10;
30 return (value - mean) / (std + eps);
31}
32
33double Relu(const double value) { return (value > 0.0) ? value : 0.0; }
34
35std::vector<double> Softmax(const std::vector<double>& value, bool use_exp) {
36 std::vector<double> result;
37 double sum = 0.0;
38 for (std::size_t i = 0; i < value.size(); ++i) {
39 double exp_value = std::max(0.001, value[i]);
40 if (use_exp) {
41 exp_value = std::exp(value[i]);
42 }
43 sum += exp_value;
44 result.push_back(exp_value);
45 }
46 for (std::size_t i = 0; i < value.size(); ++i) {
47 result[i] = result[i] / sum;
48 }
49 return result;
50}
51
52int SolveQuadraticEquation(const std::vector<double>& coefficients,
53 std::pair<double, double>* roots) {
54 if (coefficients.size() != 3) {
55 return -1;
56 }
57 const double a = coefficients[0];
58 const double b = coefficients[1];
59 const double c = coefficients[2];
60 if (std::fabs(a) <= std::numeric_limits<double>::epsilon()) {
61 return -1;
62 }
63
64 double delta = b * b - 4.0 * a * c;
65 if (delta < 0.0) {
66 return -1;
67 }
68
69 double sqrt_delta = std::sqrt(delta);
70 roots->first = (-b + sqrt_delta) * 0.5 / a;
71 roots->second = (-b - sqrt_delta) * 0.5 / a;
72 return 0;
73}
74
75double EvaluateQuinticPolynomial(const std::array<double, 6>& coeffs,
76 const double t, const uint32_t order,
77 const double end_t, const double end_v) {
78 if (t >= end_t) {
79 switch (order) {
80 case 0: {
81 double end_value =
82 ((((coeffs[5] * end_t + coeffs[4]) * end_t + coeffs[3]) * end_t +
83 coeffs[2]) *
84 end_t +
85 coeffs[1]) *
86 end_t +
87 coeffs[0];
88 return end_value + end_v * (t - end_t);
89 }
90 case 1: {
91 return end_v;
92 }
93 default: { return 0.0; }
94 }
95 }
96 switch (order) {
97 case 0: {
98 return ((((coeffs[5] * t + coeffs[4]) * t + coeffs[3]) * t + coeffs[2]) *
99 t +
100 coeffs[1]) *
101 t +
102 coeffs[0];
103 }
104 case 1: {
105 return (((5.0 * coeffs[5] * t + 4.0 * coeffs[4]) * t + 3.0 * coeffs[3]) *
106 t +
107 2.0 * coeffs[2]) *
108 t +
109 coeffs[1];
110 }
111 case 2: {
112 return (((20.0 * coeffs[5] * t + 12.0 * coeffs[4]) * t) +
113 6.0 * coeffs[3]) *
114 t +
115 2.0 * coeffs[2];
116 }
117 case 3: {
118 return (60.0 * coeffs[5] * t + 24.0 * coeffs[4]) * t + 6.0 * coeffs[3];
119 }
120 case 4: {
121 return 120.0 * coeffs[5] * t + 24.0 * coeffs[4];
122 }
123 case 5: {
124 return 120.0 * coeffs[5];
125 }
126 default:
127 return 0.0;
128 }
129}
130
131double EvaluateQuarticPolynomial(const std::array<double, 5>& coeffs,
132 const double t, const uint32_t order,
133 const double end_t, const double end_v) {
134 if (t >= end_t) {
135 switch (order) {
136 case 0: {
137 double end_value =
138 (((coeffs[4] * end_t + coeffs[3]) * end_t + coeffs[2]) * end_t +
139 coeffs[1]) *
140 end_t +
141 coeffs[0];
142 return end_value + (t - end_t) * end_v;
143 }
144 case 1: {
145 return end_v;
146 }
147 default: { return 0.0; }
148 }
149 }
150 switch (order) {
151 case 0: {
152 return (((coeffs[4] * t + coeffs[3]) * t + coeffs[2]) * t + coeffs[1]) *
153 t +
154 coeffs[0];
155 }
156 case 1: {
157 return ((4.0 * coeffs[4] * t + 3.0 * coeffs[3]) * t + 2.0 * coeffs[2]) *
158 t +
159 coeffs[1];
160 }
161 case 2: {
162 return (12.0 * coeffs[4] * t + 6.0 * coeffs[3]) * t + 2.0 * coeffs[2];
163 }
164 case 3: {
165 return 24.0 * coeffs[4] * t + 6.0 * coeffs[3];
166 }
167 case 4: {
168 return 24.0 * coeffs[4];
169 }
170 default:
171 return 0.0;
172 }
173}
174
175double EvaluateCubicPolynomial(const std::array<double, 4>& coefs,
176 const double t, const uint32_t order,
177 const double end_t, const double end_v) {
178 if (t > end_t) {
179 switch (order) {
180 case 0: {
181 double end_value =
182 ((coefs[3] * end_t + coefs[2]) * end_t + coefs[1]) * end_t +
183 coefs[0];
184 return end_value + (t - end_t) * end_v;
185 }
186 case 1: {
187 return end_v;
188 }
189 default: { return 0.0; }
190 }
191 }
192
193 switch (order) {
194 case 0: {
195 return ((coefs[3] * t + coefs[2]) * t + coefs[1]) * t + coefs[0];
196 }
197 case 1: {
198 return (3.0 * coefs[3] * t + 2.0 * coefs[2]) * t + coefs[1];
199 }
200 case 2: {
201 return 6.0 * coefs[3] * t + 2.0 * coefs[2];
202 }
203 case 3: {
204 return 6.0 * coefs[3];
205 }
206 default:
207 return 0.0;
208 }
209}
210
211double GetSByConstantAcceleration(const double v0, const double acceleration,
212 const double t) {
213 if (acceleration > -FLAGS_double_precision) {
214 return v0 * t + 0.5 * acceleration * t * t;
215 }
216 double t_stop = v0 / (-acceleration);
217 double t_actual = std::min(t, t_stop);
218 return v0 * t_actual + 0.5 * acceleration * t_actual * t_actual;
219}
220
221} // namespace math_util
222
223namespace predictor_util {
224
227
228void TranslatePoint(const double translate_x, const double translate_y,
229 TrajectoryPoint* point) {
230 if (point == nullptr || !point->has_path_point()) {
231 AERROR << "Point is nullptr or has NO path_point.";
232 return;
233 }
234 const double original_x = point->path_point().x();
235 const double original_y = point->path_point().y();
236 point->mutable_path_point()->set_x(original_x + translate_x);
237 point->mutable_path_point()->set_y(original_y + translate_y);
238}
239
241 Eigen::Matrix<double, 6, 1>* state,
242 const Eigen::Matrix<double, 6, 6>& transition, double theta,
243 const double start_time, const std::size_t num, const double period,
244 std::vector<TrajectoryPoint>* points) {
245 double x = (*state)(0, 0);
246 double y = (*state)(1, 0);
247 double v_x = (*state)(2, 0);
248 double v_y = (*state)(3, 0);
249 double acc_x = (*state)(4, 0);
250 double acc_y = (*state)(5, 0);
251
252 for (std::size_t i = 0; i < num; ++i) {
253 double speed = std::hypot(v_x, v_y);
254 double acc = 0.0;
255 if (speed <= std::numeric_limits<double>::epsilon()) {
256 speed = 0.0;
257 v_x = 0.0;
258 v_y = 0.0;
259 acc_x = 0.0;
260 acc_y = 0.0;
261 acc = 0.0;
262 } else {
263 speed = std::fmin(speed, FLAGS_vehicle_max_speed);
264 }
265
266 // update theta and acc
267 if (speed > std::numeric_limits<double>::epsilon()) {
268 if (points->size() > 0) {
269 TrajectoryPoint& prev_trajectory_point = points->back();
270 PathPoint* prev_path_point = prev_trajectory_point.mutable_path_point();
271 theta = std::atan2(y - prev_path_point->y(), x - prev_path_point->x());
272 prev_path_point->set_theta(theta);
273 acc = (speed - prev_trajectory_point.v()) / period;
274 prev_trajectory_point.set_a(acc);
275 }
276 } else {
277 if (points->size() > 0) {
278 theta = points->back().path_point().theta();
279 }
280 }
281
282 // update state
283 if (!FLAGS_free_move_predict_with_accelerate) {
284 acc_x = 0.0;
285 acc_y = 0.0;
286 }
287
288 (*state)(2, 0) = v_x;
289 (*state)(3, 0) = v_y;
290 (*state)(4, 0) = acc_x;
291 (*state)(5, 0) = acc_y;
292
293 // obtain position
294 x = (*state)(0, 0);
295 y = (*state)(1, 0);
296
297 // Generate trajectory point
298 TrajectoryPoint trajectory_point;
299 PathPoint path_point;
300 path_point.set_x(x);
301 path_point.set_y(y);
302 path_point.set_z(0.0);
303 path_point.set_theta(theta);
304 trajectory_point.mutable_path_point()->CopyFrom(path_point);
305 trajectory_point.set_v(speed);
306 trajectory_point.set_a(acc);
307 trajectory_point.set_relative_time(start_time +
308 static_cast<double>(i) * period);
309 points->emplace_back(std::move(trajectory_point));
310
311 // Update position, velocity and acceleration
312 (*state) = transition * (*state);
313 x = (*state)(0, 0);
314 y = (*state)(1, 0);
315 v_x = (*state)(2, 0);
316 v_y = (*state)(3, 0);
317 acc_x = (*state)(4, 0);
318 acc_y = (*state)(5, 0);
319 }
320}
321
322double AdjustSpeedByCurvature(const double speed, const double curvature) {
323 if (std::abs(curvature) < FLAGS_turning_curvature_lower_bound) {
324 return speed;
325 }
326 if (std::abs(curvature) > FLAGS_turning_curvature_upper_bound) {
327 return FLAGS_speed_at_upper_curvature;
328 }
330 FLAGS_speed_at_lower_curvature, FLAGS_turning_curvature_lower_bound,
331 FLAGS_speed_at_upper_curvature, FLAGS_turning_curvature_upper_bound,
332 curvature);
333}
334
335} // namespace predictor_util
336} // namespace prediction
337} // namespace apollo
Linear interpolation functions.
#define AERROR
Definition log.h:44
T lerp(const T &x0, const double t0, const T &x1, const double t1, const double t)
Linear interpolation between two points of type T.
double EvaluateQuarticPolynomial(const std::array< double, 5 > &coeffs, const double t, const uint32_t order, const double end_t, const double end_v)
Evaluate quartic polynomial.
double GetSByConstantAcceleration(const double v0, const double acceleration, const double t)
double EvaluateQuinticPolynomial(const std::array< double, 6 > &coeffs, const double t, const uint32_t order, const double end_t, const double end_v)
Evaluate quintic polynomial.
double EvaluateCubicPolynomial(const std::array< double, 4 > &coefs, const double t, const uint32_t order, const double end_t, const double end_v)
Evaluate cubic polynomial.
double Relu(const double value)
RELU function used in neural networks as an activation function.
std::vector< double > Softmax(const std::vector< double > &value, bool use_exp)
Softmax function used in neural networks as an activation function.
int SolveQuadraticEquation(const std::vector< double > &coefficients, std::pair< double, double > *roots)
Solve quadratic equation.
double Normalize(const double value, const double mean, const double std)
Normalize the value by specified mean and standard deviation.
double AdjustSpeedByCurvature(const double speed, const double curvature)
Adjust a speed value according to a curvature.
void GenerateFreeMoveTrajectoryPoints(Eigen::Matrix< double, 6, 1 > *state, const Eigen::Matrix< double, 6, 6 > &transition, double theta, const double start_time, const std::size_t num, const double period, std::vector< TrajectoryPoint > *points)
Generate a set of free move trajectory points
void TranslatePoint(const double translate_x, const double translate_y, TrajectoryPoint *point)
Translate a point.
class register implement
Definition arena_queue.h:37
Definition future.h:29