15 const std::vector<custom_interfaces::msg::PathPoint> &pathpoint_array,
const Position& position) {
16 double min_distance = 1e9;
17 double closest_point_velocity = 0;
20 int closest_point_id = -1;
21 for (
size_t i = 1; i < pathpoint_array.size(); i++) {
22 aux_point =
Position(pathpoint_array[i].x, pathpoint_array[i].y);
24 if (distance < min_distance) {
25 min_distance = distance;
26 closest_point = aux_point;
27 closest_point_id =
static_cast<int>(i);
28 closest_point_velocity = pathpoint_array[i].v;
31 return std::make_tuple(closest_point, closest_point_id, closest_point_velocity);
36 const std::vector<custom_interfaces::msg::PathPoint> &pathpoint_array,
39 if (pathpoint_array.empty()) {
40 RCLCPP_DEBUG(rclcpp::get_logger(
"control"),
"Empty path");
41 return std::make_tuple(
Position(), 0.0,
true);
45 closest_point_id = std::clamp(closest_point_id, 0,
static_cast<int>(pathpoint_array.size()) - 1);
49 for (
size_t i = 0; i < pathpoint_array.size(); i++) {
50 size_t index_a = (
static_cast<size_t>(closest_point_id) + i) % pathpoint_array.size();
51 size_t index_b = (
static_cast<size_t>(closest_point_id) + i + 1) % pathpoint_array.size();
54 if (index_b < index_a) {
55 Position first_point(pathpoint_array.front().x, pathpoint_array.front().y);
56 Position last_point(pathpoint_array.back().x, pathpoint_array.back().y);
61 if (start_end_distance > last_to_first_max_dist) {
62 if (closest_point_id == 1) {
64 const auto &cp = pathpoint_array[
static_cast<size_t>(closest_point_id)];
65 return std::make_tuple(
Position(cp.x, cp.y), cp.v,
false);
67 RCLCPP_DEBUG(rclcpp::get_logger(
"control"),
68 "Lookahead extends beyond path end and it is not a closed track, using last "
69 "point of the path as lookahead point");
70 return std::make_tuple(
Position(pathpoint_array.back().x, pathpoint_array.back().y),
71 pathpoint_array.back().v,
false);
76 auto point_a =
Position(pathpoint_array[index_a].x, pathpoint_array[index_a].y);
77 auto point_b =
Position(pathpoint_array[index_b].x, pathpoint_array[index_b].y);
88 if (point_a.x == point_b.x) {
89 RCLCPP_DEBUG(rclcpp::get_logger(
"control"),
"Vertical line!!");
91 double delta = lookahead_distance * lookahead_distance - std::pow(result_x - rear_axis_point.
x, 2);
95 double y1 = rear_axis_point.
y + std::sqrt(delta);
96 double y2 = rear_axis_point.
y - std::sqrt(delta);
98 if (y1 >= std::min(point_a.y, point_b.y) && y1 <= std::max(point_a.y, point_b.y)) {
106 double m = (point_b.y - point_a.y) / (point_b.x - point_a.x);
107 double c = point_a.y - m * point_a.x;
116 double A = 1 + m * m;
117 double B = 2 * (m * c - rear_axis_point.
x - m * rear_axis_point.
y);
118 double C = rear_axis_point.
x * rear_axis_point.
x + c * c +
119 rear_axis_point.
y * rear_axis_point.
y - 2 * c * rear_axis_point.
y - lookahead_distance * lookahead_distance;
121 double delta = B * B - 4 * A * C;
127 double x1 = (-B + std::sqrt(delta)) / (2 * A);
128 double x2 = (-B - std::sqrt(delta)) / (2 * A);
130 double y1 = m * x1 + c;
131 double y2 = m * x2 + c;
134 if (x1 >= std::min(point_a.x, point_b.x) && x1 <= std::max(point_a.x, point_b.x) &&
135 y1 >= std::min(point_a.y, point_b.y) && y1 <= std::max(point_a.y, point_b.y)) {
144 return std::make_tuple(
Position(result_x, result_y),
145 (pathpoint_array[index_a].v + pathpoint_array[index_b].v) / 2.0,
false);
147 RCLCPP_DEBUG(rclcpp::get_logger(
"control"),
"No lookahead point found");
148 return std::make_tuple(
Position(), 0.0,
true);
std::tuple< Position, int, double > get_closest_point(const std::vector< custom_interfaces::msg::PathPoint > &pathpoint_array, const Position &position)
Find the closest point on the path.
std::tuple< Position, double, bool > get_lookahead_point(const std::vector< custom_interfaces::msg::PathPoint > &pathpoint_array, int closest_point_id, double lookahead_distance, Position rear_axis_position, double last_to_first_max_dist)
Update Lookahead point.