Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
path_calculation.hpp
Go to the documentation of this file.
1#ifndef SRC_PLANNING_INCLUDE_PLANNING_PATH_CALCULATION_HPP_
2#define SRC_PLANNING_INCLUDE_PLANNING_PATH_CALCULATION_HPP_
3
4#include <cmath>
5#include <map>
6#include <memory>
7#include <queue>
8#include <utility>
9#include <vector>
10
18#include "rclcpp/rclcpp.hpp"
19
23
34public:
38 PathCalculation() = default;
39
46
47 // ===================== Public Core Methods =====================
48
58 std::vector<PathPoint> calculate_path(const std::vector<Cone>& cone_array);
59
69 std::vector<PathPoint> calculate_trackdrive(const std::vector<Cone>& cone_array);
70
79 void set_vehicle_pose(const common_lib::structures::Pose& vehicle_pose);
80
88 bool is_map_closed(std::vector<PathPoint>& path) const;
89
90 // ===================== Public Accessor Methods =====================
91
97 std::vector<PathPoint> get_path_to_car() const;
98
103 const std::vector<std::pair<Point, Point>>& get_triangulations() const;
104
109 const std::vector<PathPoint>& get_yellow_cones() const;
110
115 const std::vector<PathPoint>& get_blue_cones() const;
116
117private:
118 // ===================== Configuration and State =====================
119
122
123 // Vehicle and path state
126 bool initial_pose_set_ = false;
127
128 // Path storage
129 std::vector<Colorpoint> path_to_car_;
130 std::vector<Colorpoint> past_path_;
132
133 // Midpoints for the current track
134 std::vector<std::shared_ptr<Midpoint>> midpoints_;
135
136 // Path construction state (temporary during calculation)
137 std::vector<Colorpoint> current_path_;
138 std::vector<PathPoint> yellow_cones_;
139 std::vector<PathPoint> blue_cones_;
140 std::unordered_map<Point, std::shared_ptr<Midpoint>> point_to_midpoint_;
141 std::unordered_set<std::shared_ptr<Midpoint>> visited_midpoints_;
142 std::unordered_set<std::shared_ptr<Cone>> discarded_cones_;
143
144 // ===================== Path Calculation Methods =====================
145
153
161
170 void extend_path(int max_points);
171
178 void clear_path_state();
179
189 int reset_path(bool should_reset);
190
191 // ===================== Path Search and Optimization =====================
192
201 std::pair<std::shared_ptr<Midpoint>, std::shared_ptr<Midpoint>> select_starting_midpoints();
202
213 std::vector<std::shared_ptr<Midpoint>> select_candidate_midpoints(const Midpoint& anchor_pose,
214 int num_candidates) const;
215
228 std::pair<double, std::shared_ptr<Midpoint>> find_best_next_midpoint(
229 int depth, const std::shared_ptr<Midpoint>& previous,
230 const std::shared_ptr<Midpoint>& current, double max_cost) const;
231
243 double calculate_cost(double previous_x, double previous_y, double current_x, double current_y,
244 double next_x, double next_y) const;
245
246 // ===================== Cone Discarding Methods =====================
247
256
266 void discard_cone(const std::shared_ptr<Midpoint>& last_mp,
267 const std::shared_ptr<Midpoint>& current_mp);
268
276
284
285 // ===================== Utility Methods =====================
286
296 std::shared_ptr<Midpoint> find_nearest_midpoint(const Point& target) const;
297
311 std::pair<int, double> find_best_loop_closure(const std::vector<PathPoint>& path) const;
312
319 std::vector<PathPoint> get_path_points_from_colorpoints(
320 const std::vector<Colorpoint>& colorpoints) const;
321};
322
323#endif // SRC_PLANNING_INCLUDE_PLANNING_PATH_CALCULATION_HPP_
Generates midpoints between cones using Delaunay triangulation.
Generates optimal local paths.
PathCalculation(const PathCalculationConfig &config)
Construct a new PathCalculation object with configuration.
std::pair< std::shared_ptr< Midpoint >, std::shared_ptr< Midpoint > > select_starting_midpoints()
Select the first two midpoints to start the path.
std::vector< Colorpoint > past_path_
int reset_path(bool should_reset)
Handle path reset when completing a lap.
const std::vector< PathPoint > & get_yellow_cones() const
Gets the list of yellow cones detected or used in the path calculation.
std::vector< std::shared_ptr< Midpoint > > select_candidate_midpoints(const Midpoint &anchor_pose, int num_candidates) const
Select candidate midpoints in front of the vehicle.
std::unordered_set< std::shared_ptr< Midpoint > > visited_midpoints_
void discard_cone(const std::shared_ptr< Midpoint > &last_mp, const std::shared_ptr< Midpoint > &current_mp)
Determine which cone to discard between two consecutive midpoints.
double calculate_cost(double previous_x, double previous_y, double current_x, double current_y, double next_x, double next_y) const
Calculate the cost of transitioning through three consecutive points.
std::vector< PathPoint > get_path_to_car() const
Get the path from the start to a lookback distance behind the car’s current position.
common_lib::structures::Pose vehicle_pose_
std::vector< PathPoint > calculate_path(const std::vector< Cone > &cone_array)
Generate a path from an array of cones.
void initialize_path_from_initial_pose()
Initialize the path from the vehicle's initial pose.
void remove_invalid_neighbors()
Remove invalid neighbors from all midpoint connection lists.
bool is_map_closed(std::vector< PathPoint > &path) const
Checks if the path forms a closed loop and closes it if the transition cost is below config_....
std::unordered_set< std::shared_ptr< Cone > > discarded_cones_
std::vector< PathPoint > yellow_cones_
PathCalculation()=default
Construct a new default PathCalculation object.
std::shared_ptr< Midpoint > find_nearest_midpoint(const Point &target) const
Find the nearest valid midpoint to a target position.
void extend_path(int max_points)
Extend the path by adding midpoints.
void clear_path_state()
Reset path construction state.
MidpointGenerator midpoint_generator_
void update_path_from_past_path()
Initialize the path from the previous path.
void invalidate_midpoints_with_discarded_cones()
Mark midpoints as invalid if they use discarded cones.
const std::vector< PathPoint > & get_blue_cones() const
Gets the list of blue cones detected or used in the path calculation.
std::vector< PathPoint > blue_cones_
const std::vector< std::pair< Point, Point > > & get_triangulations() const
Get the triangulation segments used in path generation.
std::pair< double, std::shared_ptr< Midpoint > > find_best_next_midpoint(int depth, const std::shared_ptr< Midpoint > &previous, const std::shared_ptr< Midpoint > &current, double max_cost) const
Recursively find the best next midpoint using depth-first search.
std::vector< PathPoint > calculate_trackdrive(const std::vector< Cone > &cone_array)
Generate a closed loop path for trackdrive competition.
common_lib::structures::Pose initial_pose_
std::vector< std::shared_ptr< Midpoint > > midpoints_
std::pair< int, double > find_best_loop_closure(const std::vector< PathPoint > &path) const
Finds the best loop closure point in a path.
std::vector< Colorpoint > current_path_
std::unordered_map< Point, std::shared_ptr< Midpoint > > point_to_midpoint_
void discard_cones_along_path()
Invalidate cones and midpoints along the last path segment.
void set_vehicle_pose(const common_lib::structures::Pose &vehicle_pose)
Set the current vehicle position and orientation.
std::vector< PathPoint > get_path_points_from_colorpoints(const std::vector< Colorpoint > &colorpoints) const
Converts a vector of Colorpoint objects into a vector of PathPoint objects.
std::vector< Colorpoint > path_to_car_
PathCalculationConfig config_
K::Point_2 Point
Definition marker.hpp:18
MidPoint struct represents a potential path point with connections.
Definition midpoint.hpp:17
Struct for pose representation.
Definition pose.hpp:13