HEBI C++ API  3.13.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  double getFeedbackFrequency() const { return feedback_frequency_; }
31  bool hasCommandLifetime() const { return has_command_lifetime_; }
32  double getCommandLifetime() const { return command_lifetime_; }
33  // Return HRDF absolute file path (optional)
34  const std::string& getHrdf() const { return hrdf_; }
35  // Return gain for specific key
36  std::string getGains(const std::string& key) const { return gains_.count(key) == 0 ? "" : gains_.at(key); }
37  // Return all gains (absolute paths; may be empty)
38  const std::map<std::string, std::string>& getGains() const { return gains_; }
39  // Get absolute path of the parent directory of the config file
40  const std::string& getParentDirectory() const { return location_; }
41  // Get ordered list of plugin parameters
42  const std::vector<arm::PluginConfig>& getPluginConfigs() const { return plugin_configs_; }
43 
44  // Each user data field can have a dictionary of three types of parameters.
45  struct UserData {
46  // Optional boolean values
47  std::map<std::string, bool> bools_;
48 
49  // Optional float values
50  std::map<std::string, double> floats_;
51 
52  // Optional string values
53  std::map<std::string, std::string> strings_;
54 
55  // Optional lists of boolean values
56  std::map<std::string, std::vector<bool>> bool_lists_;
57 
58  // Optional lists of float values
59  std::map<std::string, std::vector<double>> float_lists_;
60 
61  // Optional lists of string values
62  std::map<std::string, std::vector<std::string>> string_lists_;
63 
64  // Getter for a bool value
65  bool getBool(const std::string& key, bool default_value = false) const {
66  auto it = bools_.find(key);
67  if (it != bools_.end()) {
68  return it->second;
69  }
70  return default_value; // Default value if the key is not found
71  }
72 
73  // Check if a bool key exists
74  bool hasBool(const std::string& key) const {
75  return bools_.find(key) != bools_.end();
76  }
77 
78  // Getter for a float value
79  double getFloat(const std::string& key, double default_value = 0.0) const {
80  auto it = floats_.find(key);
81  if (it != floats_.end()) {
82  return it->second;
83  }
84  return default_value; // Default value if the key is not found
85  }
86 
87  // Check if a float key exists
88  bool hasFloat(const std::string& key) const {
89  return floats_.find(key) != floats_.end();
90  }
91 
92  // Getter for a string value
93  std::string getString(const std::string& key, std::string default_value = "") const {
94  auto it = strings_.find(key);
95  if (it != strings_.end()) {
96  return it->second;
97  }
98  return default_value; // Default value if the key is not found
99  }
100 
101  // Check if a string key exists
102  bool hasString(const std::string& key) const {
103  return strings_.find(key) != strings_.end();
104  }
105 
106  // Getter for a bool list
107  std::vector<bool> getBoolList(const std::string& key, std::vector<bool> default_value = {}) const {
108  auto it = bool_lists_.find(key);
109  if (it != bool_lists_.end()) {
110  return it->second;
111  }
112  return default_value; // Default empty list if the key is not found
113  }
114 
115  // Check if a bool list key exists
116  bool hasBoolList(const std::string& key) const {
117  return bool_lists_.find(key) != bool_lists_.end();
118  }
119 
120  // Getter for a float list
121  std::vector<double> getFloatList(const std::string& key, std::vector<double> default_value = {}) const {
122  auto it = float_lists_.find(key);
123  if (it != float_lists_.end()) {
124  return it->second;
125  }
126  return default_value; // Default empty list if the key is not found
127  }
128 
129  // Check if a float list key exists
130  bool hasFloatList(const std::string& key) const {
131  return float_lists_.find(key) != float_lists_.end();
132  }
133 
134  // Getter for a string list
135  std::vector<std::string> getStringList(const std::string& key, std::vector<std::string> default_value = {}) const {
136  auto it = string_lists_.find(key);
137  if (it != string_lists_.end()) {
138  return it->second;
139  }
140  return default_value; // Default empty list if the key is not found
141  }
142 
143  // Check if a string list key exists
144  bool hasStringList(const std::string& key) const {
145  return string_lists_.find(key) != string_lists_.end();
146  }
147  };
148 
149  // Any listed user_data keys
150  const UserData& getUserData() const { return user_data_; }
151 
152 private:
153  RobotConfig() = default;
154 
155  std::vector<std::string> names_;
156  std::vector<std::string> families_;
157  bool has_feedback_frequency_{};
158  double feedback_frequency_{};
159  bool has_command_lifetime_{};
160  double command_lifetime_{};
161  // Stored as an absolute path for reading later
162  std::string hrdf_;
163  // Stored as absolute paths for reading later
164  std::map<std::string, std::string> gains_;
165  // plugins
166  std::vector<arm::PluginConfig> plugin_configs_;
167  // Absolute path of parent directory
168  std::string location_;
169  // user_data
170  UserData user_data_;
171 };
172 
173 } // namespace hebi
const std::map< std::string, std::string > & getGains() const
Definition: robot_config.hpp:38
const std::string & getParentDirectory() const
Definition: robot_config.hpp:40
Definition: group.hpp:35
bool hasFloat(const std::string &key) const
Definition: robot_config.hpp:88
Definition: arm.cpp:10
std::vector< bool > getBoolList(const std::string &key, std::vector< bool > default_value={}) const
Definition: robot_config.hpp:107
std::map< std::string, bool > bools_
Definition: robot_config.hpp:47
std::vector< double > getFloatList(const std::string &key, std::vector< double > default_value={}) const
Definition: robot_config.hpp:121
const std::vector< std::string > & getFamilies() const
Definition: robot_config.hpp:28
bool hasFloatList(const std::string &key) const
Definition: robot_config.hpp:130
bool hasFeedbackFrequency() const
Definition: robot_config.hpp:29
bool hasBoolList(const std::string &key) const
Definition: robot_config.hpp:116
Definition: robot_config.hpp:45
double getCommandLifetime() const
Definition: robot_config.hpp:32
const UserData & getUserData() const
Definition: robot_config.hpp:150
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:36
std::map< std::string, std::string > strings_
Definition: robot_config.hpp:53
const std::vector< arm::PluginConfig > & getPluginConfigs() const
Definition: robot_config.hpp:42
std::string getString(const std::string &key, std::string default_value="") const
Definition: robot_config.hpp:93
std::map< std::string, std::vector< std::string > > string_lists_
Definition: robot_config.hpp:62
bool hasBool(const std::string &key) const
Definition: robot_config.hpp:74
std::map< std::string, std::vector< double > > float_lists_
Definition: robot_config.hpp:59
std::map< std::string, std::vector< bool > > bool_lists_
Definition: robot_config.hpp:56
std::vector< std::string > getStringList(const std::string &key, std::vector< std::string > default_value={}) const
Definition: robot_config.hpp:135
std::map< std::string, double > floats_
Definition: robot_config.hpp:50
bool hasCommandLifetime() const
Definition: robot_config.hpp:31
Definition: robot_config.hpp:13
double getFeedbackFrequency() const
Definition: robot_config.hpp:30
bool getBool(const std::string &key, bool default_value=false) const
Definition: robot_config.hpp:65
const std::string & getHrdf() const
Definition: robot_config.hpp:34
double getFloat(const std::string &key, double default_value=0.0) const
Definition: robot_config.hpp:79
bool hasStringList(const std::string &key) const
Definition: robot_config.hpp:144
bool hasString(const std::string &key) const
Definition: robot_config.hpp:102
const std::vector< std::string > & getNames() const
Definition: robot_config.hpp:26