forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathresample.cpp
270 lines (234 loc) · 10.2 KB
/
resample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2021 Tier IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "autoware/velocity_smoother/resample.hpp"
#include "autoware/motion_utils/resample/resample.hpp"
#include "autoware/motion_utils/trajectory/conversion.hpp"
#include "autoware/motion_utils/trajectory/trajectory.hpp"
#include "autoware/universe_utils/geometry/geometry.hpp"
#include "autoware/velocity_smoother/trajectory_utils.hpp"
#include <algorithm>
#include <vector>
namespace autoware::velocity_smoother
{
namespace resampling
{
TrajectoryPoints resampleTrajectory(
const TrajectoryPoints & input, const double v_current,
const geometry_msgs::msg::Pose & current_pose, const double nearest_dist_threshold,
const double nearest_yaw_threshold, const ResampleParam & param, const bool use_zoh_for_v)
{
// Arc length from the initial point to the closest point
const size_t current_seg_idx =
autoware::motion_utils::findFirstNearestSegmentIndexWithSoftConstraints(
input, current_pose, nearest_dist_threshold, nearest_yaw_threshold);
const double negative_front_arclength_value = autoware::motion_utils::calcSignedArcLength(
input, current_pose.position, current_seg_idx, input.at(0).pose.position, 0);
const auto front_arclength_value = std::fabs(negative_front_arclength_value);
const auto dist_to_closest_stop_point =
autoware::motion_utils::calcDistanceToForwardStopPoint(input, current_pose);
// Get the resample size from the closest point
const double trajectory_length = autoware::motion_utils::calcArcLength(input);
const double Nt = param.resample_time / std::max(param.dense_resample_dt, 0.001);
const double ds_nominal =
std::max(v_current * param.dense_resample_dt, param.dense_min_interval_distance);
const double Ns = param.min_trajectory_length / std::max(ds_nominal, 0.001);
const double N = std::max(Nt, Ns);
std::vector<double> out_arclength;
// Step1. Resample front trajectory
constexpr double front_ds = 0.1;
for (double ds = 0.0; ds <= front_arclength_value; ds += front_ds) {
out_arclength.push_back(ds);
}
if (std::fabs(out_arclength.back() - front_arclength_value) < 1e-3) {
out_arclength.back() = front_arclength_value;
} else {
out_arclength.push_back(front_arclength_value);
}
// Step2. Resample behind trajectory
double dist_i = 0.0;
bool is_zero_point_included = false;
bool is_endpoint_included = false;
for (size_t i = 1; static_cast<double>(i) <= N; ++i) {
double ds = ds_nominal;
if (i > Nt) {
// if the planning time is not enough to see the desired distance,
// change the interval distance to see far.
ds = std::max(param.sparse_min_interval_distance, param.sparse_resample_dt * v_current);
}
dist_i += ds;
// Check if the distance is longer than max_trajectory_length
if (dist_i > param.max_trajectory_length) {
break; // distance is over max.
}
// Check if the distance is longer than input arclength
if (dist_i + front_arclength_value >= trajectory_length) {
is_endpoint_included = true; // distance is over input endpoint.
break;
}
// Check if the distance is longer than minimum_trajectory_length
if (i > Nt && dist_i >= param.min_trajectory_length) {
if (
std::fabs(out_arclength.back() - (param.min_trajectory_length + front_arclength_value)) <
1e-3) {
out_arclength.back() = param.min_trajectory_length + front_arclength_value;
} else {
out_arclength.push_back(param.min_trajectory_length + front_arclength_value);
}
break;
}
// Handle Close Stop Point
if (
dist_to_closest_stop_point && dist_i > *dist_to_closest_stop_point &&
!is_zero_point_included) {
if (std::fabs(dist_i - *dist_to_closest_stop_point) > 1e-3) {
// dist_i is much bigger than zero_vel_arclength_value
if (
!out_arclength.empty() &&
std::fabs(out_arclength.back() - (*dist_to_closest_stop_point + front_arclength_value)) <
1e-3) {
out_arclength.back() = *dist_to_closest_stop_point + front_arclength_value;
} else {
out_arclength.push_back(*dist_to_closest_stop_point + front_arclength_value);
}
} else {
// dist_i is close to the zero_vel_arclength_value
dist_i = *dist_to_closest_stop_point;
}
is_zero_point_included = true;
}
out_arclength.push_back(dist_i + front_arclength_value);
}
if (input.size() < 2 || out_arclength.size() < 2 || trajectory_length < out_arclength.back()) {
return input;
}
const auto output_traj = autoware::motion_utils::resampleTrajectory(
autoware::motion_utils::convertToTrajectory(input), out_arclength, false, true, use_zoh_for_v);
auto output = autoware::motion_utils::convertToTrajectoryPointArray(output_traj);
// add end point directly to consider the endpoint velocity.
if (is_endpoint_included) {
constexpr double ep_dist = 1.0E-3;
if (autoware::universe_utils::calcDistance2d(output.back(), input.back()) < ep_dist) {
output.back() = input.back();
} else {
output.push_back(input.back());
}
}
return output;
}
TrajectoryPoints resampleTrajectory(
const TrajectoryPoints & input, const geometry_msgs::msg::Pose & current_pose,
const double nearest_dist_threshold, const double nearest_yaw_threshold,
const ResampleParam & param, const double nominal_ds, const bool use_zoh_for_v)
{
// input arclength
const double trajectory_length = autoware::motion_utils::calcArcLength(input);
const auto dist_to_closest_stop_point =
autoware::motion_utils::calcDistanceToForwardStopPoint(input, current_pose);
// distance to stop point
double stop_arclength_value = param.max_trajectory_length;
if (dist_to_closest_stop_point) {
stop_arclength_value = std::min(*dist_to_closest_stop_point, stop_arclength_value);
}
if (param.min_trajectory_length < stop_arclength_value) {
stop_arclength_value = param.min_trajectory_length;
}
// Do dense resampling before the stop line(3[m] ahead of the stop line)
const double start_stop_arclength_value = std::max(stop_arclength_value - 3.0, 0.0);
std::vector<double> out_arclength;
// Step1. Resample front trajectory
// Arc length from the initial point to the closest point
const size_t current_seg_idx =
autoware::motion_utils::findFirstNearestSegmentIndexWithSoftConstraints(
input, current_pose, nearest_dist_threshold, nearest_yaw_threshold);
const double negative_front_arclength_value = autoware::motion_utils::calcSignedArcLength(
input, current_pose.position, current_seg_idx, input.at(0).pose.position,
static_cast<size_t>(0));
const auto front_arclength_value = std::fabs(negative_front_arclength_value);
for (double s = 0.0; s <= front_arclength_value; s += nominal_ds) {
out_arclength.push_back(s);
}
if (std::fabs(out_arclength.back() - front_arclength_value) < 1e-3) {
out_arclength.back() = front_arclength_value;
} else {
out_arclength.push_back(front_arclength_value);
}
// Step2. Resample behind trajectory
double dist_i{0.0};
bool is_zero_point_included{false};
bool is_endpoint_included{false};
while (rclcpp::ok()) {
double ds = nominal_ds;
if (start_stop_arclength_value <= dist_i && dist_i <= stop_arclength_value) {
// Dense sampling before the stop point
ds = 0.01;
}
dist_i += ds;
// Check if the distance is longer than max_trajectory_length
if (dist_i > param.max_trajectory_length) {
break; // distance is over max.
}
// Check if the distance is longer than input arclength
if (dist_i + front_arclength_value >= trajectory_length) {
is_endpoint_included = true; // distance is over input endpoint.
break;
}
// Check if the distance is longer than minimum_trajectory_length
if (dist_i >= param.min_trajectory_length) {
if (
std::fabs(out_arclength.back() - (param.min_trajectory_length + front_arclength_value)) <
1e-3) {
out_arclength.back() = param.min_trajectory_length + front_arclength_value;
} else {
out_arclength.push_back(param.min_trajectory_length + front_arclength_value);
}
break;
}
// Handle Close Stop Point
if (dist_i > stop_arclength_value && !is_zero_point_included) {
if (std::fabs(dist_i - stop_arclength_value) > 1e-3) {
// dist_i is much bigger than zero_vel_arclength_value
if (
!out_arclength.empty() &&
std::fabs(out_arclength.back() - (stop_arclength_value + front_arclength_value)) < 1e-3) {
out_arclength.back() = stop_arclength_value + front_arclength_value;
} else {
out_arclength.push_back(stop_arclength_value + front_arclength_value);
}
} else {
// dist_i is close to the zero_vel_arclength_value
dist_i = stop_arclength_value;
}
is_zero_point_included = true;
}
out_arclength.push_back(dist_i + front_arclength_value);
}
if (input.size() < 2 || out_arclength.size() < 2 || trajectory_length < out_arclength.back()) {
return input;
}
const auto output_traj = autoware::motion_utils::resampleTrajectory(
autoware::motion_utils::convertToTrajectory(input), out_arclength, false, true, use_zoh_for_v);
auto output = autoware::motion_utils::convertToTrajectoryPointArray(output_traj);
// add end point directly to consider the endpoint velocity.
if (is_endpoint_included) {
constexpr double ep_dist = 1.0E-3;
if (autoware::universe_utils::calcDistance2d(output.back(), input.back()) < ep_dist) {
output.back() = input.back();
} else {
output.push_back(input.back());
}
}
return output;
}
} // namespace resampling
} // namespace autoware::velocity_smoother