Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#include "utils.hpp"
2
3std::ifstream open_read_file(const std::string &filename) {
4 RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "Start openReadFile");
5 std::string file_prefix = ament_index_cpp::get_package_share_directory("planning");
6 file_prefix = file_prefix.substr(0, file_prefix.find("install"));
7 std::string logger_variable = file_prefix + filename;
8 std::ifstream file(file_prefix + filename);
9 if (!file.is_open()) {
10 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "ERROR opening file: %s\n", logger_variable.c_str());
11 } else {
12 RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "Successfully opened %s \n",
13 logger_variable.c_str());
14 }
15 RCLCPP_DEBUG(rclcpp::get_logger("rclcpp"), "End openReadFile");
16 return file;
17}
18
19std::vector<common_lib::structures::Cone> cone_vector_from_file(const std::string &path) {
20 std::string x;
21 std::string y;
22 std::string color;
23 std::ifstream track_file = open_read_file(path);
24 std::vector<common_lib::structures::Cone> output;
25 while (track_file >> x >> y >> color) {
26 float x_value = stof(x);
27 float y_value = stof(y);
29 if (color == "blue_cone") {
30 cone_color = common_lib::competition_logic::Color::BLUE;
31 } else if (color == "yellow_cone") {
32 cone_color = common_lib::competition_logic::Color::YELLOW;
33 } else if (color == "orange_cone") {
34 cone_color = common_lib::competition_logic::Color::ORANGE;
35 } else if (color == "large_orange_cone") {
36 cone_color = common_lib::competition_logic::Color::LARGE_ORANGE;
37 } else {
38 cone_color = common_lib::competition_logic::Color::UNKNOWN;
39 }
41 cone.position.x = static_cast<double>(x_value);
42 cone.position.y = static_cast<double>(y_value);
43 cone.color = cone_color;
44 output.push_back(cone);
45 }
46 track_file.close();
47 return output;
48}
49
50std::pair<std::vector<common_lib::structures::Cone>, std::vector<common_lib::structures::Cone>>
51track_from_file(const std::string &path) {
52 std::string x;
53 std::string y;
54 std::string color;
55 std::ifstream track_file = open_read_file(path);
56 std::vector<common_lib::structures::Cone> left_output;
57 std::vector<common_lib::structures::Cone> right_output;
58 while (track_file >> x >> y >> color) {
59 float x_value = stof(x);
60 float y_value = stof(y);
62 cone.position.x = static_cast<double>(x_value);
63 cone.position.y = static_cast<double>(y_value);
64 if (color == "blue_cone" || color == "blue") {
65 cone.color = common_lib::competition_logic::Color::BLUE;
66 left_output.push_back(cone);
67 } else if (color == "yellow_cone" || color == "yellow") {
68 cone.color = common_lib::competition_logic::Color::YELLOW;
69 right_output.push_back(cone);
70 }
71 }
72 track_file.close();
73 return std::make_pair(left_output, right_output);
74}
75
76std::vector<common_lib::structures::PathPoint> path_from_file(const std::string &path) {
77 std::string x;
78 std::string y;
79 std::string v;
80 std::ifstream path_file = open_read_file(path);
81 std::vector<common_lib::structures::PathPoint> output;
82 while (path_file >> x >> y >> v) {
83 float x_coordinate = stof(x);
84 float y_coordinate = stof(y);
85 float velocity = stof(v);
87 path_point.position.x = static_cast<double>(x_coordinate);
88 path_point.position.y = static_cast<double>(y_coordinate);
89 path_point.ideal_velocity = static_cast<double>(velocity);
90 output.push_back(path_point);
91 }
92 path_file.close();
93 return output;
94}
95
96void extract_info(const std::string_view &filename_view, int &size, int &n_outliers) {
97 std::string filename(filename_view);
98 size_t pos1 = filename.find("_");
99 size_t pos2 = filename.find("_", pos1 + 1);
100 size_t pos3 = filename.find(".", pos2 + 1);
101 std::string size_str = filename.substr(pos1 + 1, pos2 - pos1 - 1);
102 std::string n_outliers_str = filename.substr(pos2 + 1, pos3 - pos2 - 1);
103 std::istringstream(size_str) >> size;
104 std::istringstream(n_outliers_str) >> n_outliers;
105}
106
107float consecutive_max_distance(const std::vector<common_lib::structures::Cone> &cones) {
108 float max_distance = 0.0;
109 for (size_t i = 1; i < cones.size(); i++) {
110 auto distance = static_cast<float>(cones[i].position.euclidean_distance(cones[i - 1].position));
111 if (distance > max_distance) {
112 max_distance = distance;
113 }
114 }
115 return max_distance;
116}
117
118std::vector<std::pair<double, double>> order_vector_of_pairs(
119 const std::vector<std::pair<double, double>> &vec) {
120 std::vector<std::pair<double, double>> result = vec;
121 std::sort(result.begin(), result.end());
122 return result;
123}
124
126 auto now = std::chrono::system_clock::now();
127 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
128 std::tm now_tm;
129 localtime_r(&now_time, &now_tm);
130
131 std::stringstream ss;
132 ss << std::put_time(&now_tm, "%Y-%m-%d-%H:%M");
133 return ss.str();
134}
135
136float round_n(float num, int decimal_places) {
137 num *= static_cast<float>(pow(10, decimal_places));
138 auto intermediate = static_cast<int>(round(num));
139 num = static_cast<float>(static_cast<double>(intermediate) / pow(10, decimal_places));
140 return num;
141}
std::vector< std::pair< double, double > > order_vector_of_pairs(const std::vector< std::pair< double, double > > &vec)
orders a vector of pairs to make it easier to compare them
Definition utils.cpp:118
std::string get_current_date_time_as_string()
Get current date and time as a string.
Definition utils.cpp:125
void extract_info(const std::string_view &filename_view, int &size, int &n_outliers)
Extracts the size and number of outliers from a filename.
Definition utils.cpp:96
std::vector< common_lib::structures::Cone > cone_vector_from_file(const std::string &path)
Retrieves a cone vector from a file.
Definition utils.cpp:19
std::vector< common_lib::structures::PathPoint > path_from_file(const std::string &path)
Retrieves a path point vector from a file.
Definition utils.cpp:76
std::ifstream open_read_file(const std::string &filename)
Opens a file for reading.
Definition utils.cpp:3
float consecutive_max_distance(const std::vector< common_lib::structures::Cone > &cones)
Retrieves the max consecutive distance between adjacent cones in a vector.
Definition utils.cpp:107
std::pair< std::vector< common_lib::structures::Cone >, std::vector< common_lib::structures::Cone > > track_from_file(const std::string &path)
Retrieves a track (a pair of colored cone vectors from each side) from a file.
Definition utils.cpp:51
float round_n(float num, int decimal_places)
rounds float to n decimal places
Definition utils.cpp:136
common_lib::competition_logic::Color color
Definition cone.hpp:14
double round(double number, int n)
round to n decimal places
Definition tests.cpp:17