controllerclientcpp  0.6.1
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義 ページ
mujinideal_densowave.cpp
説明を見る。
1 // -*- coding: utf-8 -*-
7 
8 #include <boost/thread/thread.hpp> // for sleep
9 
10 #include <iostream>
11 
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, controller2;
22  // licensekey "username:password"
23  //controller = CreateControllerClient(licensekey);
24  if( argc >= 5 ) {
25  controller = CreateControllerClient(argv[1], argv[2], argv[3], argv[4]);
26  }
27  if( argc == 4 ) {
28  controller = CreateControllerClient(argv[1], argv[2], argv[3]);
29  }
30  else if( argc == 3 ) {
31  controller = CreateControllerClient(argv[1], argv[2]);
32  }
33  else {
34  controller = CreateControllerClient(argv[1]);
35  }
36  std::cout << "connected to controller v" << controller->GetVersion() << std::endl;
37 
38  controller->SetDefaultSceneType("wincaps");
39  controller->SetDefaultTaskType("itlplanning");
40  controller->SetLanguage("ja");
41 
42  std::string sceneuri = "mujin:/vs060a3_test0/test0.WPJ";
43  std::string scenepk = controller->GetScenePrimaryKeyFromURI_UTF8(sceneuri);
44 
45  // upload a Wincaps file
46  controller->SyncUpload_UTF8("../share/mujincontrollerclient/densowave_wincaps_data/vs060a3_test0/test0.WPJ", "mujin:/vs060a3_test0/", "wincaps");
47 
48  // Register the scene, this also imports all PAC Script programs into "itlplanning" tasks
49  SceneResourcePtr scene = controller->RegisterScene_UTF8(sceneuri, "wincaps");
50 
51  // test0.WPJ should have a grabtest.pcs PAC script registered
52  // Query the task and execute it
53  TaskResourcePtr task = scene->GetOrCreateTaskFromName_UTF8("grabtest", "itlplanning");
54 
55  // [Optional] get the task parameters and set optimiation value to 0.5
56  ITLPlanningTaskParameters taskparameters;
57  task->GetTaskParameters(taskparameters);
58  taskparameters.optimizationvalue = 0.5;
59  task->SetTaskParameters(taskparameters);
60 
61  // start task execution
62  task->Execute();
63 
64  std::cout << "waiting for task result" << std::endl;
65  // query the results until they are complete, should take several seconds
67  JobStatus status;
68  int iterations = 0, maxiterations = 4000;
69  while (1) {
70  result = task->GetResult();
71  if( !!result ) {
72  break;
73  }
74  task->GetRunTimeStatus(status);
75  std::cout << "current job status=" << status.code << ": " << status.message << std::endl;
76  if( status.code == JSC_Succeeded ) {
77  break;
78  }
79  else if( status.code == JSC_Unknown ) {
80  // wait 30s, then fail
81  if( iterations > 3 ) {
82  std::cout << "task won't start for some reason" << std::endl;
83  return 1;
84  }
85  }
86  else if( status.code == JSC_Aborted || status.code == JSC_Preempted ) {
87  std::cout << "task failed execution " << std::endl;
88  return 1;
89  }
90  else if( status.code != JSC_Active && status.code != JSC_Pending && status.code != JSC_Succeeded ) {
91  std::cout << "unexpected job status so quitting " << std::endl;
92  return 1;
93  }
94 
95  boost::this_thread::sleep(boost::posix_time::milliseconds(5000)); // 5s
96  ++iterations;
97  if( iterations > maxiterations ) {
98  controller->CancelAllJobs();
99  throw MujinException("operation timed out, cancelling all jobs and quitting", MEC_Timeout);
100  }
101  }
102 
103  std::string errormessage = result->Get("errormessage");
104  if( errormessage.size() > 0 ) {
105  std::cout << "task failed with: " << errormessage << std::endl;
106  return 2;
107  }
108 
109  // task succeed, so get the results
110  RobotControllerPrograms programs;
111  result->GetPrograms(programs);
112  std::cout << "found " << programs.programs.size() << " programs" << std::endl;
113  for(std::map<std::string, RobotProgramData>::iterator it = programs.programs.begin(); it != programs.programs.end(); ++it ) {
114  std::cout << "[" << it->first << "]" << std::endl << it->second.programdata << std::endl << std::endl;
115  }
116  std::string sOriginalTaskTime = result->Get("task_time");
117  std::cout << "final task_time is " << sOriginalTaskTime << std::endl;
118 
119  OptimizationResourcePtr optimization = task->GetOrCreateOptimizationFromName_UTF8("myopt0","robotplacement");
120 
122  optparams.targetname = "0 instobject ";
123  optparams.framename = "0 robot"; // use the robot frame
124  optparams.unit = "mm";
125  optparams.minrange[0] = -100; // -X
126  optparams.maxrange[0] = 100; // +X
127  optparams.stepsize[0] = 50; // dX
128  // don't move in y
129  optparams.minrange[1] = 0; // -Y
130  optparams.maxrange[1] = 0; // +Y
131  optparams.stepsize[1] = 0; // dY
132  // don't move in z
133  optparams.minrange[2] = 0; // -Z
134  optparams.maxrange[2] = 0; // +Z
135  optparams.stepsize[2] = 0; // dZ
136  // 4 angles, each 90 degrees apart
137  optparams.minrange[3] = -180; // -angle
138  optparams.maxrange[3] = 90; // +angle
139  optparams.stepsize[3] = 90; // dangle
140  optparams.ignorebasecollision = 0;
141  optparams.topstorecandidates = 20; // store only the top 20 candidates
142  optimization->SetOptimizationParameters(optparams);
143 
144  optimization->Execute();
145 
146  std::vector< PlanningResultResourcePtr > results;
147 
148  iterations = 0;
149  while(1) {
150  optimization->GetRunTimeStatus (status);
151 
152  std::cout << "current job status=" << status.code << ": " << status.message << std::endl;
153  if( status.code == JSC_Succeeded ) {
154  break;
155  }
156  else if( status.code == JSC_Unknown ) {
157  // wait 30s, then fail
158  if( iterations > 3 ) {
159  std::cout << "task won't start for some reason" << std::endl;
160  return 1;
161  }
162  }
163  else if( status.code == JSC_Aborted || status.code == JSC_Preempted ) {
164  std::cout << "task failed execution " << std::endl;
165  return 1;
166  }
167  else if( status.code != JSC_Active && status.code != JSC_Pending && status.code != JSC_Succeeded ) {
168  std::cout << "unexpected job status so quitting " << std::endl;
169  return 1;
170  }
171 
172  optimization->GetResults(results, 0, 1);
173  if( results.size() > 0 ) {
174  std::cout << "top result task_time=" << results.at(0)->Get("task_time") << " seconds, original task = " << sOriginalTaskTime << " seconds" << std::endl;
175  }
176  boost::this_thread::sleep(boost::posix_time::milliseconds(5000)); // 5s
177 
178  ++iterations;
179  if( iterations > maxiterations ) {
180  controller->CancelAllJobs();
181  throw MujinException("operation timed out, cancelling all jobs and quitting", MEC_Timeout);
182  }
183  }
184 
185  std::cout << "optimiation completed!" << std::endl;
186  }
187  catch(const MujinException& ex) {
188  std::cout << "exception thrown: " << ex.message() << std::endl;
189  }
191 }