Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
galaxis::utils Namespace Reference

Classes

class  MicroStopwatch
 

Functions

long long get_time_now_ms ()
 
long long get_time_now_us ()
 
shared_ptr< vector< string > > read_from_file_to_string (string const &p_filepath)
 
shared_ptr< vector< float > > read_from_file_to_float (string const &p_filepath)
 
template<typename T >
void write_to_file (string const &p_filepath, std::vector< std::vector< T >> const &p_data, string const &p_delimiter=",")
 
shared_ptr< std::map< std::string, std::string > > read_configuration (string const &p_filepath)
 
template<typename... Args>
void log_nonewline (const Args &... args)
 
template<typename... Args>
void log_cout (const Args &... args)
 
template<typename... Args>
void log (const Args &... args)
 
template<typename... Args>
void log_warning (const Args &... args)
 
template<typename... Args>
void log_error (const Args &... args)
 
template<typename... Args>
void log_debug (const Args &... args)
 
float evaluate_polynom (polynom const &p_polynomRef, float p_x)
 
double evaluate_polynom (std::vector< double > const &p_polynomRef, double p_x)
 
float calculate_gradient (std::vector< float > const &p_polynomRef, float p_x)
 
double calculate_gradient (std::vector< double > const &p_polynomRef, double p_x)
 
double calculate_gradient (DynamicPolynom p_polynomVecSPtr, double p_x)
 
shared_ptr< json > read_json_file (std::string path)
 
shared_ptr< json > read_system_settings (std::string key)
 
shared_ptr< std::string > read_file_to_string (std::string path)
 
template<typename T , typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool check_parameter (rclcpp::Parameter const &param, rcl_interfaces::msg::SetParametersResult &result, T min=numeric_limits< T >::min(), T max=numeric_limits< T >::max())
 

Function Documentation

◆ calculate_gradient() [1/3]

double galaxis::utils::calculate_gradient ( DynamicPolynom  p_polynomVecSPtr,
double  p_x 
)

Definition at line 96 of file poly.cpp.

96  {
97  float value = 0.0;
98 
99  float iValue;
100  for(size_t i = 1; i < p_polynomVecSPtr->size(); ++i) {
101  iValue = 1;
102 
103  // i*x^(i-1)
104  for(size_t j = 0; j < i - 1; ++j) {
105  iValue *= p_x;
106  }
107 
108  value += p_polynomVecSPtr->at(i) * i * iValue;
109  }
110 
111  return value;
112 }

◆ calculate_gradient() [2/3]

double galaxis::utils::calculate_gradient ( std::vector< double > const &  p_polynomRef,
double  p_x 
)

Definition at line 78 of file poly.cpp.

78  {
79  float value = 0.0;
80 
81  float iValue;
82  for(size_t i = 1; i < p_polynomRef.size(); ++i) {
83  iValue = 1;
84 
85  // i*x^(i-1)
86  for(size_t j = 0; j < i - 1; ++j) {
87  iValue *= p_x;
88  }
89 
90  value += p_polynomRef[i] * i * iValue;
91  }
92 
93  return value;
94 }

◆ calculate_gradient() [3/3]

float galaxis::utils::calculate_gradient ( std::vector< float > const &  p_polynomRef,
float  p_x 
)

Definition at line 60 of file poly.cpp.

60  {
61  float value = 0.0;
62 
63  float iValue;
64  for(size_t i = 1; i < p_polynomRef.size(); ++i) {
65  iValue = 1;
66 
67  // i*x^(i-1)
68  for(size_t j = 0; j < i - 1; ++j) {
69  iValue *= p_x;
70  }
71 
72  value += p_polynomRef[i] * i * iValue;
73  }
74 
75  return value;
76 }

Referenced by AutoPilot::get_drive_information(), and main().

Here is the caller graph for this function:

◆ check_parameter()

template<typename T , typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
bool galaxis::utils::check_parameter ( rclcpp::Parameter const &  param,
rcl_interfaces::msg::SetParametersResult &  result,
T  min = numeric_limits<T>::min(),
T  max = numeric_limits<T>::max() 
)

Definition at line 29 of file settings.hpp.

34  {
35 
36  T paramValue;
37  if( std::is_same<T, int>::value ) {
38  if( param.get_type() == rclcpp::ParameterType::PARAMETER_INTEGER ) {
39  paramValue = param.as_int();
40  }
41  else {
42  result.successful = false;
43  result.reason += "[" + param.get_name() + "] Type wrong. Must be INTEGER; ";
44  return false;
45  }
46  }
47  else if ( std::is_same<T, double>::value || std::is_same<T, float>::value ) {
48  if( param.get_type() == rclcpp::ParameterType::PARAMETER_DOUBLE ) {
49  paramValue = param.as_double();
50  }
51  else {
52  result.successful = false;
53  result.reason += "[" + param.get_name() + "] Type wrong. Must be DOUBLE; ";
54  return false;
55  }
56  }
57  else {
58  result.successful = false;
59  result.reason += "[" + param.get_name() + "] Type wrong.; ";
60  return false;
61  }
62 
63  if( paramValue < min ) {
64  result.successful = false;
65  result.reason += "[" + param.get_name() + "] Value too low. Must be larger or equal to " + std::to_string(min) + " ; ";
66  return false;
67  }
68  if( paramValue > max ) {
69  result.successful = false;
70  result.reason += "[" + param.get_name() + "] Value too high. Must be lower or equal to " + std::to_string(max) + " ; ";
71  return false;
72  }
73 
74  return true;
75  }

◆ evaluate_polynom() [1/2]

float galaxis::utils::evaluate_polynom ( polynom const &  p_polynomRef,
float  p_x 
)

Definition at line 50 of file poly.cpp.

50  {
51  std::vector<double> poly;
52  for(auto const& elt : p_polynomRef) {
53  poly.push_back(elt);
54  }
55 
56  return static_cast<float>(evaluate_polynom(poly,p_x));
57 }

Referenced by galaxis::PolySpline::evaluate(), and AutoPilot::get_lane_centers_via_polynom_thread_helper().

Here is the caller graph for this function:

◆ evaluate_polynom() [2/2]

double galaxis::utils::evaluate_polynom ( std::vector< double > const &  p_polynomRef,
double  p_x 
)

Definition at line 32 of file poly.cpp.

32  {
33  float value = 0.0;
34 
35  float iValue;
36  for(size_t i = 0; i < p_polynomRef.size(); ++i) {
37  iValue = 1;
38 
39  // x^i
40  for(size_t j = 0; j < i; ++j) {
41  iValue *= p_x;
42  }
43 
44  value += p_polynomRef[i] * iValue;
45  }
46 
47  return value;
48 }

◆ get_time_now_ms()

long long galaxis::utils::get_time_now_ms ( )

Definition at line 6 of file clock.cpp.

6  {
7  auto time_now = std::chrono::system_clock::now();
8  auto time_now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_now);
9  auto time_value = time_now_ms.time_since_epoch();
10  return time_value.count();
11 }

◆ get_time_now_us()

long long galaxis::utils::get_time_now_us ( )

Definition at line 13 of file clock.cpp.

13  {
14  auto time_now = std::chrono::high_resolution_clock::now();
15  auto time_now_ns = std::chrono::time_point_cast<std::chrono::microseconds>(time_now);
16  auto time_value = time_now_ns.time_since_epoch();
17  return time_value.count();
18 }

Referenced by galaxis::utils::MicroStopwatch::get(), and galaxis::utils::MicroStopwatch::start().

Here is the caller graph for this function:

◆ log()

template<typename... Args>
void galaxis::utils::log ( const Args &...  args)

Definition at line 33 of file logger.hpp.

33  {
34 #ifdef _LOGGER_INFO
35  (std::clog << ... << args);
36  std::clog << "\n";
37 #endif
38  }

Referenced by AutoPilotNode::AutoPilotNode(), galaxis::control::TwoAndAHalfCarrots::calculate_best_yaw_rate(), ControllerNode::calculate_turn_angle(), VCINodeAsync::camera_publish_loop(), LaneDetectionGT::checkReady(), ControllerNode::ControllerNode(), GalaxisNode::create_parameters(), AutoPilotNode::debug_publish_loop(), CameraFrameObserver::FrameReceived(), LaneDetectionGT::groundtruthCallback(), LaneDetectionGT::groundtruthObstacleCallback(), ECUNode::init(), Realsense::init(), MapDataLayer::init(), AutoPilotNode::lane_detection_shared_memory_loop(), LaneDetectionGT::LaneDetectionGT(), RemoteControl::loop(), LaneDetectionGT::loop(), masterNode::masterNode(), ECUNode::motion_callback(), StopLineDetection::new_parameters_callback(), RealSenseNode::new_parameters_callback(), ControllerNode::new_parameters_callback(), AutoPilotNode::new_parameters_callback(), RealSenseNode::object_detection(), ControllerNode::on_controller_reference_callback(), AutoPilotNode::on_ecu_measurement_callback(), ControllerNode::on_map_debug_image_callback(), ControllerNode::on_measurement_callback(), AutoPilotNode::on_obstacle_callback(), ControllerNode::on_pathpolynom_callback(), StopLineDetection::on_stop_line_detection_callback(), AutoPilotNode::on_vci_lane_detection_callback(), QRCodeDetectionNode::onImgCallback(), ControllerNode::publish_control_values(), ECUNode::publishMeasurements(), RemoteControl::RemoteControl(), AutoPilotNode::send_controller_references(), ControllerNode::send_debug_messages(), AutoPilotNode::send_full_map_debug_image(), AutoPilotNode::send_map_debug_image(), ECUNode::settingsvalues_callback(), SimulationAdapterNode::SimulationAdapterNode(), SimulationAdapterNode::simulationUpdateLoop(), PolyFit< T >::solveRLS(), StopLineDetection::StopLineDetection(), SimulationAdapterNode::updatePose(), SimulationAdapterNode::vehicleControlCallback(), and masterNode::~masterNode().

Here is the caller graph for this function:

◆ log_cout()

template<typename... Args>
void galaxis::utils::log_cout ( const Args &...  args)

Definition at line 25 of file logger.hpp.

25  {
26 #ifdef _LOGGER_COUT
27  (std::cout << ... << args);
28  std::cout << "\n";
29 #endif
30  }

Referenced by Decision::update().

Here is the caller graph for this function:

◆ log_debug()

template<typename... Args>
void galaxis::utils::log_debug ( const Args &...  args)

◆ log_error()

template<typename... Args>
void galaxis::utils::log_error ( const Args &...  args)

Definition at line 50 of file logger.hpp.

50  {
51 #ifdef _LOGGER_ERROR
52  std::cerr << "ERROR: ";
53  (std::cerr << ... << args);
54  std::cerr << "\n";
55 #ifdef _LOGGER_ERROR_THROW_EXCEPTION
56  throw std::runtime_error("Look at the ERROR above. (thrown from utilities/.../logger.hpp)");
57 #endif
58 #endif
59  }

Referenced by GalaxisNode::enable_parameters(), galaxis::control::TwoAndAHalfCarrots::get_pose_series(), AutoPilotNode::on_vci_lane_detection_callback(), read_from_file_to_string(), RealSenseNode::RealSenseNode(), AutoPilotNode::send_controller_references(), galaxis::control::TwoAndAHalfCarrots::set_horizon(), QRCodeDetectionNode::test_detection_loop(), and write_to_file().

Here is the caller graph for this function:

◆ log_nonewline()

template<typename... Args>
void galaxis::utils::log_nonewline ( const Args &...  args)

Definition at line 18 of file logger.hpp.

18  {
19 #ifdef _LOGGER_INFO
20  (std::clog << ... << args);
21 #endif
22  }

Referenced by RemoteControl::loop().

Here is the caller graph for this function:

◆ log_warning()

template<typename... Args>
void galaxis::utils::log_warning ( const Args &...  args)

Definition at line 41 of file logger.hpp.

41  {
42 #ifdef _LOGGER_WARNING
43  std::clog << "WARNING: ";
44  (std::clog << ... << args);
45  std::clog << "\n";
46 #endif
47  }

Referenced by Localizer::add_state(), AlviumAsyncCamera::AlviumAsyncCamera(), AutoPilotNode::AutoPilotNode(), galaxis::control::TwoAndAHalfCarrots::calculate_best_yaw_rate(), VCINodeAsync::camera_publish_loop(), GalaxisNode::create_parameters(), CameraFrameObserver::FrameReceived(), GalaxisNode::GalaxisNode(), AutoPilot::get_drive_information(), PositionTimepointBuffer::get_last_by_time(), galaxis::control::TwoAndAHalfCarrots::get_last_first_poseseries(), galaxis::control::TwoAndAHalfCarrots::get_last_second_poseseries(), MapDataLayer::getLineObject(), LaneDetectionGT::groundtruthCallback(), Realsense::init(), AutoPilotNode::lane_detection_shared_memory_loop(), ControllerNode::loop(), LaneDetectionGT::loop(), StopLineDetection::new_parameters_callback(), RealSenseNode::new_parameters_callback(), ControllerNode::new_parameters_callback(), AutoPilotNode::new_parameters_callback(), RealSenseNode::object_detection_loop(), ControllerNode::on_measurement_callback(), AutoPilotNode::on_obstacle_callback(), AutoPilotNode::on_vci_lane_detection_callback(), AlviumAsyncCamera::pop_image(), ECUNode::publishMeasurements(), read_configuration(), AutoPilotNode::send_full_map_debug_image(), AutoPilotNode::send_map_debug_image(), galaxis::control::TwoAndAHalfCarrots::set_horizon(), Decision::set_parking_state(), AlviumAsyncCamera::start_grabbing_detached(), MapDataLayer::updateCell(), and VCINodeAsync::VCINodeAsync().

Here is the caller graph for this function:

◆ read_configuration()

shared_ptr< std::map< std::string, std::string > > galaxis::utils::read_configuration ( string const &  p_filepath)

Definition at line 41 of file file.cpp.

41  {
42  shared_ptr<std::map<std::string, std::string>> contentSPtr = std::make_shared<std::map<std::string, std::string>>();
43 
44  ifstream file;
45  file.open(p_filepath);
46 
47  if(file.is_open()) {
48 
49  string line;
50  while(getline(file, line)) {
51  if(!line.empty()) {
52 
53  size_t lineDelimiterPosition = line.find("=");
54  if (lineDelimiterPosition == std::string::npos || lineDelimiterPosition == 0) continue; // continue if line not in format: key=value
55 
56  std::string key = line.substr(0, lineDelimiterPosition);
57  std::string value = line.substr(lineDelimiterPosition + 1, line.size());
58 
59  if (contentSPtr->find(key) == contentSPtr->end())
60  contentSPtr->insert(std::pair<std::string, std::string>(key, value));
61  else
62  log_warning("[File] Duplicated Keys while loading config file: ", p_filepath);
63 
64  }
65  }
66 
67  } else {
68  log_warning("Failed to open file.");
69  throw std::runtime_error("Failed to open file.");
70  }
71 
72  file.close();
73 
74  return contentSPtr;
75 }

References log_warning().

Referenced by LaneDetectionGT::LaneDetectionGT().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_file_to_string()

shared_ptr< std::string > galaxis::utils::read_file_to_string ( std::string  path)

Definition at line 24 of file settings.cpp.

24  {
25  // open file and append to string
26  std::ifstream file;
27  file.open(path);
28  std::shared_ptr<std::string> fileContent = std::make_shared<std::string>();
29 
30  if(file.is_open()) {
31  std::string line;
32 
33  while(getline(file, line)) {
34  (*fileContent) += line;
35  }
36 
37  file.close();
38  }
39  else {
40  log_debug("Failed to load file: ", path);
41  throw std::runtime_error("Failed to load file.");
42  }
43 
44  return fileContent;
45 }

References log_debug().

Referenced by read_json_file(), and read_system_settings().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_from_file_to_float()

shared_ptr< vector< float > > galaxis::utils::read_from_file_to_float ( string const &  p_filepath)

Definition at line 29 of file file.cpp.

29  {
30  auto contentStringSPtr = galaxis::utils::read_from_file_to_string(p_filepath);
31 
32  shared_ptr<vector<float>> contentUPtr = std::make_shared<vector<float>>();
33 
34  for(auto& c: *contentStringSPtr) {
35  contentUPtr->push_back(stof(c));
36  }
37 
38  return move(contentUPtr);
39 }

References read_from_file_to_string().

Here is the call graph for this function:

◆ read_from_file_to_string()

shared_ptr< vector< string > > galaxis::utils::read_from_file_to_string ( string const &  p_filepath)

Definition at line 5 of file file.cpp.

5  {
6  shared_ptr<vector<string>> contentUPtr = std::make_shared<vector<string>>();
7 
8  ifstream file;
9  file.open(p_filepath);
10 
11  if(file.is_open()) {
12 
13  string line;
14  while(getline(file, line)) {
15  if(!line.empty()) {
16  contentUPtr->push_back(line);
17  }
18  }
19 
20  } else {
21  log_error("Failed to open file.");
22  }
23 
24  file.close();
25 
26  return move(contentUPtr);
27 }

References log_error().

Referenced by read_from_file_to_float().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_json_file()

shared_ptr< json > galaxis::utils::read_json_file ( std::string  path)

Definition at line 5 of file settings.cpp.

5  {
6  auto fileContentRawSPtr = read_file_to_string(path);
7 
8  auto filecontentSPtr = std::make_shared<json>();
9  *filecontentSPtr = nlohmann::json::parse(*fileContentRawSPtr);
10 
11  return filecontentSPtr;
12 }

References read_file_to_string().

Referenced by AutoPilotNode::AutoPilotNode(), ControllerNode::ControllerNode(), GalaxisNode::GalaxisNode(), RealSenseNode::RealSenseNode(), and StopLineDetection::StopLineDetection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_system_settings()

shared_ptr< json > galaxis::utils::read_system_settings ( std::string  key)

Definition at line 14 of file settings.cpp.

14  {
15  std::string systemSettingsPath = "settings/runtime_system.json";
16 
17  auto fileContentRawSPtr = read_file_to_string(systemSettingsPath);
18 
19  auto filecontentSPtr = std::make_shared<json>();
20  *filecontentSPtr = nlohmann::json::parse(*fileContentRawSPtr)[key];
21  return filecontentSPtr;
22 }

References read_file_to_string().

Referenced by GalaxisNode::enable_parameters().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_to_file()

template<typename T >
void galaxis::utils::write_to_file ( string const &  p_filepath,
std::vector< std::vector< T >> const &  p_data,
string const &  p_delimiter = "," 
)

Definition at line 21 of file file.hpp.

21  {
22  ofstream file;
23  file.open(p_filepath);
24 
25  if(file.is_open()) {
26 
27  for(auto const& r: p_data) {
28  for(auto const& c: r) {
29  file << c << p_delimiter;
30  }
31  file << "\n";
32  }
33 
34  } else {
35  log_error("Failed to write file.");
36  }
37 
38  file.close();
39  }

References log_error().

Here is the call graph for this function:
galaxis::utils::log_error
void log_error(const Args &... args)
Definition: logger.hpp:50
galaxis::utils::evaluate_polynom
float evaluate_polynom(polynom const &p_polynomRef, float p_x)
Definition: poly.cpp:50
galaxis::utils::read_file_to_string
shared_ptr< std::string > read_file_to_string(std::string path)
Definition: settings.cpp:24
galaxis::utils::log_warning
void log_warning(const Args &... args)
Definition: logger.hpp:41
galaxis::utils::read_from_file_to_string
shared_ptr< vector< string > > read_from_file_to_string(string const &p_filepath)
Definition: file.cpp:5
galaxis::utils::log_debug
void log_debug(const Args &... args)
Definition: logger.hpp:62