controllerclientcpp  0.6.1
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義 ページ
mujinexecutetask_fast.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;
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  std::string sceneuri = "mujin:/densowave_wincaps_data/vs060a3_test0/test0.WPJ";
37  std::string scenepk = controller->GetScenePrimaryKeyFromURI_UTF8(sceneuri);
38 
39  controller->SyncUpload_UTF8("../share/mujincontrollerclient/densowave_wincaps_data/vs060a3_test0/test0.WPJ", "mujin:/densowave_wincaps_data/vs060a3_test0/", "wincaps");
40  SceneResourcePtr scene = controller->RegisterScene_UTF8(sceneuri, "wincaps");
41 
42  TaskResourcePtr task = scene->GetOrCreateTaskFromName_UTF8("task0", "itlplanning");
44  info.optimizationvalue = 0.2; // set the optimization value [0,1]
45  info.program = "settool(1)\n\
46 set(clearance,40)\n\
47 move(translation(0,0,20)*p[Work0/2])\n\
48 movel(p[Work0/2])\n\
49 movel(translation(0,0,20)*p[Work0/2])\n\
50 move(translation(0,0,20)*p[Work0/3])\n\
51 movel(p[Work0/3])\n\
52 movel(translation(0,0,20)*p[Work0/3])\n\
53 ";
54  task->SetTaskParameters(info);
55 
56  // if necessary, can cancel all current jobs. this is not required
57  controller->CancelAllJobs();
58 
59  task->Execute();
60 
61  std::cout << "waiting for task result" << std::endl;
62 
63  // query the results until they are complete, should take several seconds
65  JobStatus status;
66  int iterations = 0, maxiterations = 4000;
67  while (1) {
68  result = task->GetResult();
69  if( !!result ) {
70  break;
71  }
72  task->GetRunTimeStatus(status);
73  std::cout << "current job status=" << status.code << ": " << status.message << std::endl;
74  if( status.code == JSC_Succeeded ) {
75  break;
76  }
77  else if( status.code == JSC_Unknown ) {
78  // wait 30s, then fail
79  if( iterations > 3 ) {
80  std::cout << "task won't start for some reason" << std::endl;
81  return 1;
82  }
83  }
84  else if( status.code == JSC_Aborted || status.code == JSC_Preempted ) {
85  std::cout << "task failed execution " << std::endl;
86  return 1;
87  }
88  else if( status.code != JSC_Active && status.code != JSC_Pending && status.code != JSC_Succeeded ) {
89  std::cout << "unexpected job status so quitting " << std::endl;
90  return 1;
91  }
92 
93  boost::this_thread::sleep(boost::posix_time::milliseconds(5000)); // 5s
94  ++iterations;
95  if( iterations > maxiterations ) {
96  controller->CancelAllJobs();
97  throw MujinException("operation timed out, cancelling all jobs and quitting", MEC_Timeout);
98  }
99  }
100 
101  RobotControllerPrograms programs;
102  result->GetPrograms(programs);
103  std::cout << "found " << programs.programs.size() << " programs" << std::endl;
104  for(std::map<std::string, RobotProgramData>::iterator it = programs.programs.begin(); it != programs.programs.end(); ++it ) {
105  std::cout << "[" << it->first << "]" << std::endl << it->second.programdata << std::endl << std::endl;
106  }
107  std::cout << "final task_time is " << result->Get("task_time") << std::endl;
108  }
109  catch(const MujinException& ex) {
110  std::cout << "exception thrown: " << ex.message() << std::endl;
111  }
113 }