Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
colorpoint.cpp
Go to the documentation of this file.
2
3void Colorpoint::extract_cones(std::vector<Colorpoint>& colorpoints,
4 std::vector<PathPoint>& yellow_cones,
5 std::vector<PathPoint>& blue_cones) {
6 // Need at least 2 points to determine path direction
7 if (colorpoints.size() < 2) {
8 return;
9 }
10 // Process all colorpoints except the last one
11 for (size_t i = 0; i < colorpoints.size() - 1; i++) {
12 color_cones(colorpoints[i], colorpoints[i + 1], yellow_cones, blue_cones);
13 }
14
15 // Handle the last point using cone matching from second-to-last point
16 color_last_point(colorpoints, yellow_cones, blue_cones);
17}
18
19void Colorpoint::color_cones(Colorpoint& colorpoint, const Colorpoint& next_colorpoint,
20 std::vector<PathPoint>& yellow_cones,
21 std::vector<PathPoint>& blue_cones) {
22 // Calculate path direction vector from current point to next point
23 double path_dx = next_colorpoint.point.x() - colorpoint.point.x();
24 double path_dy = next_colorpoint.point.y() - colorpoint.point.y();
25
26 // Calculate vector from path point to cone1
27 double cone1_dx = colorpoint.cone1.position.x - colorpoint.point.x();
28 double cone1_dy = colorpoint.cone1.position.y - colorpoint.point.y();
29
30 // Calculate vector from path point to cone2
31 double cone2_dx = colorpoint.cone2.position.x - colorpoint.point.x();
32 double cone2_dy = colorpoint.cone2.position.y - colorpoint.point.y();
33
34 // Calculate cross products to determine which side each cone is on
35 double cross_product1 = path_dx * cone1_dy - path_dy * cone1_dx;
36 double cross_product2 = path_dx * cone2_dy - path_dy * cone2_dx;
37
38 // Positive value of cross product means cone is on the left and negative on the right
39 if (cross_product1 > cross_product2) {
40 color_pair_of_cones(colorpoint.cone2, colorpoint.cone1, yellow_cones, blue_cones);
41 } else {
42 color_pair_of_cones(colorpoint.cone1, colorpoint.cone2, yellow_cones, blue_cones);
43 }
44}
45
46void Colorpoint::add_cones_by_reference(const Cone& reference_cone, Cone& matching_cone,
47 Cone& other_cone,
48 std::vector<PathPoint>& yellow_cones,
49 std::vector<PathPoint>& blue_cones) {
50 if (reference_cone.color == Color::BLUE) {
51 color_pair_of_cones(other_cone, matching_cone, yellow_cones, blue_cones);
52 } else {
53 color_pair_of_cones(matching_cone, other_cone, yellow_cones, blue_cones);
54 }
55}
56
57void Colorpoint::color_last_point(std::vector<Colorpoint>& colorpoints,
58 std::vector<PathPoint>& yellow_cones,
59 std::vector<PathPoint>& blue_cones) {
60 const Colorpoint& second_to_last = colorpoints[colorpoints.size() - 2];
61 Colorpoint& last = colorpoints.back();
62
63 // Check which cone from second-to-last matches which cone in last
64 if (second_to_last.cone1 == last.cone1) {
65 add_cones_by_reference(second_to_last.cone1, last.cone1, last.cone2, yellow_cones, blue_cones);
66 } else if (second_to_last.cone1 == last.cone2) {
67 add_cones_by_reference(second_to_last.cone1, last.cone2, last.cone1, yellow_cones, blue_cones);
68 } else if (second_to_last.cone2 == last.cone1) {
69 add_cones_by_reference(second_to_last.cone2, last.cone1, last.cone2, yellow_cones, blue_cones);
70 } else if (second_to_last.cone2 == last.cone2) {
71 add_cones_by_reference(second_to_last.cone2, last.cone2, last.cone1, yellow_cones, blue_cones);
72 } else {
73 RCLCPP_WARN(rclcpp::get_logger("planning"),
74 "The last cone does not match with any previous cone.");
75 }
76}
77
78void Colorpoint::color_pair_of_cones(Cone& yellow_cone, Cone& blue_cone,
79 std::vector<PathPoint>& yellow_cones,
80 std::vector<PathPoint>& blue_cones) {
81 yellow_cone.color = Color::YELLOW;
82 blue_cone.color = Color::BLUE;
83 (void)yellow_cones.emplace_back(yellow_cone.position.x, yellow_cone.position.y);
84 (void)blue_cones.emplace_back(blue_cone.position.x, blue_cone.position.y);
85}
Path point with two boundary cones, provides cone classification into left/right boundaries.
Point point
The path point (midpoint between cones).
static void color_pair_of_cones(Cone &yellow_cone, Cone &blue_cone, std::vector< PathPoint > &yellow_cones, std::vector< PathPoint > &blue_cones)
Assigns colors to a cone pair and appends them to the output vectors.
static void add_cones_by_reference(const Cone &reference_cone, Cone &matching_cone, Cone &other_cone, std::vector< PathPoint > &yellow_cones, std::vector< PathPoint > &blue_cones)
Classifies a cone pair using a reference cone whose color is already known.
static void extract_cones(std::vector< Colorpoint > &colorpoints, std::vector< PathPoint > &yellow_cones, std::vector< PathPoint > &blue_cones)
Extracts and classifies all cones from a sequence of colorpoints.
Definition colorpoint.cpp:3
Cone cone2
Second cone associated with this path point.
static void color_last_point(std::vector< Colorpoint > &colorpoints, std::vector< PathPoint > &yellow_cones, std::vector< PathPoint > &blue_cones)
Classifies the cones of the last colorpoint using a shared cone from the previous one.
Cone cone1
First cone associated with this path point.
static void color_cones(Colorpoint &colorpoint, const Colorpoint &next_colorpoint, std::vector< PathPoint > &yellow_cones, std::vector< PathPoint > &blue_cones)
Classifies the cones of a single colorpoint using the direction to the next point.
common_lib::competition_logic::Color color
Definition cone.hpp:14