controllerclientcpp  0.6.1
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義 ページ
mujinexecutetask_robodia.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  controller->SyncUpload_UTF8("../share/mujincontrollerclient/robodia_demo1/robodia_demo1.xml", "mujin:/robodia_demo1/", "cecrobodiaxml");
37 
38  std::string sceneuri = "mujin:/robodia_demo1/robodia_demo1.xml";
39  SceneResourcePtr scene = controller->RegisterScene_UTF8(sceneuri, "cecrobodiaxml");
40 
41  std::vector<SceneResource::InstObjectPtr> instobjects;
42  scene->GetInstObjects(instobjects);
43  std::cout << "scene instance objects: ";
44  for(size_t i = 0; i < instobjects.size(); ++i) {
45  std::cout << instobjects[i]->name << ", ";
46  }
47  std::cout << std::endl;
48 
49  // execute a task
50 
51  TaskResourcePtr task = scene->GetOrCreateTaskFromName_UTF8("task0", "itlplanning");
53  info.optimizationvalue = 0.2; // set the optimization value [0,1]
54  info.vrcruns = 0; // turn off VRC since this environment does not have VRC files
55  info.program = "settool(Tcp_HAND2)\n\
56 move(p[3_home])\n\
57 move(p[3_home_R])\n\
58 move(p[3_s1_R])\n\
59 movel(p[3_s2_R])\n\
60 movel(p[3_s1_R])\n\
61 move(p[3_home_R])\n\
62 move(p[3_home_L])\n\
63 move(p[3_e1_L])\n\
64 movel(p[3_e2_L])\n\
65 movel(p[3_e1_L])\n\
66 move(p[3_home_L])\n\
67 move(p[3_home])\n\
68 ";
69  task->SetTaskParameters(info);
70 
71  // if necessary, can cancel all current jobs. this is not required
72  controller->CancelAllJobs();
73 
74  task->Execute();
75 
76  std::cout << "waiting for task result" << std::endl;
77 
78  // query the results until they are complete, should take several seconds
80  JobStatus status;
81  int iterations = 0, maxiterations = 4000;
82  while (1) {
83  result = task->GetResult();
84  if( !!result ) {
85  break;
86  }
87  task->GetRunTimeStatus(status);
88  std::cout << "current job status=" << status.code << ": " << status.message << std::endl;
89  if( status.code == JSC_Succeeded ) {
90  break;
91  }
92  else if( status.code == JSC_Unknown ) {
93  // wait 30s, then fail
94  if( iterations > 3 ) {
95  std::cout << "task won't start for some reason" << std::endl;
96  return 1;
97  }
98  }
99  else if( status.code == JSC_Aborted || status.code == JSC_Preempted ) {
100  std::cout << "task failed execution " << std::endl;
101  return 1;
102  }
103  else if( status.code != JSC_Active && status.code != JSC_Pending && status.code != JSC_Succeeded ) {
104  std::cout << "unexpected job status so quitting " << std::endl;
105  return 1;
106  }
107 
108  boost::this_thread::sleep(boost::posix_time::milliseconds(5000)); // 5s
109  ++iterations;
110  if( iterations > maxiterations ) {
111  controller->CancelAllJobs();
112  throw MujinException("operation timed out, cancelling all jobs and quitting", MEC_Timeout);
113  }
114  }
115 
116  std::string cecrobodiazipdata;
117  result->GetAllRawProgramData(cecrobodiazipdata, "cecrobodiasim");
118  std::cout << "got robodia simulation file, size=" << cecrobodiazipdata.size() << " bytes" << std::endl;
119  // get any other programs
120  RobotControllerPrograms programs;
121  result->GetPrograms(programs);
122  std::cout << "found " << programs.programs.size() << " programs" << std::endl;
123  for(std::map<std::string, RobotProgramData>::iterator it = programs.programs.begin(); it != programs.programs.end(); ++it ) {
124  std::cout << "[" << it->first << "]" << std::endl << it->second.programdata << std::endl << std::endl;
125  }
126  std::cout << "final task_time is " << result->Get("task_time") << std::endl;
127  }
128  catch(const MujinException& ex) {
129  std::cout << "exception thrown: " << ex.message() << std::endl;
130  }
131  // destroy all mujin controller resources
133 }