Apollo 10.0
自动驾驶开放平台
speed_data.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
22
23#include <algorithm>
24#include <mutex>
25#include <utility>
26
27#include "absl/strings/str_cat.h"
28#include "absl/strings/str_join.h"
29
35
36namespace apollo {
37namespace planning {
38
40
41SpeedData::SpeedData(std::vector<SpeedPoint> speed_points)
42 : std::vector<SpeedPoint>(std::move(speed_points)) {
43 std::sort(begin(), end(), [](const SpeedPoint& p1, const SpeedPoint& p2) {
44 return p1.t() < p2.t();
45 });
46}
47
48void SpeedData::AppendSpeedPoint(const double s, const double time,
49 const double v, const double a,
50 const double da) {
51 static std::mutex mutex_speedpoint;
52 UNIQUE_LOCK_MULTITHREAD(mutex_speedpoint);
53
54 if (!empty()) {
55 ACHECK(back().t() < time);
56 }
57 push_back(common::util::PointFactory::ToSpeedPoint(s, time, v, a, da));
58}
59
60bool SpeedData::EvaluateByTime(const double t,
61 common::SpeedPoint* const speed_point) const {
62 if (size() < 2) {
63 return false;
64 }
65 if (!(front().t() < t + 1.0e-6 && t - 1.0e-6 < back().t())) {
66 return false;
67 }
68
69 auto comp = [](const common::SpeedPoint& sp, const double t) {
70 return sp.t() < t;
71 };
72
73 auto it_lower = std::lower_bound(begin(), end(), t, comp);
74 if (it_lower == end()) {
75 *speed_point = back();
76 } else if (it_lower == begin()) {
77 *speed_point = front();
78 } else {
79 const auto& p0 = *(it_lower - 1);
80 const auto& p1 = *it_lower;
81 double t0 = p0.t();
82 double t1 = p1.t();
83
84 speed_point->Clear();
85 speed_point->set_s(common::math::lerp(p0.s(), t0, p1.s(), t1, t));
86 speed_point->set_t(t);
87 if (p0.has_v() && p1.has_v()) {
88 speed_point->set_v(common::math::lerp(p0.v(), t0, p1.v(), t1, t));
89 }
90 if (p0.has_a() && p1.has_a()) {
91 speed_point->set_a(common::math::lerp(p0.a(), t0, p1.a(), t1, t));
92 }
93 if (p0.has_da() && p1.has_da()) {
94 speed_point->set_da(common::math::lerp(p0.da(), t0, p1.da(), t1, t));
95 }
96 }
97 return true;
98}
99
100bool SpeedData::EvaluateByS(const double s,
101 common::SpeedPoint* const speed_point) const {
102 if (size() < 2) {
103 return false;
104 }
105 if (!(front().s() < s + 1.0e-6 && s - 1.0e-6 < back().s())) {
106 return false;
107 }
108
109 auto comp = [](const common::SpeedPoint& sp, const double s) {
110 return sp.s() < s;
111 };
112
113 auto it_lower = std::lower_bound(begin(), end(), s, comp);
114 if (it_lower == end()) {
115 *speed_point = back();
116 } else if (it_lower == begin()) {
117 *speed_point = front();
118 } else {
119 const auto& p0 = *(it_lower - 1);
120 const auto& p1 = *it_lower;
121 double s0 = p0.s();
122 double s1 = p1.s();
123
124 speed_point->Clear();
125 speed_point->set_s(s);
126 speed_point->set_t(common::math::lerp(p0.t(), s0, p1.t(), s1, s));
127 if (p0.has_v() && p1.has_v()) {
128 speed_point->set_v(common::math::lerp(p0.v(), s0, p1.v(), s1, s));
129 }
130 if (p0.has_a() && p1.has_a()) {
131 speed_point->set_a(common::math::lerp(p0.a(), s0, p1.a(), s1, s));
132 }
133 if (p0.has_da() && p1.has_da()) {
134 speed_point->set_da(common::math::lerp(p0.da(), s0, p1.da(), s1, s));
135 }
136 }
137 return true;
138}
139
140double SpeedData::TotalTime() const {
141 if (empty()) {
142 return 0.0;
143 }
144 return back().t() - front().t();
145}
146
148 if (empty()) {
149 return 0.0;
150 }
151 return back().s() - front().s();
152}
153
154std::string SpeedData::DebugString() const {
155 const auto limit = std::min(
156 size(), static_cast<size_t>(FLAGS_trajectory_point_num_for_debug));
157 return absl::StrCat(
158 "[\n",
159 absl::StrJoin(begin(), begin() + limit, ",\n",
161 "]\n");
162}
163
164} // namespace planning
165} // namespace apollo
static SpeedPoint ToSpeedPoint(const double s, const double t, const double v=0, const double a=0, const double da=0)
bool EvaluateByS(const double s, common::SpeedPoint *const speed_point) const
void AppendSpeedPoint(const double s, const double time, const double v, const double a, const double da)
Definition speed_data.cc:48
virtual std::string DebugString() const
bool EvaluateByTime(const double time, common::SpeedPoint *const speed_point) const
Definition speed_data.cc:60
Planning module main class.
Linear interpolation functions.
#define ACHECK(cond)
Definition log.h:80
Some util functions.
#define UNIQUE_LOCK_MULTITHREAD(mutex_type)
Definition util.h:162
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.
class register implement
Definition arena_queue.h:37
Definition future.h:29
Some string util functions.