Cheetah Software  1.0
ControlParameters Class Reference

#include <ControlParameters.h>

+ Inheritance diagram for ControlParameters:
+ Collaboration diagram for ControlParameters:

Public Member Functions

 ControlParameters (const std::string &name)
 
bool isFullyInitialized ()
 
void initializeDouble (const std::string &name, double d)
 
void initializeFloat (const std::string &name, float f)
 
void initializeInteger (const std::string &name, s64 i)
 
void initializeVec3f (const std::string &name, Vec3< float > &v)
 
void initializeVec3d (const std::string &name, Vec3< double > &v)
 
void lockMutex ()
 
void unlockMutex ()
 
void writeToIniFile (const std::string &path)
 
void initializeFromIniFile (const std::string &path)
 
void initializeFromYamlFile (const std::string &path)
 
void defineAndInitializeFromYamlFile (const std::string &path)
 
void writeToYamlFile (const std::string &path)
 
std::string generateUnitializedList ()
 

Public Attributes

ControlParameterCollection collection
 

Protected Attributes

std::string _name
 
std::mutex _mutex
 

Detailed Description

Parent class for groups of parameters RobotParameters and SimulatorParameters inherit from this class

Definition at line 391 of file ControlParameters.h.

Constructor & Destructor Documentation

ControlParameters::ControlParameters ( const std::string &  name)
inline

Each control parameter group must have a unique name so the ini files don't mixed up

Parameters
name

Definition at line 398 of file ControlParameters.h.

398 : collection(name), _name(name) {}
ControlParameterCollection collection

Member Function Documentation

void ControlParameters::defineAndInitializeFromYamlFile ( const std::string &  path)

Definition at line 280 of file ControlParameters.cpp.

References ControlParameter::_kind, ControlParameterCollection::_name, DOUBLE, f(), FLOAT, ControlParameter::initializeDouble(), ControlParameter::initializeFloat(), ControlParameter::initializeInteger(), ControlParameter::initializeVec3d(), ControlParameter::initializeVec3f(), S64, VEC3_DOUBLE, VEC3_FLOAT, and YAML_COLLECTION_NAME_KEY.

280  {
281  ParamHandler paramHandler(path);
282 
283  if (!paramHandler.fileOpenedSuccessfully()) {
284  printf(
285  "[ERROR] Could not open yaml file %s : not initializing control "
286  "parameters!\n",
287  path.c_str());
288  throw std::runtime_error("yaml file bad");
289  }
290 
291  std::string name;
292  if (!paramHandler.getString(YAML_COLLECTION_NAME_KEY, name)) {
293  printf("[ERROR] YAML doesn't have a a collection name field named %s\n",
295  throw std::runtime_error("yaml file bad");
296  }
297 
298  if (name != _name) {
299  printf(
300  "[ERROR] YAML file %s has collection name %s which cannot be used to "
301  "initialize %s\n",
302  path.c_str(), name.c_str(), _name.c_str());
303  throw std::runtime_error("yaml file bad");
304  }
305 
306  std::vector<std::string> keys = paramHandler.getKeys();
307 
308  for (auto& key : keys) {
309  if (key == YAML_COLLECTION_NAME_KEY) continue;
310  std::string valueString;
311  paramHandler.getString(key, valueString);
313  if(valueString.empty()) {
315  } else {
317  }
318 
319  ControlParameter* cp = new ControlParameter(key, kind);
320  collection.addParameter(cp, key);
321  switch (cp->_kind) {
323  double d;
324  assert(paramHandler.getValue(key, d));
325  cp->initializeDouble(d);
326  } break;
327 
329  float f;
330  assert(paramHandler.getValue(key, f));
331  cp->initializeFloat(f);
332  } break;
333 
335  s64 f;
336  assert(paramHandler.getValue(key, f));
337  cp->initializeInteger(f);
338  } break;
339 
341  std::vector<double> vv;
342  assert(paramHandler.getVector(key, vv));
343  assert(vv.size() == 3);
344  Vec3<double> v(vv[0], vv[1], vv[2]);
345  cp->initializeVec3d(v);
346  } break;
347 
349  std::vector<float> vv;
350  assert(paramHandler.getVector(key, vv));
351  assert(vv.size() == 3);
352  Vec3<float> v(vv[0], vv[1], vv[2]);
353  cp->initializeVec3f(v);
354  } break;
355 
356  default:
357  throw std::runtime_error("can't read type " +
358  std::to_string((u32)cp->_kind) +
359  " from yaml file");
360  break;
361  }
362  }
363 }
ControlParameterCollection collection
ControlParameterValueKind _kind
ControlParameterValueKind
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
int64_t s64
Definition: cTypes.h:24
void initializeVec3d(const Vec3< double > &v)
void initializeInteger(s64 i)
void initializeDouble(double d)
uint32_t u32
Definition: cTypes.h:18
#define YAML_COLLECTION_NAME_KEY
void initializeVec3f(const Vec3< float > &v)
void initializeFloat(float f)
void addParameter(ControlParameter *param, const std::string &name)
MX f(const MX &x, const MX &u)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string ControlParameters::generateUnitializedList ( )

Definition at line 89 of file ControlParameters.cpp.

89  {
90  std::string result;
91  for (auto& kv : collection._map) {
92  if (!kv.second->_set) {
93  result += kv.second->_name + " :\n";
94  }
95  }
96 
97  return result;
98 }
ControlParameterCollection collection
std::map< std::string, ControlParameter * > _map

+ Here is the caller graph for this function:

void ControlParameters::initializeDouble ( const std::string &  name,
double  d 
)
inline

Directly initialize a given control parameter

Definition at line 408 of file ControlParameters.h.

408  {
410  }
ControlParameterCollection collection
void initializeDouble(double d)
ControlParameter & lookup(const std::string &name)

+ Here is the caller graph for this function:

void ControlParameters::initializeFloat ( const std::string &  name,
float  f 
)
inline

Definition at line 412 of file ControlParameters.h.

412  {
414  }
ControlParameterCollection collection
ControlParameter & lookup(const std::string &name)
void initializeFloat(float f)
MX f(const MX &x, const MX &u)

+ Here is the caller graph for this function:

void ControlParameters::initializeFromIniFile ( const std::string &  path)

Definition at line 204 of file ControlParameters.cpp.

References ControlParameter::_kind, ControlParameterCollection::_name, DOUBLE, FLOAT, ControlParameter::initializeDouble(), ControlParameter::initializeFloat(), ControlParameter::initializeInteger(), and S64.

204  {
205  INIReader iniReader(path);
206  if (iniReader.ParseError() < 0) {
207  printf(
208  "[ERROR] Could not open ini file %s : not initializing control "
209  "parameters!\n",
210  path.c_str());
211  throw std::runtime_error("ini file bad");
212  }
213 
214  std::set<std::string> sections = iniReader.GetSections();
215 
216  if (sections.size() != 1) {
217  printf(
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");
222  }
223 
224  std::string sectionName = *(sections.begin());
225 
226  if (sectionName != _name) {
227  printf(
228  "[ERROR] INI file %s has section name %s, which cannot be used to "
229  "initialize %s\n",
230  path.c_str(), sectionName.c_str(), _name.c_str());
231  throw std::runtime_error("ini file bad");
232  }
233 
234  std::set<std::string> parameterNames = iniReader.GetFields(sectionName);
235 
236  for (auto& name : parameterNames) {
237  ControlParameter& cp = collection.lookup(name);
238  switch (cp._kind) {
240  cp.initializeDouble(iniReader.GetReal(sectionName, name, 0.));
241  break;
243  cp.initializeFloat((float)iniReader.GetReal(sectionName, name, 0.));
244  break;
246  cp.initializeInteger(iniReader.GetInteger(sectionName, name, 0));
247  break;
248  default:
249  throw std::runtime_error("can't read type " +
250  std::to_string((u32)cp._kind) +
251  " from ini file");
252  break;
253  }
254  }
255 }
ControlParameterCollection collection
ControlParameterValueKind _kind
void initializeInteger(s64 i)
void initializeDouble(double d)
uint32_t u32
Definition: cTypes.h:18
ControlParameter & lookup(const std::string &name)
void initializeFloat(float f)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ControlParameters::initializeFromYamlFile ( const std::string &  path)

Definition at line 365 of file ControlParameters.cpp.

References ControlParameter::_kind, ControlParameterCollection::_name, DOUBLE, f(), FLOAT, ControlParameter::initializeDouble(), ControlParameter::initializeFloat(), ControlParameter::initializeInteger(), ControlParameter::initializeVec3d(), ControlParameter::initializeVec3f(), S64, VEC3_DOUBLE, VEC3_FLOAT, and YAML_COLLECTION_NAME_KEY.

365  {
366  ParamHandler paramHandler(path);
367 
368  if (!paramHandler.fileOpenedSuccessfully()) {
369  printf(
370  "[ERROR] Could not open yaml file %s : not initializing control "
371  "parameters!\n",
372  path.c_str());
373  throw std::runtime_error("yaml file bad");
374  }
375 
376  std::string name;
377  if (!paramHandler.getString(YAML_COLLECTION_NAME_KEY, name)) {
378  printf("[ERROR] YAML doesn't have a a collection name field named %s\n",
380  throw std::runtime_error("yaml file bad");
381  }
382 
383  if (name != _name) {
384  printf(
385  "[ERROR] YAML file %s has collection name %s which cannot be used to "
386  "initialize %s\n",
387  path.c_str(), name.c_str(), _name.c_str());
388  throw std::runtime_error("yaml file bad");
389  }
390 
391  std::vector<std::string> keys = paramHandler.getKeys();
392 
393  for (auto& key : keys) {
394  if (key == YAML_COLLECTION_NAME_KEY) continue;
396  switch (cp._kind) {
398  double d;
399  assert(paramHandler.getValue(key, d));
400  cp.initializeDouble(d);
401  } break;
402 
404  float f;
405  assert(paramHandler.getValue(key, f));
406  cp.initializeFloat(f);
407  } break;
408 
410  s64 f;
411  assert(paramHandler.getValue(key, f));
412  cp.initializeInteger(f);
413  } break;
414 
416  std::vector<double> vv;
417  assert(paramHandler.getVector(key, vv));
418  assert(vv.size() == 3);
419  Vec3<double> v(vv[0], vv[1], vv[2]);
420  cp.initializeVec3d(v);
421  } break;
422 
424  std::vector<float> vv;
425  assert(paramHandler.getVector(key, vv));
426  assert(vv.size() == 3);
427  Vec3<float> v(vv[0], vv[1], vv[2]);
428  cp.initializeVec3f(v);
429  } break;
430 
431  default:
432  throw std::runtime_error("can't read type " +
433  std::to_string((u32)cp._kind) +
434  " from yaml file");
435  break;
436  }
437  }
438 }
ControlParameterCollection collection
ControlParameterValueKind _kind
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
int64_t s64
Definition: cTypes.h:24
void initializeVec3d(const Vec3< double > &v)
void initializeInteger(s64 i)
void initializeDouble(double d)
uint32_t u32
Definition: cTypes.h:18
#define YAML_COLLECTION_NAME_KEY
ControlParameter & lookup(const std::string &name)
void initializeVec3f(const Vec3< float > &v)
void initializeFloat(float f)
MX f(const MX &x, const MX &u)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ControlParameters::initializeInteger ( const std::string &  name,
s64  i 
)
inline

Definition at line 416 of file ControlParameters.h.

416  {
418  }
ControlParameterCollection collection
void initializeInteger(s64 i)
ControlParameter & lookup(const std::string &name)

+ Here is the caller graph for this function:

void ControlParameters::initializeVec3d ( const std::string &  name,
Vec3< double > &  v 
)
inline

Definition at line 424 of file ControlParameters.h.

424  {
426  }
ControlParameterCollection collection
void initializeVec3d(const Vec3< double > &v)
ControlParameter & lookup(const std::string &name)

+ Here is the caller graph for this function:

void ControlParameters::initializeVec3f ( const std::string &  name,
Vec3< float > &  v 
)
inline

Definition at line 420 of file ControlParameters.h.

420  {
422  }
ControlParameterCollection collection
ControlParameter & lookup(const std::string &name)
void initializeVec3f(const Vec3< float > &v)

+ Here is the caller graph for this function:

bool ControlParameters::isFullyInitialized ( )
inline

If true, all parameters have been initialized in one way or another

Definition at line 403 of file ControlParameters.h.

403 { return collection.checkIfAllSet(); }
ControlParameterCollection collection
bool checkIfAllSet()
are all the control parameters initialized?

+ Here is the caller graph for this function:

void ControlParameters::lockMutex ( )
inline

Definition at line 428 of file ControlParameters.h.

428 { _mutex.lock(); }

+ Here is the caller graph for this function:

void ControlParameters::unlockMutex ( )
inline

Definition at line 430 of file ControlParameters.h.

430 { _mutex.unlock(); }

+ Here is the caller graph for this function:

void ControlParameters::writeToIniFile ( const std::string &  path)

Definition at line 196 of file ControlParameters.cpp.

References writeStringToFile().

196  {
198 }
ControlParameterCollection collection
std::string printToIniString()
print all control parameters in the INI file format
void writeStringToFile(const std::string &fileName, const std::string &fileData)
Definition: utilities.cpp:7

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ControlParameters::writeToYamlFile ( const std::string &  path)

Definition at line 200 of file ControlParameters.cpp.

References writeStringToFile().

200  {
202 }
ControlParameterCollection collection
void writeStringToFile(const std::string &fileName, const std::string &fileData)
Definition: utilities.cpp:7

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

std::mutex ControlParameters::_mutex
protected

Definition at line 445 of file ControlParameters.h.

std::string ControlParameters::_name
protected

Definition at line 444 of file ControlParameters.h.

ControlParameterCollection ControlParameters::collection

Definition at line 441 of file ControlParameters.h.


The documentation for this class was generated from the following files: