9 #include "ParamHandler.hpp" 14 #define YAML_COLLECTION_NAME_KEY "__collection-name__" 30 return "unknown-ControlParameterValueKind";
45 result += std::to_string(value.
i);
60 result +=
"<unknown type " + std::to_string((
u32)(kind)) +
61 "> (add it yourself in ControlParameterInterface.h!)";
68 for (
auto& kv :
_map) {
69 if (!kv.second->_set) {
77 for (
auto& kv :
_map) {
78 kv.second->_set =
false;
83 for(
auto& kv :
_map) {
91 for (
auto& kv : collection._map) {
92 if (!kv.second->_set) {
93 result += kv.second->_name +
" :\n";
105 result +=
_name +
"]\n\n";
107 std::vector<std::string> lines;
110 int maxLength_name = 0;
111 for (
auto& kv :
_map) {
112 maxLength_name = std::max(maxLength_name, (
int)kv.first.length());
113 lines.push_back(kv.first);
118 int maxLength_number = 0;
119 for (
auto& kv : _map) {
120 int charsToAdd = maxLength_name - (int)lines[i].length();
121 assert(charsToAdd >= 0);
122 for (
int j = 0; j < charsToAdd; j++) {
123 lines[i].push_back(
' ');
126 lines[i] += kv.second->toString();
127 maxLength_number = std::max(maxLength_number, (
int)lines[i].length());
133 for (
auto& kv : _map) {
134 int charsToAdd = maxLength_number - (int)lines[i].length();
135 assert(charsToAdd >= 0);
136 for (
int j = 0; j < charsToAdd; j++) {
137 lines[i].push_back(
' ');
140 if (kv.second->_units.empty()) {
141 lines[i] +=
"No units specified. Add them!";
143 lines[i] += kv.second->_units;
150 for (
auto& line : lines) {
151 result += line +
"\n";
162 result +=
_name +
"\n\n";
164 std::vector<std::string> lines;
167 int maxLength_name = 0;
168 for (
auto& kv :
_map) {
169 maxLength_name = std::max(maxLength_name, (
int)kv.first.length());
170 lines.push_back(kv.first);
175 int maxLength_number = 0;
176 for (
auto& kv : _map) {
177 int charsToAdd = maxLength_name - (int)lines[i].length();
178 assert(charsToAdd >= 0);
179 for (
int j = 0; j < charsToAdd; j++) {
180 lines[i].push_back(
' ');
183 lines[i] += kv.second->toString();
184 maxLength_number = std::max(maxLength_number, (
int)lines[i].length());
189 for (
auto& line : lines) {
190 result += line +
"\n";
205 INIReader iniReader(path);
206 if (iniReader.ParseError() < 0) {
208 "[ERROR] Could not open ini file %s : not initializing control " 211 throw std::runtime_error(
"ini file bad");
214 std::set<std::string> sections = iniReader.GetSections();
216 if (sections.size() != 1) {
218 "[ERROR] INI file %s had %ld sections (expected 1) : not initializing " 219 "control parameters\n",
220 path.c_str(), sections.size());
221 throw std::runtime_error(
"ini file bad");
224 std::string sectionName = *(sections.begin());
226 if (sectionName !=
_name) {
228 "[ERROR] INI file %s has section name %s, which cannot be used to " 230 path.c_str(), sectionName.c_str(),
_name.c_str());
231 throw std::runtime_error(
"ini file bad");
234 std::set<std::string> parameterNames = iniReader.GetFields(sectionName);
236 for (
auto& name : parameterNames) {
249 throw std::runtime_error(
"can't read type " +
258 if(str.find(
'[') != std::string::npos) {
260 if(str.find(
'f') != std::string::npos) {
267 if(str.find(
'.') != std::string::npos) {
268 if(str.find(
'f') != std::string::npos) {
281 ParamHandler paramHandler(path);
283 if (!paramHandler.fileOpenedSuccessfully()) {
285 "[ERROR] Could not open yaml file %s : not initializing control " 288 throw std::runtime_error(
"yaml file bad");
293 printf(
"[ERROR] YAML doesn't have a a collection name field named %s\n",
295 throw std::runtime_error(
"yaml file bad");
300 "[ERROR] YAML file %s has collection name %s which cannot be used to " 302 path.c_str(), name.c_str(),
_name.c_str());
303 throw std::runtime_error(
"yaml file bad");
306 std::vector<std::string> keys = paramHandler.getKeys();
308 for (
auto& key : keys) {
310 std::string valueString;
311 paramHandler.getString(key, valueString);
313 if(valueString.empty()) {
320 collection.addParameter(cp, key);
324 assert(paramHandler.getValue(key, d));
330 assert(paramHandler.getValue(key, f));
336 assert(paramHandler.getValue(key, f));
341 std::vector<double> vv;
342 assert(paramHandler.getVector(key, vv));
343 assert(vv.size() == 3);
349 std::vector<float> vv;
350 assert(paramHandler.getVector(key, vv));
351 assert(vv.size() == 3);
357 throw std::runtime_error(
"can't read type " +
366 ParamHandler paramHandler(path);
368 if (!paramHandler.fileOpenedSuccessfully()) {
370 "[ERROR] Could not open yaml file %s : not initializing control " 373 throw std::runtime_error(
"yaml file bad");
378 printf(
"[ERROR] YAML doesn't have a a collection name field named %s\n",
380 throw std::runtime_error(
"yaml file bad");
385 "[ERROR] YAML file %s has collection name %s which cannot be used to " 387 path.c_str(), name.c_str(),
_name.c_str());
388 throw std::runtime_error(
"yaml file bad");
391 std::vector<std::string> keys = paramHandler.getKeys();
393 for (
auto& key : keys) {
399 assert(paramHandler.getValue(key, d));
405 assert(paramHandler.getValue(key, f));
411 assert(paramHandler.getValue(key, f));
416 std::vector<double> vv;
417 assert(paramHandler.getVector(key, vv));
418 assert(vv.size() == 3);
424 std::vector<float> vv;
425 assert(paramHandler.getVector(key, vv));
426 assert(vv.size() == 3);
432 throw std::runtime_error(
"can't read type " +
ControlParameterValueKind getControlParameterValueKindFromString(const std::string &str)
std::string getCurrentTimeAndDate()
ControlParameterValueKind _kind
ControlParameterValueKind
std::map< std::string, ControlParameter * > _map
std::string printToIniString()
print all control parameters in the INI file format
std::string numberToString(T number)
typename Eigen::Matrix< T, 3, 1 > Vec3
void initializeVec3d(const Vec3< double > &v)
void initializeInteger(s64 i)
std::string controlParameterValueToString(ControlParameterValue value, ControlParameterValueKind kind)
void initializeDouble(double d)
void writeStringToFile(const std::string &fileName, const std::string &fileData)
void writeToYamlFile(const std::string &path)
#define YAML_COLLECTION_NAME_KEY
bool checkIfAllSet()
are all the control parameters initialized?
void writeToIniFile(const std::string &path)
std::string printToYamlString()
void initializeVec3f(const Vec3< float > &v)
void defineAndInitializeFromYamlFile(const std::string &path)
void initializeFloat(float f)
Interface to set gains/control parameters for simulator and robot These are designed to be updated in...
void initializeFromIniFile(const std::string &path)
std::string controlParameterValueKindToString(ControlParameterValueKind valueKind)
void initializeFromYamlFile(const std::string &path)
std::string generateUnitializedList()
MX f(const MX &x, const MX &u)