Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
helper_functions.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2024 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17#include <algorithm>
19
20
21namespace basic_autonomy
22{
23namespace waypoint_generation
24{
25 int get_nearest_point_index(const std::vector<lanelet::BasicPoint2d>& points,
26 const carma_planning_msgs::msg::VehicleState& state)
27 {
28 lanelet::BasicPoint2d veh_point(state.x_pos_global, state.y_pos_global);
29 double min_distance = std::numeric_limits<double>::max();
30 int i = 0;
31 int best_index = 0;
32 for (const auto& p : points)
33 {
34 double distance = lanelet::geometry::distance2d(p, veh_point);
35 if (distance < min_distance)
36 {
37 best_index = i;
38 min_distance = distance;
39 }
40 i++;
41 }
42 return best_index;
43 }
44
45 int get_nearest_point_index(const std::vector<PointSpeedPair>& points,
46 const carma_planning_msgs::msg::VehicleState& state)
47 {
48 lanelet::BasicPoint2d veh_point(state.x_pos_global, state.y_pos_global);
49 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(BASIC_AUTONOMY_LOGGER), "veh_point: " << veh_point.x() << ", " << veh_point.y());
50 double min_distance = std::numeric_limits<double>::max();
51 int i = 0;
52 int best_index = 0;
53 for (const auto& p : points)
54 {
55 double distance = lanelet::geometry::distance2d(p.point, veh_point);
56 if (distance < min_distance)
57 {
58 best_index = i;
59 min_distance = distance;
60 }
61 i++;
62 }
63 return best_index;
64 }
65
67 const std::vector<carma_planning_msgs::msg::TrajectoryPlanPoint>& trajectory,
68 const lanelet::BasicPoint2d& position)
69 {
70 size_t closest_idx = 0;
71 double min_dist = std::numeric_limits<double>::max();
72
73 for (size_t i = 0; i < trajectory.size(); i++)
74 {
75 auto dist = sqrt(pow(position.x() - trajectory.at(i).x, 2) +
76 pow(position.y() - trajectory.at(i).y, 2));
77
78 if (dist < min_dist)
79 {
80 min_dist = dist;
81 closest_idx = i;
82 }
83 }
84
85 return closest_idx;
86 }
87
88 int get_nearest_index_by_downtrack(const std::vector<lanelet::BasicPoint2d>& points, const carma_wm::WorldModelConstPtr& wm, double target_downtrack)
89 {
90 if(std::empty(points)){
91 RCLCPP_WARN_STREAM(rclcpp::get_logger(BASIC_AUTONOMY_LOGGER), "Empty points vector received, returning -1");
92 return -1;
93 }
94
95 // Find first point with a downtrack greater than target_downtrack
96 const auto itr = std::find_if(std::cbegin(points), std::cend(points),
97 [&wm = std::as_const(wm), target_downtrack](const auto & point) { return wm->routeTrackPos(point).downtrack > target_downtrack; });
98
99 int best_index = std::size(points) - 1;
100
101 // Set best_index to the last point with a downtrack less than target_downtrack
102 if(itr != std::cbegin(points)){
103 best_index = std::distance(std::cbegin(points), std::prev(itr));
104 }
105 else{
106 best_index = 0;
107 }
108
109 RCLCPP_DEBUG_STREAM(rclcpp::get_logger(BASIC_AUTONOMY_LOGGER), "get_nearest_index_by_downtrack>> Found best_index: " << best_index<<", points[i].x(): " << points.at(best_index).x() << ", points[i].y(): " << points.at(best_index).y());
110
111 return best_index;
112 }
113
114 void split_point_speed_pairs(const std::vector<PointSpeedPair>& points,
115 std::vector<lanelet::BasicPoint2d>* basic_points,
116 std::vector<double>* speeds)
117 {
118 basic_points->reserve(points.size());
119 speeds->reserve(points.size());
120
121 for (const auto& p : points)
122 {
123 basic_points->push_back(p.point);
124 speeds->push_back(p.speed);
125 }
126 }
127
128 int get_nearest_index_by_downtrack(const std::vector<PointSpeedPair>& points, const carma_wm::WorldModelConstPtr& wm,
129 const carma_planning_msgs::msg::VehicleState& state)
130 {
131 lanelet::BasicPoint2d state_pos(state.x_pos_global, state.y_pos_global);
132 double ending_downtrack = wm->routeTrackPos(state_pos).downtrack;
133 std::vector<lanelet::BasicPoint2d> basic_points;
134 std::vector<double> speeds;
135 split_point_speed_pairs(points, &basic_points, &speeds);
136 return get_nearest_index_by_downtrack(basic_points, wm, ending_downtrack);
137 }
138
139 int get_nearest_index_by_downtrack(const std::vector<lanelet::BasicPoint2d>& points, const carma_wm::WorldModelConstPtr& wm,
140 const carma_planning_msgs::msg::VehicleState& state)
141 {
142 lanelet::BasicPoint2d state_pos(state.x_pos_global, state.y_pos_global);
143 double ending_downtrack = wm->routeTrackPos(state_pos).downtrack;
144 return get_nearest_index_by_downtrack(points, wm, ending_downtrack);
145 }
146
147} // namespace waypoint_generation
148} // namespace basic_autonomy
void split_point_speed_pairs(const std::vector< PointSpeedPair > &points, std::vector< lanelet::BasicPoint2d > *basic_points, std::vector< double > *speeds)
Helper method to split a list of PointSpeedPair into separate point and speed lists.
static const std::string BASIC_AUTONOMY_LOGGER
int get_nearest_point_index(const std::vector< lanelet::BasicPoint2d > &points, const carma_planning_msgs::msg::VehicleState &state)
Returns the nearest point (in terms of cartesian 2d distance) to the provided vehicle pose in the pro...
int get_nearest_index_by_downtrack(const std::vector< lanelet::BasicPoint2d > &points, const carma_wm::WorldModelConstPtr &wm, double target_downtrack)
Returns the nearest "less than" point to the provided vehicle pose in the provided list by utilizing ...
std::shared_ptr< const WorldModel > WorldModelConstPtr
Definition: WorldModel.hpp:454