Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
settings.hpp
Go to the documentation of this file.
1 #ifndef _SETTINGS_H_
2 #define _SETTINGS_H_
3 
4 #include "logger.hpp"
5 #include <memory>
6 
7 #include <iostream>
8 #include <string>
9 
10 #include <nlohmann/json.hpp>
11 
12 #include "rclcpp/rclcpp.hpp"
13 
14 #include "rcl_interfaces/msg/set_parameters_result.hpp"
15 #include "rcl_interfaces/msg/floating_point_range.hpp"
16 #include "rcl_interfaces/msg/parameter_descriptor.hpp"
17 
18 using namespace std;
19 using nlohmann::json;
20 
21 namespace galaxis {
22  namespace utils {
23 
24  shared_ptr<json> read_json_file(std::string path);
25  shared_ptr<json> read_system_settings(std::string key);
26  shared_ptr<std::string> read_file_to_string(std::string path);
27 
28  template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
30  rclcpp::Parameter const& param,
31  rcl_interfaces::msg::SetParametersResult& result,
32  T min = numeric_limits<T>::min(),
33  T max = numeric_limits<T>::max()
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  }
76 
77  // enum ParameterType {
78  // BOOLEAN,
79  // INTEGER,
80  // DOUBLE,
81  // STRING,
82  // NOTEXIT
83  // };
84 
85  // class Settings {
86  // private:
87  // std::map<std::string, bool> booleanParamMap;
88  // std::map<std::string, int> integerParamMap;
89  // std::map<std::string, double> doubleParamMap;
90  // std::map<std::string, std::string> stringParamMap;
91 
92  // bool loadLock = false;
93 
94  // public:
95  // Settings();
96  // ~Settings();
97  // void load(std::shared_ptr<json> settingsJson);
98 
99  // ParameterType getType();
100  // bool getParamBool(std::string key);
101  // double getParamDouble(std::string key);
102  // std::string getParamString(std::string key);
103  // };
104 
105  } // namespace utils
106 } // namespace galaxis
107 
108 #endif // _SETTINGS_H_
json
nlohmann::json json
Definition: AutoPilotNode.hpp:21
galaxis::utils::read_file_to_string
shared_ptr< std::string > read_file_to_string(std::string path)
Definition: settings.cpp:24
galaxis::utils::read_system_settings
shared_ptr< json > read_system_settings(std::string key)
Definition: settings.cpp:14
galaxis::utils::read_json_file
shared_ptr< json > read_json_file(std::string path)
Definition: settings.cpp:5
galaxis
Definition: MapClasses.hpp:10
logger.hpp
galaxis::utils::check_parameter
bool check_parameter(rclcpp::Parameter const &param, rcl_interfaces::msg::SetParametersResult &result, T min=numeric_limits< T >::min(), T max=numeric_limits< T >::max())
Definition: settings.hpp:29