main.cpp
Go to the documentation of this file.
1 
18 #include <cstdio>
19 #include <iostream>
20 #include <cstring>
21 #include <fstream>
22 
23 #include <boost/program_options.hpp>
24 
25 #include <log.h>
26 #include <frame.h>
27 #include <datapairs.h>
28 #include <value_setter.h>
29 #include <optimizer_goal.h>
34 #include <optimizer_container.h>
35 #include <optimizer_iv.h>
36 
37 using namespace robotLibPbD;
38 namespace po = boost::program_options;
39 
40 // main function
41 int main(int argc, char *argv[])
42 {
43  std::string filename, initial, cfg;
44  unsigned int iterations;
45  bool useRandom;
46  double trim, epsilon;
47  bool useShowResult;
48  //unsigned int trimming, examples;
49  unsigned int examplesMax;
50  unsigned int counterMod;
51  double ivLineWidth, ivSphereRadius, ivCoordScale;
52 
53 
54  po::options_description desc("Usage : kinematic_chain_optimizer [options]");
55  desc.add_options()
56  ( "help,h","show help screen")
57  ( "exit","show only initial error")
58  ( "filename",po::value<std::string>(&filename)->default_value("data/calib_dump"),"load data from filename")
59  ( "initial",po::value<std::string>(&initial)->default_value(""), "Initial value for optimization, e.g. 0.6 0.4 0.3 (n = 3)")
60  ( "cfg",po::value<std::string>(&cfg)->default_value("data/frames.xml"),"load cfg from filename")
61  ( "debug",po::value<int>(&globalLog.debugLevel)->default_value(5),"debug level")
62  ( "trim",po::value<double>(&trim)->default_value(0.2),"trimming percentage")
63  ( "epsilon",po::value<double>(&epsilon)->default_value(1.0e-8),"convergence limit, 0.00000001")
64  ( "random",po::value<bool>(&useRandom)->default_value(false),"use random start value" )
65  ( "always",po::value<bool>(&useShowResult)->default_value(false),"always show result" )
66  ( "max",po::value<unsigned int>(&examplesMax)->default_value(10000000),"use max number of data" )
67  ( "iterations",po::value<unsigned int>(&iterations)->default_value(500),"use max number of iterations" )
68  ( "debugiterations",po::value<unsigned int>(&counterMod)->default_value(100),"show result each x iterations" )
69  ( "ivlinewidth",po::value<double>(&ivLineWidth)->default_value(0.5),"width of Inventor line")
70  ( "ivsphereradius",po::value<double>(&ivSphereRadius)->default_value(1.0),"radius of Inventor sphere")
71  ( "ivcoordscale",po::value<double>(&ivCoordScale)->default_value(0.2),"scaleFactor of Inventor coordinate system")
72  ;
73 
74  po::variables_map vm;
75  po::store(po::parse_command_line(argc, argv, desc), vm);
76  po::notify(vm);
77 
78  if (vm.count("help")) {
79  std::cout << desc <<std::endl;
80  return 0;
81  }
82 
83  bool doQuit = vm.count("exit");
84 
85  // parse initial values
86  std::vector<double> initialValues;
87  strToArray(initial, initialValues);
88 
89  // set random generator
90  unsigned int seed = (unsigned int) getTickCount();
91  LOG_MSG(2, "Random Generator Seed is %d\n", seed);
92 
93  setUniformSeed(seed);
94  srand(seed);
95 
96  COptimizerIv optimizer;
97 
98  // set parameters
99  optimizer.setEpsilon(epsilon);
100  optimizer.setTrimming(trim);
101  optimizer.setIterations(iterations);
102  optimizer.setRandomStart(useRandom);
103  optimizer.setQuit(doQuit);
104  optimizer.setDataMax(examplesMax);
105  optimizer.setShowResult(useShowResult);
106  optimizer.setShowModulo(counterMod);
107  optimizer.setIvLineWidth(ivLineWidth);
108  optimizer.setIvSphereRadius(ivSphereRadius);
109  optimizer.setIvCoordScale(ivCoordScale);
110 
111  optimizer.load(cfg, filename);
112 
113  optimizer.run(initialValues);
114 
115  COptimizerResult result;
116  if (!optimizer.getResult(result))
117  {
118  LOG_MSG(1, "Error: Optimization failed\n.");
119  }
120  else
121  {
122  printf("Showing final result:\n");
123  printf("Value: %f\n", result.result);
124  printf("Values: %s\n", arrayToString(result.values).c_str());
125 
126  for (unsigned int i=0; i<result.optimizedPoses.size(); i++)
127  {
128  CMatrix pose = result.optimizedPoses[i].second;
129  CVec position, quater;
130  position = pose[3];
131  CMathLib::quaternionFromMatrix(pose, quater);
132 
133  printf("Name: %s Position (xyz): %f %f %f Quaternion (wxyz): %f %f %f %f\n", result.optimizedPoses[i].first.c_str(),
134  position.x, position.y, position.z,
135  quater.w, quater.x, quater.y, quater.z);
136  }
137 
138  optimizer.generateInventor(result.values, "data/plot.iv");
139  }
140 
141  return 0;
142 }
143 
144 
PRECISION z
Definition: vecmath.h:60
void setShowModulo(unsigned int counterMod)
Definition: optimizer.h:105
std::vector< std::pair< std::string, CMatrix > > optimizedPoses
Definition: optimizer.h:42
void run(const std::vector< double > &initialValues)
Definition: optimizer.cpp:465
CLog globalLog
Definition: log.cpp:28
bool getResult(COptimizerResult &out)
Definition: optimizer.cpp:569
void setIterations(unsigned int iterations)
Definition: optimizer.h:100
void setIvLineWidth(double ivLineWidth)
Definition: optimizer_iv.h:38
bool generateInventor(std::vector< double > values, std::string filename, std::string ivCoordFilename="data/coord.iv")
std::vector< double > values
Definition: optimizer.h:43
std::string arrayToString(std::vector< T > &array)
Definition: utils.h:84
int debugLevel
Definition: log.h:49
PRECISION w
Definition: vecmath.h:60
static void quaternionFromMatrix(const CMatrix &mat, CVec &quaternion)
Transforms a homogenous matrix into quaternion representation.
Definition: vecmath.cpp:525
void setRandomStart(bool useRandom)
Definition: optimizer.h:101
void setIvSphereRadius(double ivSphereRadius)
Definition: optimizer_iv.h:39
void setUniformSeed(unsigned int seed)
Definition: vecmath.cpp:1059
Homogenous vector.
Definition: vecmath.h:57
void setQuit(bool doQuit)
Definition: optimizer.h:102
Homogenous matrix.
Definition: vecmath.h:187
#define LOG_MSG(index, format,...)
Definition: log.h:32
void setEpsilon(double eps)
Definition: optimizer.h:98
void setDataMax(unsigned int examplesMax)
Definition: optimizer.h:103
void setTrimming(double trim)
Definition: optimizer.h:99
void setShowResult(bool useShowResult)
Definition: optimizer.h:104
void strToArray(std::string text, std::vector< double > &result, std::string delimiter=" ")
Definition: utils.cpp:161
void load(std::string cfg, std::string data, unsigned int start=0)
Definition: optimizer.cpp:451
void setIvCoordScale(double ivCoordScale)
Definition: optimizer_iv.h:40
PRECISION y
Definition: vecmath.h:60
int main(int argc, char *argv[])
Definition: main.cpp:41
PRECISION x
Definition: vecmath.h:60
unsigned long getTickCount()
Definition: utils.cpp:351


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:35:36