HEBI C++ API  3.15.0
robot_config.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
8 #include "arm/plugin_config.hpp"
9 
10 namespace hebi {
11 
12 // A wrapper for loading robot configurations. (See https://github.com/HebiRobotics/robot-config)
13 class RobotConfig {
14 public:
15  // Reads the robot config from the given file. This reads all included
16  // parameters/references, and verifies that all referenced paths exist and
17  // load properly.
18  //
19  // Any errors when loading are added to the passed in vector. For non-fatal
20  // errors, an object may still be returned.
21  static std::unique_ptr<RobotConfig> loadConfig(std::string filepath, std::vector<std::string>& errors);
22 
24 
25  // Return names (required)
26  const std::vector<std::string>& getNames() const { return names_; }
27  // Return families (required)
28  const std::vector<std::string>& getFamilies() const { return families_; }
29  bool hasFeedbackFrequency() const { return has_feedback_frequency_; }
30  // Feedback frequency [Hz]
31  double getFeedbackFrequency() const { return feedback_frequency_; }
32  bool hasCommandLifetime() const { return has_command_lifetime_; }
33  // Command lifetime [s]
34  double getCommandLifetime() const { return command_lifetime_; }
35  // Return HRDF absolute file path (optional)
36  const std::string& getHrdf() const { return hrdf_; }
37  // Return gain for specific key
38  std::string getGains(const std::string& key) const { return gains_.count(key) == 0 ? "" : gains_.at(key); }
39  // Return all gains (absolute paths; may be empty)
40  const std::map<std::string, std::string>& getGains() const { return gains_; }
41  // Get absolute path of the parent directory of the config file
42  const std::string& getParentDirectory() const { return location_; }
43  // Get ordered list of plugin parameters
44  const std::vector<arm::PluginConfig>& getPluginConfigs() const { return plugin_configs_; }
45 
46  // Each user data field can have a dictionary of three types of parameters.
47  struct UserData {
48  // Optional boolean values
49  std::map<std::string, bool> bools_;
50 
51  // Optional float values
52  std::map<std::string, double> floats_;
53 
54  // Optional string values
55  std::map<std::string, std::string> strings_;
56 
57  // Optional lists of boolean values
58  std::map<std::string, std::vector<bool>> bool_lists_;
59 
60  // Optional lists of float values
61  std::map<std::string, std::vector<double>> float_lists_;
62 
63  // Optional lists of string values
64  std::map<std::string, std::vector<std::string>> string_lists_;
65 
66  // Getter for a bool value
67  bool getBool(const std::string& key, bool default_value = false) const {
68  auto it = bools_.find(key);
69  if (it != bools_.end()) {
70  return it->second;
71  }
72  return default_value; // Default value if the key is not found
73  }
74 
75  // Check if a bool key exists
76  bool hasBool(const std::string& key) const {
77  return bools_.find(key) != bools_.end();
78  }
79 
80  // Getter for a float value
81  double getFloat(const std::string& key, double default_value = 0.0) const {
82  auto it = floats_.find(key);
83  if (it != floats_.end()) {
84  return it->second;
85  }
86  return default_value; // Default value if the key is not found
87  }
88 
89  // Check if a float key exists
90  bool hasFloat(const std::string& key) const {
91  return floats_.find(key) != floats_.end();
92  }
93 
94  // Getter for a string value
95  std::string getString(const std::string& key, std::string default_value = "") const {
96  auto it = strings_.find(key);
97  if (it != strings_.end()) {
98  return it->second;
99  }
100  return default_value; // Default value if the key is not found
101  }
102 
103  // Check if a string key exists
104  bool hasString(const std::string& key) const {
105  return strings_.find(key) != strings_.end();
106  }
107 
108  // Getter for a bool list
109  std::vector<bool> getBoolList(const std::string& key, std::vector<bool> default_value = {}) const {
110  auto it = bool_lists_.find(key);
111  if (it != bool_lists_.end()) {
112  return it->second;
113  }
114  return default_value; // Default empty list if the key is not found
115  }
116 
117  // Check if a bool list key exists
118  bool hasBoolList(const std::string& key) const {
119  return bool_lists_.find(key) != bool_lists_.end();
120  }
121 
122  // Getter for a float list
123  std::vector<double> getFloatList(const std::string& key, std::vector<double> default_value = {}) const {
124  auto it = float_lists_.find(key);
125  if (it != float_lists_.end()) {
126  return it->second;
127  }
128  return default_value; // Default empty list if the key is not found
129  }
130 
131  // Check if a float list key exists
132  bool hasFloatList(const std::string& key) const {
133  return float_lists_.find(key) != float_lists_.end();
134  }
135 
136  // Getter for a string list
137  std::vector<std::string> getStringList(const std::string& key, std::vector<std::string> default_value = {}) const {
138  auto it = string_lists_.find(key);
139  if (it != string_lists_.end()) {
140  return it->second;
141  }
142  return default_value; // Default empty list if the key is not found
143  }
144 
145  // Check if a string list key exists
146  bool hasStringList(const std::string& key) const {
147  return string_lists_.find(key) != string_lists_.end();
148  }
149  };
150 
151  // Any listed user_data keys
152  const UserData& getUserData() const { return user_data_; }
153 
154 private:
155  RobotConfig() = default;
156 
157  std::vector<std::string> names_;
158  std::vector<std::string> families_;
159  bool has_feedback_frequency_{};
160  double feedback_frequency_{};
161  bool has_command_lifetime_{};
162  double command_lifetime_{};
163  // Stored as an absolute path for reading later
164  std::string hrdf_;
165  // Stored as absolute paths for reading later
166  std::map<std::string, std::string> gains_;
167  // plugins
168  std::vector<arm::PluginConfig> plugin_configs_;
169  // Absolute path of parent directory
170  std::string location_;
171  // user_data
172  UserData user_data_;
173 };
174 
175 } // namespace hebi
const std::map< std::string, std::string > & getGains() const
Definition: robot_config.hpp:40
const std::string & getParentDirectory() const
Definition: robot_config.hpp:42
Definition: group.hpp:35
bool hasFloat(const std::string &key) const
Definition: robot_config.hpp:90
Definition: arm.cpp:10
std::vector< bool > getBoolList(const std::string &key, std::vector< bool > default_value={}) const
Definition: robot_config.hpp:109
std::map< std::string, bool > bools_
Definition: robot_config.hpp:49
std::vector< double > getFloatList(const std::string &key, std::vector< double > default_value={}) const
Definition: robot_config.hpp:123
const std::vector< std::string > & getFamilies() const
Definition: robot_config.hpp:28
bool hasFloatList(const std::string &key) const
Definition: robot_config.hpp:132
bool hasFeedbackFrequency() const
Definition: robot_config.hpp:29
bool hasBoolList(const std::string &key) const
Definition: robot_config.hpp:118
Definition: robot_config.hpp:47
double getCommandLifetime() const
Definition: robot_config.hpp:34
const UserData & getUserData() const
Definition: robot_config.hpp:152
static std::unique_ptr< RobotConfig > loadConfig(std::string filepath, std::vector< std::string > &errors)
Definition: robot_config.cpp:73
std::string getGains(const std::string &key) const
Definition: robot_config.hpp:38
std::map< std::string, std::string > strings_
Definition: robot_config.hpp:55
const std::vector< arm::PluginConfig > & getPluginConfigs() const
Definition: robot_config.hpp:44
std::string getString(const std::string &key, std::string default_value="") const
Definition: robot_config.hpp:95
std::map< std::string, std::vector< std::string > > string_lists_
Definition: robot_config.hpp:64
bool hasBool(const std::string &key) const
Definition: robot_config.hpp:76
std::map< std::string, std::vector< double > > float_lists_
Definition: robot_config.hpp:61
std::map< std::string, std::vector< bool > > bool_lists_
Definition: robot_config.hpp:58
std::vector< std::string > getStringList(const std::string &key, std::vector< std::string > default_value={}) const
Definition: robot_config.hpp:137
std::map< std::string, double > floats_
Definition: robot_config.hpp:52
bool hasCommandLifetime() const
Definition: robot_config.hpp:32
Definition: robot_config.hpp:13
double getFeedbackFrequency() const
Definition: robot_config.hpp:31
bool getBool(const std::string &key, bool default_value=false) const
Definition: robot_config.hpp:67
const std::string & getHrdf() const
Definition: robot_config.hpp:36
double getFloat(const std::string &key, double default_value=0.0) const
Definition: robot_config.hpp:81
bool hasStringList(const std::string &key) const
Definition: robot_config.hpp:146
bool hasString(const std::string &key) const
Definition: robot_config.hpp:104
const std::vector< std::string > & getNames() const
Definition: robot_config.hpp:26