controllerclientcpp  0.6.1
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義 ページ
mujinupdateenvironmentstate.cpp
説明を見る。
2 #include <iostream>
3 
4 #if defined(_WIN32) || defined(_WIN64)
5 #include <windows.h>
6 #undef GetUserName // clashes with ControllerClient::GetUserName
7 #endif // defined(_WIN32) || defined(_WIN64)
8 
9 
10 #ifndef MUJIN_TIME
11 #define MUJIN_TIME
12 #include <time.h>
13 
14 #ifndef _WIN32
15 #if !(defined(CLOCK_GETTIME_FOUND) && (POSIX_TIMERS > 0 || _POSIX_TIMERS > 0))
16 #include <sys/time.h>
17 #endif // !(defined(CLOCK_GETTIME_FOUND) && (POSIX_TIMERS > 0 || _POSIX_TIMERS > 0))
18 #else
19 #include <sys/timeb.h> // ftime(), struct timeb
20 inline void usleep(unsigned long microseconds) {
21  Sleep((microseconds+999)/1000);
22 }
23 #endif // _WIN32
24 
25 #ifdef _WIN32
26 inline unsigned long long GetMilliTime()
27 {
28  LARGE_INTEGER count, freq;
29  QueryPerformanceCounter(&count);
30  QueryPerformanceFrequency(&freq);
31  return (unsigned long long)((count.QuadPart * 1000) / freq.QuadPart);
32 }
33 
34 #else
35 
36 inline void GetWallTime(unsigned int& sec, unsigned int& nsec)
37 {
38 #if defined(CLOCK_GETTIME_FOUND) && (POSIX_TIMERS > 0 || _POSIX_TIMERS > 0)
39  struct timespec start;
40  clock_gettime(CLOCK_REALTIME, &start);
41  sec = start.tv_sec;
42  nsec = start.tv_nsec;
43 #else
44  struct timeval timeofday;
45  gettimeofday(&timeofday,NULL);
46  sec = timeofday.tv_sec;
47  nsec = timeofday.tv_usec * 1000;
48 #endif //defined(CLOCK_GETTIME_FOUND) && (POSIX_TIMERS > 0 || _POSIX_TIMERS > 0)
49 }
50 
51 inline unsigned long long GetMilliTime()
52 {
53  unsigned int sec,nsec;
54  GetWallTime(sec,nsec);
55  return (unsigned long long)sec*1000 + (unsigned long long)nsec/1000000;
56 }
57 #endif // _WIN32
58 #endif // MUJIN_TIME
59 
60 using namespace mujinclient;
61 
62 #include <boost/program_options.hpp>
63 
64 
65 int main(int argc, char ** argv)
66 {
67  // parse command line arguments
68  namespace bpo = boost::program_options;
69  bpo::options_description desc("Options");
70 
71  desc.add_options()
72  ("help,h", "produce help message")
73  ("controller_ip", bpo::value<std::string>()->required(), "ip of the mujin controller, e.g. controller")
74  ("controller_port", bpo::value<unsigned int>()->default_value(80), "port of the mujin controller, e.g. 80")
75  ("controller_username_password", bpo::value<std::string>()->required(), "username and password to the mujin controller, e.g. username:password")
76  ("controller_command_timeout", bpo::value<double>()->default_value(10), "command timeout in seconds, e.g. 10")
77  ("locale", bpo::value<std::string>()->default_value("en_US"), "locale to use for the mujin controller client")
78  ("binpicking_task_scenepk", bpo::value<std::string>()->required(), "scene pk of the binpicking task on the mujin controller, e.g. officeboltpicking.mujin.dae")
79  ("taskparameters", bpo::value<std::string>()->required(), "binpicking task parameters, e.g. {'robotname': 'robot', 'robots':{'robot': {'externalCollisionIO':{}, 'gripperControlInfo':{}, 'robotControllerUri': '', robotDeviceIOUri': '', 'toolname': 'tool'}}}")
80  ("slaverequestid", bpo::value<std::string>()->required(), "slaverequestid, e.g. hostname_slave0")
81  ("robotname", bpo::value<std::string>()->required(), "robot name, e.g. VS060A3-AV6-NNN-NNN")
82  ("regionname", bpo::value<std::string>()->required(), "regionname, e.g. smallcontainer")
83  ("objectupdatename", bpo::value<std::string>()->required(), "target object name, e.g. detected")
84  ("objecturi", bpo::value<std::string>()->required(), "target object uri, e.g. mujin:/bolt0.mujin.dae")
85  ("objectconfidence", bpo::value<std::string>()->default_value("{\"global_confidence\":1.0}"), "target object confidence")
86  ("objectextra", bpo::value<std::string>()->default_value(""), "target object extras, e.g. {\"randombox\": {\"height\":100,\"width\":100,\"length\":100}}")
87  ("waitinterval", bpo::value<unsigned int>()->default_value(500), "update interval in ms")
88  ("pointsfilename", bpo::value<std::string>()->required(), "path to text file containing commaseparated point xyz positions in millimeter")
89  ("pointsize", bpo::value<double>()->default_value(0.005), "pointcloud pointsize in millimeter")
90  ("obstaclename", bpo::value<std::string>()->default_value("__dynamicobstacle__"), "pointcloud obstacle name")
91  ;
92 
93  bpo::variables_map opts;
94  bpo::store(bpo::parse_command_line(argc, argv, desc), opts);
95  bool badargs = false;
96  try {
97  bpo::notify(opts);
98  }
99  catch(...) {
100  badargs = true;
101  }
102  if(opts.count("help") || badargs) {
103  std::cout << "Usage: " << argv[0] << " [OPTS]" << std::endl;
104  std::cout << std::endl;
105  std::cout << desc << std::endl;
106  return (1);
107  }
108 
109  const std::string controllerIp = opts["controller_ip"].as<std::string>();
110  const unsigned int controllerPort = opts["controller_port"].as<unsigned int>();
111  const std::string controllerUsernamePass = opts["controller_username_password"].as<std::string>();
112  const double controllerCommandTimeout = opts["controller_command_timeout"].as<double>();
113  const std::string binpickingTaskScenePk = opts["binpicking_task_scenepk"].as<std::string>();
114  const std::string robotname = opts["robotname"].as<std::string>();
115  const std::string objectupdatename = opts["objectupdatename"].as<std::string>();
116  const std::string objecturi = opts["objecturi"].as<std::string>();
117  const std::string objectconfidence = opts["objectconfidence"].as<std::string>();
118  const std::string objectextra = opts["objectextra"].as<std::string>();
119  const std::string regionname = opts["regionname"].as<std::string>();
120  const std::string taskparameters = opts["taskparameters"].as<std::string>();
121  const std::string slaverequestid = opts["slaverequestid"].as<std::string>();
122  const unsigned int waitinterval = opts["waitinterval"].as<unsigned int>();
123  const std::string pointsfilename = opts["pointsfilename"].as<std::string>();
124  const double pointsize = opts["pointsize"].as<double>();
125  const std::string obstaclename = opts["obstaclename"].as<std::string>();
126  const std::string locale = opts["locale"].as<std::string>();
127 
128  std::string tasktype = "binpicking";
129  try {
130  // connect to mujin controller
131  std::stringstream url_ss;
132  url_ss << "http://"<< controllerIp << ":" << controllerPort;
133  ControllerClientPtr controllerclient = CreateControllerClient(controllerUsernamePass, url_ss.str());
134  SceneResourcePtr scene(new SceneResource(controllerclient, binpickingTaskScenePk));
135 
136  // initialize binpicking task
137  BinPickingTaskResourcePtr pBinpickingTask = scene->GetOrCreateBinPickingTaskFromName_UTF8(tasktype+std::string("task1"), tasktype);
138  std::string userinfo_json = "{\"username\": \"" + controllerclient->GetUserName() + "\", \"locale\": \"" + locale + "\"}";
139  std::cout << "initialzing binpickingtask in UpdateEnvironmentThread with userinfo=" + userinfo_json << " taskparameters=" << taskparameters << " slaverequestid=" << slaverequestid << std::endl;
140  pBinpickingTask->Initialize(taskparameters, controllerCommandTimeout, userinfo_json, slaverequestid);
141 
142  // populate dummy data
143  std::vector<BinPickingTaskResource::DetectedObject> detectedobjects;
144  std::vector<Real> points;
145  std::string resultstate;
146 
147  // create object
149  detectedobject.name = str(boost::format("%s_%d") % objectupdatename % 0);
150  detectedobject.object_uri = objecturi;
151  Transform transform;
152  transform.quaternion[0] = 1; transform.quaternion[1] = 0; transform.quaternion[2] = 0; transform.quaternion[3] = 0;
153  transform.translate[0] = 450; transform.translate[1] = 0; transform.translate[2] = 60; // in milimeter
154  detectedobject.transform = transform;
155  detectedobject.confidence = objectconfidence;
156  detectedobject.timestamp = GetMilliTime();
157  detectedobject.extra = objectextra;
158 
159  // load pointcloud from file, assuming comma seprated xyz coordinates in millimeter
160  std::ifstream pointsfile(pointsfilename.c_str());
161  while (pointsfile) {
162  std::string s;
163  if (!std::getline(pointsfile, s)) {
164  break;
165  }
166  std::istringstream ss(s);
167  while (ss) {
168  std::string s;
169  if (!std::getline(ss, s, ',')) {
170  break;
171  }
172  points.push_back(boost::lexical_cast<double>(s));
173  }
174  }
175  std::cout << "loaded " << points.size() / 3 << " points from " << pointsfilename << std::endl;
176 
177  // update environment loop
178  std::string inputdataunit = "mm";
179  while (1) {
180  try {
181  pBinpickingTask->UpdateEnvironmentState(objectupdatename, detectedobjects, points, resultstate, pointsize, obstaclename, inputdataunit, controllerCommandTimeout);
182  std::cout << "UpdateEnvironmentState with " << detectedobjects.size() << " objects " << (points.size()/3.) << " points" << std::endl;
183  }
184  catch(const std::exception& ex) {
185  std::cerr << "Failed to update environment state: " << ex.what() << "." << std::endl;
186 
187  }
188  boost::this_thread::sleep(boost::posix_time::milliseconds(waitinterval));
189  }
190  }
191  catch(const std::exception& ex) {
192  std::stringstream errss;
193  errss << "Caught exception " << ex.what();
194  std::cerr << errss.str() << std::endl;
195  }
196  catch (...) {
197  std::stringstream errss;
198  errss << "Caught unknown exception!";
199  std::cerr << errss.str() << std::endl;
200  }
201 }