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