Apollo 11.0
自动驾驶开放平台
util.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
18
19#include <algorithm>
20#include <limits>
21#include <vector>
22
27
28namespace apollo {
29namespace planning {
30namespace util {
31
34
35bool IsVehicleStateValid(const VehicleState& vehicle_state) {
36 if (std::isnan(vehicle_state.x()) || std::isnan(vehicle_state.y()) ||
37 std::isnan(vehicle_state.z()) || std::isnan(vehicle_state.heading()) ||
38 std::isnan(vehicle_state.kappa()) ||
39 std::isnan(vehicle_state.linear_velocity()) ||
40 std::isnan(vehicle_state.linear_acceleration())) {
41 return false;
42 }
43 return true;
44}
45
47 const PlanningCommand& second) {
48 if (first.has_header() && second.has_header()) {
49 const auto& first_header = first.header();
50 const auto& second_header = second.header();
51 return (first_header.sequence_num() != second_header.sequence_num() ||
52 first_header.module_name() != second_header.module_name() ||
53 first_header.timestamp_sec() != second_header.timestamp_sec());
54 }
55 return true;
56}
57
60 const double adc_front_edge_s, const double stop_line_s) {
61 double adc_speed = vehicle_state->linear_velocity();
62 const double max_adc_stop_speed = common::VehicleConfigHelper::Instance()
63 ->GetConfig()
64 .vehicle_param()
65 .max_abs_speed_when_stopped();
66 if (adc_speed < max_adc_stop_speed) {
67 return 0.0;
68 }
69
70 double stop_distance = 0;
71
72 if (stop_line_s > adc_front_edge_s) {
73 stop_distance = stop_line_s - adc_front_edge_s;
74 }
75 if (stop_distance < 1e-5) {
76 return std::numeric_limits<double>::max();
77 }
78 return (adc_speed * adc_speed) / (2 * stop_distance);
79}
80
81/*
82 * @brief: check if a stop_sign_overlap is still along reference_line
83 */
84bool CheckStopSignOnReferenceLine(const ReferenceLineInfo& reference_line_info,
85 const std::string& stop_sign_overlap_id) {
86 const std::vector<PathOverlap>& stop_sign_overlaps =
87 reference_line_info.reference_line().map_path().stop_sign_overlaps();
88 auto stop_sign_overlap_it =
89 std::find_if(stop_sign_overlaps.begin(), stop_sign_overlaps.end(),
90 [&stop_sign_overlap_id](const PathOverlap& overlap) {
91 return overlap.object_id == stop_sign_overlap_id;
92 });
93 return (stop_sign_overlap_it != stop_sign_overlaps.end());
94}
95
96/*
97 * @brief: check if a traffic_light_overlap is still along reference_line
98 */
100 const ReferenceLineInfo& reference_line_info,
101 const std::string& traffic_light_overlap_id) {
102 const std::vector<PathOverlap>& traffic_light_overlaps =
103 reference_line_info.reference_line().map_path().signal_overlaps();
104 auto traffic_light_overlap_it =
105 std::find_if(traffic_light_overlaps.begin(), traffic_light_overlaps.end(),
106 [&traffic_light_overlap_id](const PathOverlap& overlap) {
107 return overlap.object_id == traffic_light_overlap_id;
108 });
109 return (traffic_light_overlap_it != traffic_light_overlaps.end());
110}
111
112/*
113 * @brief: check if ADC is till inside a pnc-junction
114 */
115bool CheckInsideJunction(const ReferenceLineInfo& reference_line_info) {
116 const double adc_front_edge_s = reference_line_info.AdcSlBoundary().end_s();
117 const double adc_back_edge_s = reference_line_info.AdcSlBoundary().start_s();
118
119 hdmap::PathOverlap junction_overlap;
120 reference_line_info.GetJunction(adc_front_edge_s, &junction_overlap);
121 if (junction_overlap.object_id.empty()) {
122 return false;
123 }
124
125 static constexpr double kIntersectionPassDist = 2.0; // unit: m
126 const double distance_adc_pass_intersection =
127 adc_back_edge_s - junction_overlap.end_s;
128 ADEBUG << "distance_adc_pass_intersection[" << distance_adc_pass_intersection
129 << "] junction_overlap[" << junction_overlap.object_id << "] start_s["
130 << junction_overlap.start_s << "]";
131
132 return distance_adc_pass_intersection < kIntersectionPassDist;
133}
134
135/*
136 * @brief: get files at a path
137 */
138void GetFilesByPath(const boost::filesystem::path& path,
139 std::vector<std::string>* files) {
140 ACHECK(files);
141 if (!boost::filesystem::exists(path)) {
142 return;
143 }
144 if (boost::filesystem::is_regular_file(path)) {
145 AINFO << "Found record file: " << path.c_str();
146 files->push_back(path.c_str());
147 return;
148 }
149 if (boost::filesystem::is_directory(path)) {
150 for (auto& entry : boost::make_iterator_range(
151 boost::filesystem::directory_iterator(path), {})) {
152 GetFilesByPath(entry.path(), files);
153 }
154 }
155}
156
157/*
158 * @brief: get path equivalent ego width
159 */
160double CalculateEquivalentEgoWidth(const ReferenceLineInfo& reference_line_info,
161 double s, bool* is_left) {
162 const auto& vehicle_param =
163 common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param();
164 double max_kappa = 1.0 / vehicle_param.min_turn_radius();
165 double half_wb = 0.5 * vehicle_param.wheel_base();
166 double front_l = vehicle_param.front_edge_to_center() - half_wb;
167 double half_w = 0.5 * vehicle_param.width();
168 double current_heading =
169 reference_line_info.reference_line().GetReferencePoint(s).heading();
170 double heading_f = reference_line_info.reference_line()
171 .GetReferencePoint(s + front_l)
172 .heading();
173 double heading_b = reference_line_info.reference_line()
174 .GetReferencePoint(s - half_wb)
175 .heading();
176 // average kappa from front edge to center
177 double kappa_f =
178 apollo::common::math::NormalizeAngle(heading_f - current_heading) /
179 front_l;
180 // average kappa from center to rear axle
181 double kappa_b =
182 apollo::common::math::NormalizeAngle(current_heading - heading_b) /
183 half_wb;
184 *is_left = (kappa_b < 0.0);
185 // prevent devide by 0, and quit if lane bends to difference direction
186 if (kappa_f * kappa_b < 0.0 || fabs(kappa_f) < 1e-6) {
187 return half_w;
188 }
189
190 kappa_f = std::min(fabs(kappa_f), max_kappa);
191 kappa_b = std::min(fabs(kappa_b), max_kappa);
192 double theta = apollo::common::math::Vec2d(1.0, half_wb * kappa_b).Angle();
193 double sint = std::sin(theta);
194 double cost = std::cos(theta);
195 double r_f = 1.0 / kappa_f;
196 double eq_half_w =
197 apollo::common::math::Vec2d(front_l * cost - half_w * sint,
198 r_f + front_l * sint + half_w * cost)
199 .Length() -
200 r_f;
201 return std::max(eq_half_w, half_w);
202}
203
205 const apollo::hdmap::LaneInfoConstPtr lane_info, double s, bool* is_left) {
206 const auto& vehicle_param =
207 common::VehicleConfigHelper::Instance()->GetConfig().vehicle_param();
208 double max_kappa = 1.0 / vehicle_param.min_turn_radius();
209 double half_wb = 0.5 * vehicle_param.wheel_base();
210 double front_l = vehicle_param.front_edge_to_center() - half_wb;
211 double half_w = 0.5 * vehicle_param.width();
212 double current_heading = lane_info->Heading(s);
213 double heading_f = lane_info->Heading(s + front_l);
214 double heading_b = lane_info->Heading(s - half_wb);
215 // average kappa from front edge to center
216 double kappa_f =
217 apollo::common::math::NormalizeAngle(heading_f - current_heading) /
218 front_l;
219 // average kappa from center to rear axle
220 double kappa_b =
221 apollo::common::math::NormalizeAngle(current_heading - heading_b) /
222 half_wb;
223 *is_left = (kappa_b < 0.0);
224 // prevent devide by 0, and quit if lane bends to difference direction
225 if (kappa_f * kappa_b < 0.0 || fabs(kappa_f) < 1e-6) {
226 return half_w;
227 }
228
229 kappa_f = std::min(fabs(kappa_f), max_kappa);
230 kappa_b = std::min(fabs(kappa_b), max_kappa);
231 double theta = apollo::common::math::Vec2d(1.0, half_wb * kappa_b).Angle();
232 double sint = std::sin(theta);
233 double cost = std::cos(theta);
234 double r_f = 1.0 / kappa_f;
235 double eq_half_w =
236 apollo::common::math::Vec2d(front_l * cost - half_w * sint,
237 r_f + front_l * sint + half_w * cost)
238 .Length() -
239 r_f;
240 return std::max(eq_half_w, half_w);
241}
242
243bool left_arc_bound_with_heading(double delta_x, double r, double heading,
244 double* result) {
245 // calculate △L(positive or negative) with an arc with given radius, and given
246 // init_heading the circle can be written as (x-Rsin)^2 + (y+Rcos)^2 = R^2 the
247 // upper side of the circel = (sqrt(R^2 - (x-Rsin)^2) - Rcos) is what we need
248 if (delta_x > r * (1.0 + std::sin(heading)) - 1e-6) {
249 *result = std::numeric_limits<double>::lowest();
250 return false;
251 }
252
253 *result = std::sqrt(r * r - std::pow(delta_x - r * std::sin(heading), 2)) -
254 r * std::cos(heading);
255 return true;
256}
257
258bool right_arc_bound_with_heading(double delta_x, double r, double heading,
259 double* result) {
260 // calculate △L(positive or negative) with an arc with given radius, and given
261 // init_heading the circle can be written as (x+Rsin)^2 + (y-Rcos)^2 = R^2 the
262 // upper side of the circel = (Rcos - sqrt(R^2 - (x+Rsin)^2) )is what we need
263 if (delta_x > r * (1.0 - std::sin(heading)) - 1e-6) {
264 *result = std::numeric_limits<double>::max();
265 return false;
266 }
267 *result = r * std::cos(heading) -
268 std::sqrt(r * r - std::pow(delta_x + r * std::sin(heading), 2));
269 return true;
270}
271
273 double heading,
274 double kappa,
275 double* result) {
276 // calculate △L(positive or negative) with an arc with given radius, and given
277 // init_heading the circle can be written as (x-Rsin)^2 + (y+Rcos)^2 = R^2 the
278 // upper side of the circel = (sqrt(R^2 - (x-Rsin)^2) - Rcos) is what we need
279 if (heading > 0 || kappa < 0 ||
280 delta_x > r * (1.0 - std::sin(heading)) - 1e-6) {
281 *result = std::numeric_limits<double>::lowest();
282 return false;
283 }
284 if (delta_x < -r * std::sin(heading)) {
285 *result = r * std::cos(heading) -
286 std::sqrt(r * r - std::pow(delta_x - r * std::sin(heading), 2));
287 } else {
288 *result = std::sqrt(r * r - std::pow(delta_x + r * std::sin(heading), 2)) -
289 r * (2 - std::cos(heading));
290 }
291 return true;
292}
293
295 double heading,
296 double kappa,
297 double* result) {
298 // calculate △L(positive or negative) with an arc with given radius, and given
299 // init_heading the circle can be written as (x+Rsin)^2 + (y-Rcos)^2 = R^2 the
300 // upper side of the circel = (Rcos - sqrt(R^2 - (x+Rsin)^2) )is what we need
301 if (heading < 0 || kappa > 0 ||
302 delta_x > r * (1.0 - std::sin(heading)) - 1e-6) {
303 *result = std::numeric_limits<double>::max();
304 return false;
305 }
306 if (delta_x < r * std::sin(heading)) {
307 *result = std::sqrt(r * r - std::pow(delta_x - r * std::sin(heading), 2)) -
308 r * std::cos(heading);
309 } else {
310 *result = r * (2 - std::cos(heading)) -
311 std::sqrt(r * r - std::pow(delta_x - r * std::sin(heading), 2));
312 }
313 return true;
314}
315
316} // namespace util
317} // namespace planning
318} // namespace apollo
double linear_velocity() const
Get the vehicle's linear velocity.
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
double Length() const
Gets the length of the vector
Definition vec2d.cc:33
double Angle() const
Gets the angle between the vector and the positive x semi-axis
Definition vec2d.cc:37
double heading() const
Definition path.h:118
const std::vector< PathOverlap > & signal_overlaps() const
Definition path.h:301
const std::vector< PathOverlap > & stop_sign_overlaps() const
Definition path.h:307
ReferenceLineInfo holds all data for one reference line.
int GetJunction(const double s, hdmap::PathOverlap *junction_overlap) const
const ReferenceLine & reference_line() const
const SLBoundary & AdcSlBoundary() const
const hdmap::Path & map_path() const
ReferencePoint GetReferencePoint(const double s) const
Planning module main class.
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AINFO
Definition log.h:42
double NormalizeAngle(const double angle)
Normalize angle to [-PI, PI).
Definition math_utils.cc:53
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
bool left_arc_bound_with_heading(double delta_x, double r, double heading, double *result)
Definition util.cc:243
bool CheckInsideJunction(const ReferenceLineInfo &reference_line_info)
Definition util.cc:115
bool CheckStopSignOnReferenceLine(const ReferenceLineInfo &reference_line_info, const std::string &stop_sign_overlap_id)
Definition util.cc:84
bool right_arc_bound_with_heading(double delta_x, double r, double heading, double *result)
Definition util.cc:258
double CalculateEquivalentEgoWidth(const ReferenceLineInfo &reference_line_info, double s, bool *is_left)
Definition util.cc:160
bool left_arc_bound_with_heading_with_reverse_kappa(double delta_x, double r, double heading, double kappa, double *result)
Definition util.cc:272
void GetFilesByPath(const boost::filesystem::path &path, std::vector< std::string > *files)
Definition util.cc:138
bool IsVehicleStateValid(const VehicleState &vehicle_state)
Definition util.cc:35
double GetADCStopDeceleration(apollo::common::VehicleStateProvider *vehicle_state, const double adc_front_edge_s, const double stop_line_s)
Definition util.cc:58
bool IsDifferentRouting(const PlanningCommand &first, const PlanningCommand &second)
Definition util.cc:46
bool right_arc_bound_with_heading_with_reverse_kappa(double delta_x, double r, double heading, double kappa, double *result)
Definition util.cc:294
bool CheckTrafficLightOnReferenceLine(const ReferenceLineInfo &reference_line_info, const std::string &traffic_light_overlap_id)
Definition util.cc:99
class register implement
Definition arena_queue.h:37
std::string object_id
Definition path.h:95
optional apollo::common::Header header