28double Normalize(
const double value,
const double mean,
const double std) {
29 const double eps = 1e-10;
30 return (value - mean) / (
std + eps);
33double Relu(
const double value) {
return (value > 0.0) ? value : 0.0; }
35std::vector<double>
Softmax(
const std::vector<double>& value,
bool use_exp) {
36 std::vector<double> result;
38 for (std::size_t i = 0; i < value.size(); ++i) {
39 double exp_value = std::max(0.001, value[i]);
41 exp_value = std::exp(value[i]);
44 result.push_back(exp_value);
46 for (std::size_t i = 0; i < value.size(); ++i) {
47 result[i] = result[i] / sum;
53 std::pair<double, double>* roots) {
54 if (coefficients.size() != 3) {
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()) {
64 double delta = b * b - 4.0 * a * c;
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;
76 const double t,
const uint32_t order,
77 const double end_t,
const double end_v) {
82 ((((coeffs[5] * end_t + coeffs[4]) * end_t + coeffs[3]) * end_t +
88 return end_value + end_v * (t - end_t);
93 default: {
return 0.0; }
98 return ((((coeffs[5] * t + coeffs[4]) * t + coeffs[3]) * t + coeffs[2]) *
105 return (((5.0 * coeffs[5] * t + 4.0 * coeffs[4]) * t + 3.0 * coeffs[3]) *
112 return (((20.0 * coeffs[5] * t + 12.0 * coeffs[4]) * t) +
118 return (60.0 * coeffs[5] * t + 24.0 * coeffs[4]) * t + 6.0 * coeffs[3];
121 return 120.0 * coeffs[5] * t + 24.0 * coeffs[4];
124 return 120.0 * coeffs[5];
132 const double t,
const uint32_t order,
133 const double end_t,
const double end_v) {
138 (((coeffs[4] * end_t + coeffs[3]) * end_t + coeffs[2]) * end_t +
142 return end_value + (t - end_t) * end_v;
147 default: {
return 0.0; }
152 return (((coeffs[4] * t + coeffs[3]) * t + coeffs[2]) * t + coeffs[1]) *
157 return ((4.0 * coeffs[4] * t + 3.0 * coeffs[3]) * t + 2.0 * coeffs[2]) *
162 return (12.0 * coeffs[4] * t + 6.0 * coeffs[3]) * t + 2.0 * coeffs[2];
165 return 24.0 * coeffs[4] * t + 6.0 * coeffs[3];
168 return 24.0 * coeffs[4];
176 const double t,
const uint32_t order,
177 const double end_t,
const double end_v) {
182 ((coefs[3] * end_t + coefs[2]) * end_t + coefs[1]) * end_t +
184 return end_value + (t - end_t) * end_v;
189 default: {
return 0.0; }
195 return ((coefs[3] * t + coefs[2]) * t + coefs[1]) * t + coefs[0];
198 return (3.0 * coefs[3] * t + 2.0 * coefs[2]) * t + coefs[1];
201 return 6.0 * coefs[3] * t + 2.0 * coefs[2];
204 return 6.0 * coefs[3];
213 if (acceleration > -FLAGS_double_precision) {
214 return v0 * t + 0.5 * acceleration * t * t;
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;
223namespace predictor_util {
230 if (point ==
nullptr || !point->has_path_point()) {
231 AERROR <<
"Point is nullptr or has NO path_point.";
236 point->mutable_path_point()->set_x(original_x + translate_x);
237 point->mutable_path_point()->set_y(original_y + translate_y);
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);
252 for (std::size_t i = 0; i < num; ++i) {
253 double speed = std::hypot(v_x, v_y);
255 if (speed <= std::numeric_limits<double>::epsilon()) {
263 speed = std::fmin(speed, FLAGS_vehicle_max_speed);
267 if (speed > std::numeric_limits<double>::epsilon()) {
268 if (points->size() > 0) {
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);
277 if (points->size() > 0) {
283 if (!FLAGS_free_move_predict_with_accelerate) {
288 (*state)(2, 0) = v_x;
289 (*state)(3, 0) = v_y;
290 (*state)(4, 0) = acc_x;
291 (*state)(5, 0) = acc_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));
312 (*state) = transition * (*state);
315 v_x = (*state)(2, 0);
316 v_y = (*state)(3, 0);
317 acc_x = (*state)(4, 0);
318 acc_y = (*state)(5, 0);
323 if (std::abs(curvature) < FLAGS_turning_curvature_lower_bound) {
326 if (std::abs(curvature) > FLAGS_turning_curvature_upper_bound) {
327 return FLAGS_speed_at_upper_curvature;
330 FLAGS_speed_at_lower_curvature, FLAGS_turning_curvature_lower_bound,
331 FLAGS_speed_at_upper_curvature, FLAGS_turning_curvature_upper_bound,
Linear interpolation functions.
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.
optional PathPoint path_point