controllerclientcpp  0.6.1
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義 ページ
mujinshowresults.cpp
説明を見る。
1 // -*- coding: utf-8 -*-
7 
8 #include <iostream>
9 #include <vector>
10 
11 using namespace std;
12 using namespace mujinclient;
13 
14 int main(int argc, char ** argv)
15 {
16  if( argc < 2 ) {
17  std::cout << "need username:password. Example: mujinclienttest myuser:mypass [url]\n\nurl - [optional] For example https://controller.mujin.co.jp/" << std::endl;
18  return 1;
19  }
20  try {
21  ControllerClientPtr controller;
22  if( argc >= 5 ) {
23  controller = CreateControllerClient(argv[1], argv[2], argv[3], argv[4]);
24  }
25  if( argc == 4 ) {
26  controller = CreateControllerClient(argv[1], argv[2], argv[3]);
27  }
28  else if( argc == 3 ) {
29  controller = CreateControllerClient(argv[1], argv[2]);
30  }
31  else {
32  controller = CreateControllerClient(argv[1]);
33  }
34  std::cout << "connected to controller v" << controller->GetVersion() << std::endl;
35 
36  // get all supported keys
37  std::vector<std::string> scenekeys;
38  controller->GetScenePrimaryKeys(scenekeys);
39 
40  cout << "user has " << scenekeys.size() << " scenes: " << endl;
41  for(size_t i = 0; i < scenekeys.size(); ++i) {
42  cout << scenekeys[i] << endl;
43  }
44 
45  SceneResourcePtr scene;
46  // if YG_LAYOUT exists, open it, otherwise open the first file
47  if( find(scenekeys.begin(),scenekeys.end(), string("YG_LAYOUT")) != scenekeys.end() ) {
48  scene.reset(new SceneResource(controller, "YG_LAYOUT"));
49  }
50  else {
51  cout << "opening scene " << scenekeys.at(0) << endl;
52  scene.reset(new SceneResource(controller, scenekeys.at(0)));
53  }
54 
55  // open the first task
56  std::vector<std::string> taskkeys;
57  scene->GetTaskPrimaryKeys(taskkeys);
58  if( taskkeys.size() == 0 ) {
59  std::cout << "no tasks for this scene" << std::endl;
60  return 0;
61  }
62  TaskResourcePtr task(new TaskResource(controller, taskkeys.at(0)));
63 
64  cout << "got task " << task->Get("name") << endl;
65  cout << "program is " << task->Get("taskgoalxml") << endl;
66 
67  PlanningResultResourcePtr result = task->GetResult();
68  EnvironmentState envstate;
69  if( !!result ) {
70  cout << "result for task exists and can be completed in " << result->Get("task_time") << " seconds." << endl;
71  }
72 
73  // get the first optimization
74  std::vector<std::string> optimizationkeys;
75  task->GetOptimizationPrimaryKeys(optimizationkeys);
76 
77  OptimizationResourcePtr optimization(new OptimizationResource(controller, optimizationkeys.at(0)));
78  cout << "found optimization " << optimization->Get("name") << endl;
79 
80  std::vector<PlanningResultResourcePtr> results;
81  optimization->GetResults(results,0,10);
82  if( results.size() > 0 ) {
83  cout << "the top results have times: ";
84  for(size_t i = 0; i < results.size(); ++i) {
85  cout << results[i]->Get("task_time") << ", ";
86  }
87  cout << endl;
88 
89  PlanningResultResourcePtr bestresult = results.at(0);
90  bestresult->GetEnvironmentState(envstate);
91  cout << "robot position of best result is: ";
92  for(EnvironmentState::iterator it = envstate.begin(); it != envstate.end(); ++it) {
93  InstanceObjectState objstate = it->second;
94  // for now only output translate
95  cout << it->first << "=(" << objstate.transform.translate[0] << ", " << objstate.transform.translate[1] << ", " << objstate.transform.translate[2] << "), ";
96  }
97  cout << endl;
98 
99  RobotControllerPrograms programs;
100  result->GetPrograms(programs);
101  std::cout << "found " << programs.programs.size() << " programs" << std::endl;
102  for(std::map<std::string, RobotProgramData>::iterator it = programs.programs.begin(); it != programs.programs.end(); ++it ) {
103  std::cout << "[" << it->first << "]" << std::endl << it->second.programdata << std::endl << std::endl;
104  }
105  std::cout << "final task_time is " << result->Get("task_time") << std::endl;
106  }
107  }
108  catch(const MujinException& ex) {
109  std::cout << "exception thrown: " << ex.message() << std::endl;
110  }
111  // destroy all mujin controller resources
113 }