Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
mocks.cpp
Go to the documentation of this file.
1#include "utils/mocks.hpp"
2
3#include <fstream>
4#include <iostream>
5#include <istream>
6#include <sstream>
7#include <string>
8
9#include "rclcpp/rclcpp.hpp"
10
11custom_interfaces::msg::PathPointArray planning_gtruth_fromfile(std::istream& in) {
12 std::string x;
13 std::string y;
14 std::string velocity;
15 custom_interfaces::msg::PathPointArray gtruth_mock;
16
17 std::string line;
18 std::getline(in, line); // ignore first line
19 while (std::getline(in, line)) {
20 std::stringstream iss(line);
21 if (getline(iss, x, ',') && getline(iss, y, ',') && getline(iss, velocity, ',')) {
22 try {
23 custom_interfaces::msg::PathPoint custom_point;
24 custom_point.x = std::stod(x);
25 custom_point.y = std::stod(y);
26 custom_point.v = std::stod(velocity);
27 gtruth_mock.pathpoint_array.push_back(custom_point);
28 } catch (const std::invalid_argument& e) {
29 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"),
30 "Invalid argument encountered while converting to double: %s \n", e.what());
31 } catch (const std::out_of_range& e) {
32 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"),
33 "Out of range exception encountered while converting to double: %s \n",
34 e.what());
35 }
36 } else if (!line.empty()) {
37 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Unexpected format in line with content: %s \n",
38 line.c_str());
39 }
40 }
41
42 return gtruth_mock;
43}
44
45custom_interfaces::msg::ConeArray se_gtruth_fromfile(std::istream& in) {
46 std::string x;
47 std::string y;
48 std::string color;
49 custom_interfaces::msg::ConeArray gtruth_mock;
50
51 std::string line;
52 std::getline(in, line); // ignore first line
53 while (std::getline(in, line)) {
54 std::stringstream iss(line);
55 if (getline(iss, x, ',') && getline(iss, y, ',') && getline(iss, color, ',')) {
56 try {
57 custom_interfaces::msg::Cone cone;
58 cone.position.x = std::stod(x);
59 cone.position.y = std::stod(y);
60 cone.color =
61 color + "_cone"; // 1 is extra space to remove first char being empty
62 gtruth_mock.cone_array.push_back(cone);
63 } catch (const std::invalid_argument& e) {
64 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"),
65 "Invalid argument encountered while converting to double: %s \n", e.what());
66 } catch (const std::out_of_range& e) {
67 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"),
68 "Out of range exception encountered while converting to double: %s \n",
69 e.what());
70 }
71 } else if (!line.empty()) {
72 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Unexpected format in line with content: %s \n",
73 line.c_str());
74 }
75 }
76 in.clear();
77 in.seekg(0);
78 return gtruth_mock;
79}
80
81std::istream& open_file_as_stream(const std::string& filename) {
82 static std::ifstream file;
83 if (file.is_open()) file.close();
84
85 file.open(filename);
86 if (!file.is_open()) {
87 RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Unable to open file %s \n", filename.c_str());
88 }
89 return file;
90}
custom_interfaces::msg::ConeArray se_gtruth_fromfile(std::istream &in)
read state estimation ground truth information from stream and place it in an array of Cone objects
Definition mocks.cpp:45
std::istream & open_file_as_stream(const std::string &filename)
recieve path to a file and return it as an input stream
Definition mocks.cpp:81
custom_interfaces::msg::PathPointArray planning_gtruth_fromfile(std::istream &in)
read planning ground truth information from stream and place it in an array of PathPoint objects
Definition mocks.cpp:11