Skip to content

Commit 89d137a

Browse files
authored
refactor(motion_velocity_smoother): remove unused optional (#1906)
* remove unused optional Signed-off-by: Takamasa Horibe <horibe.takamasa@gmail.com> * pre-commit Signed-off-by: Takamasa Horibe <horibe.takamasa@gmail.com> Signed-off-by: Takamasa Horibe <horibe.takamasa@gmail.com>
1 parent ef7dcda commit 89d137a

File tree

13 files changed

+77
-100
lines changed

13 files changed

+77
-100
lines changed

planning/behavior_velocity_planner/include/utilization/trajectory_utils.hpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,20 @@ inline bool smoothPath(
108108
*traj_lateral_acc_filtered, v0, current_pose, planner_data->ego_nearest_dist_threshold,
109109
planner_data->ego_nearest_yaw_threshold);
110110
const size_t traj_resampled_closest = motion_utils::findFirstNearestIndexWithSoftConstraints(
111-
*traj_resampled, current_pose, planner_data->ego_nearest_dist_threshold,
111+
traj_resampled, current_pose, planner_data->ego_nearest_dist_threshold,
112112
planner_data->ego_nearest_yaw_threshold);
113113
std::vector<TrajectoryPoints> debug_trajectories;
114114
// Clip trajectory from closest point
115115
TrajectoryPoints clipped;
116116
TrajectoryPoints traj_smoothed;
117117
clipped.insert(
118-
clipped.end(), traj_resampled->begin() + traj_resampled_closest, traj_resampled->end());
118+
clipped.end(), traj_resampled.begin() + traj_resampled_closest, traj_resampled.end());
119119
if (!smoother->apply(v0, a0, clipped, traj_smoothed, debug_trajectories)) {
120120
std::cerr << "[behavior_velocity][trajectory_utils]: failed to smooth" << std::endl;
121121
return false;
122122
}
123123
traj_smoothed.insert(
124-
traj_smoothed.begin(), traj_resampled->begin(),
125-
traj_resampled->begin() + traj_resampled_closest);
124+
traj_smoothed.begin(), traj_resampled.begin(), traj_resampled.begin() + traj_resampled_closest);
126125

127126
out_path = convertTrajectoryPointsToPath(traj_smoothed);
128127
return true;

planning/motion_velocity_smoother/include/motion_velocity_smoother/resample.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ struct ResampleParam
4343
double sparse_min_interval_distance; // minimum points-interval length for sparse sampling [m]
4444
};
4545

46-
boost::optional<TrajectoryPoints> resampleTrajectory(
46+
TrajectoryPoints resampleTrajectory(
4747
const TrajectoryPoints & input, const double v_current,
4848
const geometry_msgs::msg::Pose & current_pose, const double nearest_dist_threshold,
4949
const double nearest_yaw_threshold, const ResampleParam & param, const bool use_zoh_for_v = true);
5050

51-
boost::optional<TrajectoryPoints> resampleTrajectory(
51+
TrajectoryPoints resampleTrajectory(
5252
const TrajectoryPoints & input, const geometry_msgs::msg::Pose & current_pose,
5353
const double nearest_dist_threshold, const double nearest_yaw_threshold,
5454
const ResampleParam & param, const double nominal_ds, const bool use_zoh_for_v = true);

planning/motion_velocity_smoother/include/motion_velocity_smoother/smoother/analytical_jerk_constrained_smoother/analytical_jerk_constrained_smoother.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class AnalyticalJerkConstrainedSmoother : public SmootherBase
7171
const double initial_vel, const double initial_acc, const TrajectoryPoints & input,
7272
TrajectoryPoints & output, std::vector<TrajectoryPoints> & debug_trajectories) override;
7373

74-
boost::optional<TrajectoryPoints> resampleTrajectory(
74+
TrajectoryPoints resampleTrajectory(
7575
const TrajectoryPoints & input, [[maybe_unused]] const double v0,
7676
[[maybe_unused]] const geometry_msgs::msg::Pose & current_pose,
7777
[[maybe_unused]] const double nearest_dist_threshold,

planning/motion_velocity_smoother/include/motion_velocity_smoother/smoother/jerk_filtered_smoother.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class JerkFilteredSmoother : public SmootherBase
4646
const double initial_vel, const double initial_acc, const TrajectoryPoints & input,
4747
TrajectoryPoints & output, std::vector<TrajectoryPoints> & debug_trajectories) override;
4848

49-
boost::optional<TrajectoryPoints> resampleTrajectory(
49+
TrajectoryPoints resampleTrajectory(
5050
const TrajectoryPoints & input, [[maybe_unused]] const double v0,
5151
const geometry_msgs::msg::Pose & current_pose, const double nearest_dist_threshold,
5252
const double nearest_yaw_threshold) const override;

planning/motion_velocity_smoother/include/motion_velocity_smoother/smoother/l2_pseudo_jerk_smoother.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class L2PseudoJerkSmoother : public SmootherBase
4444
const double initial_vel, const double initial_acc, const TrajectoryPoints & input,
4545
TrajectoryPoints & output, std::vector<TrajectoryPoints> & debug_trajectories) override;
4646

47-
boost::optional<TrajectoryPoints> resampleTrajectory(
47+
TrajectoryPoints resampleTrajectory(
4848
const TrajectoryPoints & input, const double v0, const geometry_msgs::msg::Pose & current_pose,
4949
const double nearest_dist_threshold, const double nearest_yaw_threshold) const override;
5050

planning/motion_velocity_smoother/include/motion_velocity_smoother/smoother/linf_pseudo_jerk_smoother.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class LinfPseudoJerkSmoother : public SmootherBase
4444
const double initial_vel, const double initial_acc, const TrajectoryPoints & input,
4545
TrajectoryPoints & output, std::vector<TrajectoryPoints> & debug_trajectories) override;
4646

47-
boost::optional<TrajectoryPoints> resampleTrajectory(
47+
TrajectoryPoints resampleTrajectory(
4848
const TrajectoryPoints & input, const double v0, const geometry_msgs::msg::Pose & current_pose,
4949
const double nearest_dist_threshold, const double nearest_yaw_threshold) const override;
5050

planning/motion_velocity_smoother/include/motion_velocity_smoother/smoother/smoother_base.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SmootherBase
6565
const double initial_vel, const double initial_acc, const TrajectoryPoints & input,
6666
TrajectoryPoints & output, std::vector<TrajectoryPoints> & debug_trajectories) = 0;
6767

68-
virtual boost::optional<TrajectoryPoints> resampleTrajectory(
68+
virtual TrajectoryPoints resampleTrajectory(
6969
const TrajectoryPoints & input, const double v0, const geometry_msgs::msg::Pose & current_pose,
7070
const double nearest_dist_threshold, const double nearest_yaw_threshold) const = 0;
7171

planning/motion_velocity_smoother/src/motion_velocity_smoother_node.cpp

+14-22
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,10 @@ void MotionVelocitySmootherNode::onCurrentTrajectory(const Trajectory::ConstShar
401401
output, current_odometry_ptr_->twist.twist.linear.x, current_pose_ptr_->pose,
402402
node_param_.ego_nearest_dist_threshold, node_param_.ego_nearest_yaw_threshold,
403403
node_param_.post_resample_param, false);
404-
if (!output_resampled) {
405-
RCLCPP_WARN(get_logger(), "Failed to get the resampled output trajectory");
406-
return;
407-
}
408404

409405
// Set 0 at the end of the trajectory
410-
if (!output_resampled->empty()) {
411-
output_resampled->back().longitudinal_velocity_mps = 0.0;
406+
if (!output_resampled.empty()) {
407+
output_resampled.back().longitudinal_velocity_mps = 0.0;
412408
}
413409

414410
// update previous step infomation
@@ -417,11 +413,11 @@ void MotionVelocitySmootherNode::onCurrentTrajectory(const Trajectory::ConstShar
417413
// for reverse velocity
418414
// NOTE: this process must be in the end of the process
419415
if (is_reverse_) {
420-
flipVelocity(*output_resampled);
416+
flipVelocity(output_resampled);
421417
}
422418

423419
// publish message
424-
publishTrajectory(*output_resampled);
420+
publishTrajectory(output_resampled);
425421

426422
// publish debug message
427423
publishStopDistance(output);
@@ -523,24 +519,21 @@ bool MotionVelocitySmootherNode::smoothVelocity(
523519
*traj_steering_rate_limited, current_odometry_ptr_->twist.twist.linear.x,
524520
current_pose_ptr_->pose, node_param_.ego_nearest_dist_threshold,
525521
node_param_.ego_nearest_yaw_threshold);
526-
if (!traj_resampled) {
527-
RCLCPP_WARN(get_logger(), "Fail to do resampling before the optimization");
528-
return false;
529-
}
530-
const size_t traj_resampled_closest = findNearestIndexFromEgo(*traj_resampled);
522+
523+
const size_t traj_resampled_closest = findNearestIndexFromEgo(traj_resampled);
531524

532525
// Set 0[m/s] in the terminal point
533-
if (!traj_resampled->empty()) {
534-
traj_resampled->back().longitudinal_velocity_mps = 0.0;
526+
if (!traj_resampled.empty()) {
527+
traj_resampled.back().longitudinal_velocity_mps = 0.0;
535528
}
536529

537530
// Publish Closest Resample Trajectory Velocity
538-
publishClosestVelocity(*traj_resampled, current_pose_ptr_->pose, debug_closest_max_velocity_);
531+
publishClosestVelocity(traj_resampled, current_pose_ptr_->pose, debug_closest_max_velocity_);
539532

540533
// Clip trajectory from closest point
541534
TrajectoryPoints clipped;
542535
clipped.insert(
543-
clipped.end(), traj_resampled->begin() + traj_resampled_closest, traj_resampled->end());
536+
clipped.end(), traj_resampled.begin() + traj_resampled_closest, traj_resampled.end());
544537

545538
std::vector<TrajectoryPoints> debug_trajectories;
546539
if (!smoother_->apply(
@@ -552,8 +545,7 @@ bool MotionVelocitySmootherNode::smoothVelocity(
552545
overwriteStopPoint(clipped, traj_smoothed);
553546

554547
traj_smoothed.insert(
555-
traj_smoothed.begin(), traj_resampled->begin(),
556-
traj_resampled->begin() + traj_resampled_closest);
548+
traj_smoothed.begin(), traj_resampled.begin(), traj_resampled.begin() + traj_resampled_closest);
557549

558550
// For the endpoint of the trajectory
559551
if (!traj_smoothed.empty()) {
@@ -575,7 +567,7 @@ bool MotionVelocitySmootherNode::smoothVelocity(
575567
pub_trajectory_latacc_filtered_->publish(toTrajectoryMsg(tmp));
576568
}
577569
{
578-
auto tmp = *traj_resampled;
570+
auto tmp = traj_resampled;
579571
if (is_reverse_) flipVelocity(tmp);
580572
pub_trajectory_resampled_->publish(toTrajectoryMsg(tmp));
581573
}
@@ -588,8 +580,8 @@ bool MotionVelocitySmootherNode::smoothVelocity(
588580
if (!debug_trajectories.empty()) {
589581
for (auto & debug_trajectory : debug_trajectories) {
590582
debug_trajectory.insert(
591-
debug_trajectory.begin(), traj_resampled->begin(),
592-
traj_resampled->begin() + traj_resampled_closest);
583+
debug_trajectory.begin(), traj_resampled.begin(),
584+
traj_resampled.begin() + traj_resampled_closest);
593585
for (size_t i = 0; i < traj_resampled_closest; ++i) {
594586
debug_trajectory.at(i).longitudinal_velocity_mps =
595587
debug_trajectory.at(traj_resampled_closest).longitudinal_velocity_mps;

planning/motion_velocity_smoother/src/resample.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace motion_velocity_smoother
2424
{
2525
namespace resampling
2626
{
27-
boost::optional<TrajectoryPoints> resampleTrajectory(
27+
TrajectoryPoints resampleTrajectory(
2828
const TrajectoryPoints & input, const double v_current,
2929
const geometry_msgs::msg::Pose & current_pose, const double nearest_dist_threshold,
3030
const double nearest_yaw_threshold, const ResampleParam & param, const bool use_zoh_for_v)
@@ -142,7 +142,7 @@ boost::optional<TrajectoryPoints> resampleTrajectory(
142142
return output;
143143
}
144144

145-
boost::optional<TrajectoryPoints> resampleTrajectory(
145+
TrajectoryPoints resampleTrajectory(
146146
const TrajectoryPoints & input, const geometry_msgs::msg::Pose & current_pose,
147147
const double nearest_dist_threshold, const double nearest_yaw_threshold,
148148
const ResampleParam & param, const double nominal_ds, const bool use_zoh_for_v)
@@ -187,7 +187,7 @@ boost::optional<TrajectoryPoints> resampleTrajectory(
187187
double dist_i{0.0};
188188
bool is_zero_point_included{false};
189189
bool is_endpoint_included{false};
190-
while (true) {
190+
while (rclcpp::ok()) {
191191
double ds = nominal_ds;
192192
if (start_stop_arclength_value <= dist_i && dist_i <= stop_arclength_value) {
193193
// Dense sampling before the stop point

planning/motion_velocity_smoother/src/smoother/analytical_jerk_constrained_smoother/analytical_jerk_constrained_smoother.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ bool AnalyticalJerkConstrainedSmoother::apply(
231231
return true;
232232
}
233233

234-
boost::optional<TrajectoryPoints> AnalyticalJerkConstrainedSmoother::resampleTrajectory(
234+
TrajectoryPoints AnalyticalJerkConstrainedSmoother::resampleTrajectory(
235235
const TrajectoryPoints & input, [[maybe_unused]] const double v0,
236236
[[maybe_unused]] const geometry_msgs::msg::Pose & current_pose,
237237
[[maybe_unused]] const double nearest_dist_threshold,
@@ -240,7 +240,7 @@ boost::optional<TrajectoryPoints> AnalyticalJerkConstrainedSmoother::resampleTra
240240
TrajectoryPoints output;
241241
if (input.empty()) {
242242
RCLCPP_WARN(logger_, "Input trajectory is empty");
243-
return {};
243+
return input;
244244
}
245245

246246
const double ds = 1.0 / static_cast<double>(smoother_param_.resample.num_resample);
@@ -274,7 +274,7 @@ boost::optional<TrajectoryPoints> AnalyticalJerkConstrainedSmoother::resampleTra
274274

275275
output.push_back(input.back());
276276

277-
return boost::optional<TrajectoryPoints>(output);
277+
return output;
278278
}
279279

280280
boost::optional<TrajectoryPoints> AnalyticalJerkConstrainedSmoother::applyLateralAccelerationFilter(

planning/motion_velocity_smoother/src/smoother/jerk_filtered_smoother.cpp

+45-59
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <numeric>
2525
#include <vector>
2626

27+
#define VERBOSE_TRAJECTORY_VELOCITY false
28+
2729
namespace motion_velocity_smoother
2830
{
2931
JerkFilteredSmoother::JerkFilteredSmoother(rclcpp::Node & node) : SmootherBase(node)
@@ -71,8 +73,6 @@ bool JerkFilteredSmoother::apply(
7173
return true;
7274
}
7375

74-
bool TMP_SHOW_DEBUG_INFO = false;
75-
7676
const auto ts = std::chrono::system_clock::now();
7777

7878
const double a_max = base_param_.max_accel;
@@ -93,28 +93,29 @@ bool JerkFilteredSmoother::apply(
9393
const auto filtered =
9494
mergeFilteredTrajectory(v0, a0, a_min, j_min, forward_filtered, backward_filtered);
9595

96-
// Set debug trajectories
97-
debug_trajectories.resize(3);
98-
debug_trajectories[0] = forward_filtered;
99-
debug_trajectories[1] = backward_filtered;
100-
debug_trajectories[2] = filtered;
101-
10296
// Resample TrajectoryPoints for Optimization
10397
// TODO(planning/control team) deal with overlapped lanes with the same direction
10498
const auto initial_traj_pose = filtered.front().pose;
105-
auto opt_resampled_trajectory = resampling::resampleTrajectory(
106-
filtered, v0, initial_traj_pose, std::numeric_limits<double>::max(),
107-
std::numeric_limits<double>::max(), base_param_.resample_param);
10899

109-
if (!opt_resampled_trajectory) {
110-
RCLCPP_WARN(logger_, "Resample failed!");
111-
return false;
112-
}
100+
const auto resample = [&](const auto & trajectory) {
101+
return resampling::resampleTrajectory(
102+
trajectory, v0, initial_traj_pose, std::numeric_limits<double>::max(),
103+
std::numeric_limits<double>::max(), base_param_.resample_param);
104+
};
105+
106+
auto opt_resampled_trajectory = resample(filtered);
107+
108+
// Set debug trajectories
109+
debug_trajectories.resize(3);
110+
debug_trajectories[0] = resample(forward_filtered);
111+
debug_trajectories[1] = resample(backward_filtered);
112+
debug_trajectories[2] = resample(filtered);
113+
113114
// Ensure terminal velocity is zero
114-
opt_resampled_trajectory->back().longitudinal_velocity_mps = 0.0;
115+
opt_resampled_trajectory.back().longitudinal_velocity_mps = 0.0;
115116

116117
// If Resampled Size is too small, we don't do optimization
117-
if (opt_resampled_trajectory->size() == 1) {
118+
if (opt_resampled_trajectory.size() == 1) {
118119
// No need to do optimization
119120
output.front().longitudinal_velocity_mps = v0;
120121
output.front().acceleration_mps2 = a0;
@@ -127,7 +128,7 @@ bool JerkFilteredSmoother::apply(
127128
// to avoid getting 0 as a stop point, search zero velocity index from 1.
128129
// the size of the resampled trajectory must not be less than 2.
129130
const auto zero_vel_id = motion_utils::searchZeroVelocityIndex(
130-
*opt_resampled_trajectory, 1, opt_resampled_trajectory->size());
131+
opt_resampled_trajectory, 1, opt_resampled_trajectory.size());
131132

132133
if (!zero_vel_id) {
133134
RCLCPP_WARN(logger_, "opt_resampled_trajectory must have stop point.");
@@ -137,14 +138,14 @@ bool JerkFilteredSmoother::apply(
137138
// Clip trajectory from 0 to zero_vel_id (the size becomes zero_vel_id_ + 1)
138139
const size_t N = *zero_vel_id + 1;
139140

140-
output = *opt_resampled_trajectory;
141+
output = opt_resampled_trajectory;
141142

142143
const std::vector<double> interval_dist_arr =
143-
trajectory_utils::calcTrajectoryIntervalDistance(*opt_resampled_trajectory);
144+
trajectory_utils::calcTrajectoryIntervalDistance(opt_resampled_trajectory);
144145

145146
std::vector<double> v_max_arr(N, 0.0);
146147
for (size_t i = 0; i < N; ++i) {
147-
v_max_arr.at(i) = opt_resampled_trajectory->at(i).longitudinal_velocity_mps;
148+
v_max_arr.at(i) = opt_resampled_trajectory.at(i).longitudinal_velocity_mps;
148149
}
149150

150151
/*
@@ -306,44 +307,29 @@ bool JerkFilteredSmoother::apply(
306307

307308
qp_solver_.logUnsolvedStatus("[motion_velocity_smoother]");
308309

309-
if (TMP_SHOW_DEBUG_INFO) {
310-
// jerk calculation
311-
std::vector<double> p_jerk, jerk_converted, jerk_ideal;
312-
for (size_t i = 0; i < input.size() - 1; ++i) {
313-
double v_ref0 = filtered.at(i).longitudinal_velocity_mps;
314-
double v0 = output.at(i).longitudinal_velocity_mps;
315-
double a0 = output.at(i).acceleration_mps2;
316-
double a1 = output.at(i + 1).acceleration_mps2;
317-
double ds = interval_dist_arr.at(i);
318-
p_jerk.push_back((a1 - a0) / ds);
319-
jerk_converted.push_back((a1 - a0) / ds * v_ref0);
320-
jerk_ideal.push_back((a1 - a0) / ds * v0);
321-
}
322-
p_jerk.push_back(0.0);
323-
jerk_converted.push_back(0.0);
324-
jerk_ideal.push_back(0.0);
325-
326-
// print
327-
size_t i = 0;
328-
auto getVx = [&i](const TrajectoryPoints & trajectory) {
329-
return trajectory.at(i).longitudinal_velocity_mps;
330-
};
331-
auto getAx = [&i](const TrajectoryPoints & trajectory) {
332-
return trajectory.at(i).acceleration_mps2;
333-
};
334-
printf("v0 = %.3f, a0 = %.3f\n", v0, a0);
335-
for (; i < input.size(); ++i) {
336-
double gamma = optval.at(IDX_GAMMA0 + i);
337-
printf(
338-
"i = %lu, input: %.3f, filt_f: (%.3f, %.3f), filt_b: (%.3f, %.3f), filt_fb: (%.3f, %.3f), "
339-
"opt: (%.3f, %f), p_jerk = %.3f, p_jerk*v_ref: %.3f (min: %.3f, mac: %.3f), p_jerk*v_opt "
340-
"(to "
341-
"check): %.3f, gamma: %.3f\n",
342-
i, getVx(input), getVx(forward_filtered), getAx(forward_filtered), getVx(backward_filtered),
343-
getAx(backward_filtered), getVx(filtered), getAx(filtered), getVx(output), getAx(output),
344-
p_jerk.at(i), jerk_converted.at(i), j_min, j_max, jerk_ideal.at(i), gamma);
310+
const int status_polish = std::get<2>(result);
311+
if (status_polish != 1) {
312+
const auto msg = status_polish == 0 ? "unperformed"
313+
: status_polish == -1 ? "unsuccessful"
314+
: "unknown";
315+
RCLCPP_WARN(logger_, "osqp polish process failed : %s. The result may be inaccurate", msg);
316+
}
317+
318+
if (VERBOSE_TRAJECTORY_VELOCITY) {
319+
const auto s_output = trajectory_utils::calcArclengthArray(output);
320+
321+
std::cerr << "\n\n" << std::endl;
322+
for (size_t i = 0; i < N; ++i) {
323+
const auto v_opt = output.at(i).longitudinal_velocity_mps;
324+
const auto a_opt = output.at(i).acceleration_mps2;
325+
const auto ds = i < interval_dist_arr.size() ? interval_dist_arr.at(i) : 0.0;
326+
const auto v_rs = i < opt_resampled_trajectory.size()
327+
? opt_resampled_trajectory.at(i).longitudinal_velocity_mps
328+
: 0.0;
329+
RCLCPP_INFO(
330+
logger_, "i = %4lu | s: %5f | ds: %5f | rs: %9f | op_v: %10f | op_a: %10f |", i,
331+
s_output.at(i), ds, v_rs, v_opt, a_opt);
345332
}
346-
printf("\n");
347333
}
348334

349335
return true;
@@ -467,7 +453,7 @@ TrajectoryPoints JerkFilteredSmoother::mergeFilteredTrajectory(
467453
return merged;
468454
}
469455

470-
boost::optional<TrajectoryPoints> JerkFilteredSmoother::resampleTrajectory(
456+
TrajectoryPoints JerkFilteredSmoother::resampleTrajectory(
471457
const TrajectoryPoints & input, [[maybe_unused]] const double v0,
472458
const geometry_msgs::msg::Pose & current_pose, const double nearest_dist_threshold,
473459
const double nearest_yaw_threshold) const

0 commit comments

Comments
 (0)