Skip to content

Commit 17fcddd

Browse files
committed
fix(mission_planner): fix reroute interval calculation
Signed-off-by: Mamoru Sobue <mamoru.sobue@tier4.jp>
1 parent 3f21683 commit 17fcddd

File tree

2 files changed

+65
-54
lines changed

2 files changed

+65
-54
lines changed

planning/mission_planner/src/mission_planner/mission_planner.cpp

+62-52
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <algorithm>
3030
#include <array>
3131
#include <random>
32+
#include <utility>
3233

3334
namespace
3435
{
@@ -174,7 +175,7 @@ void MissionPlanner::on_map(const HADMapBin::ConstSharedPtr msg)
174175
map_ptr_ = msg;
175176
}
176177

177-
PoseStamped MissionPlanner::transform_pose(const PoseStamped & input)
178+
PoseStamped MissionPlanner::transform_pose(const PoseStamped & input) const
178179
{
179180
PoseStamped output;
180181
geometry_msgs::msg::TransformStamped transform;
@@ -728,7 +729,7 @@ void MissionPlanner::on_change_route_points(
728729
}
729730

730731
bool MissionPlanner::check_reroute_safety(
731-
const LaneletRoute & original_route, const LaneletRoute & target_route)
732+
const LaneletRoute & original_route, const LaneletRoute & target_route) const
732733
{
733734
if (original_route.segments.empty() || target_route.segments.empty() || !map_ptr_ || !odometry_) {
734735
RCLCPP_ERROR(get_logger(), "Check reroute safety failed. Route, map or odometry is not set.");
@@ -749,80 +750,86 @@ bool MissionPlanner::check_reroute_safety(
749750
return false;
750751
}
751752

752-
bool is_same = false;
753753
for (const auto & primitive : original_primitives) {
754754
const auto has_same = [&](const auto & p) { return p.id == primitive.id; };
755-
is_same = std::find_if(target_primitives.begin(), target_primitives.end(), has_same) !=
756-
target_primitives.end();
755+
const bool is_same =
756+
std::find_if(target_primitives.begin(), target_primitives.end(), has_same) !=
757+
target_primitives.end();
758+
if (is_same) {
759+
return true;
760+
}
757761
}
758-
return is_same;
762+
return false;
759763
};
760764

761765
// find idx of original primitives that matches the target primitives
762-
const auto start_idx_opt = std::invoke([&]() -> std::optional<size_t> {
763-
/*
764-
* find the index of the original route that has same idx with the front segment of the new
765-
* route
766-
*
767-
* start_idx
768-
* +-----------+-----------+-----------+-----------+-----------+
769-
* | | | | | |
770-
* +-----------+-----------+-----------+-----------+-----------+
771-
* | | | | | |
772-
* +-----------+-----------+-----------+-----------+-----------+
773-
* original original original original original
774-
* target target target
775-
*/
776-
const auto target_front_primitives = target_route.segments.front().primitives;
777-
for (size_t i = 0; i < original_route.segments.size(); ++i) {
778-
const auto & original_primitives = original_route.segments.at(i).primitives;
779-
if (hasSamePrimitives(original_primitives, target_front_primitives)) {
780-
return i;
766+
const auto start_idx_opt =
767+
std::invoke([&]() -> std::optional<std::pair<size_t /* original */, size_t /* target */>> {
768+
/*
769+
* find the index of the original route that has same idx with the front segment of the new
770+
* route
771+
*
772+
* start_idx == 2
773+
* +--------------+--------------+--------------+--------------+-------------+
774+
* | | | | | |
775+
* +--------------+--------------+--------------+--------------+-------------+
776+
* | | | | | |
777+
* +--------------+--------------+--------------+--------------+-------------+
778+
* original[0] original[1] original[2] original[3] original[4]
779+
* target[0] target[1] target[2]
780+
*/
781+
const auto target_front_primitives = target_route.segments.front().primitives;
782+
for (size_t i = 0; i < original_route.segments.size(); ++i) {
783+
const auto & original_primitives = original_route.segments.at(i).primitives;
784+
if (hasSamePrimitives(original_primitives, target_front_primitives)) {
785+
return std::make_pair(i, 0);
786+
}
781787
}
782-
}
783788

784-
/*
785-
* find the target route that has same idx with the front segment of the original route
786-
*
787-
* start_idx
788-
* +-----------+-----------+-----------+-----------+-----------+
789-
* | | | | | |
790-
* +-----------+-----------+-----------+-----------+-----------+
791-
* | | | | | |
792-
* +-----------+-----------+-----------+-----------+-----------+
793-
*                original original original
794-
* target target target target target
795-
*/
796-
const auto original_front_primitives = original_route.segments.front().primitives;
797-
for (size_t i = 0; i < target_route.segments.size(); ++i) {
798-
const auto & target_primitives = target_route.segments.at(i).primitives;
799-
if (hasSamePrimitives(target_primitives, original_front_primitives)) {
800-
return 0;
789+
/*
790+
* find the target route that has same idx with the front segment of the original route
791+
*
792+
* start_idx == 0
793+
* +--------------+--------------+--------------+--------------+-------------+
794+
* | | | | | |
795+
* +--------------+--------------+--------------+--------------+-------------+
796+
* | | | | | |
797+
* +--------------+--------------+--------------+--------------+-------------+
798+
* original[0] original[1] original[2]
799+
* target[0] target[1] target[2] target[3] target[4]
800+
*/
801+
const auto original_front_primitives = original_route.segments.front().primitives;
802+
for (size_t i = 0; i < target_route.segments.size(); ++i) {
803+
const auto & target_primitives = target_route.segments.at(i).primitives;
804+
if (hasSamePrimitives(target_primitives, original_front_primitives)) {
805+
return std::make_pair(0, i);
806+
}
801807
}
802-
}
803808

804-
return std::nullopt;
805-
});
809+
return std::nullopt;
810+
});
806811
if (!start_idx_opt.has_value()) {
807812
RCLCPP_ERROR(
808813
get_logger(), "Check reroute safety failed. Cannot find the start index of the route.");
809814
return false;
810815
}
811-
const size_t start_idx = start_idx_opt.value();
816+
const auto [start_idx, start_idx_target] = start_idx_opt.value();
812817

813818
// find last idx that matches the target primitives
814819
size_t end_idx = start_idx;
815-
for (size_t i = 1; i < target_route.segments.size(); ++i) {
820+
size_t end_idx_target = start_idx_target;
821+
for (size_t i = 1; i < target_route.segments.size() - start_idx_target; ++i) {
816822
if (start_idx + i > original_route.segments.size() - 1) {
817823
break;
818824
}
819825

820826
const auto & original_primitives = original_route.segments.at(start_idx + i).primitives;
821-
const auto & target_primitives = target_route.segments.at(i).primitives;
827+
const auto & target_primitives = target_route.segments.at(start_idx_target + i).primitives;
822828
if (!hasSamePrimitives(original_primitives, target_primitives)) {
823829
break;
824830
}
825831
end_idx = start_idx + i;
832+
end_idx_target = end_idx_target + i;
826833
}
827834

828835
// create map
@@ -870,7 +877,7 @@ bool MissionPlanner::check_reroute_safety(
870877
}
871878

872879
// check if the goal is inside of the target terminal lanelet
873-
const auto & target_end_primitives = target_route.segments.at(end_idx - start_idx).primitives;
880+
const auto & target_end_primitives = target_route.segments.at(end_idx_target).primitives;
874881
const auto & target_goal = target_route.goal_pose;
875882
for (const auto & target_end_primitive : target_end_primitives) {
876883
const auto lanelet = lanelet_map_ptr_->laneletLayer.get(target_end_primitive.id);
@@ -882,8 +889,11 @@ bool MissionPlanner::check_reroute_safety(
882889
lanelet::utils::to2D(target_goal_position).basicPoint())
883890
.length;
884891
const double target_lanelet_length = lanelet::utils::getLaneletLength2d(lanelet);
885-
const double dist = target_lanelet_length - dist_to_goal;
886-
accumulated_length = std::max(accumulated_length - dist, 0.0);
892+
// NOTE: `accumulated_length` here contains the length of the entire target_end_primitive, so
893+
// the remaining distance from the goal to the end of the target_end_primitive needs to be
894+
// subtracted.
895+
const double remaining_dist = target_lanelet_length - dist_to_goal;
896+
accumulated_length = std::max(accumulated_length - remaining_dist, 0.0);
887897
break;
888898
}
889899
}

planning/mission_planner/src/mission_planner/mission_planner.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class MissionPlanner : public rclcpp::Node
7171
std::string map_frame_;
7272
tf2_ros::Buffer tf_buffer_;
7373
tf2_ros::TransformListener tf_listener_;
74-
PoseStamped transform_pose(const PoseStamped & input);
74+
PoseStamped transform_pose(const PoseStamped & input) const;
7575

7676
rclcpp::Subscription<Odometry>::SharedPtr sub_odometry_;
7777
rclcpp::Subscription<HADMapBin>::SharedPtr sub_vector_map_;
@@ -144,7 +144,8 @@ class MissionPlanner : public rclcpp::Node
144144

145145
double reroute_time_threshold_{10.0};
146146
double minimum_reroute_length_{30.0};
147-
bool check_reroute_safety(const LaneletRoute & original_route, const LaneletRoute & target_route);
147+
bool check_reroute_safety(
148+
const LaneletRoute & original_route, const LaneletRoute & target_route) const;
148149

149150
std::shared_ptr<LaneletRoute> normal_route_{nullptr};
150151
std::shared_ptr<LaneletRoute> mrm_route_{nullptr};

0 commit comments

Comments
 (0)